How to render chart from code behind
Chart can be rendered from the code behind by initializing the required properties in controller and passing those properties via ViewData to client side.
The following code illustrates the initialization of chart properties in the controller.
//Namespace to get the datavisualization (chart) component properties using Syncfusion.JavaScript.DataVisualization.Models;
//Initializing the chart model ChartProperties chartModel = new ChartProperties(); InitializeChart(chartModel);
//Passing chart properties using the ViewData ViewData["ChartModel"] = chartModel;
private void InitializeChart(ChartProperties chartModel) { //Initialize the data source for chart List<LineChartData> data = new List<LineChartData>(); data.Add(new LineChartData(2005, 28)); //...
Series series = new Series(); series.DataSource = data; series.XName = "Xvalue"; series.YName = "YValue1"; //...
}
|
Binding the chart properties passed via ViewData from the controller in the client side as below.
@{Html.EJ().Chart("container", (Syncfusion.JavaScript.DataVisualization.Models.ChartProperties)ViewData["ChartModel"]).Render(); }
|
The following screenshot displays the line chart rendered by passing properties from server.
Sample: Chart from server