/// <summary> /// Adds a leaf node with an explicit value and explicit color. /// A leaf node cannot have children. Use Color.Transparent /// to inherit the parent node color. /// </summary> /// <param name="name"></param> /// <param name="value"></param> /// <param name="color"></param> /// <returns></returns> public void AddLeaf(string name, float value, Color color) { if (value == 0f) { return; } PieGrouper ps = AddChild(initChild(name, value, color)); ps.setDepth(); }
private PieGrouper initChild(string name, float value, Color color) { PieGrouper ps = new PieGrouper(); ps._name = name; ps._value = value; ps._color = color; ps._parent = this; return(ps); }
private PieGrouper addChild(ref List <PieGrouper> sliceList, PieGrouper pieSlice) { if (sliceList == null) { sliceList = new List <PieGrouper>(); } pieSlice._parent = this; sliceList.Add(pieSlice); return(pieSlice); }
private void setDepth() { int depth = 0; PieGrouper ps = this; ps._syncPoint = 0; while (ps._parent != null) { ps = ps._parent; depth++; if (ps._syncPoint < 0) { ps._syncPoint = depth; } } if (ps._syncPoint != depth) { throw makeException(null, 0); } }
private void syncSubnodes(PieGrouper node, List <PieGrouper>[] pointLists, ref int seriesCount) { if (node != null) { if (node._children != null && node._children.Count > 0) { // an intermediate node foreach (PieGrouper ps in node._children) { if (ps._color == Color.Transparent && node._syncColor != Color.Empty) { ps._syncColor = node._syncColor; } else { ps._syncColor = ps._color; } syncSubnodes(ps, pointLists, ref seriesCount); node._syncValue += ps._syncValue; } } else { // an leaf node node._syncValue = node._value; } if (node._syncValue != 0f) { seriesCount++; int point = node._syncPoint; if (pointLists[point] == null) { pointLists[point] = new List <PieGrouper>(); } pointLists[point].Add(node); } } }
private PieGrouper AddChild(PieGrouper pieSlice) { if (this._value != 0f) { throw makeException(pieSlice._name, 1); } if (this._children != null && this._children.Count > 0) { float childValue = this._children[0]._value; if ((childValue == 0) != (pieSlice._value == 0)) { if (childValue != 0) { throw makeException(pieSlice._name, 2); } else { throw makeException(pieSlice._name, 3); } } } return(addChild(ref _children, pieSlice)); }
private void Form1_Load(object sender, EventArgs e) { c1Chart1.BackColor = Color.FromKnownColor(KnownColor.Window); // use light colors so all of the labels are easily read. c1Chart1.ColorGeneration = ColorGeneration.Flow; // stacked pie chart c1Chart1.ChartGroups.Group0.ChartType = Chart2DTypeEnum.Pie; c1Chart1.ChartGroups.Group0.Stacked = chkStacked.Checked; // 3d effects if (chk3D.Checked) { c1Chart1.ChartArea.PlotArea.View3D.Depth = 15; c1Chart1.ChartArea.PlotArea.View3D.Elevation = 75; } // with tooltips c1Chart1.ToolTip.SelectAction = SelectActionEnum.Click; c1Chart1.ToolTip.Enabled = true; // maximize the ChartArea c1Chart1.ChartArea.Margins.SetMargins(0, 0, 0, 0); c1Chart1.ChartArea.Style.Border.BorderStyle = BorderStyleEnum.None; // Use the PieGrouper class to input the data. PieGrouper pgBaseNode = new PieGrouper(); PieGrouper pgRootNode = null; PieGrouper pgMidNode = null; // Brackets are not required, but show the nesting of // the data nodes. pgRootNode = pgBaseNode.AddRoot("Beverages", Color.LightBlue); { pgMidNode = pgRootNode.AddIntermediate("Cold", Color.Transparent); { pgMidNode.AddLeaf("Apple Cider", 300, Color.Transparent); pgMidNode.AddLeaf("Orange juice", 200, Color.Orange); } pgMidNode = pgRootNode.AddIntermediate("Hot", Color.Empty); { pgMidNode.AddLeaf("Tea", 120, Color.Transparent); pgMidNode.AddLeaf("Chocolate", 80, Color.Transparent); } } pgRootNode = pgBaseNode.AddRoot("Dairy", Color.LightCoral); { pgMidNode = pgRootNode.AddIntermediate("Confections", Color.Transparent); { pgMidNode.AddLeaf("Ice Cream", 256, Color.Transparent); } pgMidNode = pgRootNode.AddIntermediate("Cheeses", Color.Transparent); { pgMidNode.AddLeaf("Cheddar", 350, Color.Transparent); pgMidNode.AddLeaf("Rocky Cheese", 310, Color.Transparent); } } pgRootNode = pgBaseNode.AddRoot("Condiments", Color.LightGray); { pgMidNode = pgRootNode.AddIntermediate("Sweet"); { pgMidNode.AddLeaf("Demiglace", 220); pgMidNode.AddLeaf("Pure sugar", 180); } pgMidNode = pgRootNode.AddIntermediate("Spicy"); { pgMidNode.AddLeaf("Bl. Pepper", 150); pgMidNode.AddLeaf("Cayenne", 170); pgMidNode.AddLeaf("Chili", 84); pgMidNode.AddLeaf("Jalepeno", 52); } } // save the class instance to allow changes later. pieGrouper = pgBaseNode; // create the new ChartDataSeriesCollection with the current settings pgBaseNode.AddToChartDataSeriesCollection(chkAscending.Checked, c1Chart1.ChartGroups.Group0.ChartData.SeriesList); }
/// <summary> /// Using the data structures created through the use of AddRoot, AddIntermediate and AddLeaf /// methods, populates the specified ChartDataSeriesCollection for a stacked Pie chart with /// segments. /// </summary> /// <param name="ascending"></param> /// <param name="cdsc"></param> /// <param name="dataLabelText"></param> public void AddToChartDataSeriesCollection(bool ascending, ChartDataSeriesCollection cdsc, string dataLabelText) { int pointCount = _syncPoint; List <PieGrouper>[] pointLists = new List <PieGrouper> [pointCount]; syncNodes(pointLists); cdsc.Clear(); float[] pointArray = new float[pointCount]; pointArray.Initialize(); if (dataLabelText == null) { dataLabelText = "{#TEXT}\r\n{#YVAL} ({%YVAL:0.00%})"; } for (int i = 0; i < pointCount; i++) { int j = ascending ? i : pointCount - 1 - i; List <PieGrouper> pointList = pointLists[j]; foreach (PieGrouper ps in pointList) { ChartDataSeries cds = cdsc.AddNewSeries(); cds.Label = ps._name; if (ps._syncColor != Color.Empty) { if (ps._syncColor == Color.Transparent) { PieGrouper ps1 = ps; while (ps1 != null && ps1._syncColor == Color.Transparent) { ps1 = ps1._parent; } if (ps1 != null && ps1._syncColor != Color.Transparent) { if (ps1._syncColor == Color.Empty) { ps1._syncColor = cds.FillStyle.Color1; } ps._syncColor = ps1._syncColor; for (ps1 = ps; ps1 != null && ps1._syncColor == Color.Transparent; ps1 = ps1._parent) { ps1._syncColor = ps._syncColor; } } } cds.FillStyle.Color1 = ps._syncColor; } else { ps._syncColor = cds.FillStyle.Color1; } pointArray[i] = ps._syncValue; cds.Y.CopyDataIn(pointArray); cds.DataLabel.Compass = LabelCompassEnum.RadialText; cds.DataLabel.Offset = -5; cds.DataLabel.Text = dataLabelText; cds.DataLabel.Visible = true; cds.TooltipText = cds.DataLabel.Text; } pointArray[i] = 0f; } }