From e3098e6e1676ce8144b61aa92c277d25147d514b Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sun, 14 Jul 2024 15:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96`Axis`=E7=9A=84`Time`?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=BD=B4=E7=9A=84=E6=BB=9A=E5=8A=A8=E8=A1=A8?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation~/zh/changelog.md | 1 + Runtime/Component/Axis/AxisHandler.cs | 6 ------ Runtime/Utilities/DateTimeUtil.cs | 13 +++++++++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index e3551017..c00da3c6 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -75,6 +75,7 @@ slug: /changelog ## master +* (2024.07.14) 优化`Axis`的`Time`时间轴的滚动表现 * (2024.07.12) 优化`Label`显示体验 * (2024.07.06) 修复`Chart`在动态创建时背景没有自适应的问题 (#323) diff --git a/Runtime/Component/Axis/AxisHandler.cs b/Runtime/Component/Axis/AxisHandler.cs index cb6dac93..5fe982b9 100644 --- a/Runtime/Component/Axis/AxisHandler.cs +++ b/Runtime/Component/Axis/AxisHandler.cs @@ -720,12 +720,6 @@ namespace XCharts if (AxisHelper.NeedShowSplit(axis)) { var size = AxisHelper.GetScaleNumber(axis, axisLength, dataZoom); - if (axis.IsTime()) - { - size += 1; - if (!ChartHelper.IsEquals(axis.GetLastLabelValue(), axis.context.maxValue)) - size += 1; - } var tickWidth = axis.axisTick.GetWidth(theme.tickWidth); var tickColor = axis.axisTick.GetColor(theme.tickColor); var current = orient == Orient.Horizonal ? startX : startY; diff --git a/Runtime/Utilities/DateTimeUtil.cs b/Runtime/Utilities/DateTimeUtil.cs index 82e5b441..3f090be2 100644 --- a/Runtime/Utilities/DateTimeUtil.cs +++ b/Runtime/Utilities/DateTimeUtil.cs @@ -49,6 +49,11 @@ namespace XCharts.Runtime } } + public static DateTime GetDateTime(double timestamp) + { + return k_DateTime1970.AddSeconds(timestamp); + } + public static DateTime GetDateTime(int timestamp) { return k_DateTime1970.AddSeconds(timestamp); @@ -97,6 +102,8 @@ namespace XCharts.Runtime /// internal static float UpdateTimeAxisDateTimeList(List list, int minTimestamp, int maxTimestamp, int splitNumber) { + var firstValue = list.Count > 0 ? list[0] : 0; + var secondValue = list.Count > 1 ? list[1] : 0; list.Clear(); var range = maxTimestamp - minTimestamp; if (range <= 0) return 0; @@ -107,7 +114,8 @@ namespace XCharts.Runtime if (range >= ONE_YEAR * MIN_TIME_SPLIT_NUMBER) { var num = Math.Max(range / (splitNumber * ONE_YEAR), 1); - var dtStart = new DateTime(dtMin.Year + 1, 1, 1); + var dtStart = (firstValue == 0 || secondValue == 0) ? new DateTime(dtMin.Year + 1, 1, 1) : + (minTimestamp > firstValue ? DateTimeUtil.GetDateTime(secondValue) : DateTimeUtil.GetDateTime(firstValue)); tick = num * 365 * 24 * 3600; while (dtStart.Ticks < dtMax.Ticks) { @@ -118,7 +126,8 @@ namespace XCharts.Runtime else if (range >= ONE_MONTH * MIN_TIME_SPLIT_NUMBER) { var num = Math.Max(range / (splitNumber * ONE_MONTH), 1); - var dtStart = new DateTime(dtMin.Year, dtMin.Month, 1).AddMonths(1); + var dtStart = (firstValue == 0 || secondValue == 0) ? (new DateTime(dtMin.Year, dtMin.Month, 1).AddMonths(1)) : + (minTimestamp > firstValue ? DateTimeUtil.GetDateTime(secondValue) : DateTimeUtil.GetDateTime(firstValue)); tick = num * 30 * 24 * 3600; while (dtStart.Ticks < dtMax.Ticks) {