diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md index 5c9018ea..73870a3b 100644 --- a/CHANGELOG-EN.md +++ b/CHANGELOG-EN.md @@ -1,6 +1,8 @@ # 更新日志 +* (2020.08.29) Added the `onPointerClickPie` of `PieChart`, a callback function of click pie area. +* (2020.08.29) Added the `onPointerClickBar` of `BarChart`, a callback function of click bar. * (2020.08.24) Release `V1.6.0` version * (2020.08.23) Refactor code, replace `Color` with `Color32` for reduce implicit conversion (Can cause custom colors to lose, reference [Q&A 29](https://github.com/monitor1394/unity-ugui-XCharts/blob/master/Assets/XCharts/Documentation/xcharts-questions-and-answers-EN.md) to upgrade) * (2020.08.15) Optimize `PieChart` drawing performance effect #85 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5655b27a..6cf3f3b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 更新日志 +* (2020.08.29) 增加`PieChart`的`onPointerClickPie`点击扇形图扇区回调 +* (2020.08.29) 增加`BarChart`的`onPointerClickBar`点击柱形图柱条回调 * (2020.08.24) 发布`v1.6.0`版本 * (2020.08.23) 重构代码,将与绘制相关的`Color`改为`Color32`,减少隐式转换(更新后会导致自定义的颜色丢失,可参考[问答29](https://github.com/monitor1394/unity-ugui-XCharts/blob/master/Assets/XCharts/Documentation/XCharts问答.md)进行升级) * (2020.08.15) 优化`PieChart`绘制表现效果#85 diff --git a/Documentation/XChartsAPI.md b/Documentation/XChartsAPI.md index 135b3576..a0f39b1c 100644 --- a/Documentation/XChartsAPI.md +++ b/Documentation/XChartsAPI.md @@ -103,10 +103,14 @@ * 继承自 `BaseChart`。 * 继承自 `CoordinateChart`。 +* `BarChart.onPointerClickBar`:点击柱条回调。参数:`eventData`, `dataIndex` + ## `PieChart` * 继承自 `BaseChart`。 +* `PieChart.onPointerClickPie`:点击柱条回调。参数:`eventData`, `serieIndex`, `dataIndex` + ## `RadarChart` * 继承自 `BaseChart`。 diff --git a/Documentation/xcharts-api-EN.md b/Documentation/xcharts-api-EN.md index 135b3576..a0f39b1c 100644 --- a/Documentation/xcharts-api-EN.md +++ b/Documentation/xcharts-api-EN.md @@ -103,10 +103,14 @@ * 继承自 `BaseChart`。 * 继承自 `CoordinateChart`。 +* `BarChart.onPointerClickBar`:点击柱条回调。参数:`eventData`, `dataIndex` + ## `PieChart` * 继承自 `BaseChart`。 +* `PieChart.onPointerClickPie`:点击柱条回调。参数:`eventData`, `serieIndex`, `dataIndex` + ## `RadarChart` * 继承自 `BaseChart`。 diff --git a/Examples/Runtime/Example02_ChartEvent.cs b/Examples/Runtime/Example02_ChartEvent.cs index 4be8cdc1..c1e93af6 100644 --- a/Examples/Runtime/Example02_ChartEvent.cs +++ b/Examples/Runtime/Example02_ChartEvent.cs @@ -31,32 +31,32 @@ namespace XCharts.Examples chart.onScroll = OnScroll; } - void OnPointerEnter(BaseGraph chart, PointerEventData eventData) + void OnPointerEnter(PointerEventData eventData, BaseGraph chart) { //Debug.LogError("enter:" + chart); } - void OnPointerExit(BaseGraph chart, PointerEventData eventData) + void OnPointerExit(PointerEventData eventData, BaseGraph chart) { //Debug.LogError("exit:" + chart); } - void OnPointerDown(BaseGraph chart, PointerEventData eventData) + void OnPointerDown(PointerEventData eventData, BaseGraph chart) { //Debug.LogError("down:" + chart); } - void OnPointerUp(BaseGraph chart, PointerEventData eventData) + void OnPointerUp(PointerEventData eventData, BaseGraph chart) { //Debug.LogError("up:" + chart); } - void OnPointerClick(BaseGraph chart, PointerEventData eventData) + void OnPointerClick(PointerEventData eventData, BaseGraph chart) { //Debug.LogError("click:" + chart); } - void OnScroll(BaseGraph chart, PointerEventData eventData) + void OnScroll(PointerEventData eventData, BaseGraph chart) { //Debug.LogError("scroll:" + chart); } diff --git a/Runtime/API/BarChart_API.cs b/Runtime/API/BarChart_API.cs new file mode 100644 index 00000000..6ba5b351 --- /dev/null +++ b/Runtime/API/BarChart_API.cs @@ -0,0 +1,22 @@ +/******************************************/ +/* */ +/* Copyright (c) 2018 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ + +using UnityEngine; +using System; +using UnityEngine.EventSystems; + +namespace XCharts +{ + public partial class BarChart + { + /// + /// the callback function of click bar. + /// 点击柱形图柱条回调。参数:eventData, dataIndex + /// + public Action onPointerClickBar { set { m_OnPointerClickBar = value; m_ForceOpenRaycastTarget = true; } } + } +} \ No newline at end of file diff --git a/Runtime/API/BarChart_API.cs.meta b/Runtime/API/BarChart_API.cs.meta new file mode 100644 index 00000000..7238897f --- /dev/null +++ b/Runtime/API/BarChart_API.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 73ae9e0b481fc4587828be9149026b0f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/API/BaseGraph_API.cs b/Runtime/API/BaseGraph_API.cs index 22e4015f..39178d0b 100644 --- a/Runtime/API/BaseGraph_API.cs +++ b/Runtime/API/BaseGraph_API.cs @@ -65,39 +65,39 @@ namespace XCharts /// /// 鼠标点击回调。 /// - public Action onPointerClick { set { m_OnPointerClick = value; m_ForceOpenRaycastTarget = true; } } + public Action onPointerClick { set { m_OnPointerClick = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标按下回调。 /// - public Action onPointerDown { set { m_OnPointerDown = value; m_ForceOpenRaycastTarget = true; } } + public Action onPointerDown { set { m_OnPointerDown = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标弹起回调。 /// - public Action onPointerUp { set { m_OnPointerUp = value; m_ForceOpenRaycastTarget = true; } } + public Action onPointerUp { set { m_OnPointerUp = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标进入回调。 /// - public Action onPointerEnter { set { m_OnPointerEnter = value; m_ForceOpenRaycastTarget = true; } } + public Action onPointerEnter { set { m_OnPointerEnter = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标退出回调。 /// - public Action onPointerExit { set { m_OnPointerExit = value; m_ForceOpenRaycastTarget = true; } } + public Action onPointerExit { set { m_OnPointerExit = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标开始拖拽回调。 /// - public Action onBeginDrag { set { m_OnBeginDrag = value; m_ForceOpenRaycastTarget = true; } } + public Action onBeginDrag { set { m_OnBeginDrag = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标拖拽回调。 /// - public Action onDrag { set { m_OnDrag = value; m_ForceOpenRaycastTarget = true; } } + public Action onDrag { set { m_OnDrag = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标结束拖拽回调。 /// - public Action onEndDrag { set { m_OnEndDrag = value; m_ForceOpenRaycastTarget = true; } } + public Action onEndDrag { set { m_OnEndDrag = value; m_ForceOpenRaycastTarget = true; } } /// /// 鼠标滚动回调。 /// - public Action onScroll { set { m_OnScroll = value; m_ForceOpenRaycastTarget = true; } } + public Action onScroll { set { m_OnScroll = value; m_ForceOpenRaycastTarget = true; } } /// /// 设置图形的宽高(在非stretch pivot下才有效,其他情况需要自己调整RectTransform) diff --git a/Runtime/API/PieChart_API.cs b/Runtime/API/PieChart_API.cs new file mode 100644 index 00000000..a4ecfc7b --- /dev/null +++ b/Runtime/API/PieChart_API.cs @@ -0,0 +1,22 @@ +/******************************************/ +/* */ +/* Copyright (c) 2018 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ + +using UnityEngine; +using System; +using UnityEngine.EventSystems; + +namespace XCharts +{ + public partial class PieChart + { + /// + /// the callback function of click pie area. + /// 点击饼图区域回调。参数:PointerEventData,SerieIndex,SerieDataIndex + /// + public Action onPointerClickPie { set { m_OnPointerClickPie = value; m_ForceOpenRaycastTarget = true; } } + } +} \ No newline at end of file diff --git a/Runtime/API/PieChart_API.cs.meta b/Runtime/API/PieChart_API.cs.meta new file mode 100644 index 00000000..4507e9ad --- /dev/null +++ b/Runtime/API/PieChart_API.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 85c7f8858dca444f88830d61b9af3700 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/BarChart.cs b/Runtime/BarChart.cs index 389d7c1a..789c9d3f 100644 --- a/Runtime/BarChart.cs +++ b/Runtime/BarChart.cs @@ -5,7 +5,9 @@ /* */ /******************************************/ +using System; using UnityEngine; +using UnityEngine.EventSystems; namespace XCharts { @@ -13,9 +15,15 @@ namespace XCharts [ExecuteInEditMode] [RequireComponent(typeof(RectTransform))] [DisallowMultipleComponent] - public class BarChart : CoordinateChart + public partial class BarChart : CoordinateChart { + protected Action m_OnPointerClickBar; + protected override void Awake() + { + base.Awake(); + raycastTarget = false; + } #if UNITY_EDITOR protected override void Reset() { @@ -27,9 +35,22 @@ namespace XCharts for (int i = 0; i < 5; i++) { AddXAxisData("x" + (i + 1)); - AddData(0, Random.Range(10, 90)); + AddData(0, UnityEngine.Random.Range(10, 90)); } } #endif + + public override void OnPointerDown(PointerEventData eventData) + { + base.OnPointerDown(eventData); + if (m_OnPointerClickBar == null) return; + if (pointerPos == Vector2.zero) return; + UpdateTooltipValue(pointerPos); + var dataIndex = m_Tooltip.runtimeDataIndex[0]; + if (dataIndex >= 0) + { + m_OnPointerClickBar(eventData, dataIndex); + } + } } } diff --git a/Runtime/Internal/BaseGraphic.cs b/Runtime/Internal/BaseGraph.cs similarity index 86% rename from Runtime/Internal/BaseGraphic.cs rename to Runtime/Internal/BaseGraph.cs index d20e7555..a72a15ac 100644 --- a/Runtime/Internal/BaseGraphic.cs +++ b/Runtime/Internal/BaseGraph.cs @@ -33,15 +33,15 @@ namespace XCharts protected bool m_IsControlledByLayout = false; protected Vector3 m_LastLocalPosition; - protected Action m_OnPointerClick; - protected Action m_OnPointerDown; - protected Action m_OnPointerUp; - protected Action m_OnPointerEnter; - protected Action m_OnPointerExit; - protected Action m_OnBeginDrag; - protected Action m_OnDrag; - protected Action m_OnEndDrag; - protected Action m_OnScroll; + protected Action m_OnPointerClick; + protected Action m_OnPointerDown; + protected Action m_OnPointerUp; + protected Action m_OnPointerEnter; + protected Action m_OnPointerExit; + protected Action m_OnBeginDrag; + protected Action m_OnDrag; + protected Action m_OnEndDrag; + protected Action m_OnScroll; protected Vector2 chartAnchorMax { get { return m_GraphMinAnchor; } } protected Vector2 chartAnchorMin { get { return m_GraphMaxAnchor; } } @@ -228,53 +228,53 @@ namespace XCharts public virtual void OnPointerClick(PointerEventData eventData) { - if (m_OnPointerClick != null) m_OnPointerClick(this, eventData); + if (m_OnPointerClick != null) m_OnPointerClick(eventData, this); } public virtual void OnPointerDown(PointerEventData eventData) { - if (m_OnPointerDown != null) m_OnPointerDown(this, eventData); + if (m_OnPointerDown != null) m_OnPointerDown(eventData, this); } public virtual void OnPointerUp(PointerEventData eventData) { - if (m_OnPointerUp != null) m_OnPointerUp(this, eventData); + if (m_OnPointerUp != null) m_OnPointerUp(eventData, this); } public virtual void OnPointerEnter(PointerEventData eventData) { isPointerInChart = true; - if (m_OnPointerEnter != null) m_OnPointerEnter(this, eventData); + if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this); } public virtual void OnPointerExit(PointerEventData eventData) { isPointerInChart = false; - if (m_OnPointerExit != null) m_OnPointerExit(this, eventData); + if (m_OnPointerExit != null) m_OnPointerExit(eventData, this); } public virtual void OnBeginDrag(PointerEventData eventData) { if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData); - if (m_OnBeginDrag != null) m_OnBeginDrag(this, eventData); + if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this); } public virtual void OnEndDrag(PointerEventData eventData) { if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData); - if (m_OnEndDrag != null) m_OnEndDrag(this, eventData); + if (m_OnEndDrag != null) m_OnEndDrag(eventData, this); } public virtual void OnDrag(PointerEventData eventData) { if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData); - if (m_OnDrag != null) m_OnDrag(this, eventData); + if (m_OnDrag != null) m_OnDrag(eventData, this); } public virtual void OnScroll(PointerEventData eventData) { if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData); - if (m_OnScroll != null) m_OnScroll(this, eventData); + if (m_OnScroll != null) m_OnScroll(eventData, this); } } } diff --git a/Runtime/Internal/CoordinateChart.cs b/Runtime/Internal/CoordinateChart.cs index ff9a42e5..3987f4f6 100644 --- a/Runtime/Internal/CoordinateChart.cs +++ b/Runtime/Internal/CoordinateChart.cs @@ -250,111 +250,7 @@ namespace XCharts } else { - var isCartesian = IsValue(); - int dataCount = m_Series.list.Count > 0 ? m_Series.list[0].GetDataList(dataZoom).Count : 0; - for (int i = 0; i < m_XAxises.Count; i++) - { - var xAxis = m_XAxises[i]; - var yAxis = m_YAxises[i]; - if (!xAxis.show && !yAxis.show) continue; - if (isCartesian && xAxis.show && yAxis.show) - { - var yRate = (yAxis.runtimeMaxValue - yAxis.runtimeMinValue) / m_CoordinateHeight; - var xRate = (xAxis.runtimeMaxValue - xAxis.runtimeMinValue) / m_CoordinateWidth; - var yValue = yRate * (local.y - m_CoordinateY - yAxis.runtimeZeroYOffset); - if (yAxis.runtimeMinValue > 0) yValue += yAxis.runtimeMinValue; - m_Tooltip.runtimeYValues[i] = yValue; - var xValue = xRate * (local.x - m_CoordinateX - xAxis.runtimeZeroXOffset); - if (xAxis.runtimeMinValue > 0) xValue += xAxis.runtimeMinValue; - m_Tooltip.runtimeXValues[i] = xValue; - - for (int j = 0; j < m_Series.Count; j++) - { - var serie = m_Series.GetSerie(j); - for (int n = 0; n < serie.data.Count; n++) - { - var serieData = serie.data[n]; - var xdata = serieData.GetData(0, xAxis.inverse); - var ydata = serieData.GetData(1, yAxis.inverse); - var symbol = SerieHelper.GetSerieSymbol(serie, serieData); - var symbolSize = symbol.GetSize(serieData == null ? null : serieData.data); - if (Mathf.Abs(xValue - xdata) / xRate < symbolSize - && Mathf.Abs(yValue - ydata) / yRate < symbolSize) - { - m_Tooltip.runtimeDataIndex[i] = n; - serieData.highlighted = true; - } - else - { - serieData.highlighted = false; - } - } - } - } - else if (IsCategory()) - { - - for (int j = 0; j < xAxis.GetDataNumber(m_DataZoom); j++) - { - float splitWid = AxisHelper.GetDataWidth(xAxis, m_CoordinateWidth, dataCount, m_DataZoom); - float pX = m_CoordinateX + j * splitWid; - if ((xAxis.boundaryGap && (local.x > pX && local.x <= pX + splitWid)) || - (!xAxis.boundaryGap && (local.x > pX - splitWid / 2 && local.x <= pX + splitWid / 2))) - { - m_Tooltip.runtimeXValues[i] = j; - m_Tooltip.runtimeDataIndex[i] = j; - break; - } - } - for (int j = 0; j < yAxis.GetDataNumber(m_DataZoom); j++) - { - float splitWid = AxisHelper.GetDataWidth(yAxis, m_CoordinateHeight, dataCount, m_DataZoom); - float pY = m_CoordinateY + j * splitWid; - if ((yAxis.boundaryGap && (local.y > pY && local.y <= pY + splitWid)) || - (!yAxis.boundaryGap && (local.y > pY - splitWid / 2 && local.y <= pY + splitWid / 2))) - { - m_Tooltip.runtimeYValues[i] = j; - break; - } - } - } - else if (xAxis.IsCategory()) - { - var value = (yAxis.runtimeMaxValue - yAxis.runtimeMinValue) * (local.y - m_CoordinateY - yAxis.runtimeZeroYOffset) / m_CoordinateHeight; - if (yAxis.runtimeMinValue > 0) value += yAxis.runtimeMinValue; - m_Tooltip.runtimeYValues[i] = value; - for (int j = 0; j < xAxis.GetDataNumber(m_DataZoom); j++) - { - float splitWid = AxisHelper.GetDataWidth(xAxis, m_CoordinateWidth, dataCount, m_DataZoom); - float pX = m_CoordinateX + j * splitWid; - if ((xAxis.boundaryGap && (local.x > pX && local.x <= pX + splitWid)) || - (!xAxis.boundaryGap && (local.x > pX - splitWid / 2 && local.x <= pX + splitWid / 2))) - { - m_Tooltip.runtimeXValues[i] = j; - m_Tooltip.runtimeDataIndex[i] = j; - break; - } - } - } - else if (yAxis.IsCategory()) - { - var value = (xAxis.runtimeMaxValue - xAxis.runtimeMinValue) * (local.x - m_CoordinateX - xAxis.runtimeZeroXOffset) / m_CoordinateWidth; - if (xAxis.runtimeMinValue > 0) value += xAxis.runtimeMinValue; - m_Tooltip.runtimeXValues[i] = value; - for (int j = 0; j < yAxis.GetDataNumber(m_DataZoom); j++) - { - float splitWid = AxisHelper.GetDataWidth(yAxis, m_CoordinateHeight, dataCount, m_DataZoom); - float pY = m_CoordinateY + j * splitWid; - if ((yAxis.boundaryGap && (local.y > pY && local.y <= pY + splitWid)) || - (!yAxis.boundaryGap && (local.y > pY - splitWid / 2 && local.y <= pY + splitWid / 2))) - { - m_Tooltip.runtimeYValues[i] = j; - m_Tooltip.runtimeDataIndex[i] = j; - break; - } - } - } - } + UpdateTooltipValue(local); } if (m_Tooltip.IsSelected()) { @@ -373,6 +269,115 @@ namespace XCharts } } + protected void UpdateTooltipValue(Vector2 local) + { + var isCartesian = IsValue(); + int dataCount = m_Series.list.Count > 0 ? m_Series.list[0].GetDataList(dataZoom).Count : 0; + for (int i = 0; i < m_XAxises.Count; i++) + { + var xAxis = m_XAxises[i]; + var yAxis = m_YAxises[i]; + if (!xAxis.show && !yAxis.show) continue; + if (isCartesian && xAxis.show && yAxis.show) + { + var yRate = (yAxis.runtimeMaxValue - yAxis.runtimeMinValue) / m_CoordinateHeight; + var xRate = (xAxis.runtimeMaxValue - xAxis.runtimeMinValue) / m_CoordinateWidth; + var yValue = yRate * (local.y - m_CoordinateY - yAxis.runtimeZeroYOffset); + if (yAxis.runtimeMinValue > 0) yValue += yAxis.runtimeMinValue; + m_Tooltip.runtimeYValues[i] = yValue; + var xValue = xRate * (local.x - m_CoordinateX - xAxis.runtimeZeroXOffset); + if (xAxis.runtimeMinValue > 0) xValue += xAxis.runtimeMinValue; + m_Tooltip.runtimeXValues[i] = xValue; + + for (int j = 0; j < m_Series.Count; j++) + { + var serie = m_Series.GetSerie(j); + for (int n = 0; n < serie.data.Count; n++) + { + var serieData = serie.data[n]; + var xdata = serieData.GetData(0, xAxis.inverse); + var ydata = serieData.GetData(1, yAxis.inverse); + var symbol = SerieHelper.GetSerieSymbol(serie, serieData); + var symbolSize = symbol.GetSize(serieData == null ? null : serieData.data); + if (Mathf.Abs(xValue - xdata) / xRate < symbolSize + && Mathf.Abs(yValue - ydata) / yRate < symbolSize) + { + m_Tooltip.runtimeDataIndex[i] = n; + serieData.highlighted = true; + } + else + { + serieData.highlighted = false; + } + } + } + } + else if (IsCategory()) + { + + for (int j = 0; j < xAxis.GetDataNumber(m_DataZoom); j++) + { + float splitWid = AxisHelper.GetDataWidth(xAxis, m_CoordinateWidth, dataCount, m_DataZoom); + float pX = m_CoordinateX + j * splitWid; + if ((xAxis.boundaryGap && (local.x > pX && local.x <= pX + splitWid)) || + (!xAxis.boundaryGap && (local.x > pX - splitWid / 2 && local.x <= pX + splitWid / 2))) + { + m_Tooltip.runtimeXValues[i] = j; + m_Tooltip.runtimeDataIndex[i] = j; + break; + } + } + for (int j = 0; j < yAxis.GetDataNumber(m_DataZoom); j++) + { + float splitWid = AxisHelper.GetDataWidth(yAxis, m_CoordinateHeight, dataCount, m_DataZoom); + float pY = m_CoordinateY + j * splitWid; + if ((yAxis.boundaryGap && (local.y > pY && local.y <= pY + splitWid)) || + (!yAxis.boundaryGap && (local.y > pY - splitWid / 2 && local.y <= pY + splitWid / 2))) + { + m_Tooltip.runtimeYValues[i] = j; + break; + } + } + } + else if (xAxis.IsCategory()) + { + var value = (yAxis.runtimeMaxValue - yAxis.runtimeMinValue) * (local.y - m_CoordinateY - yAxis.runtimeZeroYOffset) / m_CoordinateHeight; + if (yAxis.runtimeMinValue > 0) value += yAxis.runtimeMinValue; + m_Tooltip.runtimeYValues[i] = value; + for (int j = 0; j < xAxis.GetDataNumber(m_DataZoom); j++) + { + float splitWid = AxisHelper.GetDataWidth(xAxis, m_CoordinateWidth, dataCount, m_DataZoom); + float pX = m_CoordinateX + j * splitWid; + if ((xAxis.boundaryGap && (local.x > pX && local.x <= pX + splitWid)) || + (!xAxis.boundaryGap && (local.x > pX - splitWid / 2 && local.x <= pX + splitWid / 2))) + { + m_Tooltip.runtimeXValues[i] = j; + m_Tooltip.runtimeDataIndex[i] = j; + break; + } + } + } + else if (yAxis.IsCategory()) + { + var value = (xAxis.runtimeMaxValue - xAxis.runtimeMinValue) * (local.x - m_CoordinateX - xAxis.runtimeZeroXOffset) / m_CoordinateWidth; + if (xAxis.runtimeMinValue > 0) value += xAxis.runtimeMinValue; + m_Tooltip.runtimeXValues[i] = value; + for (int j = 0; j < yAxis.GetDataNumber(m_DataZoom); j++) + { + float splitWid = AxisHelper.GetDataWidth(yAxis, m_CoordinateHeight, dataCount, m_DataZoom); + float pY = m_CoordinateY + j * splitWid; + if ((yAxis.boundaryGap && (local.y > pY && local.y <= pY + splitWid)) || + (!yAxis.boundaryGap && (local.y > pY - splitWid / 2 && local.y <= pY + splitWid / 2))) + { + m_Tooltip.runtimeYValues[i] = j; + m_Tooltip.runtimeDataIndex[i] = j; + break; + } + } + } + } + } + protected StringBuilder sb = new StringBuilder(100); protected override void UpdateTooltip() { diff --git a/Runtime/PieChart.cs b/Runtime/PieChart.cs index 49de3e89..043ccb4f 100644 --- a/Runtime/PieChart.cs +++ b/Runtime/PieChart.cs @@ -5,6 +5,7 @@ /* */ /******************************************/ +using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; @@ -15,11 +16,13 @@ namespace XCharts [ExecuteInEditMode] [RequireComponent(typeof(RectTransform))] [DisallowMultipleComponent] - public class PieChart : BaseChart + public partial class PieChart : BaseChart { private bool isDrawPie; private bool m_IsEnterLegendButtom; + protected Action m_OnPointerClickPie; + protected override void Awake() { base.Awake(); @@ -529,28 +532,28 @@ namespace XCharts public override void OnPointerDown(PointerEventData eventData) { base.OnPointerDown(eventData); - - Vector2 local; - if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, - eventData.position, canvas.worldCamera, out local)) - { - return; - } + if (pointerPos == Vector2.zero) return; + var refresh = false; for (int i = 0; i < m_Series.Count; i++) { var serie = m_Series.GetSerie(i); if (serie.type != SerieType.Pie) continue; - var index = GetPosPieIndex(serie, local); + var index = GetPosPieIndex(serie, pointerPos); if (index >= 0) { + refresh = true; for (int j = 0; j < serie.data.Count; j++) { if (j == index) serie.data[j].selected = !serie.data[j].selected; else serie.data[j].selected = false; } + if (m_OnPointerClickPie != null) + { + m_OnPointerClickPie(eventData, i, index); + } } } - RefreshChart(); + if (refresh) RefreshChart(); } } }