Articles in this section
Category / Section

How to render chart from code behind

1 min read

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.

Chart rendered by passing properties from server

 

Sample:  Chart from server

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied