性能优化

This commit is contained in:
monitor1394
2020-05-09 09:42:41 +08:00
parent ce429a0bb1
commit 5356865c40
10 changed files with 154 additions and 61 deletions

View File

@@ -626,8 +626,8 @@ namespace XCharts
/// <returns></returns>
public bool IsInChart(Vector2 local)
{
if (local.x < m_ChartX || local.x > m_ChartX + chartWidth ||
local.y < m_ChartY || local.y > m_ChartY + chartHeight)
if (local.x < m_ChartX || local.x > m_ChartX + m_ChartWidth ||
local.y < m_ChartY || local.y > m_ChartY + m_ChartHeight)
{
return false;
}

View File

@@ -747,7 +747,7 @@ namespace XCharts
m_TooltipLabel = label;
m_TooltipLabelRect = label.GetComponent<RectTransform>();
m_TooltipLabelText = label.GetComponentInChildren<Text>();
m_TooltipLabel.SetActive(true);
ChartHelper.SetActive(m_TooltipLabel, true);
}
internal void SetTooltipLabelColor(Color bgColor, Color textColor)
@@ -760,7 +760,7 @@ namespace XCharts
{
if (m_TooltipLabel && m_TooltipLabel.activeInHierarchy != flag)
{
m_TooltipLabel.SetActive(flag);
ChartHelper.SetActive(m_TooltipLabel, flag);
}
}

View File

@@ -1016,6 +1016,10 @@ namespace XCharts
/// </summary>
public void ClearData()
{
foreach (var serieData in m_Data)
{
SerieDataPool.Release(serieData);
}
m_Data.Clear();
SetVerticesDirty();
}
@@ -1049,15 +1053,15 @@ namespace XCharts
while (m_Data.Count > m_MaxCache)
{
m_NeedUpdateFilterData = true;
SerieDataPool.Release(m_Data[0]);
m_Data.RemoveAt(0);
}
}
int xValue = m_Data.Count;
var serieData = new SerieData()
{
data = new List<float>() { xValue, value },
name = dataName
};
var serieData = SerieDataPool.Get();
serieData.data.Add(xValue);
serieData.data.Add(value);
serieData.name = dataName;
serieData.index = xValue;
m_Data.Add(serieData);
m_ShowDataDimension = 1;
@@ -1087,19 +1091,11 @@ namespace XCharts
/// <param name="maxDataNumber"></param>
public SerieData AddXYData(float xValue, float yValue, string dataName = null)
{
if (m_MaxCache > 0)
{
while (m_Data.Count > m_MaxCache)
{
m_NeedUpdateFilterData = true;
m_Data.RemoveAt(0);
}
}
var serieData = new SerieData()
{
data = new List<float>() { xValue, yValue },
name = dataName
};
CheckMaxCache();
var serieData = SerieDataPool.Get();
serieData.data.Add(xValue);
serieData.data.Add(yValue);
serieData.name = dataName;
serieData.index = m_Data.Count;
m_Data.Add(serieData);
m_ShowDataDimension = 2;
@@ -1128,16 +1124,9 @@ namespace XCharts
}
else
{
if (m_MaxCache > 0)
{
while (m_Data.Count > m_MaxCache)
{
m_NeedUpdateFilterData = true;
m_Data.RemoveAt(0);
}
}
CheckMaxCache();
m_ShowDataDimension = valueList.Count;
var serieData = new SerieData();
var serieData = SerieDataPool.Get();
serieData.name = dataName;
serieData.index = m_Data.Count;
for (int i = 0; i < valueList.Count; i++)
@@ -1151,6 +1140,17 @@ namespace XCharts
}
}
private void CheckMaxCache()
{
if (m_MaxCache <= 0) return;
while (m_Data.Count > m_MaxCache)
{
m_NeedUpdateFilterData = true;
SerieDataPool.Release(m_Data[0]);
m_Data.RemoveAt(0);
}
}
/// <summary>
/// 获得维度Y索引对应的数据
/// </summary>
@@ -1572,7 +1572,7 @@ namespace XCharts
return false;
}
/// <summary>
/// 设置指定index的数据图标的尺寸

View File

@@ -229,7 +229,7 @@ namespace XCharts
#if UNITY_EDITOR
if (!Application.isPlaying) return true;
#endif
return !enable || m_IsEnd || (m_CurrDataProgress > m_DestDataProgress && m_CurrDetailProgress > m_DestDetailProgress);
return !m_Enable || m_IsEnd || (m_CurrDataProgress > m_DestDataProgress && m_CurrDetailProgress > m_DestDetailProgress);
}
public bool IsInDelay()

View File

@@ -177,6 +177,17 @@ namespace XCharts
private List<float> m_DataUpdateTime = new List<float>();
private List<bool> m_DataUpdateFlag = new List<bool>();
public void Clear()
{
m_Name = string.Empty;
m_Selected = false;
m_Radius = 0;
m_Data.Clear();
m_PreviousData.Clear();
m_DataUpdateTime.Clear();
m_DataUpdateFlag.Clear();
}
public float GetData(int index, bool inverse = false)
{
if (index >= 0 && index < m_Data.Count)

View File

@@ -15,9 +15,9 @@ namespace XCharts
{
public partial class CoordinateChart : BaseChart
{
private static readonly string s_DefaultAxisY = "axis_y";
private static readonly string s_DefaultAxisX = "axis_x";
private static readonly string s_DefaultDataZoom = "datazoom";
private static readonly string s_DefaultAxisName = "name";
private static readonly string s_DefaultAxisLabel = "label";
[SerializeField] protected Grid m_Grid = Grid.defaultGrid;
[SerializeField] protected List<XAxis> m_XAxises = new List<XAxis>();
@@ -513,9 +513,7 @@ namespace XCharts
private void InitYAxis(int yAxisIndex, YAxis yAxis)
{
yAxis.axisLabelTextList.Clear();
string objName = yAxisIndex > 0 ? s_DefaultAxisY + "2" : s_DefaultAxisY;
string objName = ChartCached.GetYAxisName(yAxisIndex);
var axisObj = ChartHelper.AddObject(objName, transform, chartAnchorMin,
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
axisObj.transform.localPosition = Vector3.zero;
@@ -567,7 +565,7 @@ namespace XCharts
switch (yAxis.axisName.location)
{
case AxisName.Location.Start:
axisName = ChartHelper.AddTextObject(objName + "_name", axisObj.transform,
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleCenter, new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(100, 20), fontSize,
yAxis.axisName.rotate, yAxis.axisName.fontStyle);
@@ -576,7 +574,7 @@ namespace XCharts
new Vector2(zeroPos.x + offset.x, coordinateY - offset.y);
break;
case AxisName.Location.Middle:
axisName = ChartHelper.AddTextObject(objName + "_name", axisObj.transform,
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleRight, new Vector2(1, 0.5f),
new Vector2(1, 0.5f), new Vector2(1, 0.5f), new Vector2(100, 20), fontSize,
yAxis.axisName.rotate, yAxis.axisName.fontStyle);
@@ -585,7 +583,7 @@ namespace XCharts
new Vector2(coordinateX - offset.x, coordinateY + coordinateHeight / 2 + offset.y);
break;
case AxisName.Location.End:
axisName = ChartHelper.AddTextObject(objName + "_name", axisObj.transform,
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleCenter, new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(100, 20), fontSize,
yAxis.axisName.rotate, yAxis.axisName.fontStyle);
@@ -601,7 +599,7 @@ namespace XCharts
{
Vector2 privot = yAxisIndex > 0 ? new Vector2(0, 0.5f) : new Vector2(1, 0.5f);
var labelParent = m_Tooltip.runtimeGameObject.transform;
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
GameObject labelObj = ChartHelper.AddTooltipLabel(ChartCached.GetAxisTooltipLabel(objName), labelParent, m_ThemeInfo.font, privot);
yAxis.SetTooltipLabel(labelObj);
yAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
yAxis.SetTooltipLabelActive(yAxis.show && m_Tooltip.show && m_Tooltip.type == Tooltip.Type.Corss);
@@ -620,7 +618,7 @@ namespace XCharts
{
xAxis.axisLabelTextList.Clear();
string objName = xAxisIndex > 0 ? ChartHelper.Cancat(s_DefaultAxisX, 2) : s_DefaultAxisX;
string objName = ChartCached.GetXAxisName(xAxisIndex);
var axisObj = ChartHelper.AddObject(objName, transform, chartAnchorMin,
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
axisObj.transform.localPosition = Vector3.zero;
@@ -635,7 +633,7 @@ namespace XCharts
{
float labelWidth = xAxis.GetScaleWidth(coordinateWidth, i, m_DataZoom);
bool inside = xAxis.axisLabel.inside;
Text txt = ChartHelper.AddTextObject(ChartHelper.Cancat(objName, i), axisObj.transform,
Text txt = ChartHelper.AddTextObject(ChartCached.GetXAxisName(xAxisIndex, i), axisObj.transform,
m_ThemeInfo.font, labelColor, TextAnchor.MiddleCenter, new Vector2(0, 1),
new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(labelWidth, 20),
xAxis.axisLabel.fontSize, xAxis.axisLabel.rotate, xAxis.axisLabel.fontStyle);
@@ -661,7 +659,7 @@ namespace XCharts
switch (xAxis.axisName.location)
{
case AxisName.Location.Start:
axisName = ChartHelper.AddTextObject(ChartHelper.Cancat(objName, "_name"), axisObj.transform,
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleRight, new Vector2(1, 0.5f),
new Vector2(1, 0.5f), new Vector2(1, 0.5f), new Vector2(100, 20), fontSize,
xAxis.axisName.rotate, xAxis.axisName.fontStyle);
@@ -670,7 +668,7 @@ namespace XCharts
new Vector2(zeroPos.x - offset.x, zeroPos.y + offset.y);
break;
case AxisName.Location.Middle:
axisName = ChartHelper.AddTextObject(ChartHelper.Cancat(objName, "_name"), axisObj.transform,
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleCenter, new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(100, 20), fontSize,
xAxis.axisName.rotate, xAxis.axisName.fontStyle);
@@ -679,7 +677,7 @@ namespace XCharts
new Vector2(coordinateX + coordinateWidth / 2 + offset.x, coordinateY - offset.y);
break;
case AxisName.Location.End:
axisName = ChartHelper.AddTextObject(ChartHelper.Cancat(objName, "_name"), axisObj.transform,
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleLeft, new Vector2(0, 0.5f),
new Vector2(0, 0.5f), new Vector2(0, 0.5f), new Vector2(100, 20), fontSize,
xAxis.axisName.rotate, xAxis.axisName.fontStyle);
@@ -694,7 +692,7 @@ namespace XCharts
{
Vector2 privot = xAxisIndex > 0 ? new Vector2(0.5f, 1) : new Vector2(0.5f, 1);
var labelParent = m_Tooltip.runtimeGameObject.transform;
GameObject labelObj = ChartHelper.AddTooltipLabel(ChartHelper.Cancat(objName, "_label"), labelParent, m_ThemeInfo.font, privot);
GameObject labelObj = ChartHelper.AddTooltipLabel(ChartCached.GetAxisTooltipLabel(objName), labelParent, m_ThemeInfo.font, privot);
xAxis.SetTooltipLabel(labelObj);
xAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
xAxis.SetTooltipLabelActive(xAxis.show && m_Tooltip.show && m_Tooltip.type == Tooltip.Type.Corss);

View File

@@ -0,0 +1,33 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
namespace XCharts
{
internal static class SerieDataPool
{
private static readonly ObjectPool<SerieData> s_ListPool = new ObjectPool<SerieData>(null, OnClear);
static void OnGet(SerieData serieData)
{
}
static void OnClear(SerieData serieData)
{
serieData.Clear();
}
public static SerieData Get()
{
return s_ListPool.Get();
}
public static void Release(SerieData toRelease)
{
s_ListPool.Release(toRelease);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6bd0d723595ba485dbe696491cf978f3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -17,11 +17,17 @@ namespace XCharts
private const string NUMERIC_FORMATTER_d = "d";
private const string NUMERIC_FORMATTER_X = "X";
private const string NUMERIC_FORMATTER_x = "x";
private static readonly string s_DefaultAxisY = "axis_y";
private static readonly string s_DefaultAxisX = "axis_x";
private static CultureInfo ci = new CultureInfo("en-us");// "en-us", "zh-cn", "ar-iq", "de-de"
private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(100);
private static Dictionary<int, string> s_SerieLabelName = new Dictionary<int, string>(1000);
private static Dictionary<int, string> s_AxisLabelName = new Dictionary<int, string>(1000);
private static Dictionary<Color, string> s_ColorDotStr = new Dictionary<Color, string>(100);
private static Dictionary<int, string> s_XAxisName = new Dictionary<int, string>();
private static Dictionary<int, string> s_YAxisName = new Dictionary<int, string>();
private static Dictionary<string, string> s_AxisLabel = new Dictionary<string, string>();
private static Dictionary<float, Dictionary<string, string>> s_NumberToStr = new Dictionary<float, Dictionary<string, string>>();
private static Dictionary<int, Dictionary<string, string>> s_PrecisionToStr = new Dictionary<int, Dictionary<string, string>>();
@@ -119,19 +125,51 @@ namespace XCharts
}
}
internal static string GetAxisLabelName(string prefix, bool isYAxis, int axisIndex, int i)
internal static string GetXAxisName(int axisIndex, int index = -1)
{
int key = (isYAxis ? 2 : 1) * 1000000 + (axisIndex + 1) * 100000 + i;
if (s_AxisLabelName.ContainsKey(key))
if (axisIndex > 0) axisIndex = 2;
if (index >= 0)
{
return s_AxisLabelName[key];
int key = (axisIndex + 1) * 10000 + index;
if (!s_XAxisName.ContainsKey(key))
{
s_XAxisName[key] = axisIndex > 0 ? s_DefaultAxisX + axisIndex + index : s_DefaultAxisX + index;
}
return s_XAxisName[key];
}
else
else if (!s_XAxisName.ContainsKey(axisIndex))
{
string name = prefix + "_" + axisIndex + "_" + i;
s_AxisLabelName[key] = name;
return name;
s_XAxisName[axisIndex] = axisIndex > 0 ? s_DefaultAxisX + axisIndex : s_DefaultAxisX;
}
return s_XAxisName[axisIndex];
}
internal static string GetYAxisName(int axisIndex, int index = -1)
{
if (axisIndex > 0) axisIndex = 2;
if (index >= 0)
{
int key = (axisIndex + 1) * 10000 + index;
if (!s_YAxisName.ContainsKey(key))
{
s_YAxisName[key] = axisIndex > 0 ? s_DefaultAxisY + axisIndex + index : s_DefaultAxisY + index;
}
return s_YAxisName[key];
}
else if (!s_YAxisName.ContainsKey(axisIndex))
{
s_YAxisName[axisIndex] = axisIndex > 0 ? s_DefaultAxisY + axisIndex : s_DefaultAxisY;
}
return s_YAxisName[axisIndex];
}
internal static string GetAxisTooltipLabel(string axisName)
{
if (!s_AxisLabel.ContainsKey(axisName))
{
s_AxisLabel[axisName] = axisName + "_label";
}
return s_AxisLabel[axisName];
}
}
}

View File

@@ -15,6 +15,8 @@ namespace XCharts
{
public static class ChartDrawer
{
private static readonly Vector2 zeroVector2 = Vector2.zero;
private static readonly Vector3 zeroVector3 = Vector3.zero;
private static UIVertex[] vertex = new UIVertex[4];
private static List<Vector3> s_CurvesPosList = new List<Vector3>();
@@ -44,7 +46,7 @@ namespace XCharts
for (int j = 0; j < 4; j++)
{
vertex[j].color = color;
vertex[j].uv0 = Vector2.zero;
vertex[j].uv0 = zeroVector2;
}
vh.AddUIVertexQuad(vertex);
}
@@ -256,7 +258,7 @@ namespace XCharts
for (int j = 0; j < 4; j++)
{
vertex[j].color = j >= 2 ? toColor : startColor;
vertex[j].uv0 = Vector2.zero;
vertex[j].uv0 = zeroVector2;
}
vh.AddUIVertexQuad(vertex);
}
@@ -519,15 +521,15 @@ namespace XCharts
UIVertex v1 = new UIVertex();
v1.position = p1;
v1.color = color;
v1.uv0 = Vector3.zero;
v1.uv0 = zeroVector2;
UIVertex v2 = new UIVertex();
v2.position = p2;
v2.color = color2;
v2.uv0 = Vector3.zero;
v2.uv0 = zeroVector2;
UIVertex v3 = new UIVertex();
v3.position = p3;
v3.color = color3;
v3.uv0 = Vector3.zero;
v3.uv0 = zeroVector2;
int startIndex = vh.currentVertCount;
vh.AddVert(v1);
vh.AddVert(v2);