将Kendo UI Dataviz图表系列绑定到特定模型

时间:2014-03-30 21:10:37

标签: json kendo-ui kendo-dataviz

是否可以从具有

等结构的远程数据源创建Kendo UI DataViz图表
    "gender": [
        {"male": 23421}, 
        {"female": 24376},
        {"unknown": 324}
        ], 

而不是使用(来自文档中的示例)

<div id="chart"></div>
<script>
$("#chart").kendoChart({
  categoryAxis: {
    field: "year"
  },
  series: [
    { field: "value" }
  ],
  dataSource: [
    { year: "2012", value: 1 },
    { year: "2013", value: 2 }
  ]
});
</script>

我想使用格式为

的数据源
<div id="chart"></div>
<script>
$("#chart").kendoChart({
  categoryAxis: {
    field: "year"
  },
  series: [
    { field: "value" }
  ],
  dataSource: [
    { "2012": 1 },
    { "2013": 2 }
  ]
});
</script>

1 个答案:

答案 0 :(得分:0)

嗯,这是Javascript中的直接转换。

convertDataSource = function(dataSource) {
  for(i in dataSource) {
    (y = {})[dataSource[i].year] = dataSource[i].value;
    dataSource[i] = y;
  }
  return dataSource;
}
convertDataSource([ { year: "2012", value: 1 }, { year: "2013", value: 2 }])
// [ { "2012": 1, "2013": 2}