We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Using entity framework in chart and pivotchart

Hi,
How can I use chart (linechar) and pivotchart with data from the database and entity framework data model.
I have attached a sample below, take a look at, and what I want to do is create line chart where the axis is the columns database table fields and use grouping and filtering with some others fields.


Regards,
Anis


1 Reply

DD Dharanidharan Dharmasivam Syncfusion Team December 22, 2017 12:54 PM UTC

Hi Anis, 

Kindly find the response for your queries below. 

Query  
Response 
How can I use chart (linechar) and pivotchart with data from the database and entity framework data model.  
Chart Response:  
We have prepared a sample with respect to your requirement. Here we have fetched data in the controller side and passed the data to view page using ViewBag and then bind the data to series.dataSource property as depicted below. Find the code snippet below to achieve this requirement.  

ASP.NET MVC: 

[Controller] 

ViewBag.datasource = new NORTHWNDEntities2().OrderTables.ToList(); 

[View page]: 
 
@(Html.EJ().Chart("chartContainer") 
       .Series(sr => 
       { 
           
          //Binding data source 
           sr.DataSource(ViewBag.datasource); 
            
          //Mapping data from data source 
           sr.XName("OrderID"); 
           sr.YName("EmployeeID"); 
           sr.Type(SeriesType.Line).Add(); 
       }) 
) 


Screenshot: 
 

Chart line sample for reference can be find from below link. 

Pivot Chart:  

We have prepared sample based on your requirement. Please find the sample in below link.  

I have attached a sample below, take a look at, and what I want to do is create line chart where the axis is the columns database table fields and use grouping and filtering with some others fields.  
Yes. You can filter PivotChart using “filters” property. Please refer below code snippet.  
Code Snippet: [cshtml]  
@Html.EJ().Pivot().PivotChart("PivotChart1").CommonSeriesOptions(comm => { comm.Type(Syncfusion.JavaScript.DataVisualization.SeriesType.Column).Tooltip(tool => { tool.Visible(true); }).EnableAnimation(true); }).Size(size => size.Height("460px").Width("100%")).PrimaryXAxis(primaryXAxis => primaryXAxis.Title(title => title.Text("Fiscal Year")).LabelRotation(0)).PrimaryYAxis(primaryYAxis => primaryYAxis.Title(title => title.Text("Customer Count"))).Legend(legend => legend.Visible(true)).ClientSideEvents(  
                                  oEve => { oEve.Load("load"); }).IsResponsive(true).DataSource(dataSource => dataSource.Rows(rows => { rows.FieldName("State").FieldCaption("State").Add(); }).Columns(columns => { }).Values(values => { values.FieldName("Amount").Add(); values.FieldName("Quantity").Add(); }).Filters(filters => { filters.FieldName("Country").FieldCaption("Country").FilterItems(item => { item.FilterType(PivotFilterType.Include).Values(val => { val.Add("Canada"); }); }).Add(); }))  
  
In the sample, we have added “State” field in row axis and “Country” field in filter axis. Then, we filter “Canada” from “Country” field using the property “FilterItems”. For more details about “FilterItems”, kindly refer below documentation.  
  
So, PivotChart displays the states belongs to “Canada” alone. Please refer below screenshot.  
  
   
  
Meanwhile, we have modified your sample as per your requirement. Please find the sample in below link.  
  
  
 


Dharani. 
  



Loader.
Live Chat Icon For mobile
Up arrow icon