*UILineChart:双坐标轴增加纵横参考线

This commit is contained in:
Sunny 2021-12-31 10:05:56 +08:00
parent 4205e98dbc
commit b657428f6e
4 changed files with 70 additions and 38 deletions

Binary file not shown.

Binary file not shown.

View File

@ -219,6 +219,13 @@ namespace Sunny.UI.Demo
option.Y2Axis.AxisLabel.DecimalCount = 1; option.Y2Axis.AxisLabel.DecimalCount = 1;
option.Y2Axis.AxisLabel.AutoFormat = false; option.Y2Axis.AxisLabel.AutoFormat = false;
option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Red, Name = "上限", Value = 3.5 });
option.Y2AxisScaleLines.Add(new UIScaleLine() { Color = Color.Gold, Name = "下限", Value = 12 });
option.XAxisScaleLines.Add(new UIScaleLine() { Color = Color.Lime, Name = "3", Value = 3 });
option.XAxisScaleLines.Add(new UIScaleLine() { Color = Color.Gold, Name = "6", Value = 6 });
LineChart.SetOption(option); LineChart.SetOption(option);
} }
} }

View File

@ -625,54 +625,79 @@ namespace Sunny.UI
private void DrawAxisScales(Graphics g) private void DrawAxisScales(Graphics g)
{ {
if (YScale == null) return; if (YScale != null)
if (Option.HaveY2) return; foreach (var line in Option.YAxisScaleLines)
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))
{ {
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos); 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))
{
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;
SizeF sf = g.MeasureString(line.Name, TempFont); using (Pen pn = new Pen(line.Color, line.Size))
{
g.DrawLine(pn, DrawOrigin.X + 1, pos, Width - Option.Grid.Right - 1, pos);
}
if (line.Left == UILeftAlignment.Left)
g.DrawString(line.Name, TempFont, line.Color, DrawOrigin.X + 4, pos - 2 - sf.Height); SizeF sf = g.MeasureString(line.Name, TempFont);
if (line.Left == UILeftAlignment.Center) line.Left = UILeftAlignment.Right;
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.Left)
if (line.Left == UILeftAlignment.Right) g.DrawString(line.Name, TempFont, line.Color, DrawOrigin.X + 4, pos - 2 - sf.Height);
g.DrawString(line.Name, TempFont, line.Color, Width - sf.Width - 4 - Option.Grid.Right, 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; int idx = 0;
foreach (var line in Option.XAxisScaleLines) 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))
{ {
g.DrawLine(pn, pos, DrawOrigin.Y - 1, pos, Option.Grid.Top + 1); float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width);
} if (pos <= Option.Grid.Left || pos >= Width - Option.Grid.Right) continue;
SizeF sf = g.MeasureString(line.Name, TempFont); using (Pen pn = new Pen(line.Color, line.Size))
float x = pos - sf.Width; {
if (x < Option.Grid.Left) x = pos + 2; g.DrawLine(pn, pos, DrawOrigin.Y - 1, pos, Option.Grid.Top + 1);
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++; SizeF sf = g.MeasureString(line.Name, TempFont);
g.DrawString(line.Name, TempFont, line.Color, x, y); 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 readonly List<UILineSelectPoint> selectPoints = new List<UILineSelectPoint>(); private readonly List<UILineSelectPoint> selectPoints = new List<UILineSelectPoint>();