Building the series of a kendo chart by looping through the json data?

女生的网名这么多〃 提交于 2019-12-25 02:42:55

问题


I am having some difficulty figuring out how to solve this problem. Basically I have some JSON returned that looks like this:

{
"pie": {
"slice": [
  {
    "-ch": "1",
    "-val": "0.0797435897435897"
  },
  {
    "-ch": "2",
    "-val": "0.00743589743589744"
  },
  {
    "-ch": "3",
    "-val": "0.247435897435897"
  },
  {
    "-ch": "4",
    "-val": "0.497179487179487"
  },
  {
    "-ch": "5",
    "-val": "0.168205128205128"
  }
]
}
}

I am getting this data from the controller to the javascript just fine. However, I want to bind the data to a pie chart. The only issue is that, I will have varying amounts of channels. In this example, I have 5 channels. My pie chart in javascript looks like this:

$("#chartDiv").kendoChart({
            title: {
                position: "bottom",
                text: "Chart"
            },
            legend: {
                visible: false
            },
            chartArea: {
                background: "transparent"
            },
            seriesDefaults: {
                type: "donut",
                startAngle: 150
            },
            series: [
            {
                name: "Chart",
                data: ???????How to get the data here????????
            }],
            tooltip: {
                visible: true,
                template: "#= category #: #= value #%"
            }
        });
    }
});

I have tried building a loop within the call, I have also tried string building the definition but it doesn't seem to like that solution either(I might of implemented this incorrectly though.) Thanks ahead for your help.


回答1:


I figured it out.

set data: dataVariable and define dataVariable like this:

for (var i = 0; i < json.Slices.length; i++) {
                dataVariable.push({
                    category: json.Slices[i].Ch,
                    value: parseFloat(json.Slices[i].val)
                });
        };


来源:https://stackoverflow.com/questions/25270719/building-the-series-of-a-kendo-chart-by-looping-through-the-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!