private Rectangle GetLabelBounds(Graphics grf, ChartLabel cl, Rectangle rectChart) { if (cl == null || string.IsNullOrEmpty(cl.Text)) { return(Rectangle.Empty); } Rectangle rect; if (cl.AutoSize) { SizeF sizef = grf.MeasureString(cl.Text, (cl.Font != null) ? cl.Font : this.Font); rect = new Rectangle(0, 0, (int)Math.Ceiling(sizef.Width), (int)Math.Ceiling(sizef.Height)); } else { rect = new Rectangle(0, 0, cl.Size.Width, cl.Size.Height); } switch (cl.Alignment) { case ContentAlignment.TopLeft: rect.Location = cl.Location; break; case ContentAlignment.TopCenter: rect.Location = new Point(rectChart.Left + rectChart.Left + (rectChart.Width - rect.Width) / 2, rectChart.Top + cl.Location.Y); break; case ContentAlignment.TopRight: rect.Location = new Point(rectChart.Right - cl.Location.X - rect.Width, rectChart.Top + cl.Location.Y); break; case ContentAlignment.MiddleLeft: rect.Location = new Point(rectChart.Left + cl.Location.X, rectChart.Top + (rectChart.Height - rect.Height) / 2); break; case ContentAlignment.MiddleCenter: rect.Location = new Point(rectChart.Left + (rectChart.Width - rect.Width) / 2, rectChart.Top + (rectChart.Height - rect.Height) / 2); break; case ContentAlignment.MiddleRight: rect.Location = new Point(rectChart.Right - cl.Location.X - rect.Width, rectChart.Top + (rectChart.Height - rect.Height) / 2); break; case ContentAlignment.BottomLeft: rect.Location = new Point(rectChart.Left + cl.Location.X, rectChart.Bottom - cl.Location.Y - rect.Height); break; case ContentAlignment.BottomCenter: rect.Location = new Point(rectChart.Left + (rectChart.Width - rect.Width) / 2, rectChart.Bottom - cl.Location.Y - rect.Height); break; case ContentAlignment.BottomRight: rect.Location = new Point(rectChart.Right - cl.Location.X - rect.Width, rectChart.Bottom - cl.Location.Y - rect.Height); break; } return(rect); }
protected virtual void DrawLable(ChartLabel cl, ChartPaintEventArgs e) { if (cl == null || !cl.Visible || string.IsNullOrEmpty(cl.Text)) { return; } //Rectangle rect_chart = new Rectangle(0, 0, this.ContentSize.Width, this.ContentSize.Height); Rectangle rect_chart = new Rectangle(0, 0, this.ContentSize.Width, this.ContentSize.Height); Rectangle rect = GetLabelBounds(e.Graphics, cl, rect_chart); if (rect.Width == 0 || rect.Height == 0) { return; } if (cl.BackColor.HasValue) { e.Graphics.FillRectangle(new SolidBrush(cl.BackColor.Value), rect); } e.Graphics.DrawString(cl.Text, (cl.Font == null ? e.Font : cl.Font), (cl.ForeColor.HasValue ? new SolidBrush(cl.ForeColor.Value) : e.ForeBrush), rect, PaintHelper.SFLeft); }