From 5b5483915b2c168b6019af57495ebea4bb195b60 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Mon, 29 Mar 2021 20:27:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=BB=98=E5=88=B6=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG-EN.md | 1 + CHANGELOG.md | 1 + Documentation/XChartsAPI.md | 5 ++++- Documentation/xcharts-api-EN.md | 5 ++++- Examples/Runtime/Example12_CustomDrawing.cs | 13 ++++++++++- Runtime/API/BaseChart_API.cs | 17 +++++++++++--- Runtime/Internal/BaseChart.cs | 25 ++++++++++++++++----- 7 files changed, 56 insertions(+), 11 deletions(-) diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md index 6d918c6f..adc84319 100644 --- a/CHANGELOG-EN.md +++ b/CHANGELOG-EN.md @@ -32,6 +32,7 @@ ## Latest +* (2021.03.29) Optimized the custom draw callback API * (2021.03.25) Added `Ganttchart` * (2021.03.22) Added `Theme` `Unbind` button to unbind theme when copying chart #118 * (2021.03.18) Fixed an issue where the check box after `Foldout` in `Inspector` could not be checked diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d44c99c..950d26c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ ## Latest +* (2021.03.29) 优化自定义绘制回调接口,增加`onCustomDrawBeforeSerie`、`onCustomDrawAfterSerie`和`onCustomDrawTop` * (2021.03.25) 增加`GanttChart`甘特图 * (2021.03.22) 增加`Theme`的`Unbind`按钮用于解绑复制图表时的主题 #118 * (2021.03.18) 修复`Inspector`下`Foldout`后的勾选框无法选中的问题 diff --git a/Documentation/XChartsAPI.md b/Documentation/XChartsAPI.md index 26e7fd4c..0554dac5 100644 --- a/Documentation/XChartsAPI.md +++ b/Documentation/XChartsAPI.md @@ -15,7 +15,10 @@ * `BaseChart.chartWidth`:图表的宽。 * `BaseChart.chartHeight`:图表的高。 * `BaseChart.forceOpenRaycastTarget`:强制开启鼠标事件检测。一般不用手动设置,内部会自动判断是否需要检测。 -* `BaseChart.onCustomDraw`:自定义绘制回调。 +* `BaseChart.onCustomDraw`:自定义底部绘制回调。在绘制Serie前调用。 +* `BaseChart.onCustomDrawBeforeSerie`:自定义Serie绘制回调。在每个Serie绘制完前调用。 +* `BaseChart.onCustomDrawAfterSerie`:自定义Serie绘制回调。在每个Serie绘制完后调用。 +* `BaseChart.onCustomDrawTop`:自定义顶部绘制回调。在绘制Tooltip前调用。 * `BaseChart.onPointerClick`:鼠标点击回调。 * `BaseChart.onPointerDown`:鼠标按下回调。 * `BaseChart.onPointerUp`:鼠标弹起回调。 diff --git a/Documentation/xcharts-api-EN.md b/Documentation/xcharts-api-EN.md index 8e4658e6..a7d60f59 100644 --- a/Documentation/xcharts-api-EN.md +++ b/Documentation/xcharts-api-EN.md @@ -15,7 +15,10 @@ * `BaseChart.chartWidth`:图表的宽。 * `BaseChart.chartHeight`:图表的高。 * `BaseChart.forceOpenRaycastTarget`:强制开启鼠标事件检测。一般不用手动设置,内部会自动判断是否需要检测。 -* `BaseChart.onCustomDraw`:自定义绘制回调。 +* `BaseChart.onCustomDraw`:自定义底部绘制回调。在绘制Serie前调用。 +* `BaseChart.onCustomDrawBeforeSerie`:自定义Serie绘制回调。在每个Serie绘制完前调用。 +* `BaseChart.onCustomDrawAfterSerie`:自定义Serie绘制回调。在每个Serie绘制完后调用。 +* `BaseChart.onCustomDrawTop`:自定义顶部绘制回调。在绘制Tooltip前调用。 * `BaseChart.onPointerClick`:鼠标点击回调。 * `BaseChart.onPointerDown`:鼠标按下回调。 * `BaseChart.onPointerUp`:鼠标弹起回调。 diff --git a/Examples/Runtime/Example12_CustomDrawing.cs b/Examples/Runtime/Example12_CustomDrawing.cs index 0d56b17f..daaa961e 100644 --- a/Examples/Runtime/Example12_CustomDrawing.cs +++ b/Examples/Runtime/Example12_CustomDrawing.cs @@ -24,7 +24,13 @@ namespace XCharts.Examples chart.onCustomDraw = delegate (VertexHelper vh) { - var dataPoints = chart.series.list[0].dataPoints; + }; + + // or + chart.onCustomDrawSerie = delegate (VertexHelper vh, Serie serie) + { + if (serie.index != 0) return; + var dataPoints = serie.dataPoints; if (dataPoints.Count > 0) { var pos = dataPoints[3]; @@ -35,6 +41,11 @@ namespace XCharts.Examples UGL.DrawCricle(vh, pos, 5, Color.blue); } }; + + // or + chart.onCustomDrawTop = delegate (VertexHelper vh) + { + }; } } } \ No newline at end of file diff --git a/Runtime/API/BaseChart_API.cs b/Runtime/API/BaseChart_API.cs index c7179e7e..4a0ec14e 100644 --- a/Runtime/API/BaseChart_API.cs +++ b/Runtime/API/BaseChart_API.cs @@ -98,11 +98,22 @@ namespace XCharts /// public Vector3 chartPosition { get { return m_ChartPosition; } } public Rect chartRect { get { return m_ChartRect; } } - /// - /// 自定义绘制回调。 + /// 自定义绘制回调。在绘制Serie前调用。 /// - public Action onCustomDraw { set { m_OnCustomDrawCallback = value; } } + public Action onCustomDraw { set { m_OnCustomDrawBaseCallback = value; } } + /// + /// 自定义Serie绘制回调。在每个Serie绘制完前调用。 + /// + public Action onCustomDrawBeforeSerie { set { m_OnCustomDrawSerieBeforeCallback = value; } } + /// + /// 自定义Serie绘制回调。在每个Serie绘制完后调用。 + /// + public Action onCustomDrawAfterSerie { set { m_OnCustomDrawSerieAfterCallback = value; } } + /// + /// 自定义Top绘制回调。在绘制Tooltip前调用。 + /// + public Action onCustomDrawTop { set { m_OnCustomDrawTopCallback = value; } } /// /// the callback function of click pie area. /// 点击饼图区域回调。参数:PointerEventData,SerieIndex,SerieDataIndex diff --git a/Runtime/Internal/BaseChart.cs b/Runtime/Internal/BaseChart.cs index 41a26bed..4fdf765e 100644 --- a/Runtime/Internal/BaseChart.cs +++ b/Runtime/Internal/BaseChart.cs @@ -69,7 +69,10 @@ namespace XCharts protected Vector2 m_ChartPivot; protected Vector2 m_ChartSizeDelta; protected Rect m_ChartRect = new Rect(0, 0, 0, 0); - protected Action m_OnCustomDrawCallback; + protected Action m_OnCustomDrawBaseCallback; + protected Action m_OnCustomDrawTopCallback; + protected Action m_OnCustomDrawSerieBeforeCallback; + protected Action m_OnCustomDrawSerieAfterCallback; protected Action m_OnPointerClickPie; internal bool m_RefreshLabel = false; @@ -818,6 +821,10 @@ namespace XCharts DrawPainterBase(vh); DrawLegend(vh); foreach (var drawSerie in m_DrawSeries) drawSerie.DrawBase(vh); + if (m_OnCustomDrawBaseCallback != null) + { + m_OnCustomDrawBaseCallback(vh); + } } protected virtual void OnDrawPainterSerie(VertexHelper vh, Painter painter) @@ -830,7 +837,15 @@ namespace XCharts for (int i = painter.index * rate; i < (painter.index + 1) * rate && i < maxSeries; i++) { var serie = m_Series.GetSerie(i); + if (m_OnCustomDrawSerieBeforeCallback != null) + { + m_OnCustomDrawSerieBeforeCallback.Invoke(vh, serie); + } DrawPainterSerie(vh, serie); + if (m_OnCustomDrawSerieAfterCallback != null) + { + m_OnCustomDrawSerieAfterCallback(vh, serie); + } } m_RefreshLabel = true; } @@ -838,11 +853,11 @@ namespace XCharts protected virtual void OnDrawPainterTop(VertexHelper vh, Painter painter) { vh.Clear(); - if (m_OnCustomDrawCallback != null) - { - m_OnCustomDrawCallback(vh); - } DrawPainterTop(vh); + if (m_OnCustomDrawTopCallback != null) + { + m_OnCustomDrawTopCallback(vh); + } DrawTooltip(vh); }