/// <summary> /// Counts the width of the axis y. /// </summary> /// <param name="g">The g.</param> /// <param name="font">The font.</param> /// <returns>System.Single.</returns> private float CountAxisYWidth(Graphics g, Font font) { if (!_visible) { return(0f); } float num = 0f; if (!string.IsNullOrEmpty(_title)) { SizeF size = g.MeasureString(_title, font); num = ChartsHelper.ConvertSize(size, -90f).Width + 3f; } if (!_labelVisible || _labels.Count < 1) { return(num); } maxLabelHeight = 0f; maxLabelWidth = 0f; for (int i = 0; i < _labels.Count; i++) { SizeF sizeF = g.MeasureString(_labels[i].Content, font); maxLabelHeight = ((sizeF.Height > maxLabelHeight) ? sizeF.Height : maxLabelHeight); maxLabelWidth = ((sizeF.Width > maxLabelWidth) ? sizeF.Width : maxLabelWidth); } return(num + maxLabelWidth + 3f); }
/// <summary> /// Paints the main. /// </summary> /// <param name="g">The g.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> private void PaintMain(Graphics g, int width, int height) { if (barChartItems == null || barChartItems.Length <= 0) { if (DesignMode) { barChartItems = new BarChartItem[] { new BarChartItem() }; } else { return; } } if (data_values == null && DesignMode) { data_values = new double[5][]; for (int i = 0; i < data_values.Length; i++) { data_values[i] = new double[] { i + 1 }; } } double num = (data_values != null && data_values.Length != 0) ? ChartsHelper.CalculateMaxSectionFrom(data_values) : 5; if (value_max_left > 0) { num = value_max_left; } int intLeftX = (int)g.MeasureString(num.ToString(), Font).Width + 3; if (intLeftX < 50) { intLeftX = 50; } //处理辅助线左侧间距 if (auxiliary_lines != null && auxiliary_lines.Count > 0) { var maxAuxiliaryWidth = auxiliary_lines.Max(p => g.MeasureString((p.Tip + "" + p.Value), Font).Width); if (intLeftX < maxAuxiliaryWidth) { intLeftX = (int)maxAuxiliaryWidth + 5; } } int intRightX = 20; //顶部距离 int intTopY = 25; if (!string.IsNullOrEmpty(value_title)) { intTopY += 20; } //写标题 if (!string.IsNullOrEmpty(value_title)) { g.DrawString(value_title, Font, brush_deep, new Rectangle(0, 0, width - 1, intTopY - 10), format_center); } //画项目名称颜色 if (ShowChartItemName && !(barChartItems.Length == 1 && string.IsNullOrEmpty(barChartItems[0].ItemName))) { int intItemNameRowCount = 0; int intItemNameWidth = 0; int intItemNameComCount = 0; intItemNameWidth = (int)barChartItems.Max(p => g.MeasureString(p.ItemName, Font).Width) + 40; intItemNameComCount = this.Width / intItemNameWidth; intItemNameRowCount = barChartItems.Length / intItemNameComCount + (barChartItems.Length % intItemNameComCount != 0 ? 1 : 0); int intItemNameHeight = (int)g.MeasureString("A", Font).Height; for (int i = 0; i < intItemNameRowCount; i++) { int intLeft = (this.Width - (intItemNameWidth * ((i == intItemNameRowCount - 1) ? (barChartItems.Length % intItemNameComCount) : intItemNameComCount))) / 2; int intTop = intTopY - 15 + intItemNameHeight * i + 10; for (int j = i * intItemNameComCount; j < barChartItems.Length && j < (i + 1) * intItemNameComCount; j++) { Rectangle rectColor = new Rectangle(intLeft + (j % intItemNameComCount) * intItemNameWidth, intTop, 20, intItemNameHeight); g.FillRectangle(new SolidBrush(barChartItems[j].BarBackColor ?? ControlHelper.Colors[j]), rectColor); g.DrawString(barChartItems[j].ItemName, Font, new SolidBrush(ForeColor), new Point(rectColor.Right + 2, rectColor.Top)); } } intTopY += intItemNameRowCount * (intItemNameHeight + 20); } int intBottomY = 25; //处理x坐标文字高度 if (data_texts != null && data_texts.Length > 0) { var maxTextsHeight = data_texts.Max(p => g.MeasureString(p, Font).Height); if (intBottomY < maxTextsHeight) { intBottomY = (int)maxTextsHeight + 5; } } //画xy轴 Point[] array2 = new Point[3] { new Point(intLeftX, intTopY - 8), new Point(intLeftX, height - intBottomY), new Point(width - intRightX + 5, height - intBottomY) }; g.DrawLine(pen_normal, array2[0], array2[1]); g.DrawLine(pen_normal, array2[1], array2[2]); ChartsHelper.PaintTriangle(g, brush_deep, new Point(intLeftX, intTopY - 8), 4, GraphDirection.Upward); ChartsHelper.PaintTriangle(g, brush_deep, new Point(width - intRightX + 5, height - intBottomY), 4, GraphDirection.Rightward); //画横向分割线 for (int j = 0; j <= value_Segment; j++) { float value = (float)((double)j * (double)(num - value_min_left) / (double)value_Segment + (double)value_min_left); float num6 = ChartsHelper.ComputePaintLocationY((float)num, value_min_left, height - intTopY - intBottomY, value) + (float)intTopY; if (IsNeedPaintDash(num6)) { g.DrawLine(pen_normal, intLeftX - 4, num6, intLeftX - 1, num6); g.DrawString(layoutRectangle: new RectangleF(0f, num6 - 19f, intLeftX - 4, 40f), s: value.ToString(), font: Font, brush: brush_deep, format: format_right); if (j > 0 && value_IsRenderDashLine) { g.DrawLine(pen_dash, intLeftX, num6, width - intRightX, num6); } } } //计算辅助线y坐标 for (int i = 0; i < auxiliary_lines.Count; i++) { auxiliary_lines[i].PaintValue = ChartsHelper.ComputePaintLocationY((float)num, value_min_left, height - intTopY - intBottomY, auxiliary_lines[i].Value) + (float)intTopY; } //画辅助线 for (int k = 0; k < auxiliary_lines.Count; k++) { g.DrawLine(auxiliary_lines[k].GetPen(), intLeftX - 4, auxiliary_lines[k].PaintValue, intLeftX - 1, auxiliary_lines[k].PaintValue); g.DrawString(layoutRectangle: new RectangleF(0f, auxiliary_lines[k].PaintValue - 9f, intLeftX - 4, 20f), s: auxiliary_lines[k].Tip + "" + auxiliary_lines[k].Value.ToString(), font: Font, brush: auxiliary_lines[k].LineTextBrush, format: format_right); g.DrawLine(auxiliary_lines[k].GetPen(), intLeftX, auxiliary_lines[k].PaintValue, width - intRightX, auxiliary_lines[k].PaintValue); } if (data_values == null || data_values.Length == 0 || data_values.Max(p => p.Length) <= 0) { return; } //x轴分隔宽度 float fltSplitWidth = (float)(width - intLeftX - 1 - intRightX) * 1f / (float)data_values.Length; for (int i = 0; i < data_values.Length; i++) { int intItemSplitCount = barChartItems.Length; float _fltSplitWidth = fltSplitWidth * barPercentWidth / intItemSplitCount; float _fltLeft = (float)i * fltSplitWidth + (1f - barPercentWidth) / 2f * fltSplitWidth + (float)intLeftX; for (int j = 0; j < data_values[i].Length; j++) { if (j >= intItemSplitCount) { break; } float fltValueY = ChartsHelper.ComputePaintLocationY((float)num, value_min_left, height - intTopY - intBottomY, (float)data_values[i][j]) + (float)intTopY; RectangleF rect = new RectangleF(_fltLeft + _fltSplitWidth * j + (1F - barChartItems[j].BarPercentWidth) * _fltSplitWidth / 2f, fltValueY, _fltSplitWidth * barChartItems[j].BarPercentWidth, (float)(height - intBottomY) - fltValueY); Color color = barChartItems[j].BarBackColor ?? ControlHelper.Colors[j]; //画柱状 if (useGradient) { if (rect.Height > 0f) { using (LinearGradientBrush brush = new LinearGradientBrush(new PointF(rect.X, rect.Y + rect.Height), new PointF(rect.X, rect.Y), ChartsHelper.GetColorLight(color), color)) { g.FillRectangle(brush, rect); } } } else { using (Brush brush2 = new SolidBrush(color)) { g.FillRectangle(brush2, rect); } } //写值文字 if (isShowBarValue) { using (Brush brush3 = new SolidBrush(ForeColor)) { g.DrawString(layoutRectangle: new RectangleF(rect.Left - 50f, fltValueY - (float)Font.Height - 2f, _fltSplitWidth + 100f, Font.Height + 2), s: string.Format(showBarValueFormat, data_values[i][j]), font: Font, brush: brush3, format: format_center); } } } //写x轴文字 if (data_texts != null && i < data_texts.Length) { g.DrawString(layoutRectangle: new RectangleF((float)i * fltSplitWidth + (float)intLeftX - 50f, height - intBottomY - 1, fltSplitWidth + 100f, intBottomY + 1), s: data_texts[i], font: Font, brush: brush_deep, format: format_center); } } }
/// <summary> /// Draws the axis y. /// </summary> /// <param name="g">The g.</param> /// <param name="font">The font.</param> /// <param name="border">The border.</param> /// <param name="offset">The offset.</param> /// <param name="left">The left.</param> /// <param name="right">The right.</param> internal void DrawAxisY(Graphics g, Font font, Rectangle border, int offset, ref float left, ref float right) { if (!_visible) { return; } float y = border.Top + border.Height / 2; float num = _isCustomLabels ? ((float)(border.Top + offset)) : ((float)border.Top + (float)offset % locationStep); StringFormat format; StringFormat format2; float x; float x2; if (_position == AxisPosition.RightTop) { format = AxisFormat.TitleFormatRightAxisY; format2 = AxisFormat.LabelFormatRightAxisY; x = (float)border.Right + right + Width; x2 = (float)border.Right + right + 3f; right += Width; } else { format = AxisFormat.TitleFormatLeftAxisY; format2 = AxisFormat.LabelFormatLeftAxisY; x = left; x2 = left + Width - 3f; left += Width; } if (_labelVisible) { int count = _labels.Count; if (count > 0) { RectangleF rectangleF = default(RectangleF); for (int i = 0; i < count; i++) { float num3; if (_isCustomLabels) { double num2 = _isReverse ? _labels[i].Value : _labels[count - i - 1].Value; num3 = Convert.ToSingle((double)Height / Range * (num2 - MinValue)); if (!_isReverse) { num3 = Height - num3; } } else { num3 = (float)i * locationStep; } num3 += num; if (num3 >= (float)border.Top && num3 <= (float)border.Bottom) { PointF pointF = new PointF(x2, num3); if (!rectangleF.Contains(pointF)) { string s = _isReverse ? _labels[i].Content : _labels[count - i - 1].Content; g.DrawString(s, font, _brush, pointF, format2); rectangleF = new RectangleF(x2, num3, maxLabelWidth, maxLabelHeight + 6f); } } } } } if (!string.IsNullOrEmpty(_title)) { ChartsHelper.DrawString(g, _title, font, _brush, new PointF(x, y), format, -90f); } }