Question

I am using AmCharts for displaying some charts and I am trying to dynamically add the data from a series of JSON data sets which are acquired using jquery's getJSON

My question in short is, how do I loop through my various data sets creating dataSets for the charts using something like:

$.getJSON("GET NUMBER OF DATA SETS REQUIRED", function (locdata) {

 $.each(locdata, function (i, item) {

     $.getJSON("GET DATA FOR THIS DATASET", function (data) {

         var dataSet = new AmCharts.DataSet();
         dataSet.title = locdata[i].name;
         dataSet.fieldMappings = [{
             fromField: "takings",
             toField: "takings"
         }, {
             fromField: "qty",
              toField: "qty"
         }];
         dataSet.dataProvider = data; ;
         dataSet.categoryField = "date";

         )};
    )};
)};

To explain what I am doing here:

  1. Firstly, getting some JSON output from my database to determine how many record sets are required
  2. Looping through the result from this to create recordsets
  3. During each loop, the data is acquired by a second jQuery getJSON call to grab data based on the data required for this recordset
  4. Creating an AmCharts Dataset

I can manually do this by explicitly coding to create each dataset individually and then doing

 chart.dataSets = [dataSet0, dataSet1, dataSet2, dataSet3, dataSet4];

So I guess what I need to do is turn the whole of the code above into some kind of function which generates an array of datasets as per the line above?

I'm not sure how to do this with javascript though

Was it helpful?

Solution

you can simply push each data set to chart.dataSets array:

chart.dataSets.push(dataSet);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top