[optimize] optimize interact and performance

This commit is contained in:
monitor1394
2022-04-13 13:26:46 +08:00
parent 7ca4ddcb88
commit a3d2d3f1d5
15 changed files with 156 additions and 168 deletions

View File

@@ -7,43 +7,43 @@ namespace XCharts.Runtime
{ {
public class AxisContext : MainComponentContext public class AxisContext : MainComponentContext
{ {
internal Orient orient { get; set; } public Orient orient;
public float x { get; internal set; } public float x;
public float y { get; internal set; } public float y;
public float width { get; internal set; } public float width;
public float height { get; internal set; } public float height;
public Vector3 position { get; internal set; } public Vector3 position;
public float left { get; internal set; } public float left;
public float right { get; internal set; } public float right;
public float bottom { get; internal set; } public float bottom;
public float top { get; internal set; } public float top;
/// <summary> /// <summary>
/// the current minimun value. /// the current minimun value.
/// |当前最小值。 /// |当前最小值。
/// </summary> /// </summary>
public double minValue { get; internal set; } public double minValue;
/// <summary> /// <summary>
/// the current maximum value. /// the current maximum value.
/// |当前最大值。 /// |当前最大值。
/// </summary> /// </summary>
public double maxValue { get; internal set; } public double maxValue;
/// <summary> /// <summary>
/// the offset of zero position. /// the offset of zero position.
/// |坐标轴原点在坐标轴的偏移。 /// |坐标轴原点在坐标轴的偏移。
/// </summary> /// </summary>
public float offset { get; internal set; } public float offset;
public double minMaxRange { get; internal set; } public double minMaxRange;
public float scaleWidth { get; internal set; } public float scaleWidth;
public float startAngle { get; set; } public float startAngle;
public double pointerValue { get; internal set; } public double pointerValue;
public Vector3 pointerLabelPosition { get; internal set; } public Vector3 pointerLabelPosition;
public double axisTooltipValue { get; internal set; } public double axisTooltipValue;
public List<string> runtimeData { get { return m_RuntimeData; } } public List<string> runtimeData { get { return m_RuntimeData; } }
public List<double> labelValueList { get { return m_LabelValueList; } } public List<double> labelValueList { get { return m_LabelValueList; } }
public List<ChartLabel> labelObjectList { get { return m_AxisLabelList; } } public List<ChartLabel> labelObjectList { get { return m_AxisLabelList; } }
internal List<string> filterData; internal List<string> filterData;
internal bool lastCheckInverse { get; set; } internal bool lastCheckInverse;
internal bool isNeedUpdateFilterData; internal bool isNeedUpdateFilterData;
private int filterStart; private int filterStart;

View File

@@ -34,7 +34,7 @@ namespace XCharts
return; return;
if (!grid.context.isPointerEnter) if (!grid.context.isPointerEnter)
{ {
axis.context.pointerValue = 0; axis.context.pointerValue = double.PositiveInfinity;
} }
else else
{ {

View File

@@ -36,12 +36,11 @@ namespace XCharts.Runtime
axis.context.pointerValue = double.PositiveInfinity; axis.context.pointerValue = double.PositiveInfinity;
return; return;
} }
var angleAxis = ComponentHelper.GetAngleAxis(chart.components, polar.index); var angleAxis = ComponentHelper.GetAngleAxis(chart.components, polar.index);
if (angleAxis == null) if (angleAxis == null)
return; return;
var startAngle = angleAxis.context.startAngle;
var dist = Vector3.Distance(chart.pointerPos, polar.context.center); var dist = Vector3.Distance(chart.pointerPos, polar.context.center);
axis.context.pointerValue = axis.context.minValue + (dist / polar.context.radius) * axis.context.minMaxRange; axis.context.pointerValue = axis.context.minValue + (dist / polar.context.radius) * axis.context.minMaxRange;
axis.context.pointerLabelPosition = GetLabelPosition(polar, axis, angleAxis.context.startAngle, dist); axis.context.pointerLabelPosition = GetLabelPosition(polar, axis, angleAxis.context.startAngle, dist);

View File

@@ -231,6 +231,16 @@ namespace XCharts.Runtime
return color; return color;
} }
public Color32 GetToColor()
{
if (m_Opacity == 1 || m_ToColor.a == 0)
return m_ToColor;
var color = m_ToColor;
color.a = (byte)(color.a * m_Opacity);
return color;
}
public Color32 GetColor0() public Color32 GetColor0()
{ {
if (m_Opacity == 1 || m_Color0.a == 0) if (m_Opacity == 1 || m_Color0.a == 0)

View File

@@ -179,6 +179,7 @@ namespace XCharts.Runtime
private void SetTooltipIndicatorLabel(Axis axis, ChartLabel label) private void SetTooltipIndicatorLabel(Axis axis, ChartLabel label)
{ {
if (label == null) return; if (label == null) return;
if (double.IsPositiveInfinity(axis.context.pointerValue)) return;
label.SetActive(true); label.SetActive(true);
label.SetLabelActive(true); label.SetLabelActive(true);
label.SetPosition(axis.context.pointerLabelPosition); label.SetPosition(axis.context.pointerLabelPosition);
@@ -234,7 +235,6 @@ namespace XCharts.Runtime
private void UpdateAxisPointerDataIndex(Serie serie, XAxis xAxis, YAxis yAxis, GridCoord grid, bool isTriggerAxis) private void UpdateAxisPointerDataIndex(Serie serie, XAxis xAxis, YAxis yAxis, GridCoord grid, bool isTriggerAxis)
{ {
serie.context.pointerAxisDataIndexs.Clear(); serie.context.pointerAxisDataIndexs.Clear();
if (yAxis.IsCategory()) if (yAxis.IsCategory())
{ {

View File

@@ -3,18 +3,18 @@ using UnityEngine;
namespace XCharts.Runtime namespace XCharts.Runtime
{ {
public class GridCoordContext : MainComponentContext, IRectContext public class GridCoordContext : MainComponentContext
{ {
public float x { get; internal set; } public float x;
public float y { get; internal set; } public float y;
public float width { get; internal set; } public float width;
public float height { get; internal set; } public float height;
public Vector3 position { get; internal set; } public Vector3 position;
public Vector3 center { get; internal set; } public Vector3 center;
public float left { get; internal set; } public float left;
public float right { get; internal set; } public float right;
public float bottom { get; internal set; } public float bottom;
public float top { get; internal set; } public float top;
public bool isPointerEnter { get; set; } public bool isPointerEnter;
} }
} }

View File

@@ -6,16 +6,16 @@ namespace XCharts.Runtime
{ {
public class ParallelCoordContext : MainComponentContext public class ParallelCoordContext : MainComponentContext
{ {
public float x { get; internal set; } public float x;
public float y { get; internal set; } public float y;
public float width { get; internal set; } public float width;
public float height { get; internal set; } public float height;
public Vector3 position { get; internal set; } public Vector3 position;
public float left { get; internal set; } public float left;
public float right { get; internal set; } public float right;
public float bottom { get; internal set; } public float bottom;
public float top { get; internal set; } public float top;
public bool runtimeIsPointerEnter { get; set; } public bool runtimeIsPointerEnter;
internal List<ParallelAxis> parallelAxes = new List<ParallelAxis>(); internal List<ParallelAxis> parallelAxes = new List<ParallelAxis>();
} }
} }

View File

@@ -10,12 +10,12 @@ namespace XCharts.Runtime
/// the center position of polar in container. /// the center position of polar in container.
/// |极坐标在容器中的具体中心点。 /// |极坐标在容器中的具体中心点。
/// </summary> /// </summary>
public Vector3 center { get; internal set; } public Vector3 center;
/// <summary> /// <summary>
/// the true radius of polar. /// the true radius of polar.
/// |极坐标的运行时实际半径。 /// |极坐标的运行时实际半径。
/// </summary> /// </summary>
public float radius { get; internal set; } public float radius;
public bool isPointerEnter { get; set; } public bool isPointerEnter;
} }
} }

View File

@@ -277,91 +277,72 @@ namespace XCharts.Runtime
public static Color32 GetItemColor(Serie serie, SerieData serieData, ThemeStyle theme, int index, bool highlight) public static Color32 GetItemColor(Serie serie, SerieData serieData, ThemeStyle theme, int index, bool highlight)
{ {
if (serie == null) return ChartConst.clearColor32; if (serie == null)
return ChartConst.clearColor32;
ItemStyle itemStyle = null;
if (highlight) if (highlight)
{ itemStyle = GetItemStyleEmphasis(serie, serieData);
var itemStyleEmphasis = GetItemStyleEmphasis(serie, serieData); if (itemStyle == null)
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.color)) itemStyle = GetItemStyle(serie, serieData);
{
var color = itemStyleEmphasis.color; var color = ChartHelper.IsClearColor(itemStyle.color)
ChartHelper.SetColorOpacity(ref color, itemStyleEmphasis.opacity); ? theme.GetColor(index)
return color; : itemStyle.color;
}
} if (highlight)
var itemStyle = GetItemStyle(serie, serieData); color = ChartHelper.GetHighlightColor(color);
if (!ChartHelper.IsClearColor(itemStyle.color))
{
return itemStyle.GetColor();
}
else
{
var color = theme.GetColor(index);
if (highlight) color = ChartHelper.GetHighlightColor(color);
ChartHelper.SetColorOpacity(ref color, itemStyle.opacity); ChartHelper.SetColorOpacity(ref color, itemStyle.opacity);
return color; return color;
} }
}
public static Color32 GetItemColor0(Serie serie, SerieData serieData, ThemeStyle theme, bool highlight, Color32 defaultColor) public static Color32 GetItemColor0(Serie serie, SerieData serieData, ThemeStyle theme, bool highlight, Color32 defaultColor)
{ {
if (serie == null) return ChartConst.clearColor32; if (serie == null)
return ChartConst.clearColor32;
ItemStyle itemStyle = null;
if (highlight) if (highlight)
{ itemStyle = GetItemStyleEmphasis(serie, serieData);
var itemStyleEmphasis = GetItemStyleEmphasis(serie, serieData); if (itemStyle == null)
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.color)) itemStyle = GetItemStyle(serie, serieData);
{
var color = itemStyleEmphasis.color0; var color = ChartHelper.IsClearColor(itemStyle.color0)
ChartHelper.SetColorOpacity(ref color, itemStyleEmphasis.opacity); ? defaultColor
return color; : itemStyle.color0;
}
} if (highlight)
var itemStyle = GetItemStyle(serie, serieData); color = ChartHelper.GetHighlightColor(color);
if (!ChartHelper.IsClearColor(itemStyle.color0))
{
return itemStyle.GetColor0();
}
else
{
var color = defaultColor;
if (highlight) color = ChartHelper.GetHighlightColor(color);
ChartHelper.SetColorOpacity(ref color, itemStyle.opacity); ChartHelper.SetColorOpacity(ref color, itemStyle.opacity);
return color; return color;
} }
}
public static Color32 GetItemToColor(Serie serie, SerieData serieData, ThemeStyle theme, int index, bool highlight) public static Color32 GetItemToColor(Serie serie, SerieData serieData, ThemeStyle theme, int index, bool highlight)
{ {
if (serie == null)
return ChartConst.clearColor32;
ItemStyle itemStyle = null;
if (highlight) if (highlight)
{ itemStyle = GetItemStyleEmphasis(serie, serieData);
var itemStyleEmphasis = GetItemStyleEmphasis(serie, serieData); if (itemStyle == null)
if (itemStyleEmphasis != null && !ChartHelper.IsClearColor(itemStyleEmphasis.toColor)) itemStyle = GetItemStyle(serie, serieData);
{
return itemStyleEmphasis.GetColor();
}
}
var itemStyle = GetItemStyle(serie, serieData, highlight);
if (itemStyle == null) itemStyle = serieData.itemStyle;
if (!ChartHelper.IsClearColor(itemStyle.toColor))
{
var color = itemStyle.toColor; var color = itemStyle.toColor;
if (highlight) color = ChartHelper.GetHighlightColor(color); if (ChartHelper.IsClearColor(color))
ChartHelper.SetColorOpacity(ref color, itemStyle.opacity);
return color;
}
if (!ChartHelper.IsClearColor(itemStyle.color))
{ {
var color = itemStyle.color; color = ChartHelper.IsClearColor(itemStyle.color)
if (highlight) color = ChartHelper.GetHighlightColor(color); ? theme.GetColor(index)
: itemStyle.color;
}
if (highlight)
color = ChartHelper.GetHighlightColor(color);
ChartHelper.SetColorOpacity(ref color, itemStyle.opacity); ChartHelper.SetColorOpacity(ref color, itemStyle.opacity);
return color; return color;
} }
else
{
var color = theme.GetColor(index);
if (highlight) color = ChartHelper.GetHighlightColor(color);
ChartHelper.SetColorOpacity(ref color, itemStyle.opacity);
return color;
}
}
public static bool IsDownPoint(Serie serie, int index) public static bool IsDownPoint(Serie serie, int index)
{ {

View File

@@ -1,14 +0,0 @@
using UnityEngine;
namespace XCharts.Runtime
{
public interface IRectContext
{
float x { get; }
float y { get; }
float width { get; }
float height { get; }
Vector3 position { get; }
}
}

View File

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

View File

@@ -90,7 +90,8 @@ namespace XCharts.Runtime
serie.context.pointerEnter = false; serie.context.pointerEnter = false;
foreach (var serieData in serie.data) foreach (var serieData in serie.data)
{ {
if (serieData.context.rect.Contains(chart.pointerPos)) if (serie.context.pointerAxisDataIndexs.Contains(serieData.index)
|| serieData.context.rect.Contains(chart.pointerPos))
{ {
serie.context.pointerItemDataIndex = serieData.index; serie.context.pointerItemDataIndex = serieData.index;
serie.context.pointerEnter = true; serie.context.pointerEnter = true;

View File

@@ -24,12 +24,15 @@ namespace XCharts.Runtime
public void SetValue(ref bool needInteract, float size) public void SetValue(ref bool needInteract, float size)
{ {
if (m_TargetValue != size) if (m_TargetValue != size)
{
if (float.IsNaN(m_TargetValue))
{ {
needInteract = true; needInteract = true;
m_UpdateFlag = true; m_UpdateFlag = true;
m_ValueEnable = true; m_ValueEnable = true;
m_UpdateTime = Time.time; m_UpdateTime = Time.time;
m_PreviousValue = m_TargetValue; m_PreviousValue = m_TargetValue;
}
m_TargetValue = size; m_TargetValue = size;
} }
} }
@@ -37,12 +40,15 @@ namespace XCharts.Runtime
public void SetColor(ref bool needInteract, Color32 color) public void SetColor(ref bool needInteract, Color32 color)
{ {
if (!ChartHelper.IsValueEqualsColor(color, m_TargetColor)) if (!ChartHelper.IsValueEqualsColor(color, m_TargetColor))
{
if (!ChartHelper.IsClearColor(m_TargetColor))
{ {
needInteract = true; needInteract = true;
m_UpdateFlag = true; m_UpdateFlag = true;
m_ValueEnable = true; m_ValueEnable = true;
m_UpdateTime = Time.time; m_UpdateTime = Time.time;
m_PreviousColor = m_TargetColor; m_PreviousColor = m_TargetColor;
}
m_TargetColor = color; m_TargetColor = color;
} }
} }
@@ -50,12 +56,15 @@ namespace XCharts.Runtime
{ {
SetColor(ref needInteract, color); SetColor(ref needInteract, color);
if (!ChartHelper.IsValueEqualsColor(toColor, m_TargetToColor)) if (!ChartHelper.IsValueEqualsColor(toColor, m_TargetToColor))
{
if (!ChartHelper.IsClearColor(m_TargetToColor))
{ {
needInteract = true; needInteract = true;
m_UpdateFlag = true; m_UpdateFlag = true;
m_ValueEnable = true; m_ValueEnable = true;
m_UpdateTime = Time.time; m_UpdateTime = Time.time;
m_PreviousToColor = m_TargetToColor; m_PreviousToColor = m_TargetToColor;
}
m_TargetToColor = toColor; m_TargetToColor = toColor;
} }
} }
@@ -179,7 +188,13 @@ namespace XCharts.Runtime
public void Reset() public void Reset()
{ {
m_UpdateFlag = false;
m_ValueEnable = false; m_ValueEnable = false;
m_PreviousValue = float.NaN;
m_TargetColor = ColorUtil.clearColor32;
m_TargetToColor = ColorUtil.clearColor32;
m_PreviousColor = ColorUtil.clearColor32;
m_PreviousToColor = ColorUtil.clearColor32;
} }
private bool IsValueEnable() private bool IsValueEnable()

View File

@@ -120,11 +120,8 @@ namespace XCharts.Runtime
m_Name = string.Empty; m_Name = string.Empty;
m_Show = true; m_Show = true;
m_Selected = false; m_Selected = false;
context.canShowLabel = true;
context.highlight = false;
context.children.Clear();
context.dataPoints.Clear();
m_Radius = 0; m_Radius = 0;
context.Reset();
interact.Reset(); interact.Reset();
m_Data.Clear(); m_Data.Clear();
m_PreviousData.Clear(); m_PreviousData.Clear();

View File

@@ -36,7 +36,6 @@ namespace XCharts.Runtime
public Vector3 position; public Vector3 position;
public List<Vector3> dataPoints = new List<Vector3>(); public List<Vector3> dataPoints = new List<Vector3>();
public List<SerieData> children = new List<SerieData>(); public List<SerieData> children = new List<SerieData>();
/// <summary> /// <summary>
/// 绘制区域。 /// 绘制区域。
/// </summary> /// </summary>
@@ -50,7 +49,6 @@ namespace XCharts.Runtime
public Vector3 offsetCenter; public Vector3 offsetCenter;
public float stackHeight; public float stackHeight;
public bool isClip; public bool isClip;
public bool canShowLabel = true; public bool canShowLabel = true;
public Image symbol; public Image symbol;
/// <summary> /// <summary>
@@ -58,5 +56,17 @@ namespace XCharts.Runtime
/// |该数据项是否被高亮,一般由鼠标悬停或图例悬停触发高亮。 /// |该数据项是否被高亮,一般由鼠标悬停或图例悬停触发高亮。
/// </summary> /// </summary>
public bool highlight; public bool highlight;
public void Reset()
{
canShowLabel = true;
highlight = false;
parent = null;
symbol = null;
rect = Rect.zero;
subRect = Rect.zero;
children.Clear();
dataPoints.Clear();
}
} }
} }