分组和堆积柱形图
Grouped and Stacked Column Chart
我有一个返回 JSON 的服务,例如:
{
"data": [
{
"portfolio": "Portfolio 01",
"REGN_NME": "Africa/Middle E",
"Regn_Group": "Emerging",
"sector": "Sector 08",
"exposure": 0.109977544284
},
{
"portfolio": "Portfolio 01",
"REGN_NME": "Americas",
"Regn_Group": "Frontier",
"sector": "Sector 06",
"exposure": 1.78919403995e-09
},
...
]}
我正在尝试生成分组堆叠柱形图。我需要按 "portfolio" 分组,按 "Regn_Group" 堆叠,然后聚合 "exposure"。
我可以将图表堆叠或分组,但不能同时进行。
这是我目前所拥有的
<script id="toolTipTemplate" type="text/x-kendo-tmpl">
#= series.name # - #= kendo.toString(value || 0,'p2') #
</script>
<script type="text/javascript">
$(function() {
$("#chart1").kendoChart({
title: {text: "Portfolio Sector Exposure"},
dataSource: {
transport: {
read: {
url:"exposure2.json"
}
},
schema: {
data: "data"
},
group: [{
field: "portfolio"
}],
stack: "Regn_Group"
},
legend:{
position:"bottom"
},
series: [{
type: "column",
field: "exposure",
categoryField: "sector",
aggregate: "sum"
}],
tooltip: {
visible: true,
template: $("#toolTipTemplate").html(),
}
});
});
</script>
根据文档,聚合函数仅适用于日期系列:
http://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart#configuration-series.aggregate
对于堆叠:
尝试在图表的系列属性中添加堆栈:true:
series: [{
type: "column",
name: "#= group.value #",
field: "exposure",
stack: true
}],
评论更新:
您可以使用数据源对象分组及其获取事件来转换数据以构建单独的系列和堆栈:
theseries = [];
thesectors = [];
var dataDS = new kendo.data.DataSource({
data: monthly, //the raw JSON Data
group: [
{field: "portfolio"},
{field: "Regn_Group"},
],
sort: { field: "sector", dir: "asc" }
});
//convert the data
dataDS.fetch(function(){
var view = dataDS.view();
for (i = 0; i < view.length; i++) {
var portfolio = view[i];
for (p = 0; p < portfolio.items.length; p++) {
var regn = portfolio.items[p];
var series = {};
series.name = regn.value;
series.stack = portfolio.value;
series.data = [];
for (j=0; j<regn.items.length; j++){
var data = regn.items[j];
if (i==0 && p==0) {
thesectors.push(data.sector);
}
series.data.push(data.exposure)
}
theseries.push(series);
}
}
});
Updated DEMO
我有一个返回 JSON 的服务,例如:
{
"data": [
{
"portfolio": "Portfolio 01",
"REGN_NME": "Africa/Middle E",
"Regn_Group": "Emerging",
"sector": "Sector 08",
"exposure": 0.109977544284
},
{
"portfolio": "Portfolio 01",
"REGN_NME": "Americas",
"Regn_Group": "Frontier",
"sector": "Sector 06",
"exposure": 1.78919403995e-09
},
...
]}
我正在尝试生成分组堆叠柱形图。我需要按 "portfolio" 分组,按 "Regn_Group" 堆叠,然后聚合 "exposure"。
我可以将图表堆叠或分组,但不能同时进行。
这是我目前所拥有的
<script id="toolTipTemplate" type="text/x-kendo-tmpl">
#= series.name # - #= kendo.toString(value || 0,'p2') #
</script>
<script type="text/javascript">
$(function() {
$("#chart1").kendoChart({
title: {text: "Portfolio Sector Exposure"},
dataSource: {
transport: {
read: {
url:"exposure2.json"
}
},
schema: {
data: "data"
},
group: [{
field: "portfolio"
}],
stack: "Regn_Group"
},
legend:{
position:"bottom"
},
series: [{
type: "column",
field: "exposure",
categoryField: "sector",
aggregate: "sum"
}],
tooltip: {
visible: true,
template: $("#toolTipTemplate").html(),
}
});
});
</script>
根据文档,聚合函数仅适用于日期系列: http://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart#configuration-series.aggregate
对于堆叠:
尝试在图表的系列属性中添加堆栈:true:
series: [{
type: "column",
name: "#= group.value #",
field: "exposure",
stack: true
}],
评论更新:
您可以使用数据源对象分组及其获取事件来转换数据以构建单独的系列和堆栈:
theseries = [];
thesectors = [];
var dataDS = new kendo.data.DataSource({
data: monthly, //the raw JSON Data
group: [
{field: "portfolio"},
{field: "Regn_Group"},
],
sort: { field: "sector", dir: "asc" }
});
//convert the data
dataDS.fetch(function(){
var view = dataDS.view();
for (i = 0; i < view.length; i++) {
var portfolio = view[i];
for (p = 0; p < portfolio.items.length; p++) {
var regn = portfolio.items[p];
var series = {};
series.name = regn.value;
series.stack = portfolio.value;
series.data = [];
for (j=0; j<regn.items.length; j++){
var data = regn.items[j];
if (i==0 && p==0) {
thesectors.push(data.sector);
}
series.data.push(data.exposure)
}
theseries.push(series);
}
}
});
Updated DEMO