mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-16 21:40:41 +00:00
优化SerieLabel引起的性能问题
This commit is contained in:
@@ -202,7 +202,8 @@ namespace XCharts
|
||||
labelText.text = text;
|
||||
if (m_LabelAutoSize)
|
||||
{
|
||||
var newSize = new Vector2(labelText.preferredWidth + m_LabelPaddingLeftRight * 2,
|
||||
var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
|
||||
new Vector2(labelText.preferredWidth + m_LabelPaddingLeftRight * 2,
|
||||
labelText.preferredHeight + m_LabelPaddingTopBottom * 2);
|
||||
var sizeChange = newSize.x != labelRect.sizeDelta.x || newSize.y != labelRect.sizeDelta.y;
|
||||
if (sizeChange) labelRect.sizeDelta = newSize;
|
||||
|
||||
@@ -289,7 +289,8 @@ namespace XCharts
|
||||
{
|
||||
var labelObject = ChartHelper.AddObject(s_SerieLabelObjectName, transform, chartAnchorMin,
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
ChartHelper.DestroyAllChildren(labelObject.transform);
|
||||
//ChartHelper.DestroyAllChildren(labelObject.transform);
|
||||
SerieLabelPool.ReleaseAll(labelObject.transform);
|
||||
int count = 0;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
@@ -297,8 +298,8 @@ namespace XCharts
|
||||
for (int j = 0; j < serie.data.Count; j++)
|
||||
{
|
||||
var serieData = serie.data[j];
|
||||
if (!serie.label.show && j > 100) continue;
|
||||
var textName = s_SerieLabelObjectName + "_" + i + "_" + j + "_" + serieData.name;
|
||||
if (!serie.label.show && j > 100) continue;
|
||||
var textName = ChartCached.GetSerieLabelName(s_SerieLabelObjectName, i, j);
|
||||
var color = Color.grey;
|
||||
if (serie.type == SerieType.Pie)
|
||||
{
|
||||
@@ -310,14 +311,10 @@ namespace XCharts
|
||||
color = serie.label.color != Color.clear ? serie.label.color :
|
||||
(Color)m_ThemeInfo.GetColor(i);
|
||||
}
|
||||
var backgroundColor = serie.label.backgroundColor;
|
||||
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font,
|
||||
color, backgroundColor, serie.label.fontSize, serie.label.fontStyle, serie.label.rotate,
|
||||
serie.label.backgroundWidth, serie.label.backgroundHeight);
|
||||
|
||||
var iconObj = ChartHelper.AddIcon("Icon", labelObj.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
var labelObj = SerieLabelPool.Get(textName, labelObject.transform, serie.label, m_ThemeInfo.font, color, serieData);
|
||||
var iconObj = labelObj.transform.Find("Icon").gameObject;
|
||||
serieData.SetIconObj(iconObj);
|
||||
|
||||
|
||||
var isAutoSize = serie.label.backgroundWidth == 0 || serie.label.backgroundHeight == 0;
|
||||
serieData.InitLabel(labelObj, isAutoSize, serie.label.paddingLeftRight, serie.label.paddingTopBottom);
|
||||
serieData.SetLabelActive(false);
|
||||
@@ -328,6 +325,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void InitTooltip()
|
||||
{
|
||||
var tooltipObject = ChartHelper.AddObject("tooltip", transform, chartAnchorMin,
|
||||
|
||||
59
Runtime/Internal/SerieLabelPool.cs
Normal file
59
Runtime/Internal/SerieLabelPool.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class SerieLabelPool
|
||||
{
|
||||
private static readonly Stack<GameObject> m_Stack = new Stack<GameObject>(200);
|
||||
|
||||
public static GameObject Get(string name, Transform parent, SerieLabel label, Font font, Color color, SerieData serieData)
|
||||
{
|
||||
GameObject element;
|
||||
if (m_Stack.Count == 0)
|
||||
{
|
||||
element = ChartHelper.AddSerieLabel(name, parent, font,
|
||||
color, label.backgroundColor, label.fontSize, label.fontStyle, label.rotate,
|
||||
label.backgroundWidth, label.backgroundHeight);
|
||||
ChartHelper.AddIcon("Icon", element.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
}
|
||||
else
|
||||
{
|
||||
element = m_Stack.Pop();
|
||||
element.name = name;
|
||||
element.transform.SetParent(parent);
|
||||
ChartHelper.SetActive(element, true);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
public static void Release(GameObject element)
|
||||
{
|
||||
ChartHelper.SetActive(element, false);
|
||||
//if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
|
||||
// Debug.LogError("Internal error. Trying to destroy object that is already released to pool." + element.name);
|
||||
m_Stack.Push(element);
|
||||
}
|
||||
|
||||
public static void ReleaseAll(Transform parent)
|
||||
{
|
||||
int count = parent.childCount;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Release(parent.GetChild(i).gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearAll()
|
||||
{
|
||||
m_Stack.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/SerieLabelPool.cs.meta
Normal file
11
Runtime/Internal/SerieLabelPool.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 340f267fa46e74d0bbbb0b75a20bd708
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -23,6 +23,10 @@ namespace XCharts
|
||||
private static Dictionary<int, string> s_IntToStr = new Dictionary<int, string>(1000);
|
||||
private static Dictionary<int, string> s_IntToFn = new Dictionary<int, string>(20);
|
||||
private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(1000);
|
||||
private static Dictionary<int, string> s_SerieLabelName = new Dictionary<int, string>(1000);
|
||||
private static Dictionary<int, string> s_AxisLabelName = new Dictionary<int, string>(1000);
|
||||
|
||||
|
||||
|
||||
public static string FloatToStr(float value, int f = 0, bool forceE = false)
|
||||
{
|
||||
@@ -83,5 +87,35 @@ namespace XCharts
|
||||
return s_ColorToStr[color];
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GetSerieLabelName(string prefix, int i, int j)
|
||||
{
|
||||
int key = i * 10000000 + j;
|
||||
if (s_SerieLabelName.ContainsKey(key))
|
||||
{
|
||||
return s_SerieLabelName[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
string name = prefix + "_" + i + "_" + j;
|
||||
s_SerieLabelName[key] = name;
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GetAxisLabelName(string prefix, bool isYAxis,int axisIndex, int i)
|
||||
{
|
||||
int key = (isYAxis?2:1) * 1000000 + (axisIndex+1) * 100000 + i;
|
||||
if (s_AxisLabelName.ContainsKey(key))
|
||||
{
|
||||
return s_AxisLabelName[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
string name = prefix + "_" + axisIndex + "_" + i;
|
||||
s_AxisLabelName[key] = name;
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,10 @@ namespace XCharts
|
||||
|
||||
private XChartsMgr() { }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
SerieLabelPool.ClearAll();
|
||||
}
|
||||
|
||||
public string changeLog { get; private set; }
|
||||
public string newVersion { get { return m_NewVersion; } }
|
||||
|
||||
Reference in New Issue
Block a user