diff --git a/Bin/SunnyUI.Demo.exe b/Bin/SunnyUI.Demo.exe index 6bebcf11..e7e2d03e 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 62d0ed68..04b259a8 100644 Binary files a/Bin/SunnyUI.dll and b/Bin/SunnyUI.dll differ diff --git a/SunnyUI.Demo/Charts/FBarChartEx.cs b/SunnyUI.Demo/Charts/FBarChartEx.cs index 02d7c2fb..6dd79779 100644 --- a/SunnyUI.Demo/Charts/FBarChartEx.cs +++ b/SunnyUI.Demo/Charts/FBarChartEx.cs @@ -83,6 +83,8 @@ namespace Sunny.UI.Demo option.FixedSeriesCount = 3; option.AutoSizeBars = true; + option.AutoSizeBarsCompact = true; + option.AutoSizeBarsCompactValue = 0.8f; option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Red, Name = "上限", Value = 12 }); option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Gold, Name = "下限", Value = -20 }); diff --git a/SunnyUI/Charts/UIBarChartEx.cs b/SunnyUI/Charts/UIBarChartEx.cs index 7689347f..e06ace24 100644 --- a/SunnyUI/Charts/UIBarChartEx.cs +++ b/SunnyUI/Charts/UIBarChartEx.cs @@ -92,76 +92,156 @@ namespace Sunny.UI float x1 = DrawSize.Width * 1.0f / DataCount / 4; float x2 = x1 * 2; - for (int i = 0; i < Option.SeriesCount; i++) + if (Option.AutoSizeBarsCompact) { - var series = Option.Series[i]; - Bars.TryAdd(i, new List()); - - for (int j = 0; j < series.Data.Count; j++) + for (int i = 0; i < Option.SeriesCount; i++) { - Color color = ChartStyle.GetColor(i); - if (series.Colors.Count > 0 && j >= 0 && j < series.Colors.Count) - color = series.Colors[j]; + var series = Option.Series[i]; + Bars.TryAdd(i, new List()); + float sx = barX + x1 * ((series.Data.Count * 4 - (series.Data.Count * 2 + (series.Data.Count - 1) * Option.AutoSizeBarsCompactValue)) / 2.0f) + x1; - float xx = barX + DrawSize.Width * 1.0f / DataCount / 2; - float ww = Math.Min(x2, series.MaxWidth); - xx -= ww / 2.0f; - - if (YAxisStart >= 0) + for (int j = 0; j < series.Data.Count; j++) { - float h = Math.Abs((float)(DrawSize.Height * (series.Data[j] - start * interval) / ((end - start) * interval))); + Color color = ChartStyle.GetColor(i); + if (series.Colors.Count > 0 && j >= 0 && j < series.Colors.Count) + color = series.Colors[j]; - Bars[i].Add(new BarInfo() - { - Rect = new RectangleF(xx, DrawOrigin.Y - h, ww, h), - Color = color - }); - } - else if (YAxisEnd <= 0) - { - float h = Math.Abs((float)(DrawSize.Height * (end * interval - series.Data[j]) / ((end - start) * interval))); - Bars[i].Add(new BarInfo() - { - Rect = new RectangleF(xx, Option.Grid.Top + 1, ww, h - 1), - Color = color - }); - } - else - { - float lowH = 0; - float highH = 0; - float DrawBarHeight = DrawSize.Height * 1.0f / (YAxisEnd - YAxisStart); - float lowV = 0; - float highV = 0; - for (int k = YAxisStart; k <= YAxisEnd; k++) - { - if (k < 0) lowH += DrawBarHeight; - if (k > 0) highH += DrawBarHeight; - if (k < 0) lowV += (float)YAxisInterval; - if (k > 0) highV += (float)YAxisInterval; - } + float ww = Math.Min(x2, series.MaxWidth); + float xx = sx - ww / 2.0f; - if (series.Data[j] >= 0) + if (YAxisStart >= 0) { - float h = Math.Abs((float)(highH * series.Data[j] / highV)); + float h = Math.Abs((float)(DrawSize.Height * (series.Data[j] - start * interval) / ((end - start) * interval))); + Bars[i].Add(new BarInfo() { - Rect = new RectangleF(xx, DrawOrigin.Y - lowH - h, ww, h), + Rect = new RectangleF(xx, DrawOrigin.Y - h, ww, h), + Color = color + }); + } + else if (YAxisEnd <= 0) + { + float h = Math.Abs((float)(DrawSize.Height * (end * interval - series.Data[j]) / ((end - start) * interval))); + Bars[i].Add(new BarInfo() + { + Rect = new RectangleF(xx, Option.Grid.Top + 1, ww, h - 1), Color = color }); } else { - float h = Math.Abs((float)(lowH * series.Data[j] / lowV)); + float lowH = 0; + float highH = 0; + float DrawBarHeight = DrawSize.Height * 1.0f / (YAxisEnd - YAxisStart); + float lowV = 0; + float highV = 0; + for (int k = YAxisStart; k <= YAxisEnd; k++) + { + if (k < 0) lowH += DrawBarHeight; + if (k > 0) highH += DrawBarHeight; + if (k < 0) lowV += (float)YAxisInterval; + if (k > 0) highV += (float)YAxisInterval; + } + + if (series.Data[j] >= 0) + { + float h = Math.Abs((float)(highH * series.Data[j] / highV)); + Bars[i].Add(new BarInfo() + { + Rect = new RectangleF(xx, DrawOrigin.Y - lowH - h, ww, h), + Color = color + }); + } + else + { + float h = Math.Abs((float)(lowH * series.Data[j] / lowV)); + Bars[i].Add(new BarInfo() + { + Rect = new RectangleF(xx, DrawOrigin.Y - lowH + 1, ww, h - 1), + Color = color + }); + } + } + + sx += x2 + x1 * Option.AutoSizeBarsCompactValue; + } + + barX += x2 * 2 * series.Data.Count; + } + } + else + { + for (int i = 0; i < Option.SeriesCount; i++) + { + var series = Option.Series[i]; + Bars.TryAdd(i, new List()); + + for (int j = 0; j < series.Data.Count; j++) + { + Color color = ChartStyle.GetColor(i); + if (series.Colors.Count > 0 && j >= 0 && j < series.Colors.Count) + color = series.Colors[j]; + + float xx = barX + DrawSize.Width * 1.0f / DataCount / 2; + float ww = Math.Min(x2, series.MaxWidth); + xx -= ww / 2.0f; + + if (YAxisStart >= 0) + { + float h = Math.Abs((float)(DrawSize.Height * (series.Data[j] - start * interval) / ((end - start) * interval))); + Bars[i].Add(new BarInfo() { - Rect = new RectangleF(xx, DrawOrigin.Y - lowH + 1, ww, h - 1), + Rect = new RectangleF(xx, DrawOrigin.Y - h, ww, h), Color = color }); } - } + else if (YAxisEnd <= 0) + { + float h = Math.Abs((float)(DrawSize.Height * (end * interval - series.Data[j]) / ((end - start) * interval))); + Bars[i].Add(new BarInfo() + { + Rect = new RectangleF(xx, Option.Grid.Top + 1, ww, h - 1), + Color = color + }); + } + else + { + float lowH = 0; + float highH = 0; + float DrawBarHeight = DrawSize.Height * 1.0f / (YAxisEnd - YAxisStart); + float lowV = 0; + float highV = 0; + for (int k = YAxisStart; k <= YAxisEnd; k++) + { + if (k < 0) lowH += DrawBarHeight; + if (k > 0) highH += DrawBarHeight; + if (k < 0) lowV += (float)YAxisInterval; + if (k > 0) highV += (float)YAxisInterval; + } - barX += DrawSize.Width * 1.0f / DataCount; + if (series.Data[j] >= 0) + { + float h = Math.Abs((float)(highH * series.Data[j] / highV)); + Bars[i].Add(new BarInfo() + { + Rect = new RectangleF(xx, DrawOrigin.Y - lowH - h, ww, h), + Color = color + }); + } + else + { + float h = Math.Abs((float)(lowH * series.Data[j] / lowV)); + Bars[i].Add(new BarInfo() + { + Rect = new RectangleF(xx, DrawOrigin.Y - lowH + 1, ww, h - 1), + Color = color + }); + } + } + + barX += DrawSize.Width * 1.0f / DataCount; + } } } } diff --git a/SunnyUI/Charts/UIBarChartOption.cs b/SunnyUI/Charts/UIBarChartOption.cs index 7a9bcedf..187501a9 100644 --- a/SunnyUI/Charts/UIBarChartOption.cs +++ b/SunnyUI/Charts/UIBarChartOption.cs @@ -51,6 +51,10 @@ namespace Sunny.UI /// public bool AutoSizeBars { get; set; } + public bool AutoSizeBarsCompact { get; set; } + + public float AutoSizeBarsCompactValue { get; set; } = 1.0f; + public void AddSeries(UIBarSeries series) { Series.Add(series);