我有这样的数据
这是我的Json数据格式
[{ "Value1":"B95 ",
"Value2":1.2600,
"ChartType":"column",
"ChartDescription":"Fuel Sales Quantity",
"Value3":"2015-07-27",
"SeriesField":"Value2",
"CategoryField":"Value3",
"IsGroup":true,
"IsStacked":true,
"GroupValue":"Value1",
"SortValue":"Value3"},
{"Value1":"B93",
"Value2":8.2100,
"ChartType":"column",
"ChartDescription":"Fuel Sales Quantity",
"Value3":"2015-07-22",
"SeriesField":"Value2",
"CategoryField":"Value3",
"IsGroup":true,
"IsStacked":true,
"GroupValue":"Value1",
"SortValue":"Value3"}]
我只是想用剑道柱形图每天创建一个分组堆积图表 但是当我绘制这张图表时,我有6个独特的日子,但图表只显示了4天 这里的图表示例......
数据混合或相互转移......
这是我的Javascript代码
function createChart(number) {
$("#chart-" + number).kendoChart({
dataSource: myData,
title: {
text: title
},
legend: {
visible: true,
position: "bottom",
labels: {
template: '#: chartType == "pie" ? dataItem.Value1 : chartType == "donut" ? dataItem.Value1 : text #'
}
},
seriesDefaults: {
type: chartType,
stack: stackValue
},
series: series,
valueAxis: {
labels: {
format: "{0}"
}
},
categoryAxis: categories,
tooltip: {
visible: true,
format: "{0}",
template: "#= dataItem.Value1 #: #= kendo.format('{0:N}',value) #"
}
});
}
$(document).ready(function () {
$.ajax({
url: '../Dashboards/QuerySelected',
data: { id: number, from: fromdate, to: todate, fleet: fleetident },
success: function (data) {
if (data.length != 0) {
if (data[0].IsGroup) {
myData = {
data: data,
group:{
field: data[0].GroupValue
},
sort: {
field: data[0].SortValue,
dir: "asc"
}
}
}
else {
myData = data
}
series = [{
field: data[0].SeriesField
}];
categories = {
field: data[0].CategoryField
}
stackValue = data[0].IsStacked;
chartType = data[0].ChartType;
title = data[0].ChartDescription;
createChart(number);
}
}
});
}
我的错误是否可能在图表上分组或排序? 我怎么解决这个问题 感谢名单 索尔
答案 0 :(得分:1)
您可以将数据转换为javascript中的数据,如下所示:
[
{"Category": "2015-08-10", "B93": 1.2500, "DIESEL": 1.6, "B95": 0 },
{"Category": "2015-08-06", "B93": 0, "DIESEL": 1.68, "B95": 0 }
]
一种方法是:
var ToolTipTemplate = "#= dataItem.Value1 #: #= kendo.format('{0:N}',value) #";
if (data.length != 0) {
if (data[0].IsGroup) {
var uniqueGroups = [];
var transformedData = [];
series = [];
myData = new kendo.data.DataSource({
data: data,
group: {
field: data[0].GroupValue
},
});
myData.fetch(function () {
var v = this.view();
for (var i=0; i<v.length; i++){
uniqueGroups.push(v[i].value)
series.push({
name: v[i].value,
field: v[i].value,
categoryField: "Category",
});
}
});
myData = new kendo.data.DataSource({
data: data,
group: {
field: data[0].SortValue
},
sort: {
field: data[0].SortValue,
dir: "asc"
},
});
myData.fetch(function () {
var v = this.view();
for (var i=0; i<v.length; i++){
var dataPoint = {"Category": v[i].value};
for (var j=0; j<uniqueGroups.length; j++){
dataPoint[uniqueGroups[j]] = 0;
}
for (var k=0; k<v[i].items.length; k++){
dataPoint[v[i].items[k][v[i].items[k].GroupValue]] = v[i].items[k][v[i].items[k].SeriesField];
}
transformedData.push(dataPoint);
}
});
console.log(transformedData);
myData = transformedData;
ToolTipTemplate = "#= series.name #: #= kendo.format('{0:N}',value) #";
}
else {
myData = data
series = [{
field: data[0].SeriesField
}];
categories = {
field: data[0].CategoryField
}
}
<强> DEMO 强>