Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
<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>
Posted
Updated 5-Oct-17 7:23am

1 solution

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.
C#
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 
           };
}

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));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900