diff --git a/Runtime/Component/Main/DebugInfo.cs b/Runtime/Component/Main/DebugInfo.cs new file mode 100644 index 00000000..7bc2ff84 --- /dev/null +++ b/Runtime/Component/Main/DebugInfo.cs @@ -0,0 +1,154 @@ +/************************************************/ +/* */ +/* Copyright (c) 2018 - 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/************************************************/ + +using UnityEngine; +using System; +using System.Collections.Generic; +using System.Text; +using UnityEngine.UI; + +namespace XCharts +{ + [Serializable] + public class DebugInfo + { + private bool m_ShowDebugInfo = false; + private TextStyle m_DebugInfoTextStyle = new TextStyle() + { + fontSize = 18, + backgroundColor = new Color32(32, 32, 32, 170), + color = Color.white + }; + + private static StringBuilder s_Sb = new StringBuilder(); + + private static readonly float INTERVAL = 0.2f; + private static readonly float MAXCACHE = 20; + private int m_FrameCount = 0; + private float m_LastTime = 0f; + private float m_LastCheckShowTime = 0f; + private int m_LastRefreshCount = 0; + private BaseChart m_Chart; + private ChartLabel m_Label; + private List m_FpsList = new List(); + public float fps { get; private set; } + public float avgFps { get; private set; } + public int refreshCount { get; internal set; } + internal int clickChartCount { get; set; } + + public void Init(BaseChart chart) + { + m_Chart = chart; + m_Label = AddDebugInfoObject("debug", chart.transform, m_DebugInfoTextStyle, chart.theme); + } + + public void Update() + { + if (clickChartCount >= 3) + { + m_ShowDebugInfo = !m_ShowDebugInfo; + ChartHelper.SetActive(m_Label.gameObject.transform, m_ShowDebugInfo); + clickChartCount = 0; + m_LastCheckShowTime = Time.realtimeSinceStartup; + return; + } + if (Time.realtimeSinceStartup - m_LastCheckShowTime > 0.5f) + { + m_LastCheckShowTime = Time.realtimeSinceStartup; + clickChartCount = 0; + } + if (!m_ShowDebugInfo || m_Label == null) + return; + + m_FrameCount++; + if (Time.realtimeSinceStartup - m_LastTime >= INTERVAL) + { + fps = m_FrameCount / (Time.realtimeSinceStartup - m_LastTime); + m_FrameCount = 0; + m_LastTime = Time.realtimeSinceStartup; + if (m_LastRefreshCount == refreshCount) + { + m_LastRefreshCount = 0; + refreshCount = 0; + } + m_LastRefreshCount = refreshCount; + if (m_FpsList.Count > MAXCACHE) + { + m_FpsList.RemoveAt(0); + } + m_FpsList.Add(fps); + avgFps = GetAvg(m_FpsList); + if (m_Label != null) + { + s_Sb.Length = 0; + s_Sb.AppendFormat("v{0}\n", XChartsMgr.version); + s_Sb.AppendFormat("fps : {0:f0} / {1:f0}\n", fps, avgFps); + s_Sb.AppendFormat("draw : {0}\n", refreshCount); + + var dataCount = m_Chart.series.GetAllSerieDataCount(); + SetValueWithKInfo(s_Sb, "data", dataCount); + + var vertCount = 0; + foreach (var serie in m_Chart.series.list) + vertCount += serie.runtimeVertCount; + + SetValueWithKInfo(s_Sb, "b-vert", m_Chart.m_BasePainterVertCount); + SetValueWithKInfo(s_Sb, "s-vert", vertCount); + SetValueWithKInfo(s_Sb, "t-vert", m_Chart.m_TopPainterVertCount, false); + + m_Label.SetText(s_Sb.ToString()); + } + } + } + + private static void SetValueWithKInfo(StringBuilder s_Sb, string key, int value, bool newLine = true) + { + if (value >= 1000) + s_Sb.AppendFormat("{0} : {1:f1}k", key, value * 0.001f); + else + s_Sb.AppendFormat("{0} : {1}", key, value); + if (newLine) + s_Sb.Append("\n"); + } + + private static float GetAvg(List list) + { + var total = 0f; + foreach (var v in list) total += v; + return total / list.Count; + } + + private ChartLabel AddDebugInfoObject(string name, Transform parent, TextStyle textStyle, + ChartTheme theme) + { + var anchorMax = new Vector2(0, 1); + var anchorMin = new Vector2(0, 1); + var pivot = new Vector2(0, 1); + var sizeDelta = new Vector2(130, 150); + + var labelGameObject = ChartHelper.AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta); + labelGameObject.transform.SetAsLastSibling(); + labelGameObject.hideFlags = m_Chart.chartHideFlags; + ChartHelper.SetActive(labelGameObject, m_ShowDebugInfo); + + var image = ChartHelper.GetOrAddComponent(labelGameObject); + image.color = textStyle.backgroundColor; + + var label = new ChartLabel(); + label.gameObject = labelGameObject; + label.label = ChartHelper.AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, + pivot, sizeDelta, textStyle, theme.common); + label.SetAutoSize(true); + label.label.SetActive(true); + label.label.SetAlignment(textStyle.GetAlignment(TextAnchor.UpperLeft)); + label.label.SetLocalPosition(new Vector2(3, -3)); + label.SetText("30"); + label.SetLabelColor(textStyle.color); + return label; + } + } +} \ No newline at end of file diff --git a/Runtime/Component/Main/DebugInfo.cs.meta b/Runtime/Component/Main/DebugInfo.cs.meta new file mode 100644 index 00000000..d6caf938 --- /dev/null +++ b/Runtime/Component/Main/DebugInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9ff63fec5c2f94046b4560565057db8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Component/Main/Serie.cs b/Runtime/Component/Main/Serie.cs index 277011d5..fdc50b98 100644 --- a/Runtime/Component/Main/Serie.cs +++ b/Runtime/Component/Main/Serie.cs @@ -1190,6 +1190,7 @@ namespace XCharts public float runtimeWidth { get; internal set; } public float runtimeHeight { get; internal set; } public Rect runtimeRect { get; internal set; } + public int runtimeVertCount { get; internal set; } public List runtimeSortedData { get { return m_SortedData; } } public List rootData { get { return m_RootData; } } public bool nameDirty { get { return m_NameDirty; } } diff --git a/Runtime/Component/Main/Series.cs b/Runtime/Component/Main/Series.cs index d7bba41e..1fc67355 100644 --- a/Runtime/Component/Main/Series.cs +++ b/Runtime/Component/Main/Series.cs @@ -98,6 +98,16 @@ namespace XCharts SeriesHelper.ClearNameDirty(this); } + public int GetAllSerieDataCount() + { + var count = 0; + foreach (var serie in list) + { + count += serie.dataCount; + } + return count; + } + /// /// 清空所有系列的数据 /// @@ -259,7 +269,7 @@ namespace XCharts /// public void RemoveAll() { - foreach(var serie in m_Series) serie.AnimationFadeIn(); + foreach (var serie in m_Series) serie.AnimationFadeIn(); m_Series.Clear(); } diff --git a/Runtime/Internal/BaseChart.cs b/Runtime/Internal/BaseChart.cs index 7e6f6eb8..c105e045 100644 --- a/Runtime/Internal/BaseChart.cs +++ b/Runtime/Internal/BaseChart.cs @@ -80,6 +80,8 @@ namespace XCharts internal bool m_ReinitTitle = false; internal bool m_CheckAnimation = false; internal bool m_IsPlayingAnimation = false; + internal int m_BasePainterVertCount; + internal int m_TopPainterVertCount; internal protected List m_LegendRealShowName = new List(); protected List m_PainterList = new List(); internal Painter m_PainterTop; @@ -109,6 +111,8 @@ namespace XCharts m_ComponentHandlers.Add(new VisualMapHandler(this)); m_ComponentHandlers.Add(new DataZoomHandler(this)); foreach (var draw in m_ComponentHandlers) draw.Init(); + + m_DebugInfo.Init(this); } protected override void Awake() @@ -161,6 +165,7 @@ namespace XCharts Internal_CheckAnimation(); foreach (var draw in m_DrawSeries) draw.Update(); foreach (var draw in m_ComponentHandlers) draw.Update(); + m_DebugInfo.Update(); } public Painter GetPainter(int index) @@ -791,6 +796,12 @@ namespace XCharts { } + public override void OnPointerClick(PointerEventData eventData) + { + m_DebugInfo.clickChartCount++; + base.OnPointerClick(eventData); + } + public override void OnPointerDown(PointerEventData eventData) { base.OnPointerDown(eventData); @@ -883,6 +894,7 @@ namespace XCharts { m_OnCustomDrawBaseCallback(vh); } + m_BasePainterVertCount = vh.currentVertCount; } protected virtual void OnDrawPainterSerie(VertexHelper vh, Painter painter) @@ -892,6 +904,7 @@ namespace XCharts var maxSeries = m_Series.Count; var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter); m_PainterTop.Refresh(); + m_DebugInfo.refreshCount++; for (int i = painter.index * rate; i < (painter.index + 1) * rate && i < maxSeries; i++) { var serie = m_Series.GetSerie(i); @@ -904,6 +917,7 @@ namespace XCharts { m_OnCustomDrawSerieAfterCallback(vh, serie); } + serie.runtimeVertCount = vh.currentVertCount; } m_RefreshLabel = true; } @@ -919,6 +933,7 @@ namespace XCharts m_OnCustomDrawTopCallback(vh); } DrawTooltip(vh); + m_TopPainterVertCount = vh.currentVertCount; } protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie) diff --git a/Runtime/Internal/BaseGraph.cs b/Runtime/Internal/BaseGraph.cs index 7a055939..b918f878 100644 --- a/Runtime/Internal/BaseGraph.cs +++ b/Runtime/Internal/BaseGraph.cs @@ -22,6 +22,7 @@ namespace XCharts [SerializeField] protected bool m_DebugMode = false; [SerializeField] protected bool m_EnableTextMeshPro = false; [SerializeField] protected Background m_Background = Background.defaultBackground; + protected DebugInfo m_DebugInfo = new DebugInfo(); protected Painter m_Painter; protected int m_SiblingIndex; @@ -57,7 +58,7 @@ namespace XCharts protected Vector2 graphAnchorMin { get { return m_GraphMaxAnchor; } } protected Vector2 graphPivot { get { return m_GraphPivot; } } public HideFlags chartHideFlags { get { return m_DebugMode ? HideFlags.None : HideFlags.HideInHierarchy; } } - + public DebugInfo debug { get { return m_DebugInfo; } } private ScrollRect m_ScrollRect; @@ -129,7 +130,7 @@ namespace XCharts } } - + private void CheckTextMeshPro() {