分组和堆叠柱状图

  • 本文关键字: kendo-ui kendo-dataviz
  • 更新时间 :
  • 英文 :


我有一个返回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>

根据文档,聚合函数仅适用于Date Series:http://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart configuration-series.aggregate

用于堆叠:

尝试在图表的系列属性中添加stack: true:

   series: [{
       type: "column",
       name: "#= group.value #",
       field: "exposure",
       stack: true
   }], 

UPDATE FROM COMMENTS:

您可以使用数据源对象分组及其获取事件来转换数据,以构建单独的系列和堆栈:

  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);
      }          
    }
  });
<

更新演示/strong>

相关内容

  • 没有找到相关文章

最新更新