creating highchart with ajax json data

后端 未结 3 1376
小蘑菇
小蘑菇 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);
        }
      });
     });
    
    0 讨论(0)
  • 2020-12-04 21:03

    In your ajax success function call your visitorData function with data[1].data (since that's how your json is formatted)

        $.ajax({
            url: '/visitdata',
            type: 'GET',
            async: true,
            dataType: "json",
            success: function (data) {
                visitorData(data[1].data);
    
            }
        });
    

    also, your visitorData function def is odd.

    vistorData = function(data) 
    

    or

    function vistorData(data)
    
    0 讨论(0)
  • 2020-12-04 21:17
    //parse json response
    var chartSeriesData = [];
    var chartCategory = [];
    
    $.each(response, function() {
    
      if(this.name!="TOTAL" && this.no!="0") {
    
        var series_name = this.name;
        var series_data = this.no;
    
        var series = [
          series_name,
          parseFloat(series_data)
        ];
        chartSeriesData.push(series);   
      }
    });
    
    //initialize options for highchart
    var options = {
      chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
      },
      title: {
        text: 'SalesOrder '
      },
      tooltip: {
        pointFormat: '{series.name}: <b>{point.y}</b>'
      },
      plotOptions: {
        pie: {
          allowPointSelect: true,
          cursor: 'pointer',
          center:['60%','60%'],
          size:150
          ,
          dataLabels: {
            enabled: true,
            color: '#000000',
            distance: 40,
            connectorColor: '#000000',
            format: '<b>{point.name}</b>: {point.y} '
          }
        }
      },
      series: [{
        type: 'pie',
        name: 'Browser share',
        data:chartSeriesData //load array created from json
      }]
    }
    
    //options.series[0].setData(datavaluejson);
    var chart= $('#container').highcharts(options);
    
    0 讨论(0)
提交回复
热议问题