From 495328c1e5e1ee25b59ded5be4d1811335c88881 Mon Sep 17 00:00:00 2001 From: Sunny Date: Fri, 29 Jul 2022 11:44:46 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIBarChart:=20=E6=95=B0=E6=8D=AE=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=9A=84=E5=B0=8F=E6=95=B0=E4=BD=8D=E6=95=B0=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E8=B0=83=E6=95=B4=E8=87=B3=E6=95=B0=E6=8D=AE=E5=BA=8F?= =?UTF-8?q?=E5=88=97Series.DecimalPlaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Charts/UIBarChart.cs | 45 +++++++++++++++++------------- SunnyUI/Charts/UIBarChartOption.cs | 12 ++++++++ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/SunnyUI/Charts/UIBarChart.cs b/SunnyUI/Charts/UIBarChart.cs index 1df45e65..b8ed0f9c 100644 --- a/SunnyUI/Charts/UIBarChart.cs +++ b/SunnyUI/Charts/UIBarChart.cs @@ -22,6 +22,7 @@ * 2021-01-01: V3.0.9 增加柱子上显示数值 * 2022-03-08: V3.1.1 增加X轴文字倾斜 * 2022-05-27: V3.1.9 重写Y轴坐标显示 + * 2022-07-29: V3.2.2 数据显示的小数位数重构调整至数据序列Series.DecimalPlaces ******************************************************************************/ using System; @@ -194,7 +195,8 @@ namespace Sunny.UI Rect = new RectangleF(xx, VPos, ww, (YZeroPos - VPos)), Value = series.Data[j], Color = color, - Top = true + Top = true, + Series = series, }); } else @@ -204,7 +206,8 @@ namespace Sunny.UI Rect = new RectangleF(xx, YZeroPos, ww, (VPos - YZeroPos)), Value = series.Data[j], Color = color, - Top = false + Top = false, + Series = series, }); } @@ -220,7 +223,7 @@ namespace Sunny.UI foreach (var series in Option.Series) { str += '\n'; - //str += series.Name + " : " + series.Data[i].ToString(Option.ToolTip.ValueFormat); + str += series.Name + " : " + series.Data[i].ToString("F" + series.DecimalPlaces); } Bars[0][i].Tips = str; @@ -527,23 +530,23 @@ namespace Sunny.UI if (Option.ShowValue) { - //string value = info.Value.ToString(Option.ToolTip.ValueFormat); - //SizeF sf = g.MeasureString(value, TempFont); - //if (info.Top) - //{ - // float top = info.Rect.Top - sf.Height; - // if (top > Option.Grid.Top) - // { - // g.DrawString(value, TempFont, info.Color, info.Rect.Center().X - sf.Width / 2, top); - // } - //} - //else - //{ - // if (info.Rect.Bottom + sf.Height + Option.Grid.Bottom < Height) - // { - // g.DrawString(value, TempFont, info.Color, info.Rect.Center().X - sf.Width / 2, info.Rect.Bottom); - // } - //} + string value = info.Value.ToString("F" + info.Series.DecimalPlaces); + SizeF sf = g.MeasureString(value, TempFont); + if (info.Top) + { + float top = info.Rect.Top - sf.Height; + if (top > Option.Grid.Top) + { + g.DrawString(value, TempFont, info.Color, info.Rect.Center().X - sf.Width / 2, top); + } + } + else + { + if (info.Rect.Bottom + sf.Height + Option.Grid.Bottom < Height) + { + g.DrawString(value, TempFont, info.Color, info.Rect.Center().X - sf.Width / 2, info.Rect.Bottom); + } + } } } } @@ -568,6 +571,8 @@ namespace Sunny.UI public double Value { get; set; } public bool Top { get; set; } + + public UIBarSeries Series { get; set; } } } } \ No newline at end of file diff --git a/SunnyUI/Charts/UIBarChartOption.cs b/SunnyUI/Charts/UIBarChartOption.cs index d5c95646..0021c07d 100644 --- a/SunnyUI/Charts/UIBarChartOption.cs +++ b/SunnyUI/Charts/UIBarChartOption.cs @@ -411,6 +411,11 @@ namespace Sunny.UI public class UIBarSeries : IDisposable { + public UIBarSeries(int decimalPlaces = 0) + { + DecimalPlaces = decimalPlaces; + } + public string Name { get; set; } public int MaxWidth { get; set; } = int.MaxValue; @@ -423,6 +428,13 @@ namespace Sunny.UI public readonly List Colors = new List(); + private int _decimalPlaces = 0; + public int DecimalPlaces + { + get => _decimalPlaces; + set => _decimalPlaces = Math.Max(0, value); + } + public bool ShowBarName { get; set; } public bool ShowValue { get; set; }