简体   繁体   English

如何推送数据以创建饼图-图表JS

[英]How to push data to create pie chart - chart js

I have data from SQL in json code file (checked in Fiddler) 我在json代码文件中有来自SQL的数据(已在Fiddler中检查)

{"d":[{"__type":"Dashboards.Employee","name":"Test Hen","turnover":"1500000,0000","color":"#231F20"},{"__type":"Dashboards.Employee","name":"Test Bai","turnover":"130000,0000","color":"#FFC200"}]} {“ d”:[{“ __ type”:“ Dashboards.Employee”,“名称”:“ Test Hen”,“营业额”:“ 1500000,0000”,“颜色”:“#231F20”},{“ __ type” :“ Dashboards.Employee”,“名称”:“ Test Bai”,“营业额”:“ 130000,0000”,“颜色”:“#FFC200”}]}

but i dont know, how to push them correctly in order to create pie chart 但我不知道,如何正确地推动它们以创建饼图

my ajax/javascript is here: 我的ajax / javascript在这里:

  $.ajax({
                   url: 'HelloService.asmx/GetEmployeeDetail',
                   contentType: 'application/json;charset=utf-8',
                   data: JSON.stringify({ month: number }),
                   dataType: 'json',
                   method: 'post',                     
                   success: OnSuccess_,
                   error: OnErrorCall_
               });

               function OnSuccess_(response) {


                   var aData = response.d;
                   var arr = [];
                   //var ctx = document.getElementById('pele').getContext('2d');
                   $.each(aData, function (inx, val) {
                       var obj = {};
                       obj.label = val.name;
                       obj.value = val.turnover;
                       obj.color = val.color;
                       arr.push(obj);
                   });
                   var ctx = $("#pele").get(0).getContext("2d");
                   var myPieChart = new Chart(ctx).Pie(arr);}


                   function OnErrorCall_(response) {
                   console.log(error);
                     }

                 });

             });

Am I missing something? 我想念什么吗?

If i use static values (value, label, color) pie chart works without problem. 如果我使用静态值(值,标签,颜色),则饼图可以正常工作。
Thank you 谢谢

I created the following FiddleJS for you: https://jsfiddle.net/cukyrh5h/1/ 我为您创建了以下FiddleJS: https ://jsfiddle.net/cukyrh5h/1/

You need to replace the URL of the Ajax call with yours of course. 当然,您需要将Ajax调用的URL替换为您的URL。 You had some wrong syntax (too much braches in the end of your Snippet) and the API you were using was not working with ChartJS 2.4 anymore. 您的语法错误(代码段末尾有太多分支),并且所用的API不再适用于ChartJS 2.4。

The code looks like the following: 该代码如下所示:

$.ajax({
    url:"/echo/json/",
    data:data,
    type:"POST",
    success:OnSuccess_
});

function OnSuccess_(response) {
  var aData = response.d;
  var data = {
    labels: [],
    datasets: [{
      data: [],
      backgroundColor: []
    }]
  };

  $.each(aData, function (inx, val) {
    data.labels.push(val.name);
    data.datasets[0].data.push(val.turnover);
    data.datasets[0].backgroundColor.push(val.color);
  });

  var ctx = $("#pele").get(0).getContext("2d");
  var myPieChart = new Chart(ctx, {
    type: 'pie',
    data: data
  });
}

function OnErrorCall_(response) {
  console.log(error);
}

Ok, i found problem, why i cant see my Chart. 好的,我发现了问题,为什么我看不到我的图表。 Maybe it will be useful for another members. 也许对其他成员有用。 Data in Database (turnover was daclared as money, i changed it to int .... and it works) 数据库中的数据(营业额被声明为货币,我将其更改为int ....并且有效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM