如何在饼图HighChart中显示json数据?

时间:2012-08-23 13:49:41

标签: asp.net-mvc-3 highcharts pie-chart

我所有的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数据变成饼图。我可以这样做吗

2 个答案:

答案 0 :(得分:5)

这里不是在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>

答案 1 :(得分: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]
    ]
  }]
});