0

I have to create an array that contains list of arrays for google chart pie.

[['Status', 'Count'],['New',5], ['Converted',0],['Completed',0]] 

One method I used which is quickest to implement but a 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 but seems like might be overkill for this 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 { get { 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 for better cleaner approach?

1 Answer 1

2

I think that the first option looks clean enough for the purpose.

You could group the leads on status to get the count for each:

Dictionary<int, int> leadsByStatus =
  leadsForOrganisation.GroupBy(m => m.leadStatusId)
  .ToDictionary(g => g.Key, g => g.Count());

You can use String.Format for a bit cleaner code when you put several values in a string:

string chart = String.Format(
  "[['Status', 'Count'],['New',{0}], ['Converted',{1}],['Completed',{2}]]",
  leadsByStatus[1], leadsByStatus[3], leadsByStatus[2]
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.