public PieBlock AddBlock(int index, float percent, decimal value) { PieBlock item = new PieBlock() { Index = index, Percent = percent, Value = value }; _blocks.Add(item); return(item); }
void RenderCenterLabels(System.Drawing.Graphics g, float x, float labelWidth, float percentWidth) { if (_labels.Count == 0) { return; } float rowHeight = _font.Height + 10F; float rowOffset = rowHeight / 2F; float y = (Height - _labels.Count * (rowHeight + rowOffset) + rowOffset) / 2F; System.Drawing.Brush textBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0xDA, 0xDA, 0xDA)); for (int i = 0; i < _labels.Count; i++) { Label label = _labels[i]; g.FillRectangle(label.Brush, x, y, labelWidth, rowHeight); string text = label.Name; System.Drawing.SizeF textSize = g.MeasureString(text, _font); g.DrawString(text, _font, textBrush, x + (labelWidth - textSize.Width) / 2F, y + (rowHeight - textSize.Height) / 2F); //label left percent PieBlock blockLeft = _leftPie.Blocks.Find(p => p.Index == label.Index); if (blockLeft != null) { text = NumberToString(blockLeft.Value) + " " + (blockLeft.Percent / 360F * 100F).ToString("0.#'%'").PadLeft(5, ' '); textSize = g.MeasureString(text, _font); g.DrawString(text, _font, label.Brush, x - 3F - textSize.Width, y + (rowHeight - textSize.Height) / 2F); } //label right percent PieBlock blockRight = _rightPie.Blocks.Find(p => p.Index == label.Index); if (blockRight != null) { text = (blockRight.Percent / 360F * 100F).ToString("0.#'%'"); if (text.Length != 5) { text += "".PadRight(5 - text.Length, ' '); } text += " " + NumberToString(blockRight.Value); textSize = g.MeasureString(text, _font); g.DrawString(text, _font, label.Brush, x + labelWidth + 3F, y + (rowHeight - textSize.Height) / 2F); } y += rowHeight + rowOffset; } }
void RenderPie(bool left, System.Drawing.Graphics g) { Pie pie = left ? _leftPie : _rightPie; Label firstLabel = null; Label lastLabel = null; float startAngle = 0F; for (int i = 0; i < pie.Blocks.Count; i++) { PieBlock block = pie.Blocks[i]; Label label = _labels.Find(p => p.Index == block.Index); if (label == null) { continue; } if (i == 0) { firstLabel = label; } lastLabel = label; g.FillPie(label.Brush, pie.X, pie.Y, pie.Width, pie.Height, startAngle, block.Percent); startAngle += block.Percent; } if (firstLabel == null) { firstLabel = new Label() { Index = 0, Color = System.Drawing.Color.Gray, Name = "" }; g.FillPie(firstLabel.Brush, pie.X, pie.Y, pie.Width, pie.Height, 0F, 1F); } g.DrawPie(new System.Drawing.Pen(firstLabel.Brush), pie.X, pie.Y, pie.Width, pie.Height, 0F, 360F); //bottom labels float bottomY = pie.Y + pie.Height + 15F; if (left) { float bottomX = pie.X + 4F; for (int i = 0; i < pie.Labels.Count; i++) { Label label = pie.Labels[i]; g.FillRectangle(label.Brush, bottomX, bottomY, Font.Height, Font.Height); bottomX += Font.Height + 3F; string text = label.Name; System.Drawing.SizeF textSize = g.MeasureString(text, _font); g.DrawString(text, _font, label.Brush, bottomX, bottomY + (_font.Height - textSize.Height) / 2F); bottomX += textSize.Width + 10F; if (!string.IsNullOrEmpty(label.Value)) { text = label.Value; bottomX -= 8F; textSize = g.MeasureString(text, _font); g.DrawString(text, _font, label.Brush, bottomX, bottomY + (_font.Height - textSize.Height) / 2F); bottomX += textSize.Width + 10F; } } } else { float bottomX = pie.X + pie.Width - 4F; for (int i = pie.Labels.Count - 1; i >= 0; i--) { Label label = pie.Labels[i]; string text = null; System.Drawing.SizeF textSize; if (!string.IsNullOrEmpty(label.Value)) { text = label.Value; textSize = g.MeasureString(text, _font); bottomX -= textSize.Width; g.DrawString(text, _font, label.Brush, bottomX, bottomY + (_font.Height - textSize.Height) / 2F); bottomX -= 3F; } text = label.Name; textSize = g.MeasureString(text, _font); bottomX -= textSize.Width; g.DrawString(text, _font, label.Brush, bottomX, bottomY + (_font.Height - textSize.Height) / 2F); bottomX -= 3F; bottomX -= Font.Height; g.FillRectangle(label.Brush, bottomX, bottomY, Font.Height, Font.Height); bottomX -= 10F; } } }