Search code examples
jqueryajaxkendo-uikendo-gridkendo-asp.net-mvc

How to bind data to donut kendo component


I am trying to bind data to donut kendo component. up to now my work is below

js file :-

 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 #%"
                }
            });
        }

My api response like :-

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

I followed https://demos.telerik.com/kendo-ui/donut-charts/donut-labels example. but I am getting kendo.all.js:7786 Uncaught TypeError: e.slice is not a function error . my expected result is i want to show donut chart with pending ,incomplete... percentage by total .

any idea please


Solution

  • First off, your API response is wrong. It should be an array of objects like so:

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

    Based on the above API response, you'll have to change your series configuration. In the end, your donut chart configuration should be something like below:

    <!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>

    I've added a series label and modified the tooltip to make the chart look better. You might want to look at more chart examples here.