如何在Google图表中传递viewbag数据 [英] How to pass viewbag data in Google charts

查看:80
本文介绍了如何在Google图表中传递viewbag数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用google pie chart.i通过json返回所有值并将值存储在视图包中并传递给view page.data也即将到来。但是每个功能数据都没有显示。



如何传递每个函数中的所有数据值并显示图表。



我尝试过: < br $>


控制器

Hi,im using google pie chart.i returned all values through json and stored the values in view bag and passed to view page.data also coming.but in each function data not displaying.

How to pass all data values in each function and display chart.

What I have tried:

Controller

public ActionResult PerformanceChart(string id)
      {
          ViewBag.PieChart = getMapData(id);
          return View();
      }

      public ActionResult getMapData(string id)
      {
          List<Performance> mapdata = new List<Performance>();

          var linqList = from a in bics.Performances
                         where (a.EmployeeId == id)

         select new { a.Ques1, a.Ques2, a.Ques3, a.Ques4, a.Ques5, a.Ques6, a.Ques7, a.Ques8, a.Ques9, a.Ques10 };
          //mapdata = bics.Performances.ToList();

          return Json(linqList, JsonRequestBehavior.AllowGet);
      }



ViewPage


ViewPage

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>PerformanceChart</title>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
    google.charts.load('current', {packages: ['corechart']});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
      // Define the chart to be drawn.
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Qustion');
      data.addColumn('number', 'Marks');
      var data1 = @Html.Raw(Json.Encode(ViewBag.Piechart));

        debugger;
       // data.addRows([
       //['Nitrogen', 0.78],
       //['Oxygen', 0.21],
       //['Other', 0.01]
       // ]);
        $.each(data1,function(index,value)
        {
            data.addRows([[data1]]);
            //data.addRows([[value.ProductType,value.TotalCount]]);
            //alert('Position is : '+ index + '  And  Value is : ' + value);

        });
     

      // Instantiate and draw the chart.
      var chart = new google.visualization.PieChart(document.getElementById('myPieChart'));
      chart.draw(data, null);
    }
    </script>
</head>
<body>
    <div> 
        <div id="myPieChart" />
    </div>
</body>
</html>

推荐答案

.each(data1,function(index,value)
{
data.addRows([[data1]]);
//data.addRows([[value.ProductType,value.TotalCount]]);
// alert('Position is:' + index +'和Value是:'+ value);

});


//实例化并绘制图表。
var chart = new google.visualization.PieChart(document.getElementById('myPieChart'));
chart.draw(data,null);
}
< / script>
< / head>
< body>
< div>
< div id =myPieChart/>
< / div>
< / body>
< / html>
.each(data1,function(index,value) { data.addRows([[data1]]); //data.addRows([[value.ProductType,value.TotalCount]]); //alert('Position is : '+ index + ' And Value is : ' + value); }); // Instantiate and draw the chart. var chart = new google.visualization.PieChart(document.getElementById('myPieChart')); chart.draw(data, null); } </script> </head> <body> <div> <div id="myPieChart" /> </div> </body> </html>


您将 data1 设置为JSON编码表示 JsonResult 对象。您应该将其设置为图表数据的JSON编码表示。

You're setting data1 to the JSON-encoded representation of a JsonResult object. You should be setting it to the JSON-encoded representation of the chart data instead.
private IEnumerable getMapData(string id)
{
    return from a in bics.Performances
           where a.EmployeeId == id
           select new 
           { 
               a.Ques1, 
               a.Ques2, 
               a.Ques3, 
               a.Ques4, 
               a.Ques5, 
               a.Ques6, 
               a.Ques7, 
               a.Ques8, 
               a.Ques9, 
               a.Ques10 
           };
}



您可能还需要更正 ViewBag 数据的名称,因为C#区分大小写。您的控制器设置 ViewBag.PieChart ,但您的视图正在寻找 ViewBag.Piechart


You'll probably also need to correct the name of the ViewBag data, since C# is case-sensitive. Your controller sets ViewBag.PieChart, but your view is looking for ViewBag.Piechart.

var data1 = @Html.Raw(Json.Encode(ViewBag.PieChart));


这篇关于如何在Google图表中传递viewbag数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆