creating highchart with ajax json data

后端 未结 3 1381
小蘑菇
小蘑菇 2020-12-04 20:24

I\'m trying to create a simple chart in a page using mysql data retrieved using a mysql script

I don\'t understand how to integrate the ajax call with the data requi

3条回答
  •  长情又很酷
    2020-12-04 20:59

    I think you cannot return values from the success call instead you would need to call a function instead. So set up your function that initializes your chart, and in the ajax success call that function with the data

    With your code example

    function visitorData (data) {
       $('#chart1').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Average Visitors'
        },
        xAxis: {
            categories: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        },
        yAxis: {
            title: {
                text: 'Number of visitors'
            }
        },
        series: data,
      });
    }
    $(document).ready(function() {
     $.ajax({
        url: '/visitdata',
        type: 'GET',
        async: true,
        dataType: "json",
        success: function (data) {
            visitorData(data);
        }
      });
     });
    

提交回复
热议问题