显示基于网格数据的剑道图(饼图)

时间:2017-11-20 13:14:48

标签: javascript jquery kendo-ui kendo-grid kendo-chart

我正在使用KendoUI - 网格组件

如何将其转换为Kendo Grid:

对于Eg: 我使用本地数据创建了kendo网格(表)。只要点击“生成图表”按钮,上面的表格过滤数据就会创建如下的剑道饼图......

由于我是剑道新手,有人可以给我一个答案吗?

enter image description here

代码:

var localData = [
  {
    Id: 1,
    userName: "John",
    game: "A",
    difficultyLevel: "Easy",
    startTime: "25-05-2017",
    endTime: "26-05-2017",
    score: "210"
  },
  {
    Id: 2,
    userName: "Sam",
    game: "B",
    difficultyLevel: "Hard",
    startTime: "04-11-2016",
    endTime: "01-12-2016",
    score: "4800"
  },
];

var dataSource = new kendo.data.DataSource({
  data: localData,
  schema: {
    model: {
      fields: {
        Id: {
          type: "number"
        },
        userName: {
          type: "string"
        },
        userName: {
          type: "string"
        },
        difficultyLevel: {
          type: "string"
        },        
        startTime: {
          type: "string"
        },
        endTime: {
          type: "string"
        },
        score: {
          type: "number"
        },
      }
    }
  }
});

$("#grid").kendoGrid({
  dataSource: dataSource,
  rowTemplate: $.proxy(kendo.template($("#rowTemplate").html()), dataSource),
  scrollable: true,
  height: 300,
  sortable: true,
  filterable: false,
  groupable: true,
  pageable: true,
  columns: [{
    field: "Id",
    title: "Id",
    filterable: true
  }, {
    field: "userName",
    title: "userName"
  }, {
    field: "game",
    title: "game"
  }, {
    field: "difficultyLevel",
    title: "difficultyLevel"
  }, {
    field: "startTime",
    title: "startTime"
  }, {
    field: "endTime",
    title: "endTime"
  }, {
    field: "score",
    title: "score"
  }]
});

JsFiddle

1 个答案:

答案 0 :(得分:1)

您需要准备数据到图表的格式。类似的东西:

$chartContainer.kendoChart({
    dataSource: {
    data: localData,
    schema: {
      parse: function(data) {
        return data.map(x => { 
          return { 
            value: Number(x.score),
            category: x.userName
          }
        });
      }
    }
  },
    series: [{
    type: "pie",
    field: "value",
    categoryField: "category"
  }],
  tooltip: {
    visible: true,
    template: "#= category #: #= value #"
  }
});

Updated Fiddle