// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Drawing; namespace Spludlow.Drawing { public class PieChart { public static void Draw(Spludlow.Printing.PrintDoc doc, float x, float y, float radius, float stroke, DataTable table) { string keyColumnName = table.Columns[0].ColumnName; string valueColumnName = table.Columns[1].ColumnName; Draw(doc, x, y, radius, stroke, table, keyColumnName, valueColumnName); } public static void Draw(Spludlow.Printing.PrintDoc doc, float x, float y, float radius, float stroke, DataTable table, string keyColumnName, string valueColumnName) { Dictionary values = new Dictionary(); foreach (DataRow row in table.Rows) { if (row.IsNull(valueColumnName) == true || row.IsNull(keyColumnName) == true) continue; decimal value = Math.Abs(Convert.ToDecimal(row[valueColumnName])); if (value == 0) continue; string key = Convert.ToString(row[keyColumnName]); if (values.ContainsKey(key) == false) values.Add(key, 0); values[key] = values[key] + value; } Draw(doc, x, y, radius, stroke, values); } public static void Draw(Spludlow.Printing.PrintDoc doc, float x, float y, float radius, float stroke, DataRow row) { List columnNames = new List(); foreach (DataColumn column in row.Table.Columns) columnNames.Add(column.ColumnName); Draw(doc, x, y, radius, stroke, row, columnNames.ToArray()); } public static void Draw(Spludlow.Printing.PrintDoc doc, float x, float y, float radius, float stroke, DataRow row, string[] columnNames) { Dictionary values = new Dictionary(); foreach (string columnName in columnNames) { if (row.IsNull(columnName) == true) continue; decimal value = Math.Abs(Convert.ToDecimal(row[columnName])); if (value == 0) continue; string key = columnName; if (values.ContainsKey(key) == false) values.Add(key, 0); values[key] = values[key] + value; } Draw(doc, x, y, radius, stroke, values); } public static void Draw(Spludlow.Printing.PrintDoc doc, float x, float y, float radius, float stroke, Dictionary values) { Color[] colourRange = Spludlow.Drawing.Colour.Range(values.Count < 9 ? 9 : values.Count); decimal total = 0; foreach (string key in values.Keys) total += values[key]; decimal start = 0; int index = 0; foreach (string key in values.Keys) { decimal value = values[key]; decimal end = start + value; decimal startAngle = start * (360 / total); decimal endAngle = end * (360 / total); doc.ArcSector(x, y, radius, radius, 0, 0, (float)startAngle, (float)endAngle, stroke, System.Drawing.Color.Black, colourRange[index]); //doc.Arc(x, y, radius + 2.5F, radius + 2.5F, (float)startAngle, (float)endAngle, 3.0F, colourRange[index]); //radius -= 2.5F; start = end; ++index; //break; } } } }