如何在highcharts中设置动态数据

时间:2022-12-04 15:28:56

I'm getting data from servlet and my sysout of json object which i'm sending from servlet is {"jsonArray":[{"bugzilla":20,"redmind":14}]}

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

Now my java script is

现在我的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 prints receivedData: ['bugzilla', 20],['redmind', 14] which i'm expecting but problem is when i'm setting it to

提醒打印收到数据:['bugzilla',20],['redmind',14]我期待但问题是当我将它设置为

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

then my pie chart is not working. It shows only one part like 1/4 circle with no tooltip

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

2 个解决方案

#1


15  

Your data is a String, it needs to be an array of array, where the inner array consists of two elements, the first being the key as string, and 2nd the value in numeric form.

您的数据是一个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);
 }

#2


0  

You can prepare your data first, like in the @Jugal Thakkar answer above, and then use update function available from v5.

您可以先准备好数据,如上面的@Jugal Thakkar答案,然后使用v5中提供的更新功能。

chart.update({
    series: preparedSeriesData
});

this will dynamically update the chart.

这将动态更新图表。

#1


15  

Your data is a String, it needs to be an array of array, where the inner array consists of two elements, the first being the key as string, and 2nd the value in numeric form.

您的数据是一个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);
 }

#2


0  

You can prepare your data first, like in the @Jugal Thakkar answer above, and then use update function available from v5.

您可以先准备好数据,如上面的@Jugal Thakkar答案,然后使用v5中提供的更新功能。

chart.update({
    series: preparedSeriesData
});

this will dynamically update the chart.

这将动态更新图表。