internal static SeriesCollection GetPieChartMaterials(CarboProject carboLifeProject) { SeriesCollection result = new SeriesCollection(); try { List <CarboDataPoint> PieceListMaterial = new List <CarboDataPoint>(); Func <ChartPoint, string> labelPoint = chartPoint => string.Format("{0} tCO₂", chartPoint.Y, chartPoint.Participation); PieceListMaterial = carboLifeProject.getMaterialTotals(); PieceListMaterial = PieceListMaterial.OrderByDescending(o => o.Value).ToList(); //int total = monValues.Sum(x => Convert.ToInt32(x)); double total = PieceListMaterial.Sum(x => x.Value); //Old Code: //if there are too many materials in the list combine any items over 8. /* * if (PieceListMaterial.Count > 8) * { * CarboDataPoint otherPoint = new CarboDataPoint(); * otherPoint.Name = "Miscellaneous"; * * for (int i = PieceListMaterial.Count -1; i>7; i--) * { * CarboDataPoint cp = PieceListMaterial[i] as CarboDataPoint; * otherPoint.Value += cp.Value; * PieceListMaterial.RemoveAt(i); * } * PieceListMaterial.Add(otherPoint); * } */ //New Code: Trim all values below 5%, List <int> counter = new List <int>(); CarboDataPoint otherPoint = new CarboDataPoint(); otherPoint.Name = "Miscellaneous"; for (int i = PieceListMaterial.Count - 1; i >= 0; i--) { CarboDataPoint cp = PieceListMaterial[i] as CarboDataPoint; double pecent = cp.Value / total; if (pecent <= 0.05 && pecent > 0) { //The item is too small for the graph: otherPoint.Value += cp.Value; counter.Add(i); } } //If used, add to the list, only if more than one materials / elements were merged. if (otherPoint.Value > 0 && counter.Count > 1) { PieceListMaterial.Add(otherPoint); //Remove the items from the list foreach (int i in counter) { PieceListMaterial.RemoveAt(i); } } //make positive if negative foreach (CarboDataPoint cp in PieceListMaterial) { if (cp.Value < 0) { cp.Name = "(Negative)" + cp.Name; cp.Value = cp.Value * -1; } } foreach (CarboDataPoint ppin in PieceListMaterial) { PieSeries newSeries = new PieSeries { Title = ppin.Name, Values = new ChartValues <double> { Math.Round(ppin.Value, 2) }, PushOut = 1, DataLabels = true, LabelPoint = labelPoint }; newSeries.Foreground = Brushes.Black; newSeries.FontWeight = FontWeights.Normal; newSeries.FontStyle = FontStyles.Normal; newSeries.FontSize = 12; result.Add(newSeries); } } catch { return(null); } return(result); }