编写JSON解析器来格式化饼图的数据(HighCharts) [英] Write JSON parser to format data for pie chart (HighCharts)

查看:182
本文介绍了编写JSON解析器来格式化饼图的数据(HighCharts)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与HighCharts打了几个小时的格式化数据输入到系列选项。
最后,我看到 此处 链接解决了数据格式化和输入问题。

HighCharts饼图可识别的数据格式如下所示:(格式1)以上链接:

  [[chrome,15],[firefox,20]] 
code>

实际上,我需要从外部网址输入动态数据并格式化数据,以便HighCharts可以识别它。我从URL获得的数据格式如下:(format 2)

 <$ c 
status:Stopped \ / Idle,
val:17.469444444444,
},{
status: 工作,
val:0,
},{
status:岬角转向,
val:0,
}, {
status:Transport,
val:0.15333333333333,
}
]

,它已经是JSON格式。

我只是想知道这对于我为格式2 的数据编写解析器至格式1 ?还是我错过HighCharts可以识别JSON格式数据的东西,而我实际上并不需要编写解析器?



我是HighCharts的新手,所以请随时指出,如果我的一些问题描述没有意义。谢谢!



编辑:感谢所有人回答我的问题!

脚本需要特定格式的数据,您通常必须将数据映射为适合的格式。这可以在服务器代码中修改或使用javascript



可以使用jQuery $。map 重新配置对象或数组到另一个数组。



DEMO: http:// jsfiddle。 net / qpsSe / 1



请注意,示例JSON中的尾随逗号需要移除才能验证JSON

  / *json是您的响应对象,修改变量名称以匹配* / 
var chartData = $。map(json,function(obj ,i){
return [[obj.status,obj.val]];
})

$ .map API文档



原生javascript中的替代方法

  var chartData = []; 
for(i = 0; i< json.length; i ++){
chartData.push([json [i] ['status'],json [i] ['val']])
}


I was fighting with HighCharts quite some hours for formatting the data input to the series option. Finally I saw the link here solved my problem for data formatting and input.

The data format that would be recognized by HighCharts pie chart is like this (format 1) as indicated by the link above:

[["chrome",15],["firefox",20]]

I actually want dynamic data input from external URL and format the data so that HighCharts can recognized it. The data format I got from the URL is like this (format 2):

[
    {
        "status": "Stopped \/ Idle",
        "val": 17.469444444444,
    }, {
        "status": "Working",
        "val": 0,
    }, {
        "status": "Headland Turning",
        "val": 0,
    }, {
        "status": "Transport",
        "val": 0.15333333333333,
    }
]

which is already in JSON format.

I just want to know that is that necessary for me to write a parser for the data from format 2 to format 1? Or Am I missing something that HighCharts can recognize the JSON format data and I don't actually need to write a parser?

I am new to HighCharts so feel free to point that out if some of my problem description does not make sense..Thank you!

EDIT: Thanks for all guys answering my question!

解决方案

When a script expects data in specific format, you often have to map your data to fit format. This can be modified in server code or using javascript

Can use jQuery $.map to reconfigure an object or array to another array.

DEMO: http://jsfiddle.net/qpsSe/1

Note that trailing commas in sample JSON need removing to validate JSON

/* "json" is your response object, modify variable names to match */
var chartData=$.map( json, function( obj,i){
        return [[ obj.status, obj.val]];                            
})

$.map API Docs

Alternate method in native javascript

var chartData=[];
for( i=0; i<json.length; i++){
   chartData.push( [ json[i]['status'], json[i]['val'] ])
}

这篇关于编写JSON解析器来格式化饼图的数据(HighCharts)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆