如何将数据绑定到 donut kendo 组件

How to bind data to donut kendo component

我正在尝试将数据绑定到甜甜圈 kendo 组件。到目前为止我的工作如下

js 文件:-

 function createChart() {
            $("#chart").kendoChart({
                dataSource: {
                    transport: {
                        read: {
                            url: "/ert.mvc/Summary?id=23",
                            dataType: "json",
                            type: "GET"
                        }
                    }
                },
                title: {
                    position: "bottom",
                    text: "Share of Internet Population Growth"
                },
                legend: {
                    visible: false
                },
                series: [{
                    data: [{
                        type: "donut",
                        field: "newlyRequested",
                        categoryField: "source",
                        explodeField: "explode"                        },
                        {
                            type: "donut",
                            field: "pending",
                            categoryField: "source",
                            explodeField: "explode"                               
                    }]

                }],
                seriesColors: ["#42a7ff", "#666666"],
                tooltip: {
                    visible: true,
                    template: "#= category # (#= series.name #): #= value #%"
                }
            });
        }

我的 api 回复如下:-

    {
  total: 120,
  pending: 25,
  incomplete: 10,
  newlyRequested: 10
}

我遵循了 https://demos.telerik.com/kendo-ui/donut-charts/donut-labels 的例子。 但我收到 kendo.all.js:7786 Uncaught TypeError: e.slice is not a function error 。 我的预期结果是我想显示带有未决、不完整...百分比的圆环图。

有什么想法吗

首先,您的 API 回复是错误的。它应该是一个对象数组,如下所示:

[ 
  {type: "total", value: 120},
  {type: "pending", value: 25},
  {type: "incomplete", value: 10},
  {type: "newlyRequested", value: 10}
]

根据上述 API 响应,您必须更改 series 配置。最后,您的圆环图配置应如下所示:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Kendo UI Snippet</title>

    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2021.3.914/styles/kendo.default-v2.min.css"/>

    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2021.3.914/js/kendo.all.min.js"></script>
</head>
<body>
    <div id="chart"></div>
    <script>
      $("#chart").kendoChart({
        dataSource: [
          {type: "total", value: 120},
          {type: "pending", value: 25},
          {type: "incomplete", value: 10},
          {type: "newlyRequested", value: 10}
        ],
        title: {
          position: "bottom",
          text: "Share of Internet Population Growth"
        },
        legend: {
          visible: false
        },
        series: [{
          type: "donut",
          field: "value",
          categoryField: "type",
        }],
        seriesColors: ["#42a7ff", "#666666"],
        tooltip: {
          visible: true,
          template: "#= category # (#= kendo.format('{0:P}', percentage) #): #= value #"
        },
        seriesDefaults: {
          labels: {
            template: "#= category # - #= kendo.format('{0:P}', percentage)#",
            position: "outsideEnd",
            visible: true,
            background: "transparent"
          }
        },
      });
    </script>
</body>
</html>

我添加了一个系列标签并修改了工具提示以使图表看起来更好。你可能想看看更多 chart examples here.