score:19

Accepted answer

edit: the response down below is more correct!

https://stackoverflow.com/a/8408466/387285

http://www.highcharts.com/ref/#series-object

html:

<select id="list">
<option value="a">data set a
<option value="b">data set b
</select>
<button id="change">refresh table</button>

<div id="container" style="height: 400px"></div>

javascript:

var options = {
    chart: {
        renderto: 'container',
        defaultseriestype: 'spline'
    },
    series: []
};

$("#change").click(function() {
    if ($("#list").val() == "a") {
        options.series = [{name: 'a', data: [1,2,3,2,1]}]
        // $.get('/dough/includes/live-chart.php?mode=month'
    } else {
        options.series = [{name: 'b', data: [3,2,1,2,3]}]
        // $.get('/dough/includes/live-chart.php?mode=newmode'
    } 

    var chart = new highcharts.chart(options);    
});

this is a very simple example since i don't have my files here with me but the basic idea is that every time the user selects new options for the stuff they want to see, you're going to have replace the .series data object with the new information from your server and then recreate the chart using the new highcharts.chart();.

hope this helps! john

edit:

check this out, its from something i've worked on in the past:

$("table#tblgeneralinfo2 > tbody > tr").each(function (index) {
    if (index != 0) {
        var chartnumbervalue = parseint($(this).find("td:last").text());
        var charttextvalue = $(this).find("td:first").text();
        chartoptions.series[0].data.push([charttextvalue, chartnumbervalue]);
    }
});

i had a table with information in the first and last tds that i needed to add to the pie chart. i loop through each of the rows and push in the values. note: i use chartoptions.series[0].data since pie charts only have 1 series.

score:0

data = [150,300]; // data from ajax or any other way chart.series[0].setdata(data, true);

the setdata will call redraw method.

reference: http://api.highcharts.com/highcharts/series.setdata

score:1

if you are using push to push the data to the option.series dynamically .. just use

options.series = [];  

to clear it.

options.series = [];
$("#change").click(function(){
}

score:1

you can always load a json data

here i defined chart as namespace

 $.getjson('data.json', function(data){
                chart.options.series[0].data = data[0].data;
                chart.options.series[1].data = data[1].data;
                chart.options.series[2].data = data[2].data;

                var chart = new highcharts.chart(chart.options);

            });

score:2

correct answer is:

$.each(lines, function(lineno, line) {
                    var items = line.split(',');
                    var data = {};
                    $.each(items, function(itemno, item) {
                        if (itemno === 0) {
                            data.name = item;
                        } else {
                            data.y = parsefloat(item);
                        }
                    });
                    options.series[0].data.push(data);
                    data = {};
                });

you need to flush the 'data' array.

data = {};

score:4

you need to clear the old array out before you push the new data in. there are many ways to accomplish this but i used this one:

options.series[0].data.length = 0;

so your code should look like this:

options.series[0].data.length = 0;
$.each(lines, function(lineno, line) {
                    var items = line.split(',');
                    var data = {};
                    $.each(items, function(itemno, item) {
                        if (itemno === 0) {
                            data.name = item;
                        } else {
                            data.y = parsefloat(item);
                        }
                    });
                    options.series[0].data.push(data);
                });

now when the button is clicked the old data is purged and only the new data should show up. hope that helps.

score:4

actually maybe you should choose the function update is better.
here's the document of function update http://api.highcharts.com/highcharts#series.update

you can just type code like below:

chart.series[0].update({data: [1,2,3,4,5]})

these code will merge the origin option, and update the changed data.

score:103

the other answers didn't work for me. i found the answer in their documentation:

http://api.highcharts.com/highcharts#series

using this method (see jsfiddle example):

var chart = new highcharts.chart({
    chart: {
        renderto: 'container'
    },

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]        
    }]
});

// the button action
$('#button').click(function() {
    chart.series[0].setdata([129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4] );
});

Related Query

More Query from same tag