我的ruby后端生成一个JSON哈希,其中包含子类别数组的类别(每个子类别都有总值):
{"IT, Internet, Telecom"=>[["Testing", 3], ["Banking Software", 1], ["ERP", 2], ["IT Security", 2], ["Consulting, Outsourcing", 1], ["Project Management", 1]], "Accounting, Management Accounting, Corporate Finance"=>[["Offshore", 1], ["ACCA", 1]], "Administrative Personnel"=>[["Workflow Management", 1]]}
此哈希位于数据变量中。 我需要将其转换为带有总类别数的堆积条形图(highcharts)(For" IT"它是10)以及此类别中的堆叠子类别(3,1,2,2,1) ,1)。问题是我希望类别列包含每个类别的子类别无关数据。这是我已经尝试过的:
$(function () {
function drawChart(url) {
$.getJSON(url, function (data) {
$.each(data, function (i, point) {
point.y = point.data;
});
var chart = Highcharts.chart('seekers_by_category', {
chart: {
type: 'bar'
},
title: {
text: 'Seekers category'
},
tooltip: {
enabled: true,
pointFormat: '<b>{point.y}</b> seekers'
},
xAxis: {
type: 'category'
},
plotOptions: {
bar: {
stacking: 'normal',
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.y} seekers',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
},
series: {
stacking: 'normal'
}
},
series: [
{
name: 'Seekers',
data: data
}
]
})
});
}
var mainUrl = $('#seekers_by_category').data('url');
drawChart(mainUrl);
});
答案 0 :(得分:0)
最后我将JSON转换为这种格式:
[{:name=>"Testing", :data=>[["IT, Internet, Telecom", 3]]}, {:name=>"Banking Software", :data=>[["IT, Internet, Telecom", 1]]}, {:name=>"ERP", :data=>[["IT, Internet, Telecom", 2]]}, {:name=>"IT Security", :data=>[["IT, Internet, Telecom", 2]]}, {:name=>"Consulting, Outsourcing", :data=>[["IT, Internet, Telecom", 1]]}, {:name=>"Offshore", :data=>[["Accounting, Management Accounting, Corporate Finance", 1]]}, {:name=>"Workflow Management", :data=>[["Administrative Personnel", 1]]}, {:name=>"ACCA", :data=>[["Accounting, Management Accounting, Corporate Finance", 1]]}, {:name=>"Mutual Funds", :data=>[["Banks, Investments, Finance", 1]]}, {:name=>"Project Management", :data=>[["IT, Internet, Telecom", 1]]}, {:name=>"Assistant", :data=>[["Marketing, Advertising, PR", 1]]}]
使用:
g = {}
h.each do |k,n|
a, b = k
g[b] = [] unless g.key?(b)
g[b] << [a, n]
end
g.map{ |k, v| { name: k, data: v } }
并将系列更改为:
series: data