To pass dynamic json array to Highcharts Pie Chart

后端 未结 4 1326
情歌与酒
情歌与酒 2020-12-10 09:50

I passed json encoded string(eg. $TEXT2 consisting [\"chrome\",\"15\",\"firefox\",\"20\"]) from xcode to an array(eg. arr) in javascript.Now I want to pass this array contai

4条回答
  •  半阙折子戏
    2020-12-10 10:10

    I would wrap the JSON call in the document.ready function and then wrap the plot call in the getJSON's success callback:

    $(document).ready(function() {
    
      $.getJSON("arr", function(json) {
    
        chart = new Highcharts.Chart({
          chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
          },
          title: {
            text: 'Interactive Pie'
          },
          tooltip: {
            formatter: function() {
              return ''+ this.point.name +': '+ this.y +' %';
            }
          },
          plotOptions: {
            pie: {
              allowPointSelect: true,
              cursor: 'pointer',
              dataLabels: {
                enabled: false
              },
              showInLegend: true
          },
          series: [{
            type: 'pie',
            name: 'Browser share',
            data: json
          }]
        });
      });
    });
    

    Of course for this to work, you should modify your backend code to return a properly formatted array of arrays that HighCharts expects:

    [["chrome",15],["firefox",20]]
    

    You could "fix" your returned array in the JS, but it would be better to do it in the JSON backend call.

提交回复
热议问题