KendoUI - DataViz - Grouping and Aggregating a JSON datasource within the chart

╄→尐↘猪︶ㄣ 提交于 2019-12-24 02:13:08

问题


I've been able to accomplish such grouping in certain charts and not others. For example, assume that I have a datasource that looks something like this:

var pictures = [
  { type: "JPG", len: 20, wid: 15, appr: 17.5, count: 2 },
  { type: "JPG", len: 22, wid: 17, appr: 12.5, count: 3 },
  { type: "JPG", len: 24, wid: 15, appr: 10.5, count: 1 },
  { type: "JPG", len: 22, wid: 4, appr: 17.5, count: 6 },
  { type: "PNG", len: 20, wid: 15, appr: 17.5, count: 4 },
  { type: "PNG", len: 25, wid: 7, appr: 9.5, count: 4 },
  { type: "PNG", len: 21, wid: 11, appr: 21.5, count: 1 }
];

I want to group by my category which in this case is "type", I want to sum all other fields.

I am able to do this quite simply with column charts, simply by setting:

series: [{aggregate: "sum",categoryField: "type"}]

However, this does not work with bullet or pie charts, so in search of a more universal method. I "massage" the data first by adding this at the beginning:

var dataSource = new kendo.data.DataSource({
  data: pictures,
  group: {
    field: "type",
    aggregates: [
        { field: "len", aggregate: "sum" },
        { field: "wid", aggregate: "sum" }
    ]
  }
});

dataSource.read();

I don't know why just grouping the datasource like so isn't enough.. I have to throw it into arrays and assign each array to a series like so:

var seriesA = [],
  seriesB = [],
  categories = [],
  items = dataSource.view(),
  length = items.length,
  item;

for (var i = 0; i < length; i++) {
  item = items[i];

  seriesA.push(item.aggregates.wid.sum);
  seriesB.push(item.aggregates.len.sum);
}

This works as long as having only one valueField per series works. In the case of a bullet chart, or if I want to assign a field to a plotband to a bar/column chart: it does not.

I believe I must be doing something completely wrong, as converting my ungrouped JSON object to a grouped kendo datasource should be enough. Below is my JSFiddle..

http://jsfiddle.net/DxMb8/4/

Any help would be greatly appreciated! thanks!

来源:https://stackoverflow.com/questions/19459097/kendoui-dataviz-grouping-and-aggregating-a-json-datasource-within-the-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!