This commit is contained in:
monitor1394
2022-05-22 22:17:38 +08:00
parent 003f4da9de
commit bafe032bb9
391 changed files with 3718 additions and 2774 deletions

View File

@@ -1,11 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
namespace XCharts.Runtime
{
[System.Serializable]
public class VisualMapPieces : ChildComponent
public class VisualMapRange : ChildComponent
{
[SerializeField] private double m_Min;
[SerializeField] private double m_Max;
@@ -31,6 +30,7 @@ namespace XCharts.Runtime
public bool Contains(double value, double minMaxRange)
{
if (m_Min == 0 && m_Max == 0) return false;
var cmin = System.Math.Abs(m_Min) < 1 ? minMaxRange * m_Min : m_Min;
var cmax = System.Math.Abs(m_Max) < 1 ? minMaxRange * m_Max : m_Max;
return value >= cmin && value < cmax;
@@ -101,9 +101,8 @@ namespace XCharts.Runtime
[SerializeField] private bool m_WorkOnLine = true;
[SerializeField] private bool m_WorkOnArea = false;
[SerializeField] private List<Color32> m_InRange = new List<Color32>();
[SerializeField] private List<Color32> m_OutOfRange = new List<Color32>() { Color.gray };
[SerializeField] private List<VisualMapPieces> m_Pieces = new List<VisualMapPieces>();
[SerializeField] private List<VisualMapRange> m_OutOfRange = new List<VisualMapRange>() { new VisualMapRange() { color = Color.gray } };
[SerializeField] private List<VisualMapRange> m_InRange = new List<VisualMapRange>();
public VisualMapContext context = new VisualMapContext();
@@ -331,19 +330,10 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetStruct(ref m_WorkOnArea, value)) SetVerticesDirty(); }
}
/// <summary>
/// Defines the visual color in the selected range.
/// |定义 在选中范围中 的视觉颜色。
/// </summary>
public List<Color32> inRange
{
get { return m_InRange; }
set { if (value != null) { m_InRange = value; SetVerticesDirty(); } }
}
/// <summary>
/// Defines a visual color outside of the selected range.
/// |定义 在选中范围外 的视觉颜色。
/// </summary>
public List<Color32> outOfRange
public List<VisualMapRange> outOfRange
{
get { return m_OutOfRange; }
set { if (value != null) { m_OutOfRange = value; SetVerticesDirty(); } }
@@ -351,10 +341,10 @@ namespace XCharts.Runtime
/// <summary>
/// 分段式每一段的相关配置。
/// </summary>
public List<VisualMapPieces> pieces
public List<VisualMapRange> inRange
{
get { return m_Pieces; }
set { if (value != null) { m_Pieces = value; SetVerticesDirty(); } }
get { return m_InRange; }
set { if (value != null) { m_InRange = value; SetVerticesDirty(); } }
}
public override bool vertsDirty { get { return m_VertsDirty || location.anyDirty; } }
@@ -396,73 +386,84 @@ namespace XCharts.Runtime
}
}
public float runtimeRangeMinHeight { get { return (float)((rangeMin - min) / (max - min) * itemHeight); } }
public float runtimeRangeMaxHeight { get { return (float)((rangeMax - min) / (max - min) * itemHeight); } }
public float runtimeRangeMinHeight { get { return (float) ((rangeMin - min) / (max - min) * itemHeight); } }
public float runtimeRangeMaxHeight { get { return (float) ((rangeMax - min) / (max - min) * itemHeight); } }
public List<Color32> runtimeInRange
public void AddColors(List<Color32> colors)
{
get
m_InRange.Clear();
foreach (var color in colors)
{
if (splitNumber == 0 || m_InRange.Count >= splitNumber || m_InRange.Count < 1 || IsPiecewise())
m_InRange.Add(new VisualMapRange()
{
return m_InRange;
}
else
color = color
});
}
}
public void AddColors(List<string> colors)
{
m_InRange.Clear();
foreach (var str in colors)
{
m_InRange.Add(new VisualMapRange()
{
var count = splitNumber > 0 && splitNumber <= m_InRange.Count
? splitNumber
: m_InRange.Count;
if (context.inRangeColors.Count != count)
{
context.inRangeColors.Clear();
var total = max - min;
var diff1 = total / (m_InRange.Count - 1);
var diff2 = total / splitNumber;
var inCount = 0;
var inValue = min;
var rtValue = min;
for (int i = 0; i < splitNumber; i++)
{
rtValue += diff2;
if (rtValue > inValue + diff1)
{
inValue += diff1;
inCount++;
}
if (i == splitNumber - 1)
{
context.inRangeColors.Add(m_InRange[m_InRange.Count - 1]);
}
else
{
var rate = (float)((rtValue - inValue) / diff1);
context.inRangeColors.Add(Color32.Lerp(m_InRange[inCount], m_InRange[inCount + 1], rate));
}
}
}
return context.inRangeColors;
}
color = ThemeStyle.GetColor(str)
});
}
}
public Color32 GetColor(double value)
{
switch (type)
int index = GetIndex(value);
if (index == -1)
{
case Type.Continuous:
return GetContinuousColor(value);
case Type.Piecewise:
return GetPiecesColor(value);
default:
return ColorUtil.clearColor32;
if (m_OutOfRange.Count > 0)
return m_OutOfRange[0].color;
else
return ChartConst.clearColor32;
}
if (m_Type == VisualMap.Type.Piecewise)
{
return m_InRange[index].color;
}
else
{
int splitNumber = m_InRange.Count;
var diff = (m_Max - m_Min) / (splitNumber - 1);
var nowMin = m_Min + index * diff;
var rate = (value - nowMin) / diff;
if (index == splitNumber - 1)
return m_InRange[index].color;
else
return Color32.Lerp(m_InRange[index].color, m_InRange[index + 1].color, (float) rate);
}
}
private bool IsNeedPieceColor(double value, out int index)
{
bool flag = false;
index = -1;
for (int i = 0; i < m_InRange.Count; i++)
{
var range = m_InRange[i];
if (range.min != 0 || range.max != 0)
{
flag = true;
if (range.Contains(value, max - min))
{
index = i;
return true;
}
}
}
return flag;
}
private Color32 GetPiecesColor(double value)
{
foreach (var piece in m_Pieces)
foreach (var piece in m_InRange)
{
if (piece.Contains(value, max - min))
{
@@ -470,54 +471,25 @@ namespace XCharts.Runtime
}
}
if (m_OutOfRange.Count > 0)
return m_OutOfRange[0];
return m_OutOfRange[0].color;
else
return ChartConst.clearColor32;
}
private Color32 GetContinuousColor(double value)
{
if (value < m_Min || value > m_Max)
{
if (m_OutOfRange.Count > 0)
return m_OutOfRange[0];
else
return ChartConst.clearColor32;
}
int splitNumber = runtimeInRange.Count;
if (splitNumber <= 0)
return ChartConst.clearColor32;
var index = GetIndex(value);
if (m_Type == VisualMap.Type.Piecewise)
{
if (index >= 0 && index < runtimeInRange.Count)
return runtimeInRange[index];
else
return ChartConst.clearColor32;
}
else
{
var diff = (m_Max - m_Min) / (splitNumber - 1);
var nowMin = m_Min + index * diff;
var rate = (value - nowMin) / diff;
if (index == splitNumber - 1)
return runtimeInRange[index];
else
return Color32.Lerp(runtimeInRange[index], runtimeInRange[index + 1], (float)rate);
}
}
public int GetIndex(double value)
{
int splitNumber = runtimeInRange.Count;
int splitNumber = m_InRange.Count;
if (splitNumber <= 0)
return -1;
var index = -1;
if (IsNeedPieceColor(value, out index))
{
return index;
}
value = MathUtil.Clamp(value, m_Min, m_Max);
var diff = (m_Max - m_Min) / (splitNumber - 1);
var index = -1;
for (int i = 0; i < splitNumber; i++)
{
if (value <= m_Min + (i + 1) * diff)
@@ -574,10 +546,10 @@ namespace XCharts.Runtime
var centerPos = new Vector3(chartRect.x, chartRect.y) + location.GetPosition(chartRect.width, chartRect.height);
var diff = calculable ? triangleLen : 0;
if (local.x >= centerPos.x - itemWidth / 2 - diff
&& local.x <= centerPos.x + itemWidth / 2 + diff
&& local.y >= centerPos.y - itemHeight / 2 - diff
&& local.y <= centerPos.y + itemHeight / 2 + diff)
if (local.x >= centerPos.x - itemWidth / 2 - diff &&
local.x <= centerPos.x + itemWidth / 2 + diff &&
local.y >= centerPos.y - itemHeight / 2 - diff &&
local.y <= centerPos.y + itemHeight / 2 + diff)
{
return true;
}
@@ -595,18 +567,18 @@ namespace XCharts.Runtime
{
var pos1 = centerPos + Vector3.down * itemHeight / 2;
return local.x >= centerPos.x - itemWidth / 2
&& local.x <= centerPos.x + itemWidth / 2
&& local.y >= pos1.y + runtimeRangeMinHeight
&& local.y <= pos1.y + runtimeRangeMaxHeight;
return local.x >= centerPos.x - itemWidth / 2 &&
local.x <= centerPos.x + itemWidth / 2 &&
local.y >= pos1.y + runtimeRangeMinHeight &&
local.y <= pos1.y + runtimeRangeMaxHeight;
}
else
{
var pos1 = centerPos + Vector3.left * itemHeight / 2;
return local.x >= pos1.x + runtimeRangeMinHeight
&& local.x <= pos1.x + runtimeRangeMaxHeight
&& local.y >= centerPos.y - itemWidth / 2
&& local.y <= centerPos.y + itemWidth / 2;
return local.x >= pos1.x + runtimeRangeMinHeight &&
local.x <= pos1.x + runtimeRangeMaxHeight &&
local.y >= centerPos.y - itemWidth / 2 &&
local.y <= centerPos.y + itemWidth / 2;
}
}
@@ -620,10 +592,10 @@ namespace XCharts.Runtime
var pos1 = centerPos + Vector3.down * itemHeight / 2;
var cpos = new Vector3(pos1.x + itemWidth / 2 + radius, pos1.y + runtimeRangeMinHeight - radius);
return local.x >= cpos.x - radius
&& local.x <= cpos.x + radius
&& local.y >= cpos.y - radius
&& local.y <= cpos.y + radius;
return local.x >= cpos.x - radius &&
local.x <= cpos.x + radius &&
local.y >= cpos.y - radius &&
local.y <= cpos.y + radius;
}
else
{
@@ -631,10 +603,10 @@ namespace XCharts.Runtime
var pos1 = centerPos + Vector3.left * itemHeight / 2;
var cpos = new Vector3(pos1.x + runtimeRangeMinHeight, pos1.y + itemWidth / 2 + radius);
return local.x >= cpos.x - radius
&& local.x <= cpos.x + radius
&& local.y >= cpos.y - radius
&& local.y <= cpos.y + radius;
return local.x >= cpos.x - radius &&
local.x <= cpos.x + radius &&
local.y >= cpos.y - radius &&
local.y <= cpos.y + radius;
}
}
@@ -648,10 +620,10 @@ namespace XCharts.Runtime
var pos1 = centerPos + Vector3.down * itemHeight / 2;
var cpos = new Vector3(pos1.x + itemWidth / 2 + radius, pos1.y + runtimeRangeMaxHeight + radius);
return local.x >= cpos.x - radius
&& local.x <= cpos.x + radius
&& local.y >= cpos.y - radius
&& local.y <= cpos.y + radius;
return local.x >= cpos.x - radius &&
local.x <= cpos.x + radius &&
local.y >= cpos.y - radius &&
local.y <= cpos.y + radius;
}
else
{
@@ -659,10 +631,10 @@ namespace XCharts.Runtime
var pos1 = centerPos + Vector3.left * itemHeight / 2;
var cpos = new Vector3(pos1.x + runtimeRangeMaxHeight + radius, pos1.y + itemWidth / 2 + radius);
return local.x >= cpos.x - radius
&& local.x <= cpos.x + radius
&& local.y >= cpos.y - radius
&& local.y <= cpos.y + radius;
return local.x >= cpos.x - radius &&
local.x <= cpos.x + radius &&
local.y >= cpos.y - radius &&
local.y <= cpos.y + radius;
}
}
}

View File

@@ -1,4 +1,3 @@
using System.Collections.Generic;
using UnityEngine;
@@ -15,6 +14,6 @@ namespace XCharts.Runtime
public bool maxDrag { get; internal set; }
internal List<Color32> inRangeColors = new List<Color32>();
}
}

View File

@@ -1,4 +1,3 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
@@ -63,11 +62,11 @@ namespace XCharts.Runtime
return;
}
if (local.x < chart.chartX
|| local.x > chart.chartX + chart.chartWidth
|| local.y < chart.chartY
|| local.y > chart.chartY + chart.chartHeight
|| !visualMap.IsInRangeRect(local, chart.chartRect))
if (local.x < chart.chartX ||
local.x > chart.chartX + chart.chartWidth ||
local.y < chart.chartY ||
local.y > chart.chartY + chart.chartHeight ||
!visualMap.IsInRangeRect(local, chart.chartRect))
{
if (visualMap.context.pointerIndex >= 0)
{
@@ -116,10 +115,10 @@ namespace XCharts.Runtime
var halfHig = visualMap.itemHeight / 2;
var xRadius = 0f;
var yRadius = 0f;
var splitNum = visualMap.runtimeInRange.Count;
var splitNum = visualMap.inRange.Count;
var splitWid = visualMap.itemHeight / (splitNum - 1);
var isVertical = false;
var colors = visualMap.runtimeInRange;
var colors = visualMap.inRange;
var triangeLen = chart.theme.visualMap.triangeLen;
switch (visualMap.orient)
@@ -172,8 +171,8 @@ namespace XCharts.Runtime
}
break;
}
if (visualMap.calculable
&& (visualMap.rangeMin > visualMap.min || visualMap.rangeMax < visualMap.max))
if (visualMap.calculable &&
(visualMap.rangeMin > visualMap.min || visualMap.rangeMax < visualMap.max))
{
var rangeMin = visualMap.rangeMin;
var rangeMax = visualMap.rangeMax;
@@ -189,8 +188,8 @@ namespace XCharts.Runtime
else if (rangeMin <= splitMin && rangeMax >= splitMax)
{
var splitPos = pos1 + dir * (i - 1 + 0.5f) * splitWid;
var startColor = colors[i - 1];
var toColor = visualMap.IsPiecewise() ? startColor : colors[i];
var startColor = colors[i - 1].color;
var toColor = visualMap.IsPiecewise() ? startColor : colors[i].color;
UGL.DrawRectangle(vh, splitPos, xRadius, yRadius, startColor, toColor, isVertical);
}
else if (rangeMin > splitMin && rangeMax >= splitMax)
@@ -199,7 +198,7 @@ namespace XCharts.Runtime
var splitMaxPos = pos1 + dir * i * splitWid;
var splitPos = p0 + (splitMaxPos - p0) / 2;
var startColor = visualMap.GetColor(visualMap.rangeMin);
var toColor = visualMap.IsPiecewise() ? startColor : colors[i];
var toColor = visualMap.IsPiecewise() ? startColor : colors[i].color;
var yRadius1 = Vector3.Distance(p0, splitMaxPos) / 2;
if (visualMap.orient == Orient.Vertical)
@@ -212,7 +211,7 @@ namespace XCharts.Runtime
var p0 = pos1 + dir * visualMap.runtimeRangeMaxHeight;
var splitMinPos = pos1 + dir * (i - 1) * splitWid;
var splitPos = splitMinPos + (p0 - splitMinPos) / 2;
var startColor = colors[i - 1];
var startColor = colors[i - 1].color;
var toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(visualMap.rangeMax);
var yRadius1 = Vector3.Distance(p0, splitMinPos) / 2;
@@ -242,8 +241,8 @@ namespace XCharts.Runtime
for (int i = 1; i < splitNum; i++)
{
var splitPos = pos1 + dir * (i - 1 + 0.5f) * splitWid;
var startColor = colors[i - 1];
var toColor = visualMap.IsPiecewise() ? startColor : colors[i];
var startColor = colors[i - 1].color;
var toColor = visualMap.IsPiecewise() ? startColor : colors[i].color;
UGL.DrawRectangle(vh, splitPos, xRadius, yRadius, startColor, toColor, isVertical);
}
}
@@ -272,14 +271,14 @@ namespace XCharts.Runtime
var p2 = new Vector3(centerPos.x + halfWid, Mathf.Clamp(pointerPos.y + (triangeLen / 2), p0.y, p1.y));
var p3 = new Vector3(centerPos.x + halfWid, Mathf.Clamp(pointerPos.y - (triangeLen / 2), p0.y, p1.y));
var p4 = new Vector3(centerPos.x + halfWid + triangeLen / 2, pointerPos.y);
UGL.DrawTriangle(vh, p2, p3, p4, colors[visualMap.context.pointerIndex]);
UGL.DrawTriangle(vh, p2, p3, p4, colors[visualMap.context.pointerIndex].color);
}
else
{
var p2 = new Vector3(Mathf.Clamp(pointerPos.x + (triangeLen / 2), p0.x, p1.x), centerPos.y + halfWid);
var p3 = new Vector3(Mathf.Clamp(pointerPos.x - (triangeLen / 2), p0.x, p1.x), centerPos.y + halfWid);
var p4 = new Vector3(pointerPos.x, centerPos.y + halfWid + triangeLen / 2);
UGL.DrawTriangle(vh, p2, p3, p4, colors[visualMap.context.pointerIndex]);
UGL.DrawTriangle(vh, p2, p3, p4, colors[visualMap.context.pointerIndex].color);
}
}
}
@@ -293,23 +292,21 @@ namespace XCharts.Runtime
var dir = Vector3.zero;
var halfWid = visualMap.itemWidth / 2;
var halfHig = visualMap.itemHeight / 2;
var splitNum = visualMap.runtimeInRange.Count;
var colors = visualMap.runtimeInRange;
switch (visualMap.orient)
{
case Orient.Horizonal:
for (int i = 0; i < visualMap.pieces.Count; i++)
for (int i = 0; i < visualMap.inRange.Count; i++)
{
var piece = visualMap.pieces[i];
var piece = visualMap.inRange[i];
}
break;
case Orient.Vertical:
var each = visualMap.itemHeight + visualMap.itemGap;
for (int i = 0; i < visualMap.pieces.Count; i++)
for (int i = 0; i < visualMap.inRange.Count; i++)
{
var piece = visualMap.pieces[i];
var piece = visualMap.inRange[i];
var pos = new Vector3(centerPos.x, centerPos.y - each * i);
UGL.DrawRectangle(vh, pos, halfWid, halfHig, piece.color);
}

View File

@@ -1,4 +1,3 @@
using System;
using UnityEngine;
@@ -78,7 +77,7 @@ namespace XCharts.Runtime
float startX = grid.context.x + axis.context.scaleWidth / 2;
value = (min + (pos.x - startX) / (grid.context.width - axis.context.scaleWidth) * (max - min));
if (visualMap.IsPiecewise())
value = (int)value;
value = (int) value;
}
else
{
@@ -94,7 +93,7 @@ namespace XCharts.Runtime
float startY = grid.context.y + relativedAxis.context.scaleWidth / 2;
value = (min + (pos.y - startY) / (grid.context.height - relativedAxis.context.scaleWidth) * (max - min));
if (visualMap.IsPiecewise())
value = (int)value;
value = (int) value;
}
else
{
@@ -124,7 +123,7 @@ namespace XCharts.Runtime
var grid = chart.GetChartComponent<GridCoord>(axis.gridIndex);
var value = min + (pos.x - grid.context.x) / grid.context.width * (max - min);
var rate = (value - min) / (max - min);
var color = itemStyle.GetGradientColor((float)rate, defaultColor);
var color = itemStyle.GetGradientColor((float) rate, defaultColor);
if (ChartHelper.IsClearColor(color))
return defaultColor;
@@ -139,7 +138,7 @@ namespace XCharts.Runtime
var max = axis.context.maxValue;
var value = min + (pos.x - grid.context.x) / grid.context.width * (max - min);
var rate = (value - min) / (max - min);
var color = lineStyle.GetGradientColor((float)rate, defaultColor);
var color = lineStyle.GetGradientColor((float) rate, defaultColor);
if (ChartHelper.IsClearColor(color))
return defaultColor;
@@ -153,7 +152,7 @@ namespace XCharts.Runtime
return false;
if (!visualMap.show || (!visualMap.workOnLine && !visualMap.workOnArea))
return false;
if (visualMap.inRange.Count <= 0 && visualMap.pieces.Count <= 0)
if (visualMap.inRange.Count <= 0)
return false;
return true;
}
@@ -163,7 +162,7 @@ namespace XCharts.Runtime
return false;
if (!visualMap.show || !visualMap.workOnLine)
return false;
if (visualMap.inRange.Count <= 0 && visualMap.pieces.Count <= 0)
if (visualMap.inRange.Count <= 0)
return false;
return true;
}
@@ -173,15 +172,15 @@ namespace XCharts.Runtime
return false;
if (!visualMap.show || !visualMap.workOnArea)
return false;
if (visualMap.inRange.Count <= 0 && visualMap.pieces.Count <= 0)
if (visualMap.inRange.Count <= 0)
return false;
return true;
}
public static int GetDimension(VisualMap visualMap, int serieDataCount)
{
var dimension = visualMap != null && visualMap.dimension >= 0
? visualMap.dimension : serieDataCount - 1;
var dimension = visualMap != null && visualMap.dimension >= 0 ?
visualMap.dimension : serieDataCount - 1;
if (dimension > serieDataCount - 1)
dimension = serieDataCount - 1;