如何使用高程图动态添加一系列数据到折线图

本文关键字:数据 折线图 一系列 添加 何使用 高程图 动态 | 更新日期: 2023-09-27 17:50:46

webservice的数据是从BindTrend方法中获取的,在绑定到图表之前被解析为Json对象:我的代码如下:

 var plot;
    var itemdata = [];
    var chart;
    var data = [];
 $(document).ready(function () {
            $.ajax({
            type: "POST",
            url: "ChartBinder.asmx/BindTrend",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {           
               var newJ = $.parseJSON(msg.d);               
                DrawTrend(newJ);
            },
            error: function (msg) {
                alert("Error");
            }
        });    
    })   
    function DrawTrend(plot) {
       for (i = 0; i < plot.length; i++) {
        var x = {}
        x.id=plot[i].Name;
        x.name = plot[i].Name;
        x.data = [plot[i].Value];
        itemdata.push(x);
        }
              chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 130,
                marginBottom: 25
            },
            series: itemdata
        });
    }

请注意,当我硬编码值'x.data=[1,2,3....]'时,我能够得到图表。请帮助。

如何使用高程图动态添加一系列数据到折线图

从你在评论中提到的,看起来像你的情节[i]。Value是Strings的数组,它应该是数字/浮点数组。您可以在javascript中进行如下转换。也不需要添加[ &]周围的值显式地,JS数组中默认有它。

   for (i = 0; i < plot.length; i++) {
        var x = {}
        x.id=plot[i].Name;
        x.name = plot[i].Name;
        //plot[i].Value = ["3121", "21211", "3121", "21211", "21000", "9872", "83402", "83402", "28302", "109523", "2832", "9523"];
        var stringArr=plot[i].Value;
        var floatArr=[];
        for(var j=0;j<stringArr.length;j++){
           dobleArr.push(parseFloat(stringArr[j]);
        }
        x.data=floatArr;
        itemdata.push(x);
   }

我建议检查Highcharts返回错误14

这是因为你在'for'中声明了x吗?

 var x = {}
 function DrawTrend(plot) {
       for (i = 0; i < plot.length; i++) {
        x.id=plot[i].Name;
        x.name = plot[i].Name;
        x.data = [plot[i].Value];
        itemdata.push(x);
        }