//********************************************************************* // // This method calculates the space required to draw the chart legend. // //********************************************************************* private void CalculateLegendWidthHeight() { Font fontLegend = new Font(_legendFontStyle, _legendFontSize); _legendFontHeight = fontLegend.Height + 5; _legendHeight = _legendFontHeight * (Items.Count); if (_legendHeight > Radius) { Radius = _legendHeight; } Width = 2 * _spacer + Radius; _legendWidth = _bufferSpace; Bitmap bmp = new Bitmap(Width + _legendWidth + _spacer, _spacer + _titleHeight + _spacer + Radius + _spacer); Graphics grp = Graphics.FromImage(bmp); grp.SmoothingMode = SmoothingMode.AntiAlias; for (int i = 0; i < Items.Count; i++) { ChartItem item = (ChartItem)Items[i]; SizeF LabelSize = grp.MeasureString(item.Label, new Font(_legendFontStyle, _legendFontSize)); if (LabelSize.Width > _maxLegendWidth) { _maxLegendWidth = LabelSize.Width; } } _maxLegendWidth += 15; if (_maxLegendWidth > _legendWidth) { _legendWidth = (int)Math.Round(_maxLegendWidth); } }
public override void Draw(Graphics graphics) { int width = Width; int spacer = _spacer; Rectangle pieRect = new Rectangle( (width - Radius) / 2, spacer + _titleHeight, Radius, Radius); StringFormat sf = null; try { sf = new StringFormat(); //Paint Back ground graphics.FillRectangle(new SolidBrush(_backgroundColor), 0, 0, width + _legendWidth + _spacer, spacer + _titleHeight + spacer + Radius + spacer); graphics.SmoothingMode = SmoothingMode.AntiAlias; //Align text to the right sf.Alignment = StringAlignment.Near; SizeF titlesize = graphics.MeasureString(Title, new Font(_legendFontStyle, _legendFontSize)); graphics.DrawString(Title, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), (width + _legendWidth + _spacer - titlesize.Width) / 2, spacer); //Draw all wedges and legends for (int i = 0; i < Items.Count; i++) { ChartItem item = (ChartItem)Items[i]; SolidBrush brs = null; try { brs = new SolidBrush(item.ItemColor); graphics.FillPie(brs, pieRect, item.StartPos, item.SweepSize); float fPerc = (float)100 * item.Value / _total; if (DrawPercents && fPerc >= MinPercentToDraw) { string sPerc = (Math.Round(fPerc, 2)).ToString(CultureInfo.CurrentUICulture) + "%"; SizeF PercSize = graphics.MeasureString(sPerc, new Font(_legendFontStyle, _legendFontSize)); double _angle = Math.PI * (item.StartPos + item.SweepSize / 2) / 180; double fcos = _const * Radius * Math.Cos(_angle) / 2; double fsin = _const * Radius * Math.Sin(_angle) / 2; float fx = (float)(width / 2 - PercSize.Width / 2 + fcos); float fy = (float)(spacer + _titleHeight + Radius / 2 + fsin - PercSize.Height / 2); graphics.DrawString(sPerc, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), fx, fy); } graphics.FillRectangle(brs, width + (_legendWidth - _maxLegendWidth) / 2, _titleHeight + i * _legendFontHeight + spacer * 2, 10, 10); graphics.DrawString(item.Label, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), width + (_legendWidth - _maxLegendWidth) / 2 + 15, _titleHeight + i * _legendFontHeight + spacer * 2); } finally { if (brs != null) { brs.Dispose(); } } } //draws the border around Pie graphics.DrawEllipse(new Pen(_borderColor, 1), pieRect); //draw border around Chart if (BorderWidth > 0) { graphics.DrawRectangle(new Pen(_borderColor, BorderWidth), 0, 0, width + _legendWidth + _spacer - 1, Radius + _titleHeight + 3 * spacer - 1); } //Draw Total under legend graphics.SmoothingMode = SmoothingMode.AntiAlias; } finally { if (sf != null) { sf.Dispose(); } if (graphics != null) { graphics.Dispose(); } } }
public override void Draw(Graphics graphics) { int width = Width; int spacer = _spacer; StringFormat sf = null; try { sf = new StringFormat(); //Paint Back ground graphics.FillRectangle(new SolidBrush(_backgroundColor), 0, 0, width + 20, spacer + _titleHeight + spacer + Height + 2 * spacer); graphics.SmoothingMode = SmoothingMode.AntiAlias; //Align text to the right sf.Alignment = StringAlignment.Near; SizeF titlesize = graphics.MeasureString(Title, new Font(_legendFontStyle, _legendFontSize)); graphics.DrawString(Title, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), (width - titlesize.Width) / 2, spacer); float MaxValue = 1; MaxValue = 0; foreach (ChartItem ci in Items) { if (ci.Value > MaxValue) { MaxValue = ci.Value; } } for (int i = 0; i < Items.Count; i++) { ChartItem item = (ChartItem)Items[i]; SolidBrush brs = null; try { brs = new SolidBrush(item.ItemColor); int _curHeight = (int)Math.Round((Height - _maxLegendWidth) * item.Value / MaxValue); if (_curHeight == 0) { _curHeight = 1; } string sValue = (Math.Round(item.Value, 2)).ToString(CultureInfo.CurrentUICulture); SizeF sValueSize = graphics.MeasureString(sValue, new Font(_legendFontStyle, _legendFontSize)); SizeF sLabelSize = graphics.MeasureString(item.Label, new Font(_legendFontStyle, _legendFontSize)); graphics.FillRectangle(brs, item.StartPos, spacer + _titleHeight + Height - _maxLegendWidth - _curHeight, _gistwidth, _curHeight); if (sValueSize.Width <= _gistwidth && sValueSize.Height <= _curHeight) { graphics.DrawString(sValue, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), item.StartPos + _gistwidth / 2 - sValueSize.Width / 2, spacer + _titleHeight + Height - _maxLegendWidth - _curHeight / 2 - sValueSize.Height / 2); } if (_maxLegendWidth >= _gistwidth) { System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical); graphics.DrawString(item.Label, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), item.StartPos + _gistwidth / 2 - sLabelSize.Height / 2, spacer + _titleHeight + Height - _maxLegendWidth + spacer, drawFormat); } else { graphics.DrawString(item.Label, new Font(_legendFontStyle, _legendFontSize), new SolidBrush(Color.Black), item.StartPos + _gistwidth / 2 - sLabelSize.Width / 2, spacer + _titleHeight + Height + spacer); } } finally { if (brs != null) { brs.Dispose(); } } } //draw border around Chart if (BorderWidth > 0) { graphics.DrawRectangle(new Pen(_borderColor, BorderWidth), 0, 0, width - 1, Height + _titleHeight + 4 * spacer - 1); } //Draw Total under legend graphics.SmoothingMode = SmoothingMode.AntiAlias; } finally { if (sf != null) { sf.Dispose(); } if (graphics != null) { graphics.Dispose(); } } }