diff --git a/Assets/XCharts/Runtime/Component/Main/Axis.cs b/Assets/XCharts/Runtime/Component/Main/Axis.cs index 5912ec5b..fcdd6167 100644 --- a/Assets/XCharts/Runtime/Component/Main/Axis.cs +++ b/Assets/XCharts/Runtime/Component/Main/Axis.cs @@ -554,6 +554,14 @@ public bool IsLog() return type == AxisType.Log; } + /// + /// 是否为时间轴。 + /// + public bool IsTime() + { + return type == AxisType.Time; + } + /// /// 添加一个类目到类目数据列表 /// diff --git a/Assets/XCharts/Runtime/Component/Main/DataZoom.cs b/Assets/XCharts/Runtime/Component/Main/DataZoom.cs index 70fc8ff5..1c11bbe5 100644 --- a/Assets/XCharts/Runtime/Component/Main/DataZoom.cs +++ b/Assets/XCharts/Runtime/Component/Main/DataZoom.cs @@ -9,6 +9,7 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; +using XUGL; namespace XCharts { @@ -683,10 +684,18 @@ public void Update() } } - public void Draw(VertexHelper vh) + public void DrawBase(VertexHelper vh) { } + public void DrawTop(VertexHelper vh) + { + foreach (var dataZoom in chart.dataZooms) + { + DrawDataZoomSlider(vh, dataZoom); + } + } + public void OnBeginDrag(PointerEventData eventData) { if (chart == null) return; @@ -991,6 +1000,12 @@ private void CheckDataZoomLabel(DataZoom dataZoom) dataZoom.SetStartLabelText(xAxis.data[startIndex]); dataZoom.SetEndLabelText(xAxis.data[endIndex]); } + else if (xAxis.IsTime()) + { + //TODO: + dataZoom.SetStartLabelText(""); + dataZoom.SetEndLabelText(""); + } chart.InitAxisX(); } var start = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.start / 100; @@ -1000,5 +1015,87 @@ private void CheckDataZoomLabel(DataZoom dataZoom) dataZoom.UpdateEndLabelPosition(new Vector3(end + 10, chart.chartY + dataZoom.bottom + hig / 2)); } } + + private void DrawDataZoomSlider(VertexHelper vh, DataZoom dataZoom) + { + if (!dataZoom.enable || !dataZoom.supportSlider) return; + var p1 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY); + var p2 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY + dataZoom.runtimeHeight); + var p3 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY + dataZoom.runtimeHeight); + var p4 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY); + var xAxis = chart.GetXAxis(0); + var lineColor = dataZoom.lineStyle.GetColor(chart.theme.dataZoom.dataLineColor); + var lineWidth = dataZoom.lineStyle.GetWidth(chart.theme.dataZoom.dataLineWidth); + var borderWidth = dataZoom.borderWidth == 0 ? chart.theme.dataZoom.borderWidth : dataZoom.borderWidth; + var borderColor = dataZoom.GetBorderColor(chart.theme.dataZoom.borderColor); + var backgroundColor = dataZoom.GetBackgroundColor(chart.theme.dataZoom.backgroundColor); + var areaColor = dataZoom.areaStyle.GetColor(chart.theme.dataZoom.dataAreaColor); + UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, backgroundColor); + var centerPos = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth / 2, + dataZoom.runtimeY + dataZoom.runtimeHeight / 2); + UGL.DrawBorder(vh, centerPos, dataZoom.runtimeWidth, dataZoom.runtimeHeight, borderWidth, borderColor); + if (dataZoom.showDataShadow && chart.series.Count > 0) + { + Serie serie = chart.series.list[0]; + Axis axis = chart.GetYAxis(0); + var showData = serie.GetDataList(null); + float scaleWid = dataZoom.runtimeWidth / (showData.Count - 1); + Vector3 lp = Vector3.zero; + Vector3 np = Vector3.zero; + float minValue = 0; + float maxValue = 0; + SeriesHelper.GetYMinMaxValue(chart.series, null, 0, chart.IsValue(), axis.inverse, out minValue, out maxValue); + AxisHelper.AdjustMinMaxValue(axis, ref minValue, ref maxValue, true); + + int rate = 1; + var sampleDist = serie.sampleDist < 2 ? 2 : serie.sampleDist; + var maxCount = showData.Count; + if (sampleDist > 0) rate = (int)((maxCount - serie.minShow) / (dataZoom.runtimeWidth / sampleDist)); + if (rate < 1) rate = 1; + var totalAverage = serie.sampleAverage > 0 ? serie.sampleAverage : + chart.DataAverage(ref showData, serie.sampleType, serie.minShow, maxCount, rate); + var dataChanging = false; + for (int i = 0; i < maxCount; i += rate) + { + float value = chart.SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage, i, + serie.animation.GetUpdateAnimationDuration(), ref dataChanging, axis); + float pX = dataZoom.runtimeX + i * scaleWid; + float dataHig = (maxValue - minValue) == 0 ? 0 : + (value - minValue) / (maxValue - minValue) * dataZoom.runtimeHeight; + np = new Vector3(pX, chart.chartY + dataZoom.bottom + dataHig); + if (i > 0) + { + UGL.DrawLine(vh, lp, np, lineWidth, lineColor); + Vector3 alp = new Vector3(lp.x, lp.y - lineWidth); + Vector3 anp = new Vector3(np.x, np.y - lineWidth); + + Vector3 tnp = new Vector3(np.x, chart.chartY + dataZoom.bottom + lineWidth); + Vector3 tlp = new Vector3(lp.x, chart.chartY + dataZoom.bottom + lineWidth); + UGL.DrawQuadrilateral(vh, alp, anp, tnp, tlp, areaColor); + } + lp = np; + } + if (dataChanging) + { + chart.RefreshTopPainter(); + } + } + switch (dataZoom.rangeMode) + { + case DataZoom.RangeMode.Percent: + var start = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.start / 100; + var end = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.end / 100; + var fillerColor = dataZoom.GetFillerColor(chart.theme.dataZoom.fillerColor); + + p1 = new Vector2(start, dataZoom.runtimeY); + p2 = new Vector2(start, dataZoom.runtimeY + dataZoom.runtimeHeight); + p3 = new Vector2(end, dataZoom.runtimeY + dataZoom.runtimeHeight); + p4 = new Vector2(end, dataZoom.runtimeY); + UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, fillerColor); + UGL.DrawLine(vh, p1, p2, lineWidth, fillerColor); + UGL.DrawLine(vh, p3, p4, lineWidth, fillerColor); + break; + } + } } } \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Component/Main/Series.cs b/Assets/XCharts/Runtime/Component/Main/Series.cs index 70248679..6f945313 100644 --- a/Assets/XCharts/Runtime/Component/Main/Series.cs +++ b/Assets/XCharts/Runtime/Component/Main/Series.cs @@ -279,6 +279,10 @@ public Serie AddSerie(SerieType type, string serieName, bool show = true, bool a serie.animation.Restart(); if (addToHead) m_Series.Insert(0, serie); else m_Series.Add(serie); + for (int i = 0; i < m_Series.Count; i++) + { + m_Series[i].index = i; + } SetVerticesDirty(); return serie; } diff --git a/Assets/XCharts/Runtime/Component/Main/VisualMap.cs b/Assets/XCharts/Runtime/Component/Main/VisualMap.cs index 209ee3a0..952e4ed9 100644 --- a/Assets/XCharts/Runtime/Component/Main/VisualMap.cs +++ b/Assets/XCharts/Runtime/Component/Main/VisualMap.cs @@ -674,7 +674,7 @@ public void Update() } } - public void Draw(VertexHelper vh) + public void DrawBase(VertexHelper vh) { foreach (var visualMap in chart.visualMaps) { @@ -691,6 +691,10 @@ public void Draw(VertexHelper vh) } } + public void DrawTop(VertexHelper vh) + { + } + private void CheckVisualMap(VisualMap visualMap) { if (visualMap == null || !visualMap.enable || !visualMap.show) return; diff --git a/Assets/XCharts/Runtime/Internal/BaseChart.cs b/Assets/XCharts/Runtime/Internal/BaseChart.cs index c02785bd..4464d6e5 100644 --- a/Assets/XCharts/Runtime/Internal/BaseChart.cs +++ b/Assets/XCharts/Runtime/Internal/BaseChart.cs @@ -176,6 +176,10 @@ internal void RefreshBasePainter() { m_Painter.Refresh(); } + internal void RefreshTopPainter() + { + m_PainterTop.Refresh(); + } public void RefreshPainter(int index) { @@ -861,7 +865,7 @@ protected override void OnDrawPainterBase(VertexHelper vh, Painter painter) DrawBackground(vh); DrawPainterBase(vh); DrawLegend(vh); - foreach (var draw in m_ComponentHandlers) draw.Draw(vh); + foreach (var draw in m_ComponentHandlers) draw.DrawBase(vh); foreach (var draw in m_DrawSeries) draw.DrawBase(vh); if (m_OnCustomDrawBaseCallback != null) { @@ -896,6 +900,7 @@ protected virtual void OnDrawPainterTop(VertexHelper vh, Painter painter) { vh.Clear(); DrawPainterTop(vh); + foreach (var draw in m_ComponentHandlers) draw.DrawTop(vh); if (m_OnCustomDrawTopCallback != null) { m_OnCustomDrawTopCallback(vh); diff --git a/Assets/XCharts/Runtime/Internal/CoordinateChart.cs b/Assets/XCharts/Runtime/Internal/CoordinateChart.cs index 35261942..b434d5b7 100644 --- a/Assets/XCharts/Runtime/Internal/CoordinateChart.cs +++ b/Assets/XCharts/Runtime/Internal/CoordinateChart.cs @@ -78,8 +78,10 @@ private void RefreshSeriePainterByAxisIndex(Axis axis) protected override void DrawPainterBase(VertexHelper vh) { base.DrawPainterBase(vh); - DrawCoordinate(vh); - DrawDataZoomSlider(vh); + if (!SeriesHelper.IsAnyClipSerie(m_Series)) + { + DrawCoordinate(vh); + } } protected override void DrawBackground(VertexHelper vh) @@ -178,8 +180,12 @@ protected override void DrawPainterSerie(VertexHelper vh, Serie serie) protected override void DrawPainterTop(VertexHelper vh) { - DrawAxisTick(vh); DrawClip(vh); + if (SeriesHelper.IsAnyClipSerie(m_Series)) + { + DrawCoordinate(vh); + } + DrawAxisTick(vh); DrawLabelBackground(vh); if (SeriesHelper.IsStack(m_Series)) { @@ -1280,87 +1286,7 @@ private void DrawYAxisLine(VertexHelper vh, int yAxisIndex, YAxis yAxis) } } - private void DrawDataZoomSlider(VertexHelper vh) - { - if (!dataZoom.enable || !dataZoom.supportSlider) return; - var p1 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY); - var p2 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY + dataZoom.runtimeHeight); - var p3 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY + dataZoom.runtimeHeight); - var p4 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY); - var xAxis = xAxes[0]; - var lineColor = dataZoom.lineStyle.GetColor(m_Theme.dataZoom.dataLineColor); - var lineWidth = dataZoom.lineStyle.GetWidth(m_Theme.dataZoom.dataLineWidth); - var borderWidth = dataZoom.borderWidth == 0 ? m_Theme.dataZoom.borderWidth : dataZoom.borderWidth; - var borderColor = dataZoom.GetBorderColor(m_Theme.dataZoom.borderColor); - var backgroundColor = dataZoom.GetBackgroundColor(m_Theme.dataZoom.backgroundColor); - var areaColor = dataZoom.areaStyle.GetColor(m_Theme.dataZoom.dataAreaColor); - UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, backgroundColor); - var centerPos = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth / 2, - dataZoom.runtimeY + dataZoom.runtimeHeight / 2); - UGL.DrawBorder(vh, centerPos, dataZoom.runtimeWidth, dataZoom.runtimeHeight, borderWidth, borderColor); - if (dataZoom.showDataShadow && m_Series.Count > 0) - { - Serie serie = m_Series.list[0]; - Axis axis = yAxes[0]; - var showData = serie.GetDataList(null); - float scaleWid = dataZoom.runtimeWidth / (showData.Count - 1); - Vector3 lp = Vector3.zero; - Vector3 np = Vector3.zero; - float minValue = 0; - float maxValue = 0; - SeriesHelper.GetYMinMaxValue(m_Series, null, 0, IsValue(), axis.inverse, out minValue, out maxValue); - AxisHelper.AdjustMinMaxValue(axis, ref minValue, ref maxValue, true); - - int rate = 1; - var sampleDist = serie.sampleDist < 2 ? 2 : serie.sampleDist; - var maxCount = showData.Count; - if (sampleDist > 0) rate = (int)((maxCount - serie.minShow) / (dataZoom.runtimeWidth / sampleDist)); - if (rate < 1) rate = 1; - var totalAverage = serie.sampleAverage > 0 ? serie.sampleAverage : - DataAverage(ref showData, serie.sampleType, serie.minShow, maxCount, rate); - var dataChanging = false; - for (int i = 0; i < maxCount; i += rate) - { - float value = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage, i, - serie.animation.GetUpdateAnimationDuration(), ref dataChanging, axis); - float pX = dataZoom.runtimeX + i * scaleWid; - float dataHig = (maxValue - minValue) == 0 ? 0 : - (value - minValue) / (maxValue - minValue) * dataZoom.runtimeHeight; - np = new Vector3(pX, m_ChartY + dataZoom.bottom + dataHig); - if (i > 0) - { - UGL.DrawLine(vh, lp, np, lineWidth, lineColor); - Vector3 alp = new Vector3(lp.x, lp.y - lineWidth); - Vector3 anp = new Vector3(np.x, np.y - lineWidth); - - Vector3 tnp = new Vector3(np.x, m_ChartY + dataZoom.bottom + lineWidth); - Vector3 tlp = new Vector3(lp.x, m_ChartY + dataZoom.bottom + lineWidth); - UGL.DrawQuadrilateral(vh, alp, anp, tnp, tlp, areaColor); - } - lp = np; - } - if (dataChanging) - { - RefreshChart(); - } - } - switch (dataZoom.rangeMode) - { - case DataZoom.RangeMode.Percent: - var start = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.start / 100; - var end = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.end / 100; - var fillerColor = dataZoom.GetFillerColor(m_Theme.dataZoom.fillerColor); - - p1 = new Vector2(start, dataZoom.runtimeY); - p2 = new Vector2(start, dataZoom.runtimeY + dataZoom.runtimeHeight); - p3 = new Vector2(end, dataZoom.runtimeY + dataZoom.runtimeHeight); - p4 = new Vector2(end, dataZoom.runtimeY); - UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, fillerColor); - UGL.DrawLine(vh, p1, p2, lineWidth, fillerColor); - UGL.DrawLine(vh, p3, p4, lineWidth, fillerColor); - break; - } - } + protected void DrawXTooltipIndicator(VertexHelper vh) { diff --git a/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawLine.cs b/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawLine.cs index 5effc58f..c7a53532 100644 --- a/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawLine.cs +++ b/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawLine.cs @@ -333,7 +333,7 @@ private Vector3 GetLLPos(List dataPoints, int index, Vector3 lp) return lp; } - private float DataAverage(ref List showData, SampleType sampleType, int minCount, int maxCount, int rate) + internal float DataAverage(ref List showData, SampleType sampleType, int minCount, int maxCount, int rate) { var totalAverage = 0f; if (rate > 1 && sampleType == SampleType.Peak) @@ -348,7 +348,7 @@ private float DataAverage(ref List showData, SampleType sampleType, i return totalAverage; } - private float SampleValue(ref List showData, SampleType sampleType, int rate, + internal float SampleValue(ref List showData, SampleType sampleType, int rate, int minCount, int maxCount, float totalAverage, int index, float dataChangeDuration, ref bool dataChanging, Axis axis) { diff --git a/Assets/XCharts/Runtime/Internal/Interface/IComponentHandler.cs b/Assets/XCharts/Runtime/Internal/Interface/IComponentHandler.cs index 0210f3fb..b07a46f1 100644 --- a/Assets/XCharts/Runtime/Internal/Interface/IComponentHandler.cs +++ b/Assets/XCharts/Runtime/Internal/Interface/IComponentHandler.cs @@ -14,7 +14,8 @@ public interface IComponentHandler { void Init(); void Update(); - void Draw(VertexHelper vh); + void DrawBase(VertexHelper vh); + void DrawTop(VertexHelper vh); void OnDrag(PointerEventData eventData); void OnBeginDrag(PointerEventData eventData); void OnEndDrag(PointerEventData eventData); diff --git a/Assets/XCharts/Runtime/Internal/Utility/ChartHelper.cs b/Assets/XCharts/Runtime/Internal/Utility/ChartHelper.cs index e1e73104..d4774439 100644 --- a/Assets/XCharts/Runtime/Internal/Utility/ChartHelper.cs +++ b/Assets/XCharts/Runtime/Internal/Utility/ChartHelper.cs @@ -247,7 +247,7 @@ public static ChartText AddTextObject(string name, Transform parent, Vector2 anc chartText.tmpText.alignment = textStyle.tmpAlignment; chartText.tmpText.richText = true; chartText.tmpText.raycastTarget = false; - chartText.tmpText.enableWordWrapping = textStyle.wrap; + chartText.tmpText.enableWordWrapping = textStyle.autoWrap; #else chartText.text = GetOrAddComponent(txtObj); chartText.text.font = textStyle.font == null ? theme.font : textStyle.font;