pie chart with a list


Hello
I have a list on my controller

Controller:
var generoEncuestas = (from genero in db.Encuestas
                                      select new { genero.Femenino, genero.Masculino, 
                                                           genero.SinEspecificarGenero }).ToList();

ViewBag.GeneroEncuestas = generoEncuestas;




1. Can the list be graphed?
2. How to graph in the view?


Happy New Year 2018


1 Reply

DG Durga Gopalakrishnan Syncfusion Team January 2, 2018 10:49 AM UTC

Hi Norberto, 
 
Thanks for using Syncfusion products.  
 
We have analyzed your query. Your requirement can be achieved by passing the list from controller to view through ViewBag. Now assign the data to the ‘dataSource’ property in series, then map the properties from list to xName and yName properties in series to render the graph . Please find the code snippet and screenshot below 
 
[CSHTML] 
  
@(Html.EJ().Chart("container"
.Series(sr => 
    {                          sr.DataSource((IEnumerable<object>)ViewBag.dataSource).XName("XValue").YName("YValue").Add(); 
    }) 
) 
  
[CS] 
public ActionResult Pie() 
List<ChartPieData> data = new List<ChartPieData>(); 
            data.Add(new ChartPieData("Other Personal", 80)); 
            data.Add(new ChartPieData("Medical care", 70)); 
            data.Add(new ChartPieData("Housing", 60)); 
            data.Add(new ChartPieData("Transportation", 50)); 
            data.Add(new ChartPieData("Education", 40)); 
            data.Add(new ChartPieData("Electronics", 30)); 
            ViewBag.dataSource = data; 
            return View(); 
public class ChartPieData 
    { 
        public ChartPieData(string xval, double yval) 
        { 
            this.XValue = xval; 
            this.YValue = yval; 
  
        } 
        public string XValue 
        { 
            get
            set
        } 
        public double YValue 
        { 
            get
            set
        } 
    } 
 
 
  
Sample Link: SyncfusionMvcApplication 
 
Please let me know, if you have any queries. 
 
Regards, 
Durga 


Loader.
Up arrow icon