diff --git a/Bin/SunnyUI.Demo.exe b/Bin/SunnyUI.Demo.exe index 4c6d96e3..e0062293 100644 Binary files a/Bin/SunnyUI.Demo.exe and b/Bin/SunnyUI.Demo.exe differ diff --git a/Bin/SunnyUI.dll b/Bin/SunnyUI.dll index ef860db6..5bd5a4af 100644 Binary files a/Bin/SunnyUI.dll and b/Bin/SunnyUI.dll differ diff --git a/SunnyUI.Demo/Charts/FLineChart.cs b/SunnyUI.Demo/Charts/FLineChart.cs index d84c4ad6..07bb573d 100644 --- a/SunnyUI.Demo/Charts/FLineChart.cs +++ b/SunnyUI.Demo/Charts/FLineChart.cs @@ -23,24 +23,24 @@ namespace Sunny.UI.Demo.Charts var series = option.AddSeries(new UILineSeries("Line1")); DateTime dt = new DateTime(2020, 10, 4); - series.Add(dt.AddHours(0), 1.2); - series.Add(dt.AddHours(1), 2.2); - series.Add(dt.AddHours(2), 3.2); - series.Add(dt.AddHours(3), 4.2); - series.Add(dt.AddHours(4), 3.2); - series.Add(dt.AddHours(5), 2.2); + // series.Add(dt.AddHours(0), 1.2); + // series.Add(dt.AddHours(1), 2.2); + // series.Add(dt.AddHours(2), 3.2); + // series.Add(dt.AddHours(3), 4.2); + // series.Add(dt.AddHours(4), 3.2); + // series.Add(dt.AddHours(5), 2.2); series.Symbol = UILinePointSymbol.Square; series.SymbolSize = 4; series.SymbolLineWidth = 2; series.SymbolColor = Color.Red; series = option.AddSeries(new UILineSeries("Line2", Color.Lime)); - series.Add(dt.AddHours(3), 3.3); - series.Add(dt.AddHours(4), 2.3); - series.Add(dt.AddHours(5), 2.3); - series.Add(dt.AddHours(6), 1.3); - series.Add(dt.AddHours(7), 2.3); - series.Add(dt.AddHours(8), 4.3); + // series.Add(dt.AddHours(3), 3.3); + // series.Add(dt.AddHours(4), 2.3); + // series.Add(dt.AddHours(5), 2.3); + // series.Add(dt.AddHours(6), 1.3); + // series.Add(dt.AddHours(7), 2.3); + // series.Add(dt.AddHours(8), 4.3); series.Symbol = UILinePointSymbol.Star; series.SymbolSize = 4; series.SymbolLineWidth = 2; diff --git a/SunnyUI/Charts/UILineChart.cs b/SunnyUI/Charts/UILineChart.cs index 7529b7c9..b0d8e300 100644 --- a/SunnyUI/Charts/UILineChart.cs +++ b/SunnyUI/Charts/UILineChart.cs @@ -213,6 +213,30 @@ namespace Sunny.UI } } + private void DrawSeries(Graphics g, Color color, UILineSeries series) + { + if (series.Points.Count == 0) + { + return; + } + + if (series.Points.Count == 1 && series.Symbol == UILinePointSymbol.None) + { + g.FillEllipse(color, series.Points[0].X - 2, series.Points[0].Y - 2, 4, 4); + return; + } + + using (Pen pen = new Pen(color, series.Width)) + { + g.SetHighQuality(); + if (series.Smooth) + g.DrawCurve(pen, series.Points.ToArray()); + else + g.DrawLines(pen, series.Points.ToArray()); + g.SetDefaultQuality(); + } + } + private void DrawSeries(Graphics g) { if (YScale == null) return; @@ -224,17 +248,7 @@ namespace Sunny.UI { Color color = series.Color; if (!series.CustomColor) color = ChartStyle.GetColor(idx); - - using (Pen pen = new Pen(color, series.Width)) - { - g.SetHighQuality(); - if (series.Smooth) - g.DrawCurve(pen, series.Points.ToArray()); - else - g.DrawLines(pen, series.Points.ToArray()); - g.SetDefaultQuality(); - } - + DrawSeries(g, color, series); idx++; } } @@ -251,42 +265,24 @@ namespace Sunny.UI Color color = series.Color; if (!series.CustomColor) color = ChartStyle.GetColor(idx); - using (Pen pen = new Pen(color, series.Width)) + using (Graphics graphics = bmp.Graphics()) { - Graphics graphics = bmp.Graphics(); - graphics.SetHighQuality(); - if (series.Smooth) - graphics.DrawCurve(pen, series.Points.ToArray()); - else - graphics.DrawLines(pen, series.Points.ToArray()); - graphics.SetDefaultQuality(); + DrawSeries(graphics, color, series); } if (LineOption.GreaterWarningArea != null) { - using (Pen pen = new Pen(LineOption.GreaterWarningArea.Color, series.Width)) + using (Graphics graphics = bmpGreater.Graphics()) { - Graphics graphics = bmpGreater.Graphics(); - graphics.SetHighQuality(); - if (series.Smooth) - graphics.DrawCurve(pen, series.Points.ToArray()); - else - graphics.DrawLines(pen, series.Points.ToArray()); - graphics.SetDefaultQuality(); + DrawSeries(graphics, LineOption.GreaterWarningArea.Color, series); } } if (LineOption.LessWarningArea != null) { - using (Pen pen = new Pen(LineOption.LessWarningArea.Color, series.Width)) + using (Graphics graphics = bmpLess.Graphics()) { - Graphics graphics = bmpLess.Graphics(); - graphics.SetHighQuality(); - if (series.Smooth) - graphics.DrawCurve(pen, series.Points.ToArray()); - else - graphics.DrawLines(pen, series.Points.ToArray()); - graphics.SetDefaultQuality(); + DrawSeries(graphics, LineOption.LessWarningArea.Color, series); } } @@ -296,7 +292,6 @@ namespace Sunny.UI if (LineOption.GreaterWarningArea != null) { wTop = YScale.CalcYPixel(LineOption.GreaterWarningArea.Value, DrawOrigin.Y, DrawSize.Height); - wTop = DrawOrigin.Y - wTop; g.DrawImage(bmpGreater, new Rectangle(0, 0, Width, (int)wTop), new Rectangle(0, 0, Width, (int)wTop), GraphicsUnit.Pixel); } @@ -304,7 +299,6 @@ namespace Sunny.UI if (LineOption.LessWarningArea != null) { wBottom = YScale.CalcYPixel(LineOption.LessWarningArea.Value, DrawOrigin.Y, DrawSize.Height); - wBottom = DrawOrigin.Y - wBottom; g.DrawImage(bmpLess, new Rectangle(0, (int)wBottom, Width, Height - (int)wBottom), new Rectangle(0, (int)wBottom, Width, Height - (int)wBottom), GraphicsUnit.Pixel); } @@ -398,7 +392,6 @@ namespace Sunny.UI foreach (var line in LineOption.YAxisScaleLines) { float pos = YScale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); - pos = (Height - LineOption.Grid.Bottom - pos); using (Pen pn = new Pen(line.Color, line.Size)) { g.DrawLine(pn, DrawOrigin.X, pos, Width - LineOption.Grid.Right, pos); @@ -426,6 +419,7 @@ namespace Sunny.UI selectPointsTemp.Clear(); foreach (var series in LineOption.Series.Values) { + if (series.DataCount == 0) continue; if (series.GetNearestPoint(e.Location, 4, out double x, out double y, out int index)) { UILineSelectPoint point = new UILineSelectPoint(); diff --git a/SunnyUI/Charts/UILineChartOption.cs b/SunnyUI/Charts/UILineChartOption.cs index 22a113ea..2a738608 100644 --- a/SunnyUI/Charts/UILineChartOption.cs +++ b/SunnyUI/Charts/UILineChartOption.cs @@ -267,6 +267,11 @@ namespace Sunny.UI public bool GetNearestPoint(Point p, int offset, out double x, out double y, out int index) { + x = 0; + y = 0; + index = -1; + if (PointsX.Count == 0) return false; + index = PointsX.BinarySearchNearIndex(p.X); if (p.X >= PointsX[index] - offset && p.X <= PointsX[index] + offset && p.Y >= PointsY[index] - offset && p.Y <= PointsY[index] + offset) @@ -276,8 +281,6 @@ namespace Sunny.UI return true; } - x = 0; - y = 0; return false; } diff --git a/SunnyUI/Static/UMath.cs b/SunnyUI/Static/UMath.cs index 4ee4a4e7..9386f9a4 100644 --- a/SunnyUI/Static/UMath.cs +++ b/SunnyUI/Static/UMath.cs @@ -68,6 +68,7 @@ namespace Sunny.UI /// 最近值序号 public static int BinarySearchNearIndex(this IList list, T target) where T : IComparable { + if (list.Count == 0) return -1; int i = 0, j = list.Count - 1; if (target.CompareTo(list[0]) == -1) return 0; @@ -166,7 +167,7 @@ namespace Sunny.UI public static T CheckLowerLimit(this T obj, T lowerLimit) where T : IComparable { - return obj.CompareTo(lowerLimit) == -1 ? lowerLimit : obj; + return obj.CompareTo(lowerLimit) == -1 ? lowerLimit : obj; } public static T CheckUpperLimit(this T obj, T upperLimit) where T : IComparable