如何使用 json 数据绘制高图表
How to plot a HighChart with the json data
我对图形有点陌生,并尝试了各种图形框架。我有一个 json 数据,想从中绘制一个多系列折线图。我已经为现在在Highchart中尝试的d3js工作了。我的问题是如何嵌套轴的数据。病例数 - Y 轴案例创建月 - a 轴组名称 - 是行。
提前谢谢。
JSFIDDLE 链接
http://jsfiddle.net/j_verma/juj55050/
var data = [{
"groupName": "Casio GzOne",
"caseCount": 8,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "GALAXY",
"caseCount": 80,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "HTC ",
"caseCount": 14,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "LG Mobile Phones",
"caseCount": 27,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "Motorola",
"caseCount": 29,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "Nokia Lumia",
"caseCount": 3,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "Sony Ericsson Xperia",
"caseCount": 4,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "Verizon Ellipsis",
"caseCount": 18,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "iPhone 5",
"caseCount": 29,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "iPhone 6",
"caseCount": 33,
"caseCreatedMonth": "10-2014"
}, {
"groupName": "Casio GzOne",
"caseCount": 4,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "DEFAULT",
"caseCount": 32,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "GALAXY",
"caseCount": 83,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "HTC",
"caseCount": 14,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "HTC ",
"caseCount": 7,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "LG Mobile Phones",
"caseCount": 14,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "Motorola",
"caseCount": 29,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "Nokia Lumia",
"caseCount": 3,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "Sony Ericsson Xperia",
"caseCount": 3,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "Verizon Ellipsis",
"caseCount": 3,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "iPHONE",
"caseCount": 14,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "iPhone 5",
"caseCount": 13,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "iPhone 6",
"caseCount": 16,
"caseCreatedMonth": "11-2014"
}, {
"groupName": "DEFAULT",
"caseCount": 15,
"caseCreatedMonth": "12-2014"
}, {
"groupName": "GALAXY",
"caseCount": 22,
"caseCreatedMonth": "12-2014"
}, {
"groupName": "Motorola",
"caseCount": 3,
"caseCreatedMonth": "12-2014"
}, {
"groupName": "Nokia Lumia",
"caseCount": 1,
"caseCreatedMonth": "12-2014"
}, {
"groupName": "Samsung Galaxy Note",
"caseCount": 4,
"caseCreatedMonth": "12-2014"
}, {
"groupName": "iPhone 6",
"caseCount": 4,
"caseCreatedMonth": "12-2014"
}]
console.log('Sorting Data');
var groupMap = [];
for(var i = 0;i < data.length ; i++){
console.log('Reading '+i);
var d = data[i];
if(d == undefined)
continue;
var arr = groupMap[d.groupName];
if(arr == undefined){
console.log('Adding new '+d.groupName);
groupMap[d.groupName] = {
name: d.groupName,
data: new Array()
};
arr = groupMap[d.groupName].data;
} else {
arr = arr.data;
console.log('Not Adding New :' +d.groupName);
}
arr.push(
d.caseCount
);
}
console.log(groupMap);
$('#container').highcharts({
title: {
text: 'Request Resolved',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['10-2014', '11-2014', '12-2014', ]
},
yAxis: {
min: 0,
title: {
text: 'Case Count'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series:groupMap
});
});
我解决了。任何有类似问题的人都可以检查一下这个小提琴。
http://jsfiddle.net/j_verma/juj55050/2/
$(函数 () {
var seriesData = [];
var xCategories = [];
var i, cat;
for(i = 0; i < jsonMonthData.length; i++){
cat = '' + jsonMonthData[i].caseCreatedMonth;
if(xCategories.indexOf(cat) === -1){
xCategories[xCategories.length] = cat;
}
}
console.log(xCategories);
for(i = 0; i < jsonMonthData.length; i++){
if(seriesData){
var currSeries = seriesData.filter(function(seriesObject){ return
seriesObject.name == jsonMonthData[i].groupName;});
if(currSeries.length === 0){
seriesData[seriesData.length] = currSeries = {name:
jsonMonthData[i].groupName, data: []};
}
否则 {
currSeries = currSeries[0];
}
var index = currSeries.data.length;
currSeries.data[index] = jsonMonthData[i].caseCount;
}
否则 {
seriesData[0] = {name: jsonMonthData[i].groupName, data:
[jsonMonthData[i].caseCount]}
}
};
$('#container').highcharts({
title: {
text: 'Request Resolved',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: xCategories
},
yAxis: {
min: 0,
title: {
text: 'Case Count'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series:seriesData
});
});
相关文章:
- 如何访问高图表工具提示中的任何特定数据
- 将图表动态添加到组合高图表中
- 在Firefox中导出高图表时出现黑色图像错误
- 在高图表中,每x步只显示标签
- 高图表的文本溢出问题
- 单击时获取父级子级高图表
- 高图表不使用 JSON 格式绘制
- 如何在高图表中使用系列数据绘制折线图
- 通过 sql 数据绘制高图表柱形图颜色
- 如何使用 json 数据绘制高图表
- 在高图表的最后一个点上动态绘制标记
- 使用高图表绘制实时数据图
- 从 JSON 在单个高图表图中绘制多个序列
- 高图表绘制具有多个 xAxis 和 yAxis 的线条
- 如何在高图表中绘制重叠和非重叠的多个段
- 每次更改数据集时,高图表是否都会重新绘制
- 在高图表中绘制以下 json
- 如何从一个数据数组绘制图表并从另一个数据数组在图表上制作标签?高图表 v.4.0.4.
- 高图表:存在工具提示,但图表中未绘制线条
- 如何在高图表中绘制值为0的列