I am u sing asp.net mvc 4
and placed a chart on view. i want to load its series from controller
. how can i do that ? please can anyone explain it with piece of code.
Index View
@{
var myChart = new Chart(width: 600, height: 400)
.AddTitle("Chart Title")
.AddSeries(
name: "Views",
xValue: new[] { "Monday", "Tuesday", "Wendesday", "Thursday", "Friday" },
yValues: new[] { "2", "6", "4", "5", "3" })
.AddSeries(
name: "Downloads",
xValue: new[] { "Monday", "Tuesday", "Wendesday", "Thursday", "Friday" },
yValues: new[] { "1", "2", "3", "4", "5" })
.Write();
}
Controller
public ActionResult Index()
{
return View();
}
It is absolutely possible. Let's start by creating a new view model to represent the chart data.
And in your Action method, create an object if the
MyChartData
class, Set the different property values and send it to the view.Now in your view ,which is strongly typed to a our
MyChartData
view model, we will iterate through the Series property and call theAddSeries
method.If you want to include the chart in another razor view, you can use an image tag and set the
src
attribute value to MyChart action method.Assuming
MyChart
action method exists inHomeController
.