protected void CalcAxises() { if (Option.XAxisType == UIAxisType.DateTime) { XScale = new UIDateScale(); } else { XScale = new UILinearScale(); } YScale = new UILinearScale(); //Y轴 { Option.GetAllDataYRange(out double min, out double max); if (min > 0 && max > 0 && !Option.YAxis.Scale) { min = 0; } if (min < 0 && max < 0 && !Option.YAxis.Scale) { max = 0; } YScale.SetRange(min, max); if (!Option.YAxis.MaxAuto) { YScale.Max = Option.YAxis.Max; } if (!Option.YAxis.MinAuto) { YScale.Min = Option.YAxis.Min; } YScale.AxisChange(); YLabels = YScale.CalcLabels(); } //X轴 { Option.GetAllDataXRange(out double min, out double max); XScale.SetRange(min, max); if (!Option.XAxis.MaxAuto) { XScale.Max = Option.XAxis.Max; } if (!Option.XAxis.MinAuto) { XScale.Min = Option.XAxis.Min; } XScale.AxisChange(); XLabels = XScale.CalcLabels(); } }
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); } }