I am struggling to convert ViewBag into a Json array in order to create a chart within the .Net MVC framework. Despite attempting to parse the Json data based on solutions from previous queries, the chart is not displaying anything. Here is my code:
Controller
public ActionResult RimChart()
{
List<RimCalcs> listOfCategories = null;
Connectors aConn = Connectors.GetInstance();
listOfCategories = aConn.GetRimCalc();
var dates = listOfCategories.Select(x => x.Date);
var profits = listOfCategories.Select(y => y.Rprofit);
ViewBag.Date = dates;
ViewBag.profit = profits;
return View();
}
Razor View
<!DOCTYPE HTML>
<html>
<head>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "My First Chart in CanvasJS"
},
data: [
{
// Change type to "doughnut", "line", "splineArea", etc.
type: "column",
dataPoints: [
{ label: JSON.parse('@Html.Raw(ViewBag.profit)'), y: JSON.parse('@Html.Raw(ViewBag.Date)') },
]
}
]
});
chart.render();
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
</body>
</html>