我的HighCharts可以正常使用data
内的数据作为序列,但我希望以data_New
这样的格式提供数据。如何更改我的HighChart设置以接受该格式? JSFiddle
var data = [{
name: 'Apples',
data: [5, 3, 4, 7, 2]
}, {
name: 'Oranges',
data: [2, 2, 3, 2, 1]
}, {
name: 'Plums',
data: [3, 4, 4, 2, 5]
}];
var data_New = [{
name: 'John',
Apples: 5,
Oranges: 2,
Plums: 3
}, {
name: 'Peter',
Apples: 3,
Oranges: 2,
Plums: 4
}, {
name: 'William',
Apples: 4,
Oranges: 3,
Plums: 4
},
{
name: 'Chris',
Apples: 7,
Oranges: 2,
Plums: 2
},
{
name: 'Mike',
Apples: 2,
Oranges: 1,
Plums: 5
}
];
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Stacked column chart'
},
xAxis: {
categories: ['John', 'Peter', 'William', 'Chris', 'Mike']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
shared: true
},
plotOptions: {
column: {
stacking: 'percent'
}
},
series: data_New
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
答案 0 :(得分:1)
Highcharts接受3种类型的数据集(Series
)。
在您的情况下,最简单的方法是使用Array.from()
将您的data_New
转换为Higcharts接受的结构。
示例:
var data_New = [{
name: 'John',
Apples: 5,
Oranges: 2,
Plums: 3
}, {
name: 'Peter',
Apples: 3,
Oranges: 2,
Plums: 4
}, {
name: 'William',
Apples: 4,
Oranges: 3,
Plums: 4
},
{
name: 'Chris',
Apples: 7,
Oranges: 2,
Plums: 2
},
{
name: 'Mike',
Apples: 2,
Oranges: 1,
Plums: 5
}];
var data = [{
name: 'Apples',
data: Array.from(data_New, function(a){ return a.Apples})
},{
name: 'Oranges',
data: Array.from(data_New, function(a){ return a.Oranges})
},{
name: 'Plums',
data: Array.from(data_New, function(a){ return a.Plums})
}]
console.log(data)
&#13;
您的更新fiddle
。
如果你有可变数据集结构,想要从第一个属性创建类别,从其他属性创建数据集,那就更抽象了:
var data_New = [{
name: 'John',
Apples: 5,
Oranges: 2,
Plums: 3
}, {
name: 'Peter',
Apples: 3,
Oranges: 2,
Plums: 4
}, {
name: 'William',
Apples: 4,
Oranges: 3,
Plums: 4
},
{
name: 'Chris',
Apples: 7,
Oranges: 2,
Plums: 2
},
{
name: 'Mike',
Apples: 2,
Oranges: 1,
Plums: 5
}];
// make categories from first property values
var firstProp = Object.keys(data_New[0])[0],
categories = Array.from(data_New, function(x){ return x[firstProp] });
// make data from all properties except first
var data = [];
for (i = 1; i < Object.keys(data_New[0]).length; i++) {
var prop = Object.keys(data_New[0])[i];
data.push({
name : prop,
data : Array.from(data_New, function(x){ return x[prop] })
})
}
console.log('categories:',categories,'\ndata:', data);
&#13;
唯一的条件是所有对象共享相同的数据结构。
答案 1 :(得分:0)
您可以轻松地将数据转换为Highcharts with Array.prototype.map
接受的格式sql = 'select RLAMBD from ' + OPTable
cursor.execute(sql)
print cursor.fetchone().RLAMBD