高图JSON格式的数据是正确的

时间:2016-08-19 12:00:14

标签: javascript jquery json highcharts

我有这样的代码

 public static string summarydata(string RegNo)
    {

        try
        {
            TrackDataEntities1 sd = new TrackDataEntities1();

            var mdata = new TrackDataEntities1().spsumdata(RegNo)
            .Select(s => new { month = s.Month }).ToArray();

            var sdata = new TrackDataEntities1().spsumdata(RegNo)
             .Select(s => new { s.VName, s.total }).ToArray();
            return Newtonsoft.Json.JsonConvert.SerializeObject(mdata) + "*" + Newtonsoft.Json.JsonConvert.SerializeObject(sdata);

        }
        catch (Exception)
        {
            throw new Exception();
        }

    }

现在这回复了​​我这样的数据

"[{\"month\":\"July\"},{\"month\":\"June\"},{\"month\":\"June\"},
 {\"month\":\"August\"},{\"month\":\"July\"},{\"month\":\"June\"},
 {\"month\":\"May\"},{\"month\":\"June\"}]*[{\"VName\":\"DDSB\",\"total\":1},
 {\"VName\":\"DPSB\",\"total\":1},{\"VName\":\"DSB\",\"total\":1},
 {\"VName\":\"MV\",\"total\":5},{\"VName\":\"MV\",\"total\":11},
 {\"VName\":\"MV\",\"total\":7},{\"VName\":\"MV\",\"total\":1},
 {\"VName\":\"PSB\",\"total\":1}]"

的jquery 更新的JQUERY                  $(function(){

            $('#tabledata').on('click', 'tr', function () {
                var row = $(this);
                var regno = row.find('td')[0].firstChild.data;
                var obj = {};
                obj.RegNo = regno;
                Getsumdata(obj);
                return false;

            });
        });
        function Getsumdata(obj) {
            $.ajax({
                type: "POST",
                url: "WebForm1.aspx/summarydata",
                data: JSON.stringify(obj),
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function (result) {
                   alert(JSON.stringify(result.d));

                var data1 = result.d.split('*')[0];
                console.log(typeof (data1)); //Still a String...
                var data11 = JSON.parse(data1);
                console.log(data11); //

                    $('#sum').highcharts({
                        title: {
                            text: 'Combination chart'
                        },
                        xAxis: {
                            categories: data11,

                            title: {
                                text: null
                            }
                        },
                        labels: {
                            items: [{
                                html: 'Total fruit consumption',
                                style: {
                                    left: '50px',
                                    top: '18px',
                                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
                                }
                            }]
                        },
                       // series:data2
                      series: [{
                        type: 'column',
                        name: 'Jane',
                        data: [3, 2, 1, 3, 4]
                    }, {
                        type: 'column',
                        name: 'John',
                        data: [2, 3, 5, 7, 6]
                    }, {
                        type: 'column',
                        name: 'Joe',
                        data: [4, 3, 3, 9, 0]
                    },
                   ]
                    });

                }
            });
        }
    </script>

但图表看起来像这样

chart

现在问题是JSON看起来正确我想是为什么数据没有在图表中填充..我使用BAR高图

请问任何解决方案?

2 个答案:

答案 0 :(得分:2)

  1. data2仍然是一个字符串,你必须解析它。
  2. 查看有关如何添加图表数据的Docs,您必须转换当前数据。
  3. &#13;
    &#13;
    var a = "[{\"month\":\"July\"},{\"month\":\"June\"},{\"month\":\"June\"}, {\"month\":\"August\"},{\"month\":\"July\"},{\"month\":\"June\"}, {\"month\":\"May\"},{\"month\":\"June\"}]*[{\"VName\":\"DDSB\",\"total\":1}, {\"VName\":\"DPSB\",\"total\":1},{\"VName\":\"DSB\",\"total\":1}, {\"VName\":\"MV\",\"total\":5},{\"VName\":\"MV\",\"total\":11}, {\"VName\":\"MV\",\"total\":7},{\"VName\":\"MV\",\"total\":1}, {\"VName\":\"PSB\",\"total\":1}]";
    
    var d = a.split('*')[1];
    console.log(typeof(d)); //Still a String...
    var e = JSON.parse(d);
    console.log(e); //Yay an object.
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

您似乎没有将正确的数据传递给类别和系列。 请以这样的方式转换您的数据: data1代表类别,应该看起来像这样

["May","June","July","August"]

虽然您的data2代表您要为给定月份绘制的系列数据,但应如下所示。

[1,10,12,5]