Reload chart data via JSON with Highcharts

前端 未结 8 2267
借酒劲吻你
借酒劲吻你 2020-11-28 02:11

I am trying to reload the data for a Highcharts chart via JSON based on a button click elsewhere in the page. Initially I would like to display a default set of data (spendi

相关标签:
8条回答
  • 2020-11-28 03:08

    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.

    0 讨论(0)
  • 2020-11-28 03:14

    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 = {};
    
    0 讨论(0)
提交回复
热议问题