12

At this code javascrip give an error

$.each(JSON, function(i, array) {                        
    chart.series[i].name = array.teamName;
    chart.series[i].setData(array.teamPower, true);
});

I must define the chart.series[i] because it say "Cannot set property 'name' of undefined" but i can't find a way in order to do this.

Because it fonction runs with requestData so it came after chart determine with options

function showGraph() {  
    chart = new Highcharts.Chart(option);       
}

chart: {
    renderTo: 'graphicShow',
    type: 'spline',
    events: {
        load: requestData
    }
}

...in option...

title: {
    text: 'Power %'
},
series: []

...

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
guemues
  • 569
  • 2
  • 7
  • 21

2 Answers2

34

You need to looka at the "Methods and Properties" part of the API. See http://api.highcharts.com/highcharts#Chart (There is an jsFiddle on the documentation page as well).

var chart = new Highcharts.Chart(options);
chart.addSeries({                        
    name: array.teamName,
    data: array.teamPowher
});

If you are going to add several series you should set the redraw flag to false and then call redraw manually after as that will be much faster.

var chart = new Highcharts.Chart(options);
chart.addSeries({                        
    name: array.teamName,
    data: array.teamPower
}, false);
chart.addSeries({                        
    name: array.teamName,
    data: array.teamPower
}, false);
chart.redraw();
Nils
  • 2,041
  • 1
  • 15
  • 20
  • Is there a way to reference the series by name, or is it just there for tooltips etc? – Ethereal May 31 '13 at 21:41
  • You can use the Chart.get function, see here http://api.highcharts.com/highcharts#Chart.get(). Please note the differance between "name" and "id". – Nils Jun 01 '13 at 07:28
  • does this work any differently now with `v6+`? I am getting errors in TS with this. – SovietFrontier Feb 07 '19 at 17:50
0

You too can specify as second option of addSeries a boolean value, that indicates if it redraws or not

kelgwiin
  • 766
  • 1
  • 12
  • 23