private void DrawAxis(Graphics g) { if (Option.Grid.LeftShow) { g.DrawLine(ForeColor, Option.Grid.Left, Option.Grid.Top, Option.Grid.Left, Height - Option.Grid.Bottom); } if (Option.Grid.TopShow) { g.DrawLine(ForeColor, Option.Grid.Left, Option.Grid.Top, Width - Option.Grid.Right, Option.Grid.Top); } if (Option.Grid.RightShow) { g.DrawLine(ForeColor, Width - Option.Grid.Right, Option.Grid.Top, Width - Option.Grid.Right, Height - Option.Grid.Bottom); } if (Option.Grid.BottomShow) { g.DrawLine(ForeColor, Option.Grid.Left, Height - Option.Grid.Bottom, Width - Option.Grid.Right, Height - Option.Grid.Bottom); } float zeroPos = YScale.CalcYPixel(0, DrawOrigin.Y, DrawSize.Height); if (zeroPos > Option.Grid.Top && zeroPos < Height - Option.Grid.Bottom) { if (Option.ShowZeroLine) { g.DrawLine(ForeColor, DrawOrigin.X, zeroPos, DrawOrigin.X + DrawSize.Width, zeroPos); } if (Option.ShowZeroValue) { SizeF sf = g.MeasureString("0", TempFont); g.DrawString("0", TempFont, ForeColor, DrawOrigin.X - Option.YAxis.AxisTick.Length - sf.Width, zeroPos - sf.Height / 2.0f); } } if (XScale == null || YScale == null || Y2Scale == null) { return; } //X Tick { double[] XLabels = Option.XAxis.HaveCustomLabels ? Option.XAxis.CustomLabels.LabelValues() : XScale.CalcLabels(); float[] labels = XScale.CalcXPixels(XLabels, DrawOrigin.X, DrawSize.Width); float xr = 0; for (int i = 0; i < labels.Length; i++) { float x = labels[i]; if (x < Option.Grid.Left || x > Width - Option.Grid.Right) { continue; } if (Option.XAxis.AxisLabel.Show) { string label; if (Option.XAxisType == UIAxisType.DateTime) { if (Option.XAxis.AxisLabel.AutoFormat) { label = new DateTimeInt64(XLabels[i]).ToString(XScale.Format); } else { label = new DateTimeInt64(XLabels[i]).ToString(Option.XAxis.AxisLabel.DateTimeFormat); } } else { if (Option.XAxis.AxisLabel.AutoFormat) { label = XLabels[i].ToString(XScale.Format); } else { label = XLabels[i].ToString("F" + Option.XAxis.AxisLabel.DecimalCount); } } if (Option.XAxis.HaveCustomLabels && Option.XAxis.CustomLabels.GetLabel(i).IsValid()) { label = Option.XAxis.CustomLabels.GetLabel(i); } SizeF sf = g.MeasureString(label, TempFont); float xx = x - sf.Width / 2.0f; if (xx > xr && xx + sf.Width < Width) { xr = xx + sf.Width; g.DrawString(label, TempFont, ForeColor, xx, DrawOrigin.Y + Option.XAxis.AxisTick.Length); } } if (Option.XAxis.AxisTick.Show) { g.DrawLine(ForeColor, x, DrawOrigin.Y, x, DrawOrigin.Y + Option.XAxis.AxisTick.Length); } if (x.Equals(DrawOrigin.X)) { continue; } if (x.Equals(DrawOrigin.X + DrawSize.Width)) { continue; } if (Option.XAxis.ShowGridLine) { using (Pen pn = new Pen(ForeColor)) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, x, DrawOrigin.Y, x, Option.Grid.Top); } } } SizeF sfName = g.MeasureString(Option.XAxis.Name, TempFont); g.DrawString(Option.XAxis.Name, TempFont, ForeColor, DrawOrigin.X + (DrawSize.Width - sfName.Width) / 2.0f, DrawOrigin.Y + Option.XAxis.AxisTick.Length + sfName.Height); } //Y Tick { double[] YLabels = Option.YAxis.HaveCustomLabels ? Option.YAxis.CustomLabels.LabelValues() : YScale.CalcLabels(); float[] labels = YScale.CalcYPixels(YLabels, DrawOrigin.Y, DrawSize.Height); float widthMax = 0; for (int i = 0; i < labels.Length; i++) { float y = labels[i]; if (y < Option.Grid.Top || y > Height - Option.Grid.Bottom) { continue; } string label = YLabels[i].ToString(YScale.Format); SizeF sf = g.MeasureString(label, TempFont); widthMax = Math.Max(widthMax, sf.Width); if (Option.YAxis.AxisLabel.Show) { g.DrawString(label, TempFont, ForeColor, DrawOrigin.X - Option.YAxis.AxisTick.Length - sf.Width, y - sf.Height / 2.0f); } if (Option.YAxis.AxisTick.Show) { g.DrawLine(ForeColor, DrawOrigin.X, y, DrawOrigin.X - Option.YAxis.AxisTick.Length, y); } if (y.Equals(DrawOrigin.Y)) { continue; } if (y.Equals(DrawOrigin.X - DrawSize.Height)) { continue; } if (Option.YAxis.ShowGridLine) { using (Pen pn = new Pen(ForeColor)) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, DrawOrigin.X, y, Width - Option.Grid.Right, y); } } } SizeF sfName = g.MeasureString(Option.YAxis.Name, TempFont); float xx = DrawOrigin.X - Option.YAxis.AxisTick.Length - widthMax - sfName.Height / 2.0f; float yy = Option.Grid.Top + DrawSize.Height / 2.0f; g.DrawStringRotateAtCenter(Option.YAxis.Name, TempFont, ForeColor, new PointF(xx, yy), 270); } //Y2 Tick if (Option.HaveY2) { double[] Y2Labels = Option.Y2Axis.HaveCustomLabels ? Option.Y2Axis.CustomLabels.LabelValues() : Y2Scale.CalcLabels(); float[] labels = Y2Scale.CalcYPixels(Y2Labels, DrawOrigin.Y, DrawSize.Height); float widthMax = 0; for (int i = 0; i < labels.Length; i++) { float y = labels[i]; if (y < Option.Grid.Top || y > Height - Option.Grid.Bottom) { continue; } if (Option.Y2Axis.AxisLabel.Show) { string label = Y2Labels[i].ToString(Y2Scale.Format); SizeF sf = g.MeasureString(label, TempFont); widthMax = Math.Max(widthMax, sf.Width); g.DrawString(label, TempFont, ForeColor, Width - Option.Grid.Right + Option.Y2Axis.AxisTick.Length, y - sf.Height / 2.0f); } if (Option.Y2Axis.AxisTick.Show) { g.DrawLine(ForeColor, Width - Option.Grid.Right, y, Width - Option.Grid.Right + Option.YAxis.AxisTick.Length, y); } if (y.Equals(DrawOrigin.Y)) { continue; } if (y.Equals(DrawOrigin.X - DrawSize.Height)) { continue; } using (Pen pn = new Pen(ForeColor)) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; //g.DrawLine(pn, DrawOrigin.X, y, Width - Option.Grid.Right, y); } } SizeF sfName = g.MeasureString(Option.Y2Axis.Name, TempFont); float xx = Width - Option.Grid.Right + Option.Y2Axis.AxisTick.Length + widthMax + sfName.Height / 2.0f; float yy = Option.Grid.Top + DrawSize.Height / 2.0f; g.DrawStringRotateAtCenter(Option.Y2Axis.Name, TempFont, ForeColor, new PointF(xx, yy), 90); } }
private void DrawAxisScales(Graphics g) { if (YScale != null) { foreach (var line in Option.YAxisScaleLines) { float pos = YScale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); if (pos <= Option.Grid.Top || pos >= Height - Option.Grid.Bottom) { continue; } using (Pen pn = new Pen(line.Color, line.Size)) { if (line.DashDot) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; } g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos); } SizeF sf = g.MeasureString(line.Name, TempFont); if (Option.Y2AxisScaleLines != null) { line.Left = UILeftAlignment.Left; } if (line.Left == UILeftAlignment.Left) { g.DrawString(line.Name, TempFont, line.Color, DrawOrigin.X + 4, pos - 2 - sf.Height); } if (line.Left == UILeftAlignment.Center) { g.DrawString(line.Name, TempFont, line.Color, DrawOrigin.X + (Width - Option.Grid.Left - Option.Grid.Right - sf.Width) / 2, pos - 2 - sf.Height); } if (line.Left == UILeftAlignment.Right) { g.DrawString(line.Name, TempFont, line.Color, Width - sf.Width - 4 - Option.Grid.Right, pos - 2 - sf.Height); } } } if (Y2Scale != null) { foreach (var line in Option.Y2AxisScaleLines) { float pos = Y2Scale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); if (pos <= Option.Grid.Top || pos >= Height - Option.Grid.Bottom) { continue; } using (Pen pn = new Pen(line.Color, line.Size)) { if (line.DashDot) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; } g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos); } SizeF sf = g.MeasureString(line.Name, TempFont); line.Left = UILeftAlignment.Right; if (line.Left == UILeftAlignment.Left) { g.DrawString(line.Name, TempFont, line.Color, DrawOrigin.X + 4, pos - 2 - sf.Height); } if (line.Left == UILeftAlignment.Center) { g.DrawString(line.Name, TempFont, line.Color, DrawOrigin.X + (Width - Option.Grid.Left - Option.Grid.Right - sf.Width) / 2, pos - 2 - sf.Height); } if (line.Left == UILeftAlignment.Right) { g.DrawString(line.Name, TempFont, line.Color, Width - sf.Width - 4 - Option.Grid.Right, pos - 2 - sf.Height); } } } int idx = 0; if (XScale != null) { foreach (var line in Option.XAxisScaleLines) { float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width); if (pos <= Option.Grid.Left || pos >= Width - Option.Grid.Right) { continue; } using (Pen pn = new Pen(line.Color, line.Size)) { if (line.DashDot) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; } g.DrawLine(pn, pos, DrawOrigin.Y - 1, pos, Option.Grid.Top + 1); } SizeF sf = g.MeasureString(line.Name, TempFont); float x = pos - sf.Width; if (x < Option.Grid.Left) { x = pos + 2; } float y = Option.Grid.Top + 4 + sf.Height * idx; if (y > Height - Option.Grid.Bottom) { idx = 0; y = Option.Grid.Top + 4 + sf.Height * idx; } idx++; g.DrawString(line.Name, TempFont, line.Color, x, y); } } }
private void DrawAxis(Graphics g) { g.DrawRectangle(ChartStyle.ForeColor, Option.Grid.Left, Option.Grid.Top, DrawSize.Width, DrawSize.Height); float zeroPos = YScale.CalcYPixel(0, DrawOrigin.Y, DrawSize.Height); g.DrawLine(ChartStyle.ForeColor, DrawOrigin.X, zeroPos, DrawOrigin.X + DrawSize.Width, zeroPos); if (XScale == null || YScale == null) { return; } //X Tick if (Option.XAxis.AxisTick.Show) { float[] labels = XScale.CalcXPixels(XLabels, DrawOrigin.X, DrawSize.Width); for (int i = 0; i < labels.Length; i++) { float x = labels[i]; if (Option.XAxis.AxisLabel.Show) { string label; if (Option.XAxisType == UIAxisType.DateTime) { if (Option.XAxis.AxisLabel.AutoFormat) { label = new DateTimeInt64(XLabels[i]).ToString(XScale.Format); } else { label = new DateTimeInt64(XLabels[i]).ToString(Option.XAxis.AxisLabel.DateTimeFormat); } } else { if (Option.XAxis.AxisLabel.AutoFormat) { label = XLabels[i].ToString(XScale.Format); } else { label = XLabels[i].ToString("F" + Option.XAxis.AxisLabel.DecimalCount); } } SizeF sf = g.MeasureString(label, SubFont); g.DrawString(label, SubFont, ChartStyle.ForeColor, x - sf.Width / 2.0f, DrawOrigin.Y + Option.XAxis.AxisTick.Length); } if (x.Equals(DrawOrigin.X)) { continue; } if (x.Equals(DrawOrigin.X + DrawSize.Width)) { continue; } using (Pen pn = new Pen(ChartStyle.ForeColor)) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, x, DrawOrigin.Y, x, Option.Grid.Top); } } SizeF sfName = g.MeasureString(Option.XAxis.Name, SubFont); g.DrawString(Option.XAxis.Name, SubFont, ChartStyle.ForeColor, DrawOrigin.X + (DrawSize.Width - sfName.Width) / 2.0f, DrawOrigin.Y + Option.XAxis.AxisTick.Length + sfName.Height); } //Y Tick if (Option.YAxis.AxisTick.Show) { float[] labels = YScale.CalcYPixels(YLabels, DrawOrigin.Y, DrawSize.Height); float widthMax = 0; for (int i = 0; i < labels.Length; i++) { float y = labels[i]; if (Option.YAxis.AxisLabel.Show) { string label = YLabels[i].ToString(YScale.Format); SizeF sf = g.MeasureString(label, SubFont); widthMax = Math.Max(widthMax, sf.Width); g.DrawString(label, SubFont, ChartStyle.ForeColor, DrawOrigin.X - Option.YAxis.AxisTick.Length - sf.Width, y - sf.Height / 2.0f); } if (y.Equals(DrawOrigin.Y)) { continue; } if (y.Equals(DrawOrigin.X - DrawSize.Height)) { continue; } using (Pen pn = new Pen(ChartStyle.ForeColor)) { pn.DashStyle = DashStyle.Dash; pn.DashPattern = new float[] { 3, 3 }; g.DrawLine(pn, DrawOrigin.X, y, Width - Option.Grid.Right, y); } } SizeF sfName = g.MeasureString(Option.YAxis.Name, SubFont); int xx = (int)(DrawOrigin.X - Option.YAxis.AxisTick.Length - widthMax - sfName.Height); int yy = (int)(Option.Grid.Top + (DrawSize.Height - sfName.Width) / 2); g.DrawString(Option.YAxis.Name, SubFont, ChartStyle.ForeColor, new Point(xx, yy), new StringFormat() { Alignment = StringAlignment.Center }, 270); } }