如何在高图表中设置动态数据

how to set dynamic data in highcharts

本文关键字:设置 动态 数据 高图表      更新时间:2023-09-26

我从servlet获取数据,我从servlet发送的json对象的sysout是{"jsonArray":[{"bugzilla":20,"redmind":14}]}

现在我的java脚本是

 <script type="text/javascript">
    var chart;
    $(document).ready(
            function() {
                chart = new Highcharts.Chart({
                    chart : {
                        renderTo : 'container',
                    },
                    title : {
                        text : 'Bug chart'
                    },
                    tooltip : {
                        formatter : function() {
                            var s;
                            if (this.point.name) { // the pie chart
                                s = '' + this.point.name + ': ' + this.y
                                        + ' Bugs';
                            } else {
                                s = '' + this.x + ': ' + this.y;
                            }
                            return s;
                        }
                    },
                    labels : {
                        items : [ {
                            html : 'Total Bugs',
                            style : {
                                left : '40px',
                                top : '8px',
                                color : 'black'
                            }
                        } ]
                    },
                    series : [ {
                        type : 'pie',
                        name : 'Total Bugs',
                        data : [],
                        center : [ 100, 80 ],
                        size : 100,
                        showInLegend : false,
                        dataLabels : {
                            enabled : false
                        },
                    },  ]
                }, function getdata(chart) {
                    var tmp="";
                    var receivedData="";
                    $.ajax({
                        url : 'http://localhost:8080/PRM/GraphServlet',
                        dataType : 'json',
                        error : function() {
                            alert("error occured!!!");
                        },
                        success : function(data) {
                            $.each(data.jsonArray, function(index)
                                    {
                                    $.each(data.jsonArray[index], 
                                        function(key,value) {
                                    tmp = "['" + key + "',  " + value + "],";
                                    receivedData += tmp;
                                    alert("receivedData: " + receivedData);
                                });
                            });
                            alert(receivedData.substring(0, 34));
                            chart.series[0].setData([receivedData.toString().substring(0, 34)]);

                        }
                    }
                    );
                });
            });
</script>

alert print received数据:['bugzilla',20],['admind',14]我期待着但问题是当我把它设置为时

chart.series[0].setData([receivedData.toString().substring(0,34)])

那么我的饼图就不起作用了。它只显示一个部分,如1/4圆形,没有工具提示

您的数据是String,它需要是数组的数组,其中内部数组由两个元素组成,第一个是字符串形式的键,第二个是数字形式的值。

success : function(data) {
   var chartData=[];
   $.each(data.jsonArray, function(index)
    {
     $.each(data.jsonArray[index], 
      function(key,value) {
       var point = [];
       point.push(key);
       point.push(value);
       chartData.push(point);                          
      });
   });
   chart.series[0].setData(chartData);
 }

您可以先准备数据,就像上面的@Jugal Thakkar回答一样,然后使用v5提供的update函数。

chart.update({
    series: preparedSeriesData
});

这将动态更新图表。