2

大家好,我有一个看起来像这样的 json 数据

 [ "Data":[ 
 {"Id":"1  ","EmployeeName":Anil},
 ]

我从我的控制器动作中得到的

       public ActionResult Index()
    { 
        var data = GetData();
        return View();
    }

    public JsonResult GetData()
    {
        int Param1;
        Param1 = 1;     
       var EmployeeDetails = db.EmployeeDetails.ToList<EmployeeDetail>().Select(e =>  
        new
         {
        id=e.EmployeId,
        Name = e.EmployeeName
       });
        return Json(EmployeeDetails , JsonRequestBehavior.AllowGet);
    }

现在我想要这个 json 数据到饼图..我该怎么做

4

2 回答 2

6

在这里,不要在 action 方法中调用您的 jsonresult 方法,而是使用 $.getJSON 方法

  public ActionResult Index()
    { 

    return View();
     }

  public JsonResult GetData()
   {
    int Param1;
    Param1 = 1;     
     var EmployeeDetails = db.EmployeeDetails.ToList<EmployeeDetail>().Select(e =>  
    new
     {
    id=e.EmployeId,
    Name = e.EmployeeName
   });
    return Json(EmployeeDetails , JsonRequestBehavior.AllowGet);
  }

在这里,您可以像这样调用您的方法并将其绑定到您的图表

     <script type="text/javascript">
  $(function () {
      $.getJSON('<%= Url.Action("YourMethod","YourControllerName") %>', {}, function (data) {
          var json = data;              
          var jsondata = [];              
          for (var i in json) {
             // var serie = new Array(json[i].Projects, json[i].Bugs);
              jsondata.push([json[i].EmployeId, json[i].EmployeeName]);
          }              
      var chart = new Highcharts.Chart({
          chart: {
              renderTo: 'container',
              type: 'pie',
               plotBackgroundColor: null,
               plotBorderWidth: null,
               plotShadow: false
          },
           title: {
               text: 'Resource Reports of BugTracker'
           },

          plotOptions: {
              pie: {
                  showInLegend: true,
                  animation: false,
                  allowPointSelect: true,
                  cursor: 'pointer'
              }
          },
          legend: {
              enabled: true
          },
          series: [{
              type: 'pie',                 
              data: jsondata
          }]
      });
      });
  });
    </script>
于 2012-08-24T09:43:20.007 回答
0

无论您拥有什么类型的数据,创建饼图都相当简单。在这里查看一个工作示例:http: //livecoding.io/3437833

var chart = new Highcharts.Chart({
  chart: {
    renderTo: 'container',
    plotBackgroundColor: null,
    plotBorderWidth: null,
    plotShadow: false
  },
  plotOptions: {
    pie: {
      animation: false,
      allowPointSelect: true,
      cursor: 'pointer'
    }
  },
  series: [{
    type: 'pie',
    name: 'Browser share',
    data: [
      ['Firefox',   45.0],
      ['IE',       26.8],
      ['Safari',    8.5],
      ['Opera',     6.2],
      ['Others',   0.7]
    ]
  }]
});
于 2012-08-23T15:50:31.983 回答