Files
XCharts/Runtime/Internal/BaseChart.cs

752 lines
30 KiB
C#
Raw Normal View History

using System;
2022-09-22 08:37:56 +08:00
using System.Collections;
2022-05-22 22:17:38 +08:00
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
2019-05-15 09:44:18 +08:00
using UnityEngine.EventSystems;
2022-05-22 22:17:38 +08:00
using UnityEngine.UI;
2021-01-11 08:54:28 +08:00
using XUGL;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
{
2021-11-23 13:20:07 +08:00
[AddComponentMenu("XCharts/EmptyChart", 10)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public partial class BaseChart : BaseGraph, ISerializationCallbackReceiver
2019-06-03 23:15:23 +08:00
{
2020-05-10 00:20:40 +08:00
[SerializeField] protected string m_ChartName;
2021-11-23 13:20:07 +08:00
[SerializeField] protected ThemeStyle m_Theme = new ThemeStyle();
2021-01-11 08:54:28 +08:00
[SerializeField] protected Settings m_Settings;
2021-11-23 13:20:07 +08:00
#pragma warning disable 0414
2022-05-22 22:17:38 +08:00
[SerializeField][ListForComponent(typeof(AngleAxis))] private List<AngleAxis> m_AngleAxes = new List<AngleAxis>();
[SerializeField][ListForComponent(typeof(Background))] private List<Background> m_Backgrounds = new List<Background>();
[SerializeField][ListForComponent(typeof(DataZoom))] private List<DataZoom> m_DataZooms = new List<DataZoom>();
[SerializeField][ListForComponent(typeof(GridCoord))] private List<GridCoord> m_Grids = new List<GridCoord>();
[SerializeField][ListForComponent(typeof(Legend))] private List<Legend> m_Legends = new List<Legend>();
[SerializeField][ListForComponent(typeof(MarkLine))] private List<MarkLine> m_MarkLines = new List<MarkLine>();
[SerializeField][ListForComponent(typeof(MarkArea))] private List<MarkArea> m_MarkAreas = new List<MarkArea>();
[SerializeField][ListForComponent(typeof(PolarCoord))] private List<PolarCoord> m_Polars = new List<PolarCoord>();
[SerializeField][ListForComponent(typeof(RadarCoord))] private List<RadarCoord> m_Radars = new List<RadarCoord>();
[SerializeField][ListForComponent(typeof(RadiusAxis))] private List<RadiusAxis> m_RadiusAxes = new List<RadiusAxis>();
[SerializeField][ListForComponent(typeof(Title))] private List<Title> m_Titles = new List<Title>();
[SerializeField][ListForComponent(typeof(Tooltip))] private List<Tooltip> m_Tooltips = new List<Tooltip>();
[SerializeField][ListForComponent(typeof(VisualMap))] private List<VisualMap> m_VisualMaps = new List<VisualMap>();
[SerializeField][ListForComponent(typeof(XAxis))] private List<XAxis> m_XAxes = new List<XAxis>();
[SerializeField][ListForComponent(typeof(YAxis))] private List<YAxis> m_YAxes = new List<YAxis>();
[SerializeField][ListForComponent(typeof(SingleAxis))] private List<SingleAxis> m_SingleAxes = new List<SingleAxis>();
[SerializeField][ListForComponent(typeof(ParallelCoord))] private List<ParallelCoord> m_Parallels = new List<ParallelCoord>();
[SerializeField][ListForComponent(typeof(ParallelAxis))] private List<ParallelAxis> m_ParallelAxes = new List<ParallelAxis>();
[SerializeField][ListForComponent(typeof(Comment))] private List<Comment> m_Comments = new List<Comment>();
[SerializeField][ListForSerie(typeof(Bar))] private List<Bar> m_SerieBars = new List<Bar>();
[SerializeField][ListForSerie(typeof(Candlestick))] private List<Candlestick> m_SerieCandlesticks = new List<Candlestick>();
[SerializeField][ListForSerie(typeof(EffectScatter))] private List<EffectScatter> m_SerieEffectScatters = new List<EffectScatter>();
[SerializeField][ListForSerie(typeof(Heatmap))] private List<Heatmap> m_SerieHeatmaps = new List<Heatmap>();
[SerializeField][ListForSerie(typeof(Line))] private List<Line> m_SerieLines = new List<Line>();
[SerializeField][ListForSerie(typeof(Pie))] private List<Pie> m_SeriePies = new List<Pie>();
[SerializeField][ListForSerie(typeof(Radar))] private List<Radar> m_SerieRadars = new List<Radar>();
[SerializeField][ListForSerie(typeof(Ring))] private List<Ring> m_SerieRings = new List<Ring>();
[SerializeField][ListForSerie(typeof(Scatter))] private List<Scatter> m_SerieScatters = new List<Scatter>();
[SerializeField][ListForSerie(typeof(Parallel))] private List<Parallel> m_SerieParallels = new List<Parallel>();
[SerializeField][ListForSerie(typeof(SimplifiedLine))] private List<SimplifiedLine> m_SerieSimplifiedLines = new List<SimplifiedLine>();
[SerializeField][ListForSerie(typeof(SimplifiedBar))] private List<SimplifiedBar> m_SerieSimplifiedBars = new List<SimplifiedBar>();
[SerializeField][ListForSerie(typeof(SimplifiedCandlestick))] private List<SimplifiedCandlestick> m_SerieSimplifiedCandlesticks = new List<SimplifiedCandlestick>();
2021-11-23 13:20:07 +08:00
#pragma warning restore 0414
protected List<Serie> m_Series = new List<Serie>();
protected List<MainComponent> m_Components = new List<MainComponent>();
protected Dictionary<Type, FieldInfo> m_TypeListForComponent = new Dictionary<Type, FieldInfo>();
protected Dictionary<Type, FieldInfo> m_TypeListForSerie = new Dictionary<Type, FieldInfo>();
protected Dictionary<Type, List<MainComponent>> m_ComponentMaps = new Dictionary<Type, List<MainComponent>>();
public Dictionary<Type, FieldInfo> typeListForComponent { get { return m_TypeListForComponent; } }
public Dictionary<Type, FieldInfo> typeListForSerie { get { return m_TypeListForSerie; } }
public List<MainComponent> components { get { return m_Components; } }
public List<Serie> series { get { return m_Series; } }
2020-06-05 08:52:36 +08:00
protected float m_ChartWidth;
protected float m_ChartHeight;
protected float m_ChartX;
protected float m_ChartY;
2020-05-19 07:08:57 +08:00
protected Vector3 m_ChartPosition = Vector3.zero;
protected Vector2 m_ChartMinAnchor;
protected Vector2 m_ChartMaxAnchor;
protected Vector2 m_ChartPivot;
protected Vector2 m_ChartSizeDelta;
2022-03-04 22:17:32 +08:00
2020-05-19 07:08:57 +08:00
protected Rect m_ChartRect = new Rect(0, 0, 0, 0);
2022-03-20 18:52:50 +08:00
protected Action m_OnInit;
protected Action m_OnUpdate;
2022-03-04 22:17:32 +08:00
protected Action<VertexHelper> m_OnDrawBase;
protected Action<VertexHelper> m_OnDrawUpper;
2022-03-04 22:17:32 +08:00
protected Action<VertexHelper> m_OnDrawTop;
protected Action<VertexHelper, Serie> m_OnDrawSerieBefore;
protected Action<VertexHelper, Serie> m_OnDrawSerieAfter;
2021-01-11 08:54:28 +08:00
protected Action<PointerEventData, int, int> m_OnPointerClickPie;
protected Action<int, int> m_OnPointerEnterPie;
2021-11-23 13:20:07 +08:00
protected Action<PointerEventData, int> m_OnPointerClickBar;
2022-03-04 22:17:32 +08:00
protected Action<Axis, double> m_OnAxisPointerValueChanged;
protected Action<Legend, int, string, bool> m_OnLegendClick;
protected Action<Legend, int, string> m_OnLegendEnter;
protected Action<Legend, int, string> m_OnLegendExit;
2022-03-04 22:17:32 +08:00
protected CustomDrawGaugePointerFunction m_CustomDrawGaugePointerFunction;
2021-01-11 08:54:28 +08:00
internal bool m_CheckAnimation = false;
internal protected List<string> m_LegendRealShowName = new List<string>();
protected List<Painter> m_PainterList = new List<Painter>();
internal Painter m_PainterUpper;
2021-07-15 21:18:23 +08:00
internal Painter m_PainterTop;
2021-12-08 08:31:32 +08:00
internal int m_BasePainterVertCount;
internal int m_UpperPainterVertCount;
2021-12-08 08:31:32 +08:00
internal int m_TopPainterVertCount;
2021-11-23 13:20:07 +08:00
private ThemeType m_CheckTheme = 0;
protected List<MainComponentHandler> m_ComponentHandlers = new List<MainComponentHandler>();
protected List<SerieHandler> m_SerieHandlers = new List<SerieHandler>();
2021-01-11 08:54:28 +08:00
2022-05-22 22:17:38 +08:00
protected virtual void DefaultChart() { }
2022-03-20 18:52:50 +08:00
2020-06-05 08:52:36 +08:00
protected override void InitComponent()
{
2020-06-05 08:52:36 +08:00
base.InitComponent();
2021-11-23 13:20:07 +08:00
SeriesHelper.UpdateSerieNameList(this, ref m_LegendRealShowName);
foreach (var handler in m_ComponentHandlers)
handler.InitComponent();
foreach (var handler in m_SerieHandlers)
handler.InitComponent();
m_DebugInfo.Init(this);
}
protected override void Awake()
{
2021-11-23 13:20:07 +08:00
if (m_Settings == null)
m_Settings = Settings.DefaultSettings;
CheckTheme();
2021-01-11 08:54:28 +08:00
base.Awake();
2021-11-23 13:20:07 +08:00
InitComponentHandlers();
InitSerieHandlers();
2021-07-15 21:18:23 +08:00
AnimationReset();
AnimationFadeIn();
2021-11-23 13:20:07 +08:00
XChartsMgr.AddChart(this);
}
2022-03-20 18:52:50 +08:00
protected void OnInit()
2021-01-11 08:54:28 +08:00
{
2021-11-23 13:20:07 +08:00
RemoveAllChartComponent();
OnBeforeSerialize();
AddChartComponentWhenNoExist<Title>();
AddChartComponentWhenNoExist<Tooltip>();
GetChartComponent<Title>().text = GetType().Name;
2021-11-28 20:31:41 +08:00
if (m_Theme.sharedTheme != null)
m_Theme.sharedTheme.CopyTheme(ThemeType.Default);
else
m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
2021-01-11 08:54:28 +08:00
var sizeDelta = rectTransform.sizeDelta;
if (sizeDelta.x < 580 && sizeDelta.y < 300)
{
rectTransform.sizeDelta = new Vector2(580, 300);
}
ChartHelper.HideAllObject(transform);
2022-03-20 18:52:50 +08:00
if (m_OnInit != null)
m_OnInit();
}
#if UNITY_EDITOR
protected override void Reset()
{
base.Reset();
OnInit();
DefaultChart();
2021-01-11 08:54:28 +08:00
Awake();
}
#endif
protected override void Start()
{
2019-07-28 00:44:53 +08:00
RefreshChart();
}
2020-06-05 08:52:36 +08:00
protected override void Update()
{
2021-01-11 08:54:28 +08:00
CheckTheme();
2020-06-05 08:52:36 +08:00
base.Update();
2021-01-11 08:54:28 +08:00
CheckPainter();
2019-06-13 09:53:03 +08:00
CheckRefreshChart();
Internal_CheckAnimation();
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers) handler.Update();
foreach (var handler in m_ComponentHandlers) handler.Update();
m_DebugInfo.Update();
2022-03-20 18:52:50 +08:00
if (m_OnUpdate != null)
m_OnUpdate();
2021-01-11 08:54:28 +08:00
}
public Painter GetPainter(int index)
2021-01-11 08:54:28 +08:00
{
if (index >= 0 && index < m_PainterList.Count)
{
return m_PainterList[index];
}
return null;
}
2021-07-01 07:35:55 +08:00
public void RefreshBasePainter()
2021-01-11 08:54:28 +08:00
{
m_Painter.Refresh();
}
2021-07-01 07:35:55 +08:00
public void RefreshTopPainter()
2021-05-25 23:45:15 +08:00
{
m_PainterTop.Refresh();
}
2021-01-11 08:54:28 +08:00
public void RefreshUpperPainter()
{
m_PainterUpper.Refresh();
}
2021-04-22 18:55:56 +08:00
public void RefreshPainter(int index)
2021-01-11 08:54:28 +08:00
{
var painter = GetPainter(index);
RefreshPainter(painter);
}
2021-04-22 18:55:56 +08:00
public void RefreshPainter(Serie serie)
2021-01-11 08:54:28 +08:00
{
if (serie == null) return;
2021-01-11 08:54:28 +08:00
RefreshPainter(GetPainterIndexBySerie(serie));
}
internal override void RefreshPainter(Painter painter)
{
base.RefreshPainter(painter);
if (painter != null && painter.type == Painter.Type.Serie)
{
m_PainterUpper.Refresh();
2021-01-11 08:54:28 +08:00
}
}
2021-07-01 07:35:55 +08:00
public void SetPainterActive(int index, bool flag)
2021-01-11 08:54:28 +08:00
{
var painter = GetPainter(index);
if (painter == null) return;
2022-03-04 22:17:32 +08:00
painter.SetActive(flag, m_DebugInfo.showAllChartObject);
}
2019-06-14 09:41:15 +08:00
2021-01-11 08:54:28 +08:00
protected virtual void CheckTheme()
{
2021-11-23 13:20:07 +08:00
if (m_Theme.sharedTheme == null)
{
2021-11-23 13:20:07 +08:00
m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
}
2022-03-16 13:41:26 +08:00
if (m_Theme.sharedTheme != null && m_CheckTheme != m_Theme.themeType)
2021-01-11 08:54:28 +08:00
{
2021-11-23 13:20:07 +08:00
m_CheckTheme = m_Theme.themeType;
m_Theme.sharedTheme.CopyTheme(m_CheckTheme);
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
SetAllComponentDirty();
OnThemeChanged();
2021-01-11 08:54:28 +08:00
}
}
2020-06-05 08:52:36 +08:00
protected override void CheckComponent()
{
2021-01-11 08:54:28 +08:00
base.CheckComponent();
if (m_Theme.anyDirty)
{
2021-01-11 08:54:28 +08:00
if (m_Theme.componentDirty)
{
2021-11-23 13:20:07 +08:00
SetAllComponentDirty();
}
2021-01-11 08:54:28 +08:00
if (m_Theme.vertsDirty) RefreshChart();
m_Theme.ClearDirty();
}
2021-11-23 13:20:07 +08:00
foreach (var com in m_Components)
CheckComponentDirty(com);
}
protected void CheckComponentDirty(MainComponent component)
{
if (component == null) return;
if (component.anyDirty)
{
if (component.componentDirty)
2021-11-23 13:20:07 +08:00
{
if (component.refreshComponent != null)
component.refreshComponent.Invoke();
else
component.handler.InitComponent();
2021-11-23 13:20:07 +08:00
}
if (component.vertsDirty)
{
if (component.painter != null)
{
RefreshPainter(component.painter);
}
}
component.ClearDirty();
}
}
2021-01-11 08:54:28 +08:00
protected override void SetAllComponentDirty()
{
2021-01-11 08:54:28 +08:00
base.SetAllComponentDirty();
m_Theme.SetAllDirty();
2021-11-23 13:20:07 +08:00
foreach (var com in m_Components) com.SetAllDirty();
2021-03-25 12:55:52 +08:00
m_RefreshChart = true;
}
protected override void OnDestroy()
{
for (int i = transform.childCount - 1; i >= 0; i--)
{
DestroyImmediate(transform.GetChild(i).gameObject);
}
}
2021-01-11 08:54:28 +08:00
protected virtual void CheckPainter()
2020-05-21 08:32:52 +08:00
{
2021-01-11 08:54:28 +08:00
for (int i = 0; i < m_Series.Count; i++)
2020-05-21 08:32:52 +08:00
{
2021-11-23 13:20:07 +08:00
var serie = m_Series[i];
2021-01-11 08:54:28 +08:00
serie.index = i;
SetPainterActive(i, true);
}
if (transform.childCount - 3 != m_PainterTop.transform.GetSiblingIndex())
{
m_PainterTop.transform.SetSiblingIndex(transform.childCount - 3);
}
2021-01-11 08:54:28 +08:00
}
protected override void InitPainter()
{
base.InitPainter();
2022-05-06 13:07:50 +08:00
if (settings == null) return;
m_Painter.material = settings.basePainterMaterial;
2021-01-11 08:54:28 +08:00
m_PainterList.Clear();
2021-01-17 10:16:30 +08:00
var sizeDelta = new Vector2(m_GraphWidth, m_GraphHeight);
2021-01-11 08:54:28 +08:00
for (int i = 0; i < settings.maxPainter; i++)
{
var index = settings.reversePainter ? settings.maxPainter - 1 - i : i;
var painter = ChartHelper.AddPainterObject("painter_" + index, transform, m_GraphMinAnchor,
m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + index);
2021-01-11 08:54:28 +08:00
painter.index = m_PainterList.Count;
painter.type = Painter.Type.Serie;
painter.onPopulateMesh = OnDrawPainterSerie;
2022-03-04 22:17:32 +08:00
painter.SetActive(false, m_DebugInfo.showAllChartObject);
painter.material = settings.seriePainterMaterial;
2022-02-12 20:10:29 +08:00
painter.transform.SetSiblingIndex(index + 1);
2021-01-11 08:54:28 +08:00
m_PainterList.Add(painter);
}
m_PainterUpper = ChartHelper.AddPainterObject("painter_u", transform, m_GraphMinAnchor,
m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + settings.maxPainter);
m_PainterUpper.type = Painter.Type.Top;
m_PainterUpper.onPopulateMesh = OnDrawPainterUpper;
m_PainterUpper.SetActive(true, m_DebugInfo.showAllChartObject);
m_PainterUpper.material = settings.topPainterMaterial;
m_PainterUpper.transform.SetSiblingIndex(settings.maxPainter + 1);
2021-01-17 10:16:30 +08:00
m_PainterTop = ChartHelper.AddPainterObject("painter_t", transform, m_GraphMinAnchor,
2022-05-22 22:17:38 +08:00
m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + settings.maxPainter);
2021-01-11 08:54:28 +08:00
m_PainterTop.type = Painter.Type.Top;
m_PainterTop.onPopulateMesh = OnDrawPainterTop;
2022-03-04 22:17:32 +08:00
m_PainterTop.SetActive(true, m_DebugInfo.showAllChartObject);
m_PainterTop.material = settings.topPainterMaterial;
2022-02-12 20:10:29 +08:00
m_PainterTop.transform.SetSiblingIndex(settings.maxPainter + 1);
2021-01-11 08:54:28 +08:00
}
2021-11-23 13:20:07 +08:00
internal void InitComponentHandlers()
2021-01-11 08:54:28 +08:00
{
2021-11-23 13:20:07 +08:00
m_ComponentHandlers.Clear();
m_Components.Sort();
m_ComponentMaps.Clear();
foreach (var component in m_Components)
{
2021-11-23 13:20:07 +08:00
var type = component.GetType();
List<MainComponent> list;
if (!m_ComponentMaps.TryGetValue(type, out list))
{
2021-11-23 13:20:07 +08:00
list = new List<MainComponent>();
m_ComponentMaps[type] = list;
}
2021-11-23 13:20:07 +08:00
component.index = list.Count;
list.Add(component);
CreateComponentHandler(component);
}
}
2020-06-05 08:52:36 +08:00
protected override void CheckRefreshChart()
2019-06-13 09:53:03 +08:00
{
2021-01-11 08:54:28 +08:00
if (m_Painter == null) return;
if (m_RefreshChart)
2019-06-13 09:53:03 +08:00
{
CheckRefreshPainter();
2019-06-13 09:53:03 +08:00
m_RefreshChart = false;
}
}
2021-01-11 08:54:28 +08:00
protected override void CheckRefreshPainter()
{
if (m_Painter == null) return;
m_Painter.CheckRefresh();
foreach (var painter in m_PainterList) painter.CheckRefresh();
if (m_PainterUpper != null) m_PainterUpper.CheckRefresh();
2021-02-21 02:26:36 +08:00
if (m_PainterTop != null) m_PainterTop.CheckRefresh();
2021-01-11 08:54:28 +08:00
}
public void Internal_CheckAnimation()
{
if (!m_CheckAnimation)
{
m_CheckAnimation = true;
2021-07-15 21:18:23 +08:00
AnimationFadeIn();
}
}
2020-06-05 08:52:36 +08:00
protected override void OnSizeChanged()
{
2020-06-05 08:52:36 +08:00
base.OnSizeChanged();
m_ChartWidth = m_GraphWidth;
m_ChartHeight = m_GraphHeight;
m_ChartX = m_GraphX;
m_ChartY = m_GraphY;
m_ChartPosition = m_GraphPosition;
m_ChartMinAnchor = m_GraphMinAnchor;
m_ChartMaxAnchor = m_GraphMaxAnchor;
m_ChartPivot = m_GraphPivot;
m_ChartSizeDelta = m_GraphSizeDelta;
m_ChartRect = m_GraphRect;
SetAllComponentDirty();
2021-11-23 13:20:07 +08:00
OnCoordinateChanged();
RefreshChart();
}
2022-05-22 22:17:38 +08:00
internal virtual void OnSerieDataUpdate(int serieIndex)
{
foreach (var handler in m_ComponentHandlers) handler.OnSerieDataUpdate(serieIndex);
}
2021-11-23 13:20:07 +08:00
internal virtual void OnCoordinateChanged()
{
foreach (var component in m_Components)
{
if (component is Axis)
component.SetAllDirty();
if (component is IUpdateRuntimeData)
(component as IUpdateRuntimeData).UpdateRuntimeData(m_ChartX, m_ChartY, m_ChartWidth, m_ChartHeight);
}
}
2020-06-05 08:52:36 +08:00
protected override void OnLocalPositionChanged()
2020-05-21 08:32:52 +08:00
{
2021-11-23 13:20:07 +08:00
Background background;
if (TryGetChartComponent<Background>(out background))
background.SetAllDirty();
2020-05-21 08:32:52 +08:00
}
2022-05-22 22:17:38 +08:00
protected virtual void OnThemeChanged() { }
2021-11-23 13:20:07 +08:00
public virtual void OnDataZoomRangeChanged(DataZoom dataZoom)
{
2021-11-23 13:20:07 +08:00
foreach (var index in dataZoom.xAxisIndexs)
{
var axis = GetChartComponent<XAxis>(index);
if (axis != null && axis.show) axis.SetAllDirty();
}
foreach (var index in dataZoom.yAxisIndexs)
{
var axis = GetChartComponent<YAxis>(index);
if (axis != null && axis.show) axis.SetAllDirty();
}
}
2021-11-23 13:20:07 +08:00
public override void OnPointerClick(PointerEventData eventData)
{
2021-12-08 08:31:32 +08:00
m_DebugInfo.clickChartCount++;
2021-11-23 13:20:07 +08:00
base.OnPointerClick(eventData);
foreach (var handler in m_SerieHandlers) handler.OnPointerClick(eventData);
foreach (var handler in m_ComponentHandlers) handler.OnPointerClick(eventData);
}
2021-01-11 08:54:28 +08:00
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers) handler.OnPointerDown(eventData);
2021-05-13 09:38:32 +08:00
foreach (var handler in m_ComponentHandlers) handler.OnPointerDown(eventData);
}
2021-11-23 13:20:07 +08:00
public override void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
foreach (var handler in m_SerieHandlers) handler.OnPointerUp(eventData);
foreach (var handler in m_ComponentHandlers) handler.OnPointerUp(eventData);
}
public override void OnPointerEnter(PointerEventData eventData)
{
base.OnPointerEnter(eventData);
foreach (var handler in m_SerieHandlers) handler.OnPointerEnter(eventData);
foreach (var handler in m_ComponentHandlers) handler.OnPointerEnter(eventData);
}
public override void OnPointerExit(PointerEventData eventData)
{
base.OnPointerExit(eventData);
foreach (var handler in m_SerieHandlers) handler.OnPointerExit(eventData);
foreach (var handler in m_ComponentHandlers) handler.OnPointerExit(eventData);
}
2021-05-13 09:38:32 +08:00
public override void OnBeginDrag(PointerEventData eventData)
{
base.OnBeginDrag(eventData);
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers) handler.OnBeginDrag(eventData);
2021-05-13 09:38:32 +08:00
foreach (var handler in m_ComponentHandlers) handler.OnBeginDrag(eventData);
}
public override void OnDrag(PointerEventData eventData)
{
base.OnDrag(eventData);
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers) handler.OnDrag(eventData);
2021-05-13 09:38:32 +08:00
foreach (var handler in m_ComponentHandlers) handler.OnDrag(eventData);
}
public override void OnEndDrag(PointerEventData eventData)
{
base.OnEndDrag(eventData);
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers) handler.OnEndDrag(eventData);
2021-05-13 09:38:32 +08:00
foreach (var handler in m_ComponentHandlers) handler.OnEndDrag(eventData);
}
public override void OnScroll(PointerEventData eventData)
{
base.OnScroll(eventData);
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers) handler.OnScroll(eventData);
2021-05-13 09:38:32 +08:00
foreach (var handler in m_ComponentHandlers) handler.OnScroll(eventData);
2021-01-11 08:54:28 +08:00
}
2021-11-23 13:20:07 +08:00
public virtual void OnLegendButtonClick(int index, string legendName, bool show)
2019-07-28 00:44:53 +08:00
{
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers)
2021-12-24 07:38:10 +08:00
handler.OnLegendButtonClick(index, legendName, show);
2019-07-28 00:44:53 +08:00
}
2021-11-23 13:20:07 +08:00
public virtual void OnLegendButtonEnter(int index, string legendName)
2019-07-28 00:44:53 +08:00
{
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers)
2021-12-24 07:38:10 +08:00
handler.OnLegendButtonEnter(index, legendName);
2019-07-28 00:44:53 +08:00
}
2021-11-23 13:20:07 +08:00
public virtual void OnLegendButtonExit(int index, string legendName)
{
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers)
2021-12-24 07:38:10 +08:00
handler.OnLegendButtonExit(index, legendName);
}
2021-01-11 08:54:28 +08:00
protected override void OnDrawPainterBase(VertexHelper vh, Painter painter)
{
vh.Clear();
DrawBackground(vh);
2021-01-11 08:54:28 +08:00
DrawPainterBase(vh);
2021-11-23 13:20:07 +08:00
foreach (var handler in m_ComponentHandlers) handler.DrawBase(vh);
foreach (var handler in m_SerieHandlers) handler.DrawBase(vh);
2022-03-04 22:17:32 +08:00
if (m_OnDrawBase != null)
2021-03-29 20:27:21 +08:00
{
2022-03-04 22:17:32 +08:00
m_OnDrawBase(vh);
2021-03-29 20:27:21 +08:00
}
2021-12-08 08:31:32 +08:00
m_BasePainterVertCount = vh.currentVertCount;
2021-01-11 08:54:28 +08:00
}
protected virtual void OnDrawPainterSerie(VertexHelper vh, Painter painter)
{
vh.Clear();
var maxPainter = settings.maxPainter;
var maxSeries = m_Series.Count;
var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter);
m_PainterUpper.Refresh();
2021-01-11 08:54:28 +08:00
m_PainterTop.Refresh();
2021-11-23 13:20:07 +08:00
m_DebugInfo.refreshCount++;
2021-01-11 08:54:28 +08:00
for (int i = painter.index * rate; i < (painter.index + 1) * rate && i < maxSeries; i++)
{
2021-11-23 13:20:07 +08:00
var serie = m_Series[i];
2021-12-11 18:26:28 +08:00
serie.context.colorIndex = GetLegendRealShowNameIndex(serie.legendName);
serie.context.dataPoints.Clear();
serie.context.dataIndexs.Clear();
2021-12-23 13:23:18 +08:00
serie.context.dataIgnores.Clear();
2022-03-16 13:41:26 +08:00
serie.animation.context.isAllItemAnimationEnd = true;
if (serie.show && !serie.animation.HasFadeOut())
2021-11-23 13:20:07 +08:00
{
if (!serie.context.pointerEnter)
serie.ResetInteract();
if (m_OnDrawSerieBefore != null)
{
m_OnDrawSerieBefore.Invoke(vh, serie);
}
DrawPainterSerie(vh, serie);
if (i >= 0 && i < m_SerieHandlers.Count)
{
var handler = m_SerieHandlers[i];
handler.DrawSerie(vh);
handler.RefreshLabelNextFrame();
}
if (m_OnDrawSerieAfter != null)
{
m_OnDrawSerieAfter(vh, serie);
}
2021-03-29 20:27:21 +08:00
}
2021-12-08 08:31:32 +08:00
serie.context.vertCount = vh.currentVertCount;
2021-01-11 08:54:28 +08:00
}
}
protected virtual void OnDrawPainterUpper(VertexHelper vh, Painter painter)
{
vh.Clear();
DrawPainterUpper(vh);
foreach (var draw in m_ComponentHandlers) draw.DrawUpper(vh);
if (m_OnDrawUpper != null)
{
m_OnDrawUpper(vh);
}
m_UpperPainterVertCount = vh.currentVertCount;
}
2021-01-11 08:54:28 +08:00
protected virtual void OnDrawPainterTop(VertexHelper vh, Painter painter)
{
vh.Clear();
2021-03-29 20:27:21 +08:00
DrawPainterTop(vh);
2021-05-25 23:45:15 +08:00
foreach (var draw in m_ComponentHandlers) draw.DrawTop(vh);
2022-03-04 22:17:32 +08:00
if (m_OnDrawTop != null)
{
2022-03-04 22:17:32 +08:00
m_OnDrawTop(vh);
}
2021-12-08 08:31:32 +08:00
m_TopPainterVertCount = vh.currentVertCount;
}
2022-05-22 22:17:38 +08:00
protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie) { }
2021-01-11 08:54:28 +08:00
protected virtual void DrawPainterUpper(VertexHelper vh)
{
foreach (var handler in m_SerieHandlers)
handler.DrawUpper(vh);
}
2021-01-11 08:54:28 +08:00
protected virtual void DrawPainterTop(VertexHelper vh)
{
2021-11-23 13:20:07 +08:00
foreach (var handler in m_SerieHandlers)
handler.DrawTop(vh);
}
2021-11-23 13:20:07 +08:00
protected virtual void DrawBackground(VertexHelper vh)
{
2022-03-29 22:06:10 +08:00
var background = GetChartComponent<Background>();
if (background != null && background.show)
return;
Vector3 p1 = new Vector3(chartX, chartY + chartHeight);
Vector3 p2 = new Vector3(chartX + chartWidth, chartY + chartHeight);
Vector3 p3 = new Vector3(chartX + chartWidth, chartY);
Vector3 p4 = new Vector3(chartX, chartY);
2021-11-23 13:20:07 +08:00
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, theme.backgroundColor);
}
2019-06-13 09:53:03 +08:00
2021-11-23 13:20:07 +08:00
protected int GetPainterIndexBySerie(Serie serie)
{
2021-11-23 13:20:07 +08:00
var maxPainter = settings.maxPainter;
var maxSeries = m_Series.Count;
if (maxPainter >= maxSeries) return serie.index;
else
{
2021-11-23 13:20:07 +08:00
var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter);
return serie.index / rate;
}
}
2021-11-23 13:20:07 +08:00
private void InitListForFieldInfos()
2021-07-15 21:18:23 +08:00
{
2021-11-23 13:20:07 +08:00
if (m_TypeListForSerie.Count != 0) return;
m_TypeListForComponent.Clear();
m_TypeListForSerie.Clear();
var fileds1 = GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
var fileds2 = GetType().BaseType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
var list = ListPool<FieldInfo>.Get();
list.AddRange(fileds1);
list.AddRange(fileds2);
foreach (var field in list)
{
2022-01-07 09:48:59 +08:00
var attribute1 = field.GetAttribute<ListForSerie>(false);
2021-11-23 13:20:07 +08:00
if (attribute1 != null)
m_TypeListForSerie.Add(attribute1.type, field);
2022-01-07 09:48:59 +08:00
var attribute2 = field.GetAttribute<ListForComponent>(false);
2021-11-23 13:20:07 +08:00
if (attribute2 != null)
m_TypeListForComponent.Add(attribute2.type, field);
}
ListPool<FieldInfo>.Release(list);
}
2021-11-23 13:20:07 +08:00
public void OnBeforeSerialize()
{
2022-02-25 08:10:09 +08:00
#if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
if (!UnityEditor.EditorUtility.IsDirty(this))
return;
UnityEditor.EditorUtility.ClearDirty(this);
#endif
2021-11-23 13:20:07 +08:00
InitListForFieldInfos();
foreach (var kv in m_TypeListForSerie)
{
2021-11-23 13:20:07 +08:00
ReflectionUtil.InvokeListClear(this, kv.Value);
}
2021-11-23 13:20:07 +08:00
foreach (var kv in m_TypeListForComponent)
{
ReflectionUtil.InvokeListClear(this, kv.Value);
}
foreach (var component in m_Components)
{
2021-11-23 13:20:07 +08:00
FieldInfo field;
if (m_TypeListForComponent.TryGetValue(component.GetType(), out field))
ReflectionUtil.InvokeListAdd(this, field, component);
else
Debug.LogError("No ListForComponent:" + component.GetType());
}
foreach (var serie in m_Series)
{
FieldInfo field;
2022-03-04 22:17:32 +08:00
serie.OnBeforeSerialize();
2021-11-23 13:20:07 +08:00
if (m_TypeListForSerie.TryGetValue(serie.GetType(), out field))
ReflectionUtil.InvokeListAdd(this, field, serie);
else
Debug.LogError("No ListForSerie:" + serie.GetType());
2021-01-11 08:54:28 +08:00
}
}
2021-11-23 13:20:07 +08:00
public void OnAfterDeserialize()
2021-01-11 08:54:28 +08:00
{
2021-11-23 13:20:07 +08:00
InitListForFieldInfos();
m_Components.Clear();
m_Series.Clear();
foreach (var kv in m_TypeListForComponent)
2021-01-11 08:54:28 +08:00
{
2021-11-23 13:20:07 +08:00
ReflectionUtil.InvokeListAddTo<MainComponent>(this, kv.Value, AddComponent);
}
foreach (var kv in m_TypeListForSerie)
{
2022-03-04 22:17:32 +08:00
ReflectionUtil.InvokeListAddTo<Serie>(this, kv.Value, AddSerieAfterDeserialize);
}
2021-11-23 13:20:07 +08:00
m_Series.Sort();
m_Components.Sort();
InitComponentHandlers();
InitSerieHandlers();
}
2022-09-22 08:37:56 +08:00
private IEnumerator SaveAsImageSync(string imageType, string path)
{
yield return new WaitForEndOfFrame();
ChartHelper.SaveAsImage(rectTransform, canvas, imageType, path);
}
}
2022-05-22 22:17:38 +08:00
}