From db0eb984cbf24490b871ef23d69c99dcd8ad1510 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 30 Jul 2022 11:03:49 +0800 Subject: [PATCH] =?UTF-8?q?*=20UILineChart:=20=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E7=9A=84=E5=B0=8F=E6=95=B0=E4=BD=8D=E6=95=B0?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=B0=83=E6=95=B4=E8=87=B3=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=8F=E5=88=97Series.XAxisDecimalPlaces=EF=BC=8CYAxisDecima?= =?UTF-8?q?lPlaces=20*=20UILineChart:=20=20=E6=95=B0=E6=8D=AE=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=9A=84=E6=97=A5=E6=9C=9F=E6=A0=BC=E5=BC=8F=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.XAxisDateTimeFormat=20*=20UILineChart:=20=20?= =?UTF-8?q?=E5=9D=90=E6=A0=87=E8=BD=B4=E7=9A=84=E5=B0=8F=E6=95=B0=E4=BD=8D?= =?UTF-8?q?=E6=95=B0=E9=87=8D=E6=9E=84=E8=B0=83=E6=95=B4=E8=87=B3AxisLabel?= =?UTF-8?q?.DecimalPlaces=20*=20UILineChart:=20=20=E5=9D=90=E6=A0=87?= =?UTF-8?q?=E8=BD=B4=E7=9A=84=E6=97=A5=E6=9C=9F=E6=A0=BC=E5=BC=8F=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E8=B0=83=E6=95=B4=E8=87=B3AxisLabel.DateTimeFormat=20?= =?UTF-8?q?*=20UIBarChart:=20=E6=95=B0=E6=8D=AE=E6=98=BE=E7=A4=BA=E7=9A=84?= =?UTF-8?q?=E5=B0=8F=E6=95=B0=E4=BD=8D=E6=95=B0=E9=87=8D=E6=9E=84=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E8=87=B3=E6=95=B0=E6=8D=AE=E5=BA=8F=E5=88=97Series.De?= =?UTF-8?q?cimalPlaces=20*=20UIBarChart:=20=E5=9D=90=E6=A0=87=E8=BD=B4?= =?UTF-8?q?=E7=9A=84=E5=B0=8F=E6=95=B0=E4=BD=8D=E6=95=B0=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=87=B3AxisLabel.DecimalPlaces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Charts/UIBarChart.cs | 6 ++-- SunnyUI/Charts/UILineChart.cs | 44 ++++++++++++++++++++++------- SunnyUI/Charts/UILineChartOption.cs | 7 +++++ SunnyUI/Charts/UIOption.cs | 43 ++++++++++++++++++++++------ 4 files changed, 78 insertions(+), 22 deletions(-) diff --git a/SunnyUI/Charts/UIBarChart.cs b/SunnyUI/Charts/UIBarChart.cs index b8ed0f9c..7e0f352b 100644 --- a/SunnyUI/Charts/UIBarChart.cs +++ b/SunnyUI/Charts/UIBarChart.cs @@ -23,6 +23,7 @@ * 2022-03-08: V3.1.1 增加X轴文字倾斜 * 2022-05-27: V3.1.9 重写Y轴坐标显示 * 2022-07-29: V3.2.2 数据显示的小数位数重构调整至数据序列Series.DecimalPlaces + * 2022-07-30: V3.2.2 坐标轴的小数位数重构调整至AxisLabel.DecimalPlaces ******************************************************************************/ using System; @@ -479,10 +480,7 @@ namespace Sunny.UI if (Option.YAxis.AxisLabel.Show) { - if (Option.YAxis.AxisLabel.AutoFormat) - Option.YAxis.AxisLabel.DecimalCount = YAxisDecimalCount; - - string label = YLabels[i].ToString("F" + Option.YAxis.AxisLabel.DecimalCount); + string label = YLabels[i].ToString(Option.YAxis.AxisLabel.DecimalPlaces >= 0 ? "F" + Option.YAxis.AxisLabel.DecimalPlaces : YScale.Format); SizeF sf = g.MeasureString(label, TempFont); g.DrawString(label, TempFont, ForeColor, DrawOrigin.X - Option.YAxis.AxisTick.Length - sf.Width, labels[i] - sf.Height / 2.0f); } diff --git a/SunnyUI/Charts/UILineChart.cs b/SunnyUI/Charts/UILineChart.cs index f746a426..cefdef0e 100644 --- a/SunnyUI/Charts/UILineChart.cs +++ b/SunnyUI/Charts/UILineChart.cs @@ -34,7 +34,10 @@ * 2022-04-19: V3.1.5 关闭Smooth绘制,数值差距大或者持续缩放会出错 * 2022-07-11: V3.2.1 修改两个点时可以不显示连接线 * 2022-07-26: V3.2.2 修复双Y轴数据点提示文字显示 - * 2022-07-30: V3.2.2 数据显示的小数位数重构调整至数据序列Series.XAxisDecimalPlaces,XAxisDateTimeFormat,YAxisDecimalPlaces + * 2022-07-30: V3.2.2 数据显示的小数位数重构调整至数据序列Series.XAxisDecimalPlaces,YAxisDecimalPlaces + * 2022-07-30: V3.2.2 数据显示的日期格式重构调整至数据序列Series.XAxisDateTimeFormat + * 2022-07-30: V3.2.2 坐标轴的小数位数重构调整至AxisLabel.DecimalPlaces + * 2022-07-30: V3.2.2 坐标轴的日期格式重构调整至AxisLabel.DateTimeFormat ******************************************************************************/ using System; @@ -306,17 +309,17 @@ namespace Sunny.UI string label; if (Option.XAxisType == UIAxisType.DateTime) { - if (Option.XAxis.AxisLabel.AutoFormat) + if (Option.XAxis.AxisLabel.DateTimeFormat.IsNullOrEmpty()) 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) + if (Option.XAxis.AxisLabel.DecimalPlaces < 0) label = XLabels[i].ToString(XScale.Format); else - label = XLabels[i].ToString("F" + Option.XAxis.AxisLabel.DecimalCount); + label = XLabels[i].ToString("F" + Option.XAxis.AxisLabel.DecimalPlaces); } if (Option.XAxis.HaveCustomLabels && Option.XAxis.CustomLabels.GetLabel(i).IsValid()) @@ -369,7 +372,12 @@ namespace Sunny.UI float y = labels[i]; if (y < Option.Grid.Top || y > Height - Option.Grid.Bottom) continue; - string label = YLabels[i].ToString(YScale.Format); + string label; + if (Option.YAxis.AxisLabel.DecimalPlaces < 0) + label = YLabels[i].ToString(YScale.Format); + else + label = YLabels[i].ToString("F" + Option.YAxis.AxisLabel.DecimalPlaces); + SizeF sf = g.MeasureString(label, TempFont); widthMax = Math.Max(widthMax, sf.Width); @@ -416,7 +424,12 @@ namespace Sunny.UI if (Option.Y2Axis.AxisLabel.Show) { - string label = Y2Labels[i].ToString(Y2Scale.Format); + string label; + if (Option.Y2Axis.AxisLabel.DecimalPlaces < 0) + label = Y2Labels[i].ToString(Y2Scale.Format); + else + label = Y2Labels[i].ToString("F" + Option.Y2Axis.AxisLabel.DecimalPlaces); + 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); @@ -823,10 +836,21 @@ namespace Sunny.UI sb.Append('\n'); sb.Append(Option.XAxis.Name + ": "); - if (Option.XAxisType == UIAxisType.DateTime) - sb.Append(new DateTimeInt64(point.X).ToString(point.Series.XAxisDateTimeFormat.IsValid() ? point.Series.XAxisDateTimeFormat : XScale.Format)); - else - sb.Append(point.X.ToString(point.Series.XAxisDecimalPlaces >= 0 ? "F" + point.Series.XAxisDecimalPlaces : XScale.Format)); + string customlabel = ""; + if (Option.XAxis.HaveCustomLabels) + { + int ci = (int)point.X; + customlabel = Option.XAxis.CustomLabels.GetLabel(ci); + sb.Append(customlabel); + } + + if (customlabel.IsNullOrEmpty()) + { + if (Option.XAxisType == UIAxisType.DateTime) + sb.Append(new DateTimeInt64(point.X).ToString(point.Series.XAxisDateTimeFormat.IsValid() ? point.Series.XAxisDateTimeFormat : XScale.Format)); + else + sb.Append(point.X.ToString(point.Series.XAxisDecimalPlaces >= 0 ? "F" + point.Series.XAxisDecimalPlaces : XScale.Format)); + } sb.Append('\n'); diff --git a/SunnyUI/Charts/UILineChartOption.cs b/SunnyUI/Charts/UILineChartOption.cs index 6801e3fb..18f166db 100644 --- a/SunnyUI/Charts/UILineChartOption.cs +++ b/SunnyUI/Charts/UILineChartOption.cs @@ -331,6 +331,13 @@ namespace Sunny.UI YAxisDecimalPlaces = yAxisDecimalPlaces; } + public void ClearValueFormat() + { + _dateTimeFormat = ""; + _xAxisDecimalPlaces = -1; + _yAxisDecimalPlaces = -1; + } + private int _xAxisDecimalPlaces = -1; public int XAxisDecimalPlaces { diff --git a/SunnyUI/Charts/UIOption.cs b/SunnyUI/Charts/UIOption.cs index dba999a2..85570eff 100644 --- a/SunnyUI/Charts/UIOption.cs +++ b/SunnyUI/Charts/UIOption.cs @@ -221,7 +221,7 @@ namespace Sunny.UI public string GetLabel(int i) { - if (i < Labels.Count) return Labels[i]; + if (i >= 0 && i < Labels.Count) return Labels[i]; else return string.Empty; } } @@ -235,17 +235,44 @@ namespace Sunny.UI public int Angle { get; set; } = 0; - /// - /// 小数位个数,Formatter不为空时以Formatter为准 - /// - public int DecimalCount { get; set; } = 0; + private int decimalPlaces = -1; /// - /// 日期格式化字符串,Formatter不为空时以Formatter为准 + /// 小数位个数 /// - public string DateTimeFormat { get; set; } = "HH:mm"; + public int DecimalPlaces + { + get => decimalPlaces; + set => decimalPlaces = Math.Max(0, value); + } - public bool AutoFormat { get; set; } = true; + private string dateTimeFormat = ""; + + /// + /// 日期格式化字符串 + /// + public string DateTimeFormat + { + get => dateTimeFormat; + set + { + try + { + DateTime.Now.ToString(value); + dateTimeFormat = value; + } + catch + { + dateTimeFormat = ""; + } + } + } + + public void ClearFormat() + { + dateTimeFormat = ""; + decimalPlaces = -1; + } } public class UIAxisTick