List of arrays in array for google chart in C# MVC -
i have create array contains list of arrays google chart pie.
[['status', 'count'],['new',5], ['converted',0],['completed',0]]
one method used quickest implement bit dirty:
public class dashboardvm { public dashboardvm(ienumerable<lead> leadsfororganisation) { this.leadsbyagent = new list<leadsbyagent>(); this.leads = leadsfororganisation; foreach (var groupedleads in leadsfororganisation.groupby(m=>m.contactid)) { this.leadsbyagent.add(new leadsbyagent(groupedleads)); } string chart = "[['status', 'count'],['new'," + leadsfororganisation.where(m => m.leadstatusid == 1).count() + "], ['converted'," + leadsfororganisation.where(m => m.leadstatusid == 3).count() + "],['completed'," + leadsfororganisation.where(m => m.leadstatusid == 2).count() + "]]"; this.googlechartdata = chart; } public list<leadsbyagent> leadsbyagent { get; set; } public string googlechartdata { get; set; } public ienumerable<lead> leads { get; set; } }
and on view
function drawchart() { var data = google.visualization.arraytodatatable( @html.raw(model.googlechartdata ) ); var options = { title: '', is3d: true, backgroundcolor: '#f9f9f9', width:500, height:300, }; var chart = new google.visualization.piechart(document.getelementbyid('piechart')); chart.draw(data, options); }
the second method, cleaner seems might overkill task.
public class dashboardvm { public dashboardvm(ienumerable<lead> leadsfororganisation) { this.leadsbyagent = new list<leadsbyagent>(); this.leads = leadsfororganisation; foreach (var groupedleads in leadsfororganisation.groupby(m=>m.contactid)) { this.leadsbyagent.add(new leadsbyagent(groupedleads)); } } public list<leadsbyagent> leadsbyagent { get; set; } public string gchartjavascriptjson { { return getjsonchart(); } } public ienumerable<lead> leads { get; set; } public string getjsonchart() { var data = new object[] { new object[]{"status", "count"}, new object[]{"new", this.leads.where(m => m.leadstatusid == 1).count()}, new object[]{"contacted", this.leads.where(m => m.leadstatusid == 2).count()}, new object[]{"converted", this.leads.where(m => m.leadstatusid == 3).count()} }; return new javascriptserializer().serialize(data); } }
view
function drawchart() { var data = google.visualization.arraytodatatable( @html.raw(model.gchartjavascriptjson) ); var options = { title: '', is3d: true, backgroundcolor: '#f9f9f9', width:500, height:300, }; var chart = new google.visualization.piechart(document.getelementbyid('piechart')); chart.draw(data, options); }
any ideas better cleaner approach?
i think first option looks clean enough purpose.
you group leads on status count each:
dictionary<int, int> leadsbystatus = leadsfororganisation.groupby(m => m.leadstatusid) .todictionary(g => g.key, g => g.count());
you can use string.format
bit cleaner code when put several values in string:
string chart = string.format( "[['status', 'count'],['new',{0}], ['converted',{1}],['completed',{2}]]", leadsbystatus[1], leadsbystatus[3], leadsbystatus[2] );
Comments
Post a Comment