将JSON文档转换为谷歌图表格式
Converting JSON document to Google Chart format
我是Javascript的新手,我的情况是这样的:我正在使用谷歌图表来可视化一些数据,数据包含在Elasticsearch中。
我正在用Ajax命令查询数据,但是返回的数据在当前格式的谷歌图表中不可用。
查询返回如下数据:
{
took: 5
timed_out: false
_shards: {
total: 5
successful: 5
failed: 0
}
hits: {
total: 11
max_score: 1
hits: [
{
_index: inventory
_type: on_hand
_id: 4
_score: 1
_source: {
warehouse_id: 107
date: 03-28-2013
lane: M01
routes: 383
}
}
我需要为Google Charts设置如下格式:
{
"cols": [
{"id":"","label":"Lane","type":"string"},
{"id":"","label":"Routes","type":"number"}
],
"rows": [
{"c":[{"v":"M01"},{"v":4657}]},
{"c":[{"v":"M02"},{"v":4419}]},
{"c":[{"v":"M03"},{"v":4611}]},
{"c":[{"v":"M04"},{"v":4326}]},
{"c":[{"v":"M05"},{"v":4337}]},
{"c":[{"v":"M06"},{"v":5363}]}
]
}
虽然我不期望有人为我写代码,但如果有人能给我一个很好的起点,以提取所需的数据,并添加适当的格式,如"cols": [...
和"rows":[...
等,我将非常感激。谢谢你!
我能够运行一个更新的查询,以有效的JSON格式返回结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 7,
"max_score" : 1.0,
"hits" : [ {
"_index" : "wcs",
"_type" : "routes",
"_id" : "4",
"_score" : 1.0, "_source" : {"lane":"M04","routes":"102"}
}, {
"_index" : "wcs",
"_type" : "routes",
"_id" : "5",
"_score" : 1.0, "_source" : {"lane":"M03","routes":"143"}
}, {
"_index" : "wcs",
"_type" : "routes",
"_id" : "1",
"_score" : 1.0, "_source" : {"lane":"M07","routes":"80"}
}, {
"_index" : "wcs",
"_type" : "routes",
"_id" : "6",
"_score" : 1.0, "_source" : {"lane":"M02","routes":"157"}
}, {
"_index" : "wcs",
"_type" : "routes",
"_id" : "2",
"_score" : 1.0, "_source" : {"lane":"M06","routes":"101"}
}, {
"_index" : "wcs",
"_type" : "routes",
"_id" : "7",
"_score" : 1.0, "_source" : {"lane":"M01","routes":"105"}
}, {
"_index" : "wcs",
"_type" : "routes",
"_id" : "3",
"_score" : 1.0, "_source" : {"lane":"M05","routes":"160"}
} ]
}
}
然而,所需的JSON文档实际上需要完全像我在谷歌图表的原始帖子中所显示的那样能够使用它。需要从返回的数据(如上所示)中提取"lane"
和"routes"
值,并将其格式化为原始文章中的JSON文档。再次感谢。
您应该能够做如下操作:
var json = {
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 1,
"hits": [
{
"_index": "wcs",
"_type": "routes",
"_id": "4",
"_score": 1,
"_source": {
"lane": "M04",
"routes": "102"
}
}
]
}
};
var data = {};
data.cols = [
{
"id": "",
"label": "Lane",
"type": "string"
},
{
"id": "",
"label": "Routes",
"type":"number"
}
];
data.rows = [
{
"c": [
{
"v": json.hits.hits[0]._source.lane
},
{
"v": json.hits.hits[0]._source.routes
}
]
}
];
console.log(data);
相关文章:
- 在表格格式中使用ng重复
- 获取表格格式的 Javascript 输出
- 如何使用 JQuQuery 解析 JSON 对象并在 HTML 页面中以表格格式打印输出
- 如何使用 angularjs 以表格格式显示 parse.com 的查询结果
- 格式为nnnnnnnn的字符串在电子表格中显示为日期或不正确
- 如何将角度生成的表格从数据库导出为csv格式
- 使用PHP将表格数据保存为CSV格式
- HTML
双击显示包含表格格式的新弹出框 - 使用缩写字词设置谷歌图表表格列的格式
- AngularJS/HTML 表格格式
- 以表格格式排列JSON数据
- Javascript自定义函数,用于将JSON格式的YQL查询响应导入谷歌电子表格
- 如何以表格格式显示json数组
- 将表格导出为CSV字符串格式问题到单独的单元格中
- HTML表格到Excel格式&扩展don't匹配
- 有哪些方法可以以非表格格式显示Angular中的JSON数据
- 无法使用Javascript以html格式打印表格
- 以表格格式显示JSON结果
- 如何在Java脚本弹出窗口中显示表格格式的数据
- 如何以带有数据的表格格式发送邮件