简体   繁体   English

如何制作饼图聚合数据源?

[英]How to make a Pie Chart Aggregate the Data Source?

Using Kendo UI Complete for ASP.NET MVC , version: 2013.3 1119 (Nov 20, 2013)... 使用Kendo UI Complete for ASP.NET MVC ,版本:2013。3,1119(2013年11月20日)...

If I have this bit of code: 如果我有这段代码:

$("#status-chart").kendoChart({
    dataSource: {
        data:  [
            {Status: 10}, 
            {Status: 20}, 
            {Status: 200}, 
            {Status: 200}
        ]
    },                
    series: [{
        field: 'Status',
        categoryField: "Status",
        aggregate: 'count'
    }]            
});

I get this chart: 我得到这个图表:

条形图 - 好

As you can see - Status 10 and 20 have got a value of 1 and Status 200 a value of 2. 如您所见 - 状态10和20的值为1,状态200的值为2。

Great, but what I actually want is exactly the same thing in a pie chart (so, a chart with 3 pie slices, 2 of which are exactly the same size and one that is 2 times as big as the rest). 太棒了,但我真正想要的是在饼图中完全相同的东西(所以,一个包含3个饼图的图表,其中2个是完全相同的尺寸,另一个是其余的2倍)。

I therefore thought to myself, all I need to do is just set type: "pie" like so: 因此,我想,我需要做的就是设置type: "pie"就像这样:

$("#status-chart").kendoChart({ 
    dataSource: {
        data:  [
            {Status: 10}, 
            {Status: 20}, 
            {Status: 200}, 
            {Status: 200}
        ]
    },                
    series: [{
        field: 'Status',
        categoryField: "Status",
        aggregate: 'count',
        type: "pie"
    }]            
});

But that produced this chart: 但是这产生了这个图表:

饼图 - 糟糕

You can see that Status 200 is repeated and the value is determining the size of the slices. 您可以看到状态200重复,并且值正在确定切片的大小。

So, here is my question: 所以,这是我的问题:

How can I create a pie chart that looks like the picture below but which is bound to the data source in the first code snippet above? 如何创建一个如下图所示但在上面第一个代码片段中绑定到数据源的饼图?

饼图 - 好

Incidentally, the reason I do not want to change the data source is that I wish to share it with a grid. 顺便说一句,我不想​​更改数据源的原因是我希望与网格共享它。

What you are trying to do here is to group a shared DataSource and have it only affect one widget. 您在这里要做的是对共享的DataSource进行分组,并使其仅影响一个小部件。 Furthermore, Kendo UI will return a grouped object when you group it. 此外,Kendo UI将在您对其进行分组时返回分组对象。 The Pie chart is not interested in these objects, but rather the count of the items that each of these group objects contains. 饼图对这些对象不感兴趣,而是对每个组对象包含的项的计数。 We just need to get the data in the right format. 我们只需要以正确的格式获取数据。

So you have your original DataSource (which I have extracted since it's shared with another widget). 所以你有原始的DataSource(我已经提取了它,因为它与另一个小部件共享)。 When that DataSource changes, you want to populate a second one - one that you can group without affecting the grid. 当该DataSource发生更改时,您希望填充第二个 - 您可以在不影响网格的情况下对其进行分组。

var ds = new kendo.data.DataSource({
  data:  [
    {Status: 10}, 
    {Status: 20}, 
    {Status: 200}, 
    {Status: 200}
  ],
  change: function() {
    chartData.data(this.data());
  }
});

The second DataSource (chartData) is grouped, and when it changes, it populates an array, constructing objects that the pie chart can actually understand. 第二个DataSource(chartData)被分组,当它发生变化时,它会填充一个数组,构建饼图可以实际理解的对象。

var groupedData = [];

// populate the grouped data array by grouping this datasource
// and then populating an plain array
var chartData = new kendo.data.DataSource({
  group: { field: 'Status' },
  change: function() {
    groupedData = [];
    $.each(this.view(), function() {
      groupedData.push({ field: this.value, value: this.items.length });
    });
  }
});

And then just bind your pie chart to that array 然后将您的饼图绑定到该数组

$("#status-chart").kendoChart({
  dataSource: groupedData,
  series: [{
    type: 'pie',
    field: 'value',
    categoryField: 'field'
  }]            
});

Working example: http://jsbin.com/EKuxORA/1/edit 工作示例: http//jsbin.com/EKuxORA/1/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM