/// <summary> /// 画图 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { if (this.DesignMode) { ///Y坐标轴 _mainPanel.YAxisList.Clear(); _mainPanel.YAxisList.Add(new MedAxis(this.Font, Brushes.Black, 100, 0, 20)); _mainPanel.YAxisList.MinSetp = 1; _mainPanel.YAxisList.Pen.Color = Color.Black; ///X坐标轴 _mainPanel.XAxisList.Clear(); MedAxis axis = new MedAxis(this.Font, Brushes.Black, 10, 1, 1f); _mainPanel.XAxisList.Add(axis); _mainPanel.XAxisList.MinSetp = 1; _mainPanel.XAxisList.Pen.Color = Color.Black; ///其他属性 _mainPanel.LeftMargin = 10; _mainPanel.BottomMargin = 20; _mainPanel.RectBorderPen = Pens.Gray; _mainPanel.HasAxisGridLine = false; _mainPanel.XAxisTitleAtTop = false; _mainPanel.CurveList.Clear(); MedPointList points = new MedPointList(); points.Add(1, 10); points.Add(2, 40); points.Add(3, 20); points.Add(4, 80); points.Add(5, 50); points.Add(6, 70); points.Add(7, 60); points.Add(8, 100); points.Add(9, 90); points.Add(10, 0); MedCurve curve = new MedCurve(points); _mainPanel.CurveList.Add(curve); } ///隐藏滚动条 ///如果没有坐标轴则不作图 if ((_mainPanel.XAxisList.Count == 0) || (_mainPanel.YAxisList.Count == 0)) { return; } _mainPanel.DrawGraph(e.Graphics); ///绘制光标位置 if (_drawcursorpos) { DrawCursorPos(e.Graphics); } }
public void Add(MedAxis axis) { List.Add(axis); }
/// <summary> /// 绘制刻度值标题 /// </summary> /// <param name="g">作图对象</param> /// <param name="rect">区域</param> /// <param name="pen">画笔</param> /// <param name="gridPen">网格画笔</param> /// <param name="index">坐标轴索引</param> /// <param name="minstep">最小刻度</param> /// <param name="axis">坐标轴</param> public void DrawYAxisTitle(Graphics g, Rectangle rect, Pen pen, Pen gridPen, int index, int minstep, MedAxis axis, int leftOffSet, int topOffSet) { ///刻度数量 int _count = (int)((axis.Max - axis.Min) / (axis.Step)); for (int i = 0; i <= _count; i++) { float _Y = rect.Y + i * rect.Height / _count; /////绘制刻度值标题 float _V = (axis.Max - axis.Step * i); SizeF _sizeF = g.MeasureString(_V.ToString(), axis.Font); SizeF _sizeF1 = g.MeasureString("1000", axis.Font); ///有网格 if (_gridHeightPercent > 0) { if (i < _count) { g.DrawString(_V.ToString(), axis.Font, axis.Brush, rect.X + leftOffSet - _sizeF.Width - index * _sizeF1.Width - 15 + _yAxisTitleOffset, _Y + topOffSet - _sizeF.Height / 2); } } ///无网格 else { if ((i > 0) && (i < _count)) { g.DrawString(_V.ToString(), axis.Font, axis.Brush, rect.X + leftOffSet - _sizeF.Width - index * _sizeF1.Width - 15, _Y + topOffSet - _sizeF.Height / 2 - i - 1); } else if (i > 0) { g.DrawString(_V.ToString(), axis.Font, axis.Brush, rect.X + leftOffSet - _sizeF.Width - index * _sizeF1.Width - 15, _Y + topOffSet - _sizeF.Height - 2); } else { g.DrawString(_V.ToString(), axis.Font, axis.Brush, rect.X + leftOffSet - _sizeF.Width - index * _sizeF1.Width - 15, _Y + topOffSet - 2); } } } }
/// <summary> /// 绘制Y坐标轴 /// </summary> /// <param name="g">作图对象</param> /// <param name="rect">区域</param> /// <param name="pen">画笔</param> /// <param name="gridPen">网格画笔</param> /// <param name="index">坐标轴索引</param> /// <param name="minstep">最小刻度</param> /// <param name="axis">坐标轴</param> public void DrawYAxis(Graphics g, Rectangle rect, Pen pen, Pen gridPen, int index, int minstep, MedAxis axis, int leftOffSet, int topOffSet) { ///刻度数量 int _count = (int)((axis.Max - axis.Min) / (axis.Step)); for (int i = 0; i <= _count; i++) { float _Y = rect.Y + i * rect.Height / _count; ///只绘制一次 if (index == 0) { if (_gridHeightPercent > 0) { g.DrawLine(pen, rect.X + leftOffSet, _Y + topOffSet, rect.X + rect.Width + leftOffSet, _Y + topOffSet); ///绘制网格 if ((i < _count) && (_hasAxisGridLine)) { DrawAxisGrid(g, new Rectangle(rect.X + leftOffSet, (int)_Y + topOffSet, rect.Width, (int)(rect.Height / _count)), minstep, false, gridPen); } } else { if ((_hasAxisGridLine) || (i == _count)) { g.DrawLine(pen, rect.X + leftOffSet, _Y + topOffSet - i - 1, rect.X + rect.Width + leftOffSet, _Y + topOffSet - i - 1); } else { g.DrawLine(pen, rect.X + leftOffSet, _Y + topOffSet - i - 1, rect.X + 5 + leftOffSet, _Y + topOffSet - i - 1); } ///绘制网格 if ((i < _count) && (_hasAxisGridLine)) { DrawAxisGrid(g, new Rectangle(rect.X + leftOffSet, (int)_Y + topOffSet - i - 1, rect.Width, (int)(rect.Height / _count) - 1), minstep, false, gridPen); } } } } }
/// <summary> /// 画X坐标轴 /// </summary> /// <param name="g">作图对象</param> /// <param name="rect">作图区域</param> /// <param name="pen">画笔</param> /// <param name="gridPen">网格画笔</param> /// <param name="index">坐标轴索引</param> /// <param name="minstep">最小分割</param> /// <param name="axis">坐标轴</param> public void DrawXAxis(Graphics g, Rectangle rect, Pen pen, Pen gridPen, int index, int minstep, MedAxis axis, int leftOffSet, int topOffSet) { int _count = (int)((axis.Max - axis.Min) / (axis.Step)); if (_count == 0) { return; } for (int i = 0; i <= _count; i++) { float _X = rect.X + i * rect.Width / _count; if (index == 0) { if ((_hasAxisGridLine) || (i == 0)) { g.DrawLine(pen, _X + leftOffSet, rect.Y + topOffSet, _X + leftOffSet, rect.Y + topOffSet + rect.Height); } else { g.DrawLine(pen, _X + leftOffSet, rect.Y + topOffSet + rect.Height - 10, _X + leftOffSet, rect.Y + topOffSet + rect.Height); } if ((i < _count) && _hasAxisGridLine) { DrawAxisGrid(g, new Rectangle((int)_X + leftOffSet, rect.Y + topOffSet, (int)(rect.Width / _count), rect.Height), minstep, true, gridPen); } } } }