简体   繁体   English

highchart JSON格式的数据正确

[英]Data in highchart JSON format is correct

I have code like this 我有这样的代码

 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();
        }

    }

now this return me data like this 现在这返回我这样的数据

"[{\"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 UPDATED JQUERY $(function () { 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>

but chart look like this 但是图表看起来像这样

图表

Now the question is JSON look correct i think so why data is not populated in chart .. i use BAR highchart 现在的问题是,我认为JSON看起来正确,所以为什么没有在图表中填充数据..我使用BAR highchart

any solution please? 有什么解决办法吗?

  1. data2 is still a string, you have to parse it. data2仍然是一个字符串,您必须对其进行解析。
  2. Take a look at the Docs on how to add chart data, you have to transform your current data. 查看有关如何添加图表数据的文档 ,您必须转换当前数据。

 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. 

It seems that you are not passing correct data to categories and series's. 似乎您没有将正确的数据传递给类别和系列。 Please transform your data in such a way that, data1 represent categories, should look like this 请以这样的方式转换数据,即data1代表类别,应看起来像这样

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

while your data2 represent the series data which you want to plot for a given month, should look like below. 虽然您的data2代表您要绘制的给定月份的系列数据,但应如下所示。

[1,10,12,5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM