user7603510
user7603510

Reputation:

HightChart set new data

I initialize highchart with date and value like this :

    series: [{
        name: 'Tokyo',
        data: 
        [
             [Date.parse("01/02/2017"),11],
             [Date.parse("02/02/2017"),22],
        ]

    },{
        name: 'Tehran',
        data: 
        [
             [Date.parse("01/02/2017"),33],
             [Date.parse("02/02/2017"),44],
        ]      
    }]

In above example chartArray is successfully added to HighChart.

Now I want to to set new data in chart and I use HighChart.series[0].setData() method to do this.

JS:

 $('#btnUpdate').('click', function(){
    $.post('/Reporst/GetReportsJson/', data, function (dataR) {
       var chartArray = []; //for chart value
       var dateArray = []; // for chart date, X Axis
       for (var i in dataR) {
          for (var ii in dataR[i].finalChart) {  
             dateArray.push(dataR[i].finalChart[ii].date1);
             chartArray.push(parseInt(dataR[i].finalChart[ii].value));
          }
       HighChart.series[i].setData(chartArray);      
       dateArray = []; chartArray = [];
   }
})

With this code I only set new data to chart, My Question Is : How can I set new date, new data and new label for chart?

Upvotes: 1

Views: 96

Answers (1)

The data of a series is formed of an array that contains, per data point, one array of two elements with the date as the first element (in your case) and the value as the second, as you can see in the first part of the code.

data: 
[ // Data is an array
     [Date.parse("01/02/2017"),11], // Each point is an array too
     [Date.parse("02/02/2017"),22],
]

So, when you populate the chartArray to do a setData(), it should have the same format, like this:

$('#btnUpdate').('click', function(){
    $.post('/Reporst/GetReportsJson/', data, function (dataR) {
       var chartArray = []; //for chart value
       for (var i in dataR) {
          for (var ii in dataR[i].finalChart) {  
             var date = dataR[i].finalChart[ii].date1;
             var value = parseInt(dataR[i].finalChart[ii].value);
             chartArray.push([Date.parse(date), value]); // One array with [date, value] per point, as stated above
          }
       }
       HighChart.series[i].setData(chartArray);      
       chartArray = [];
   }
})

Upvotes: 1

Related Questions