| public ActionResult PieChartOfCountry() { MVCChartModel pieChartModel = new MVCChartModel(); pieChartModel.Series3D = true; pieChartModel.Text = "Leads % Per Country"; pieChartModel.Series.Clear(); var leads = ReadSalesForceLeadData(); ChartSeries series = new ChartSeries("Country");
int count = 0; // calculate the percentage that each country represents in leads var distinctCountries = leads.Select(lead => lead.Country).Distinct(); foreach (var country in distinctCountries) { var numberOfCountries = (double) leads.Where(lead => lead.Country == country).Count(); double percentageOfCountry = (numberOfCountries/(double)leads.Count()) * 100.0d; series.Points.Add(count, percentageOfCountry); count++; }
series.Type = ChartSeriesType.Pie;
pieChartModel.Series.Add(series);
pieChartModel.ShowLegend = false;
pieChartModel.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
pieChartModel.Skins = ChartModelSkins.Office2007Blue;
pieChartModel.BorderAppearance.SkinStyle = ChartBorderSkinStyle.Pinned;
// set the text of each pie slice
for (int i = 0; i < distinctCountries.Count(); i++) { series.Styles[i].DisplayText = true; series.Styles[i].Text = distinctCountries.Skip(i).Take(1).FirstOrDefault(); } // set the color of each pie slice series.Styles[0].Interior = new BrushInfo(GradientStyle.Horizontal, new[] { Color.FromArgb(177, 140, 188), Color.FromArgb(229, 197, 221), Color.FromArgb(201, 163, 202) }); series.Styles[1].Interior = new BrushInfo(GradientStyle.Horizontal, new[] { Color.FromArgb(191, 62, 35), Color.FromArgb(226, 83, 37), Color.FromArgb(255, 195, 127) }); series.Styles[2].Interior = new BrushInfo(GradientStyle.Horizontal, new[] { Color.FromArgb(139, 193, 58), Color.FromArgb(206, 222, 103), Color.FromArgb(227, 231, 135) }); series.Styles[3].Interior = new BrushInfo(GradientStyle.Horizontal, new[] { Color.FromArgb(255, 244, 42), Color.FromArgb(253, 240, 38), Color.FromArgb(255, 216, 25) }); series.Styles[4].Interior = new BrushInfo(GradientStyle.Horizontal, new[] { Color.FromArgb(112, 227, 220), Color.FromArgb(102, 175, 201), Color.FromArgb(104, 142, 191) }); series.Styles[5].Interior = new BrushInfo(GradientStyle.Horizontal, new[] { Color.FromArgb(255, 141, 51), Color.FromArgb(248, 183, 56), Color.FromArgb(249, 228, 70) });
ViewData.Model = pieChartModel;
return View();
}
|