Reload chart data via JSON with Highcharts

前端 未结 8 2278
借酒劲吻你
借酒劲吻你 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 02:56

    EDIT: The response down below is more correct!

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

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

    HTML:

    
    
    
    

    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.

提交回复
热议问题