从 JSON 创建 HighCharts-Column 类型

Create HighCharts-Column type from JSON

我正在尝试使用来自 json 的数据创建一个列类型的图表,格式如下:

[{"id":7,"dateV":"2015-11-16","count":10},{"id":6,"dateV":"2015-11-15","count":3},{"id":5,"dateV":"2015-11-14","count":15},{"id":4,"dateV":"2015-11-13","count":10},{"id":3,"dateV":"2015-11-12","count":6},{"id":2,"dateV":"2015-11-11","count":8},{"id":1,"dateV":"2015-11-10","count":5}]

X 轴应显示日期值,Y 轴应显示计数。不知何故,我无法将此 JSON 转换为所需的格式。

这是我的代码。

<script type="text/javascript">
        $('#myButton').click(function() {
            var options = {
                    chart: {
                        renderTo: 'chartContainerDiv',
                        type: 'column'
                    },
                    series: [{}]
                };

                var url = "callToControllerURL";
                $.getJSON(url,  function(data) {
                    console.log(JSON.stringify(data));
                    options.series[0].data = JSON.stringify(data);
                    var chart = new Highcharts.Chart(options);
                });
            });
    </script>

控制台日志以上面指定的格式显示 JSON。我猜我必须从选项数组中形成 x 和 y 轴值。我该怎么做?

您需要遍历 json 数据并通过推送值创建类别数据和系列。

var jsonData = [{"id":7,"dateV":"2015-11-16","count":10},{"id":6,"dateV":"2015-11-15","count":3},{"id":5,"dateV":"2015-11-14","count":15},{"id":4,"dateV":"2015-11-13","count":10},{"id":3,"dateV":"2015-11-12","count":6},{"id":2,"dateV":"2015-11-11","count":8},{"id":1,"dateV":"2015-11-10","count":5}]; 

 var categoryData= [];
 var seriesData= [];
$.each(jsonData, function(i,item){
seriesData.push(jsonData[i].count);
categoryData.push(jsonData[i].dateV);
console.log("seriesData"+JSON.stringify(seriesData));
}); 

Working demo with your json here