优化性能,降低GC

This commit is contained in:
monitor1394
2019-07-23 21:43:01 +08:00
parent d0b51d9297
commit cb6d0765ca
12 changed files with 276 additions and 161 deletions

View File

@@ -202,15 +202,21 @@ namespace XCharts
return coordinateWidth / (m_BoundaryGap ? dataCount : dataCount - 1);
}
private Dictionary<float, string> _cacheValue2str = new Dictionary<float, string>();
public string GetLabelName(int index, float minValue, float maxValue, DataZoom dataZoom)
{
if (m_Type == AxisType.Value)
{
float value = (minValue + (maxValue - minValue) * index / (GetSplitNumber(dataZoom) - 1));
if (value - (int)value == 0)
return (value).ToString();
if (_cacheValue2str.ContainsKey(value)) return _cacheValue2str[value];
else
return (value).ToString("f1");
{
if (value - (int)value == 0)
_cacheValue2str[value] = (value).ToString();
else
_cacheValue2str[value] = (value).ToString("f1");
return _cacheValue2str[value];
}
}
var showData = GetDataList(dataZoom);
int dataCount = showData.Count;

View File

@@ -177,7 +177,7 @@ namespace XCharts
var xdata = serie.xData[n];
var ydata = serie.yData[n];
var serieData = serie.GetSerieData(n);
var symbolSize = serie.symbol.GetSize(serieData == null?null:serieData.data);
var symbolSize = serie.symbol.GetSize(serieData == null ? null : serieData.data);
if (Mathf.Abs(xValue - xdata) / xRate < symbolSize
&& Mathf.Abs(yValue - ydata) / yRate < symbolSize)
{
@@ -241,6 +241,7 @@ namespace XCharts
}
}
private StringBuilder sb = new StringBuilder(100);
protected override void RefreshTooltip()
{
base.RefreshTooltip();
@@ -268,7 +269,8 @@ namespace XCharts
return;
}
StringBuilder sb = new StringBuilder();
sb.Length = 0;
if (!isCartesian)
{
sb.Append(tempAxis.GetData(index, m_DataZoom));
@@ -279,23 +281,23 @@ namespace XCharts
if (serie.show)
{
string key = serie.name;
//if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(i);
if (!string.IsNullOrEmpty(key)) key += " : ";
float xValue, yValue;
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
if (isCartesian)
{
if (serie.selected)
{
sb.AppendFormat("{0}[{1}, {2}]\n", key, xValue, yValue);
sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "");
sb.Append("[").Append(ChartCached.FloatToStr(xValue)).Append(",")
.Append(ChartCached.FloatToStr(yValue)).Append("]\n");
}
}
else
{
string strColor = ColorUtility.ToHtmlStringRGBA(m_ThemeInfo.GetColor(i));
sb.Append("\n");
sb.AppendFormat("<color=#{0}>● </color>", strColor);
sb.AppendFormat("{0}{1}", key, yValue);
sb.Append("\n")
.Append("<color=").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
.Append(ChartCached.FloatToStr(yValue));
}
}
}
@@ -327,6 +329,7 @@ namespace XCharts
{
var showTooltipLabel = axis.show && m_Tooltip.type == Tooltip.Type.Corss;
axis.SetTooltipLabelActive(showTooltipLabel);
if (!showTooltipLabel) return;
string labelText = "";
Vector2 labelPos = Vector2.zero;
if (axis is XAxis)
@@ -335,7 +338,7 @@ namespace XCharts
var diff = axisIndex > 0 ? -axis.axisLabel.fontSize - axis.axisLabel.margin - 3.5f : axis.axisLabel.margin / 2 + 1;
if (axis.IsValue())
{
labelText = m_Tooltip.xValues[axisIndex].ToString("f2");
labelText = ChartCached.FloatToStr(m_Tooltip.xValues[axisIndex], 2);
labelPos = new Vector2(m_Tooltip.pointerPos.x, posY - diff);
}
else
@@ -353,7 +356,7 @@ namespace XCharts
var diff = axisIndex > 0 ? -axis.axisLabel.margin + 3 : axis.axisLabel.margin - 3;
if (axis.IsValue())
{
labelText = m_Tooltip.yValues[axisIndex].ToString("f2");
labelText = ChartCached.FloatToStr(m_Tooltip.yValues[axisIndex], 2);
labelPos = new Vector2(posX - diff, m_Tooltip.pointerPos.y);
}
else

View File

@@ -297,16 +297,18 @@ namespace XCharts
GetMinMaxValue(dataZoom, axisIndex, true, out minVaule, out maxValue);
}
private Dictionary<int, List<Serie>> _stackSeriesForMinMax = new Dictionary<int, List<Serie>>();
private Dictionary<int, float> _serieTotalValueForMinMax = new Dictionary<int, float>();
public void GetMinMaxValue(DataZoom dataZoom, int axisIndex, bool yValue, out int minVaule, out int maxValue)
{
float min = int.MaxValue;
float max = int.MinValue;
if (IsStack())
{
var stackSeries = GetStackSeries();
foreach (var ss in stackSeries)
GetStackSeries(ref _stackSeriesForMinMax);
foreach (var ss in _stackSeriesForMinMax)
{
var seriesTotalValue = new Dictionary<int, float>();
_serieTotalValueForMinMax.Clear();
for (int i = 0; i < ss.Value.Count; i++)
{
var serie = ss.Value[i];
@@ -314,14 +316,14 @@ namespace XCharts
var showData = yValue ? serie.GetYDataList(dataZoom) : serie.GetXDataList(dataZoom);
for (int j = 0; j < showData.Count; j++)
{
if (!seriesTotalValue.ContainsKey(j))
seriesTotalValue[j] = 0;
seriesTotalValue[j] = seriesTotalValue[j] + showData[j];
if (!_serieTotalValueForMinMax.ContainsKey(j))
_serieTotalValueForMinMax[j] = 0;
_serieTotalValueForMinMax[j] = _serieTotalValueForMinMax[j] + showData[j];
}
}
float tmax = int.MinValue;
float tmin = int.MaxValue;
foreach (var tt in seriesTotalValue)
foreach (var tt in _serieTotalValueForMinMax)
{
if (tt.Value > tmax) tmax = tt.Value;
if (tt.Value < tmin) tmin = tt.Value;
@@ -400,16 +402,17 @@ namespace XCharts
return ChartHelper.GetMinDivisibleValue(min);
}
private HashSet<string> _setForStack = new HashSet<string>();
public bool IsStack()
{
HashSet<string> sets = new HashSet<string>();
_setForStack.Clear();
foreach (var serie in m_Series)
{
if (string.IsNullOrEmpty(serie.stack)) continue;
if (sets.Contains(serie.stack)) return true;
if (_setForStack.Contains(serie.stack)) return true;
else
{
sets.Add(serie.stack);
_setForStack.Add(serie.stack);
}
}
return false;
@@ -449,6 +452,52 @@ namespace XCharts
return stackSeries;
}
private Dictionary<string, int> sets = new Dictionary<string, int>();
public void GetStackSeries(ref Dictionary<int, List<Serie>> stackSeries)
{
int count = 0;
sets.Clear();
if (stackSeries == null)
{
stackSeries = new Dictionary<int, List<Serie>>(m_Series.Count);
}
else
{
foreach (var kv in stackSeries)
{
kv.Value.Clear();
}
}
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series[i];
serie.index = i;
if (string.IsNullOrEmpty(serie.stack))
{
if (!stackSeries.ContainsKey(count))
stackSeries[count] = new List<Serie>(m_Series.Count);
stackSeries[count].Add(serie);
count++;
}
else
{
if (!sets.ContainsKey(serie.stack))
{
sets.Add(serie.stack, count);
if (!stackSeries.ContainsKey(count))
stackSeries[count] = new List<Serie>(m_Series.Count);
stackSeries[count].Add(serie);
count++;
}
else
{
int stackIndex = sets[serie.stack];
stackSeries[stackIndex].Add(serie);
}
}
}
}
public List<string> GetSerieNameList()
{
var list = new List<string>();

View File

@@ -1,4 +1,5 @@

using System.Collections.Generic;
using UnityEngine;
using System;
@@ -62,6 +63,22 @@ namespace XCharts
return m_ColorPalette[index];
}
Dictionary<int, string> _colorDic = new Dictionary<int, string>();
public string GetColorStr(int index)
{
if (index < 0)
{
index = 0;
}
index = index % m_ColorPalette.Length;
if (_colorDic.ContainsKey(index)) return _colorDic[index];
else
{
_colorDic[index] = ColorUtility.ToHtmlStringRGBA(GetColor(index));
return _colorDic[index];
}
}
public void Copy(ThemeInfo theme)
{
m_Font = theme.m_Font;

View File

@@ -1,4 +1,5 @@
using System.Xml;
using System.Collections.Generic;
using System.Xml;
using System;
using UnityEngine;
using UnityEngine.UI;
@@ -173,11 +174,13 @@ namespace XCharts
lastDataIndex[1] = dataIndex[1];
}
public bool IsSelected(){
public bool IsSelected()
{
return dataIndex[0] >= 0 || dataIndex[1] >= 0;
}
public bool IsSelectedDataIndex(int index){
public bool IsSelectedDataIndex(int index)
{
return dataIndex[0] == index || dataIndex[1] == index;
}
}