From 1b72bdf0ab9ffd91d5851f72847ba8d357335ea5 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Fri, 21 May 2021 07:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`Serie`=E5=92=8C`Axis`?= =?UTF-8?q?=E7=9A=84`insertDataToHead`=E5=8F=82=E6=95=B0=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8F=92=E5=85=A5=E5=A4=B4=E9=83=A8=E8=BF=98?= =?UTF-8?q?=E6=98=AF=E5=B0=BE=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG-EN.md | 4 +- CHANGELOG.md | 4 +- Documentation/XCharts配置项手册.md | 2 + Documentation/xcharts-configuration-EN.md | 2 + Editor/PropertyDrawers/AxisDrawer.cs | 2 + Editor/PropertyDrawers/SerieDrawer.cs | 1 + Examples/Runtime/Example_Dynamic.cs | 4 ++ Runtime/API/BaseChart_API.cs | 2 +- Runtime/Component/Main/Axis.cs | 29 ++++++--- Runtime/Component/Main/DataZoom.cs | 12 ++-- Runtime/Component/Main/Serie.cs | 33 +++++++--- Runtime/Component/Main/Tooltip.cs | 12 ++-- Runtime/Helper/AxisHelper.cs | 79 +++++++++++++++++------ Runtime/Internal/BaseGraph.cs | 12 ++-- Runtime/Internal/CoordinateChart.cs | 22 ++++--- 15 files changed, 153 insertions(+), 67 deletions(-) diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md index 53bdea47..26ab9394 100644 --- a/CHANGELOG-EN.md +++ b/CHANGELOG-EN.md @@ -1,7 +1,7 @@ # 更新日志 -[Latest](#master) +[master](#master) [v2.1.1](#v2.1.1) [v2.1.0](#v2.1.0) [v2.0.1](#v2.0.1) @@ -34,6 +34,8 @@ ## master +* (2021.05.20) Add the `insertDataHead` parameter to `Serie` and `Axis` to control whether data is inserted into the head or tail +* (2021.05.18) Optimize chart creation under 'Editor' #147 * (2021.05.16) Pull out the `Ganttchart` chart and provide it as an extension module * (2021.05.11) Added support for `VisualMap` to set color by `Piecewise` * (2021.05.09) Fixed an issue where `RingChart` could not set the background color of the ring #141 diff --git a/CHANGELOG.md b/CHANGELOG.md index de13c2cb..a15b3bf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # 更新日志 -[Latest](#master) +[master](#master) [v2.1.1](#v2.1.1) [v2.1.0](#v2.1.0) [v2.0.1](#v2.0.1) @@ -34,6 +34,8 @@ ## master +* (2021.05.20) 增加`Serie`和`Axis`的`insertDataToHead`参数控制数据插入头部还是尾部 +* (2021.05.18) 优化`Editor`下的图表创建 #147 * (2021.05.16) 抽离`GanttChart`甘特图,通过扩展模块的方式来提供 * (2021.05.11) 增加`VisualMap`对`Piecewise`分段设置颜色的支持 * (2021.05.09) 修复`RingChart`无法设置环形的背景色的问题 #141 diff --git a/Documentation/XCharts配置项手册.md b/Documentation/XCharts配置项手册.md index 3e9a013c..2b4163c8 100644 --- a/Documentation/XCharts配置项手册.md +++ b/Documentation/XCharts配置项手册.md @@ -397,6 +397,7 @@ * `boundaryGap`:坐标轴两边是否留白。默认为 `true`。 * `maxCache`:类目数据中可缓存的最大数据量。默认为`0`没有限制,大于0时超过指定值会移除旧数据再插入新数据。 * `inverse`:是否反向坐标轴。只在数值轴`Value`中有效。 +* `insertDataToHead`:添加新数据时是在列表的头部还是尾部加入。 * `data`:类目数据,在类目轴(`type: 'Category'`)中有效。 * `axisLine`:坐标轴轴线相关配置 [AxisLine](#AxisLine)。 * `axisName`:坐标轴名称相关配置 [AxisName](#AxisName)。 @@ -531,6 +532,7 @@ * `emphasis`:高亮样式 [Emphasis](#Emphasis)。 * `animation`:起始动画 [SerieAnimation](#SerieAnimation)。 * `lineArrow`:折线图的箭头 [LineArrow](#LineArrow)。 +* `insertDataToHead`:添加新数据时是在列表的头部还是尾部加入。 * `data`:系列中的数据项 [SerieData](#SerieData) 数组,可以设置`1`到`n`维数据。 ## `Serie-Line` diff --git a/Documentation/xcharts-configuration-EN.md b/Documentation/xcharts-configuration-EN.md index 0fa93b6a..3167643e 100644 --- a/Documentation/xcharts-configuration-EN.md +++ b/Documentation/xcharts-configuration-EN.md @@ -395,6 +395,7 @@ The x axis in cartesian(rectangular) coordinate. a grid component can place at m * `boundaryGap`: The boundary gap on both sides of a coordinate axis. * `maxCache`: The max number of axis data cache. The first data will be remove when the size of axis data is larger then `maxCache`. * `inverse`: Whether the axis are reversed or not. Invalid in `Category` axis. +* `insertDataToHead`: Whether to add new data at the head or at the end of the list. * `data`: Category data, valid in the `Category` axis. * `axisLine`: the style of axis line [AxisLine](#AxisLine). * `axisName`: the style of axis name [AxisName](#AxisName). @@ -472,6 +473,7 @@ Line chart serie. * `emphasis`: 高亮样式 [Emphasis](#Emphasis)。 * `animation`: 起始动画 [SerieAnimation](#SerieAnimation)。 * `lineArrow`: 折线图的箭头 [LineArrow](#LineArrow)。 +* `insertDataToHead`: Whether to add new data at the head or at the end of the list. * `data`: 系列中的数据项 [SerieData](#SerieData) 数组,可以设置`1`到`n`维数据。 ## `Serie-Bar` diff --git a/Editor/PropertyDrawers/AxisDrawer.cs b/Editor/PropertyDrawers/AxisDrawer.cs index a9f29c5c..1fe39ccb 100644 --- a/Editor/PropertyDrawers/AxisDrawer.cs +++ b/Editor/PropertyDrawers/AxisDrawer.cs @@ -68,6 +68,8 @@ namespace XCharts PropertyField(prop, "m_SplitNumber"); if (type == Axis.AxisType.Category) { + PropertyField(prop, "m_InsertDataToHead"); + PropertyField(prop, "m_MaxCache"); PropertyField(prop, "m_BoundaryGap"); } else diff --git a/Editor/PropertyDrawers/SerieDrawer.cs b/Editor/PropertyDrawers/SerieDrawer.cs index aebf1a3a..7bf862ba 100644 --- a/Editor/PropertyDrawers/SerieDrawer.cs +++ b/Editor/PropertyDrawers/SerieDrawer.cs @@ -44,6 +44,7 @@ namespace XCharts m_Heights[m_KeyName] += hig; PropertyField(prop, "m_Name"); + PropertyField(prop, "m_InsertDataToHead"); switch (serieType) { case SerieType.Line: diff --git a/Examples/Runtime/Example_Dynamic.cs b/Examples/Runtime/Example_Dynamic.cs index 62c489a3..d96a0c55 100644 --- a/Examples/Runtime/Example_Dynamic.cs +++ b/Examples/Runtime/Example_Dynamic.cs @@ -17,6 +17,7 @@ namespace XCharts.Examples { public int maxCacheDataNumber = 100; public float initDataTime = 2; + public bool insertDataToHead = false; private CoordinateChart chart; private float updateTime; @@ -36,6 +37,9 @@ namespace XCharts.Examples chart.xAxes[0].maxCache = maxCacheDataNumber; timeNow = DateTime.Now; timeNow = timeNow.AddSeconds(-maxCacheDataNumber); + + serie.insertDataToHead = insertDataToHead; + chart.xAxes[0].insertDataToHead = insertDataToHead; } void Update() diff --git a/Runtime/API/BaseChart_API.cs b/Runtime/API/BaseChart_API.cs index d8ee434d..ad220a53 100644 --- a/Runtime/API/BaseChart_API.cs +++ b/Runtime/API/BaseChart_API.cs @@ -198,7 +198,7 @@ namespace XCharts /// the added serie public virtual Serie AddSerie(SerieType type, string serieName = null, bool show = true, bool addToHead = false) { - return m_Series.AddSerie(type, serieName, addToHead); + return m_Series.AddSerie(type, serieName, show, addToHead); } /// diff --git a/Runtime/Component/Main/Axis.cs b/Runtime/Component/Main/Axis.cs index cf500b05..cee24c64 100644 --- a/Runtime/Component/Main/Axis.cs +++ b/Runtime/Component/Main/Axis.cs @@ -1,9 +1,9 @@ -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.com/monitor1394 */ -/* */ -/************************************************/ +/******************************************/ +/* */ +/* Copyright (c) 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ using System; using System.Collections.Generic; @@ -99,6 +99,7 @@ namespace XCharts [SerializeField] protected int m_CeilRate = 0; [SerializeField] protected bool m_Inverse = false; [SerializeField] private bool m_Clockwise = true; + [SerializeField] private bool m_InsertDataToHead; [SerializeField] protected List m_Data = new List(); [SerializeField] protected AxisLine m_AxisLine = AxisLine.defaultAxisLine; [SerializeField] protected AxisName m_AxisName = AxisName.defaultAxisName; @@ -340,6 +341,15 @@ namespace XCharts get { return m_SplitArea; } set { if (value != null) { m_SplitArea = value; SetVerticesDirty(); } } } + /// + /// Whether to add new data at the head or at the end of the list. + /// 添加新数据时是在列表的头部还是尾部加入。 + /// + public bool insertDataToHead + { + get { return m_InsertDataToHead; } + set { if (PropertyUtil.SetStruct(ref m_InsertDataToHead, value)) SetAllDirty(); } + } public override bool vertsDirty { get { return m_VertsDirty || axisLine.anyDirty || axisTick.anyDirty || splitLine.anyDirty || splitArea.anyDirty; } @@ -534,10 +544,11 @@ namespace XCharts while (m_Data.Count > maxCache) { m_NeedUpdateFilterData = true; - m_Data.RemoveAt(0); + m_Data.RemoveAt(m_InsertDataToHead ? m_Data.Count - 1 : 0); } } - m_Data.Add(category); + if (m_InsertDataToHead) m_Data.Insert(0, category); + else m_Data.Add(category); SetAllDirty(); } @@ -682,7 +693,7 @@ namespace XCharts internal void SetTooltipLabelActive(bool flag) { - if(m_TooltipLabel == null) return; + if (m_TooltipLabel == null) return; ChartHelper.SetActive(m_TooltipLabel, flag); } diff --git a/Runtime/Component/Main/DataZoom.cs b/Runtime/Component/Main/DataZoom.cs index af6cb9bf..6aeb8c37 100644 --- a/Runtime/Component/Main/DataZoom.cs +++ b/Runtime/Component/Main/DataZoom.cs @@ -1,9 +1,9 @@ -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.com/monitor1394 */ -/* */ -/************************************************/ +/******************************************/ +/* */ +/* Copyright (c) 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ using System.Collections.Generic; using UnityEngine; diff --git a/Runtime/Component/Main/Serie.cs b/Runtime/Component/Main/Serie.cs index af2bfd43..639afc80 100644 --- a/Runtime/Component/Main/Serie.cs +++ b/Runtime/Component/Main/Serie.cs @@ -307,6 +307,7 @@ namespace XCharts [SerializeField] private float m_Right; [SerializeField] private float m_Top; [SerializeField] private float m_Bottom; + [SerializeField] private bool m_InsertDataToHead; [SerializeField] private bool m_CustomBool1; [SerializeField] private bool m_CustomBool2; [SerializeField] private int m_CustomInt1; @@ -965,6 +966,15 @@ namespace XCharts set { if (PropertyUtil.SetStruct(ref m_CustomFloat2, value)) SetAllDirty(); } } /// + /// Whether to add new data at the head or at the end of the list. + /// 添加新数据时是在列表的头部还是尾部加入。 + /// + public bool insertDataToHead + { + get { return m_InsertDataToHead; } + set { if (PropertyUtil.SetStruct(ref m_InsertDataToHead, value)) SetAllDirty(); } + } + /// /// 系列中的数据内容数组。SerieData可以设置1到n维数据。 /// public List data { get { return m_Data; } } @@ -1295,13 +1305,19 @@ namespace XCharts serieData.data.Add(value); serieData.name = dataName; serieData.index = xValue; - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); m_ShowDataDimension = 1; SetVerticesDirty(); CheckDataName(dataName); return serieData; } + private void AddSerieDataHeadOrTail(SerieData serieData) + { + if (m_InsertDataToHead) m_Data.Insert(0, serieData); + else m_Data.Add(serieData); + } + private void CheckDataName(string dataName) { if (string.IsNullOrEmpty(dataName)) @@ -1330,7 +1346,7 @@ namespace XCharts serieData.data.Add(yValue); serieData.name = dataName; serieData.index = m_Data.Count; - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); m_ShowDataDimension = 2; SetVerticesDirty(); CheckDataName(dataName); @@ -1357,7 +1373,7 @@ namespace XCharts serieData.data.Add(heighest); serieData.name = dataName; serieData.index = m_Data.Count; - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); m_ShowDataDimension = 4; SetVerticesDirty(); CheckDataName(dataName); @@ -1393,7 +1409,7 @@ namespace XCharts { serieData.data.Add(valueList[i]); } - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); SetVerticesDirty(); CheckDataName(dataName); return serieData; @@ -1407,7 +1423,8 @@ namespace XCharts while (m_Data.Count > m_MaxCache) { m_NeedUpdateFilterData = true; - RemoveData(0); + if (m_InsertDataToHead) RemoveData(m_Data.Count - 1); + else RemoveData(0); } } @@ -1854,7 +1871,7 @@ namespace XCharts } else serieData.name = txt.Replace("\"", "").Trim(); } - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); } } else if (temp.IndexOf("value") > -1 && temp.IndexOf("name") > -1) @@ -1882,7 +1899,7 @@ namespace XCharts serieData.selected = bool.Parse(selected); } } - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); } } else @@ -1896,7 +1913,7 @@ namespace XCharts { var serieData = new SerieData(); serieData.data = new List() { i, value }; - m_Data.Add(serieData); + AddSerieDataHeadOrTail(serieData); } } } diff --git a/Runtime/Component/Main/Tooltip.cs b/Runtime/Component/Main/Tooltip.cs index eae60e7d..e32aece8 100644 --- a/Runtime/Component/Main/Tooltip.cs +++ b/Runtime/Component/Main/Tooltip.cs @@ -1,11 +1,11 @@ using System.Linq; using System.Collections.ObjectModel; -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.com/monitor1394 */ -/* */ -/************************************************/ +/******************************************/ +/* */ +/* Copyright (c) 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ using System.Collections.Generic; using UnityEngine; diff --git a/Runtime/Helper/AxisHelper.cs b/Runtime/Helper/AxisHelper.cs index 0ac7e99d..aca9b656 100644 --- a/Runtime/Helper/AxisHelper.cs +++ b/Runtime/Helper/AxisHelper.cs @@ -1,9 +1,9 @@ -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.com/monitor1394 */ -/* */ -/************************************************/ +/******************************************/ +/* */ +/* Copyright (c) 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ using UnityEngine; namespace XCharts @@ -77,6 +77,10 @@ namespace XCharts else if (axis.type == Axis.AxisType.Category) { int dataCount = axis.GetDataList(dataZoom).Count; + if (!axis.boundaryGap) + { + dataCount -= 1; + } if (axis.splitNumber <= 0 || axis.splitNumber > dataCount) return dataCount; if (dataCount >= axis.splitNumber * 2) return axis.splitNumber; else return dataCount; @@ -170,15 +174,33 @@ namespace XCharts if (dataCount <= 0) return ""; int rate = axis.boundaryGap ? (dataCount / split) : (dataCount - 1) / split; if (rate == 0) rate = 1; - int newIndex = index * rate; - if (newIndex <= dataCount - 1) + if (axis.insertDataToHead) { - return axis.axisLabel.GetFormatterContent(showData[newIndex]); + if (index > 0) + { + var residue = (dataCount - 1) - split * rate; + var newIndex = residue + (index - 1) * rate; + if (newIndex < 0) newIndex = 0; + return axis.axisLabel.GetFormatterContent(showData[newIndex]); + } + else + { + if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty; + else return axis.axisLabel.GetFormatterContent(showData[0]); + } } else { - if (axis.boundaryGap && coordinateWidth / dataCount > 10) return string.Empty; - else return axis.axisLabel.GetFormatterContent(showData[dataCount - 1]); + int newIndex = index * rate; + if (newIndex < dataCount) + { + return axis.axisLabel.GetFormatterContent(showData[newIndex]); + } + else + { + if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty; + else return axis.axisLabel.GetFormatterContent(showData[dataCount - 1]); + } } } @@ -195,14 +217,15 @@ namespace XCharts { var data = axis.GetDataList(dataZoom); var scaleNum = 0; + if (axis.boundaryGap) { - scaleNum = data.Count % splitNum == 0 ? splitNum : splitNum + 1; + scaleNum = data.Count % splitNum == 0 ? splitNum + 1 : splitNum + 2; } else { - if (data.Count - 1 < splitNum) scaleNum = splitNum; - else scaleNum = (data.Count - 1) % splitNum == 0 ? splitNum + 1 : splitNum + 1; + if (data.Count < splitNum) scaleNum = splitNum; + else scaleNum = data.Count % splitNum == 0 ? splitNum : splitNum + 1; } return scaleNum; } @@ -245,15 +268,33 @@ namespace XCharts int tick = count / splitNum; if (count <= 0) return 0; var each = coordinateWidth / count; - if (index >= num) + if (axis.insertDataToHead) { - if (axis.axisTick.alignWithLabel) return each * tick; - else return coordinateWidth - each * tick * (index - 1); + var max = axis.boundaryGap ? splitNum : splitNum - 1; + if (index == 1) + { + if (axis.axisTick.alignWithLabel) return each * tick; + else return coordinateWidth - each * tick * max; + } + else + { + if (count < splitNum) return each; + else return each * (count / splitNum); + } } else { - if (count < splitNum) return each; - else return each * (count / splitNum); + var max = axis.boundaryGap ? num - 1 : num; + if (index >= max) + { + if (axis.axisTick.alignWithLabel) return each * tick; + else return coordinateWidth - each * tick * (index - 1); + } + else + { + if (count < splitNum) return each; + else return each * (count / splitNum); + } } } else diff --git a/Runtime/Internal/BaseGraph.cs b/Runtime/Internal/BaseGraph.cs index 0e581c52..35c08818 100644 --- a/Runtime/Internal/BaseGraph.cs +++ b/Runtime/Internal/BaseGraph.cs @@ -1,9 +1,9 @@ -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.com/monitor1394 */ -/* */ -/************************************************/ +/******************************************/ +/* */ +/* Copyright (c) 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ using UnityEngine; using UnityEngine.UI; diff --git a/Runtime/Internal/CoordinateChart.cs b/Runtime/Internal/CoordinateChart.cs index 5858e6c1..3c995bca 100644 --- a/Runtime/Internal/CoordinateChart.cs +++ b/Runtime/Internal/CoordinateChart.cs @@ -1,9 +1,9 @@ -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.com/monitor1394 */ -/* */ -/************************************************/ +/******************************************/ +/* */ +/* Copyright (c) 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ using UnityEngine; using UnityEngine.UI; @@ -549,6 +549,7 @@ namespace XCharts float totalWidth = 0; float eachWidth = AxisHelper.GetEachWidth(yAxis, grid.runtimeHeight, dataZoom); float gapWidth = yAxis.boundaryGap ? eachWidth / 2 : 0; + if (yAxis.IsCategory()) splitNumber += 1; for (int i = 0; i < splitNumber; i++) { ChartText txt; @@ -666,6 +667,7 @@ namespace XCharts float eachWidth = AxisHelper.GetEachWidth(xAxis, grid.runtimeWidth, dataZoom); float gapWidth = xAxis.boundaryGap ? eachWidth / 2 : 0; float textWidth = AxisHelper.GetScaleWidth(xAxis, grid.runtimeWidth, 0, dataZoom); + //if (xAxis.IsCategory() && xAxis.boundaryGap) splitNumber += 1; for (int i = 0; i < splitNumber; i++) { var labelWidth = AxisHelper.GetScaleWidth(xAxis, grid.runtimeWidth, i + 1, dataZoom); @@ -1046,7 +1048,7 @@ namespace XCharts var grid = GetAxisGridOrDefault(yAxis); var size = AxisHelper.GetScaleNumber(yAxis, grid.runtimeWidth, dataZoom); var totalWidth = grid.runtimeY; - for (int i = 0; i <= size; i++) + for (int i = 0; i < size; i++) { var scaleWidth = AxisHelper.GetScaleWidth(yAxis, grid.runtimeHeight, i + 1, dataZoom); if (i == 0 && (!yAxis.axisTick.showStartTick || yAxis.axisTick.alignWithLabel)) @@ -1054,7 +1056,7 @@ namespace XCharts totalWidth += scaleWidth; continue; } - if (i == size && !yAxis.axisTick.showEndTick) + if (i == size - 1 && !yAxis.axisTick.showEndTick) { totalWidth += scaleWidth; continue; @@ -1163,7 +1165,7 @@ namespace XCharts var size = AxisHelper.GetScaleNumber(xAxis, grid.runtimeWidth, dataZoom); var totalWidth = grid.runtimeX; var yAxis = m_YAxes[xAxisIndex]; - for (int i = 0; i <= size; i++) + for (int i = 0; i < size; i++) { var scaleWidth = AxisHelper.GetScaleWidth(xAxis, grid.runtimeWidth, i + 1, dataZoom); if (i == 0 && (!xAxis.axisTick.showStartTick || xAxis.axisTick.alignWithLabel)) @@ -1171,7 +1173,7 @@ namespace XCharts totalWidth += scaleWidth; continue; } - if (i == size && !xAxis.axisTick.showEndTick) + if (i == size - 1 && !xAxis.axisTick.showEndTick) { totalWidth += scaleWidth; continue;