0

So I have a large amount of data that I need to display all stored in separate CSV files. So I created two charts just fine in highcharts, one line, one area, but instead of copying and pasting the function over and over again I was hoping I could just iterate through it like so:

var library = ['data/data.csv', 'data/attendanceGroup.csv'];
var libraryLength = library.length;
var area =['#attendanceRoom','#attendanceGroup'];
var i = 0;

function areaChart(){
$(function () { 
    $.get(library[i], function(csv) {
        $(area[i]).highcharts({
            chart: {
                type: 'area'
            },
            data: {
                csv: csv
            },
            title: {
                text: 'Attendance by Room'
            },          
            yAxis: {
                title: {
                text: null
                },
                minorTickInterval: 'auto'
            },

            legend:{
                align: 'left',
                verticalAlign: 'top',
                floating: true        
            },
        });
    }); 
});
}

for (i = 0; i < libraryLength; i++){
areaChart();
}

I was looking at this Manage multiple highchart charts in a single webpage using jQuery.extend() or Highcharts.setOptions but that sets options for each individual chart and then you just make them over and over again. I thought a better solution might be to just have the one function and then just re-run it for each individual chart especially since I'm pulling the data from .CSV files.

So is this possible? Or should I go with jQuery.extend()?

Thanks for any help in advance!

Community
  • 1
  • 1
Max Cohen
  • 15
  • 4

1 Answers1

0

Just two things I would improve:

  1. $(function () { }); - I would encapsulate whole JS, not only part with AJAX and Highcharts:

    $(function () { 
      var library = ['data/data.csv', 'data/attendanceGroup.csv'];
    
      ...
    
      for (i = 0; i < libraryLength; i++){
          areaChart();
      }
    });
    
  2. make library[i] and area[i] as arguments for areaChart():

    $(function () { 
      var library = ['data/data.csv', 'data/attendanceGroup.csv'];
    
      ...
    
      function areaChart(lib, area){
        $.get(lib, function(csv) {
          $(area).highcharts({
            chart: {
              type: 'area'
            },
            data: {
              csv: csv
            }
          });
        });
      }
    
      for (i = 0; i < libraryLength; i++){
        areaChart(library[i], area[i]);
      }
    });
    

Of course, you can add more params to areaChart for example type, and pass on what kind of the chart should be rendered:

$(function () { 
    var library = ['data/data.csv', 'data/attendanceGroup.csv'];
    var types = ['line', 'area'];

    ...

    function areaChart(lib, area, type){
      $.get(lib, function(csv) {
        $(area).highcharts({
          chart: {
            type: type
          },
          data: {
            csv: csv
          }
        });
      });
    }

    for (i = 0; i < libraryLength; i++){
      areaChart(library[i], area[i], types[i]);
    }
});

Don't overdo with the params, no one likes to read 10params and control order etc. Instead you may consider passing one object param (renamed from areaChart to myChart):

myChart({
  lib: library[i], 
  area: area[i], 
  type: types[i]
});

And in myChart() method:

function myChart(options) {
    $.get(options.lib, function(csv) {
        $(options.area).highcharts({
            chart: {
                type: options.type
            },
            data: {
                csv: csv
            }
        });
    });
 }
Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
  • Thank you! I don't know why this change mattered but it did and know it works beautifully. Thank you for the improvements! – Max Cohen Dec 06 '15 at 17:35
  • If something didn't work, then most probably #1 resolved your issue. It was a bit strange to wait for load event in function. – Paweł Fus Dec 07 '15 at 09:10