VisualMap支持Piecewise类型

This commit is contained in:
monitor1394
2021-05-10 12:45:58 +08:00
parent 6e35907f46
commit 6bee9b20dd
8 changed files with 165 additions and 81 deletions

View File

@@ -21,14 +21,22 @@ namespace XCharts
{ {
++EditorGUI.indentLevel; ++EditorGUI.indentLevel;
PropertyField(prop, "m_Type"); PropertyField(prop, "m_Type");
PropertyField(prop, "m_Direction");
PropertyField(prop, "m_AutoMinMax"); PropertyField(prop, "m_AutoMinMax");
PropertyField(prop, "m_Min"); PropertyField(prop, "m_Min");
PropertyField(prop, "m_Max"); PropertyField(prop, "m_Max");
PropertyField(prop, "m_SplitNumber"); PropertyField(prop, "m_SplitNumber");
PropertyField(prop, "m_Dimension"); PropertyField(prop, "m_Dimension");
PropertyListField(prop, "m_InRange");
PropertyListField(prop, "m_OutOfRange"); PropertyListField(prop, "m_OutOfRange");
var type = (VisualMap.Type)prop.FindPropertyRelative("m_Type").enumValueIndex;
switch (type)
{
case VisualMap.Type.Continuous:
PropertyListField(prop, "m_InRange");
break;
case VisualMap.Type.Piecewise:
PropertyListField(prop, "m_Pieces");
break;
}
PropertyField(prop, "m_Show"); PropertyField(prop, "m_Show");
if (prop.FindPropertyRelative("m_Show").boolValue) if (prop.FindPropertyRelative("m_Show").boolValue)
{ {
@@ -48,4 +56,23 @@ namespace XCharts
} }
} }
} }
[CustomPropertyDrawer(typeof(VisualMap.Pieces), true)]
public class PiecesDrawer : BasePropertyDrawer
{
public override string ClassName { get { return "Pieces"; } }
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
{
base.OnGUI(pos, prop, label);
if (MakeFoldout(prop, ""))
{
++EditorGUI.indentLevel;
PropertyField(prop, "m_Min");
PropertyField(prop, "m_Max");
PropertyField(prop, "m_Label");
PropertyField(prop, "m_Color");
--EditorGUI.indentLevel;
}
}
}
} }

View File

@@ -414,6 +414,7 @@ namespace XCharts
internal bool runtimeLastCheckInverse { get; set; } internal bool runtimeLastCheckInverse { get; set; }
internal double runtimeMinMaxRange { get { return m_MinMaxValueRange; } set { m_MinMaxValueRange = value; } } internal double runtimeMinMaxRange { get { return m_MinMaxValueRange; } set { m_MinMaxValueRange = value; } }
internal List<string> runtimeData { get { return m_RuntimeData; } } internal List<string> runtimeData { get { return m_RuntimeData; } }
public float runtimeScaleWidth { get; internal set; }
private int filterStart; private int filterStart;
private int filterEnd; private int filterEnd;
private int filterMinShow; private int filterMinShow;

View File

@@ -24,7 +24,7 @@ namespace XCharts
[SerializeField] protected Material m_TopPainterMaterial; [SerializeField] protected Material m_TopPainterMaterial;
[SerializeField] [Range(1, 10)] protected float m_LineSmoothStyle = 3f; [SerializeField] [Range(1, 10)] protected float m_LineSmoothStyle = 3f;
[SerializeField] [Range(1f, 20)] protected float m_LineSmoothness = 2f; [SerializeField] [Range(1f, 20)] protected float m_LineSmoothness = 2f;
[SerializeField] [Range(1f, 20)] protected float m_LineSegmentDistance = 3f; [SerializeField] [Range(0.5f, 20)] protected float m_LineSegmentDistance = 3f;
[SerializeField] [Range(1, 10)] protected float m_CicleSmoothness = 2f; [SerializeField] [Range(1, 10)] protected float m_CicleSmoothness = 2f;
[SerializeField] protected float m_LegendIconLineWidth = 2; [SerializeField] protected float m_LegendIconLineWidth = 2;
[SerializeField] private float[] m_LegendIconCornerRadius = new float[] { 0.25f, 0.25f, 0.25f, 0.25f }; [SerializeField] private float[] m_LegendIconCornerRadius = new float[] { 0.25f, 0.25f, 0.25f, 0.25f };

View File

@@ -32,25 +32,6 @@ namespace XCharts
Piecewise Piecewise
} }
/// <summary>
/// 方向。X轴还是Y轴。
/// </summary>
public enum Direction
{
/// <summary>
/// 默认方向。
/// </summary>
Default,
/// <summary>
/// X轴方向。
/// </summary>
X,
/// <summary>
/// Y轴方向。
/// </summary>
Y
}
/// <summary> /// <summary>
/// 选择模式 /// 选择模式
/// </summary> /// </summary>
@@ -66,10 +47,42 @@ namespace XCharts
Single Single
} }
[System.Serializable]
public class Pieces
{
[SerializeField] private float m_Min;
[SerializeField] private float m_Max;
[SerializeField] private string m_Label;
[SerializeField] private Color32 m_Color;
/// <summary>
/// 范围最小值
/// </summary>
public float min { get { return m_Min; } set { m_Min = value; } }
/// <summary>
/// 范围最大值
/// </summary>
public float max { get { return m_Max; } set { m_Max = value; } }
/// <summary>
/// 文字描述
/// </summary>
public string label { get { return m_Label; } set { m_Label = value; } }
/// <summary>
/// 颜色
/// </summary>
public Color32 color { get { return m_Color; } set { m_Color = value; } }
public bool Contains(float value, float minMaxRange)
{
var cmin = Mathf.Abs(m_Min) < 1 ? minMaxRange * m_Min : m_Min;
var cmax = Mathf.Abs(m_Max) < 1 ? minMaxRange * m_Max : m_Max;
return value >= cmin && value < cmax;
}
}
[SerializeField] private bool m_Enable = false; [SerializeField] private bool m_Enable = false;
[SerializeField] private bool m_Show = true; [SerializeField] private bool m_Show = true;
[SerializeField] private Type m_Type = Type.Continuous; [SerializeField] private Type m_Type = Type.Continuous;
[SerializeField] private Direction m_Direction = Direction.Default;
[SerializeField] private SelectedMode m_SelectedMode = SelectedMode.Multiple; [SerializeField] private SelectedMode m_SelectedMode = SelectedMode.Multiple;
[SerializeField] private float m_Min = 0; [SerializeField] private float m_Min = 0;
[SerializeField] private float m_Max = 100f; [SerializeField] private float m_Max = 100f;
@@ -89,7 +102,8 @@ namespace XCharts
[SerializeField] private Orient m_Orient = Orient.Horizonal; [SerializeField] private Orient m_Orient = Orient.Horizonal;
[SerializeField] private Location m_Location = Location.defaultLeft; [SerializeField] private Location m_Location = Location.defaultLeft;
[SerializeField] private List<Color32> m_InRange = new List<Color32>(); [SerializeField] private List<Color32> m_InRange = new List<Color32>();
[SerializeField] private List<Color32> m_OutOfRange = new List<Color32>(); [SerializeField] private List<Color32> m_OutOfRange = new List<Color32>() { Color.gray };
[SerializeField] private List<Pieces> m_Pieces = new List<Pieces>();
/// <summary> /// <summary>
/// Whether enable visualMap component. /// Whether enable visualMap component.
@@ -125,14 +139,6 @@ namespace XCharts
set { if (PropertyUtil.SetStruct(ref m_Type, value)) SetVerticesDirty(); } set { if (PropertyUtil.SetStruct(ref m_Type, value)) SetVerticesDirty(); }
} }
/// <summary> /// <summary>
/// 映射方向。
/// </summary>
public Direction direction
{
get { return m_Direction; }
set { if (PropertyUtil.SetStruct(ref m_Direction, value)) SetVerticesDirty(); }
}
/// <summary>
/// the selected mode for Piecewise visualMap. /// the selected mode for Piecewise visualMap.
/// 选择模式。 /// 选择模式。
/// </summary> /// </summary>
@@ -311,6 +317,14 @@ namespace XCharts
get { return m_OutOfRange; } get { return m_OutOfRange; }
set { if (value != null) { m_OutOfRange = value; SetVerticesDirty(); } } set { if (value != null) { m_OutOfRange = value; SetVerticesDirty(); } }
} }
/// <summary>
/// 分段式每一段的相关配置。
/// </summary>
public List<Pieces> pieces
{
get { return m_Pieces; }
set { if (value != null) { m_Pieces = value; SetVerticesDirty(); } }
}
public override bool vertsDirty { get { return m_VertsDirty || location.anyDirty; } } public override bool vertsDirty { get { return m_VertsDirty || location.anyDirty; } }
internal override void ClearVerticesDirty() internal override void ClearVerticesDirty()
@@ -423,6 +437,32 @@ namespace XCharts
} }
public Color32 GetColor(float value) public Color32 GetColor(float value)
{
switch (type)
{
case Type.Continuous:
return GetContinuousColor(value);
case Type.Piecewise:
return GetPiecesColor(value);
default:
return ColorUtil.clearColor32;
}
}
private Color32 GetPiecesColor(float value)
{
foreach (var piece in m_Pieces)
{
if (piece.Contains(value, max - min))
{
return piece.color;
}
}
if (m_OutOfRange.Count > 0) return m_OutOfRange[0];
else return ChartConst.clearColor32;
}
private Color32 GetContinuousColor(float value)
{ {
if (value < m_Min || value > m_Max) if (value < m_Min || value > m_Max)
{ {

View File

@@ -17,19 +17,17 @@ namespace XCharts
if (!IsNeedGradient(visualMap) || !visualMap.autoMinMax) return; if (!IsNeedGradient(visualMap) || !visualMap.autoMinMax) return;
float min = 0; float min = 0;
float max = 0; float max = 0;
switch (visualMap.direction) if (visualMap.dimension == 0)
{ {
case VisualMap.Direction.Default: min = xAxis.IsCategory() ? 0 : xAxis.runtimeMinValue;
case VisualMap.Direction.X: max = xAxis.IsCategory() ? serie.dataCount - 1 : xAxis.runtimeMaxValue;
min = xAxis.IsCategory() ? 0 : xAxis.runtimeMinValue; SetMinMax(visualMap, min, max);
max = xAxis.IsCategory() ? serie.dataCount : xAxis.runtimeMaxValue; }
SetMinMax(visualMap, min, max); else
break; {
case VisualMap.Direction.Y: min = yAxis.IsCategory() ? 0 : yAxis.runtimeMinValue;
min = yAxis.IsCategory() ? 0 : yAxis.runtimeMinValue; max = yAxis.IsCategory() ? serie.dataCount - 1 : yAxis.runtimeMaxValue;
max = yAxis.IsCategory() ? serie.dataCount : yAxis.runtimeMaxValue; SetMinMax(visualMap, min, max);
SetMinMax(visualMap, min, max);
break;
} }
} }
@@ -54,55 +52,72 @@ namespace XCharts
{ {
startColor = ChartConst.clearColor32; startColor = ChartConst.clearColor32;
toColor = ChartConst.clearColor32; toColor = ChartConst.clearColor32;
switch (visualMap.direction) if (visualMap.dimension == 0)
{ {
case VisualMap.Direction.Default: startColor = visualMap.IsPiecewise() ? visualMap.GetColor(xValue) : visualMap.GetColor(xValue - 1);
case VisualMap.Direction.X: toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(xValue);
startColor = visualMap.IsPiecewise() ? visualMap.GetColor(xValue) : visualMap.GetColor(xValue - 1); }
toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(xValue); else
break; {
case VisualMap.Direction.Y: startColor = visualMap.IsPiecewise() ? visualMap.GetColor(yValue) : visualMap.GetColor(yValue - 1);
startColor = visualMap.IsPiecewise() ? visualMap.GetColor(yValue) : visualMap.GetColor(yValue - 1); toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(yValue);
toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(yValue);
break;
} }
} }
public static Color32 GetLineGradientColor(VisualMap visualMap, Vector3 pos, CoordinateChart chart, Axis axis, Color32 defaultColor) public static Color32 GetLineGradientColor(VisualMap visualMap, Vector3 pos, CoordinateChart chart, Axis axis,
Color32 defaultColor)
{ {
float value = 0; float value = 0;
switch (visualMap.direction) var min = 0f;
var max = 0f;
if (visualMap.dimension == 0)
{ {
case VisualMap.Direction.Default: min = axis.runtimeMinValue;
case VisualMap.Direction.X: max = axis.runtimeMaxValue;
var min = axis.runtimeMinValue; var grid = chart.GetAxisGridOrDefault(axis);
var max = axis.runtimeMaxValue; if (axis.IsCategory() && axis.boundaryGap)
var grid = chart.GetAxisGridOrDefault(axis); {
float startX = grid.runtimeX + axis.runtimeScaleWidth / 2;
value = (int)(min + (pos.x - startX) / (grid.runtimeWidth - axis.runtimeScaleWidth) * (max - min));
}
else
{
value = min + (pos.x - grid.runtimeX) / grid.runtimeWidth * (max - min); value = min + (pos.x - grid.runtimeX) / grid.runtimeWidth * (max - min);
break; }
case VisualMap.Direction.Y: }
if (axis is YAxis) else
{ {
var yAxis = chart.xAxes[axis.index]; Axis yAxis;
min = yAxis.runtimeMinValue; if (axis is YAxis)
max = yAxis.runtimeMaxValue; {
} yAxis = chart.xAxes[axis.index];
else min = yAxis.runtimeMinValue;
{ max = yAxis.runtimeMaxValue;
var yAxis = chart.yAxes[axis.index]; }
min = yAxis.runtimeMinValue; else
max = yAxis.runtimeMaxValue; {
} yAxis = chart.yAxes[axis.index];
grid = chart.GetAxisGridOrDefault(axis); min = yAxis.runtimeMinValue;
max = yAxis.runtimeMaxValue;
}
var grid = chart.GetAxisGridOrDefault(axis);
if (yAxis.IsCategory() && yAxis.boundaryGap)
{
float startY = grid.runtimeY + yAxis.runtimeScaleWidth / 2;
value = (int)(min + (pos.y - startY) / (grid.runtimeHeight - yAxis.runtimeScaleWidth) * (max - min));
}
else
{
value = min + (pos.y - grid.runtimeY) / grid.runtimeHeight * (max - min); value = min + (pos.y - grid.runtimeY) / grid.runtimeHeight * (max - min);
break; }
} }
var color = visualMap.GetColor(value); var color = visualMap.GetColor(value);
if (ChartHelper.IsClearColor(color)) return defaultColor; if (ChartHelper.IsClearColor(color)) return defaultColor;
else return color; else return color;
} }
public static Color32 GetItemStyleGradientColor(ItemStyle itemStyle, Vector3 pos, CoordinateChart chart, Axis axis, Color32 defaultColor) public static Color32 GetItemStyleGradientColor(ItemStyle itemStyle, Vector3 pos, CoordinateChart chart,
Axis axis, Color32 defaultColor)
{ {
var min = axis.runtimeMinValue; var min = axis.runtimeMinValue;
var max = axis.runtimeMaxValue; var max = axis.runtimeMaxValue;
@@ -114,7 +129,8 @@ namespace XCharts
else return color; else return color;
} }
public static Color32 GetLineStyleGradientColor(LineStyle lineStyle, Vector3 pos, CoordinateChart chart, Axis axis, Color32 defaultColor) public static Color32 GetLineStyleGradientColor(LineStyle lineStyle, Vector3 pos, CoordinateChart chart,
Axis axis, Color32 defaultColor)
{ {
var min = axis.runtimeMinValue; var min = axis.runtimeMinValue;
var max = axis.runtimeMaxValue; var max = axis.runtimeMaxValue;

View File

@@ -878,7 +878,7 @@ namespace XCharts
if (axis.IsCategory()) if (axis.IsCategory())
{ {
axis.runtimeMinValue = 0; axis.runtimeMinValue = 0;
axis.runtimeMaxValue = SeriesHelper.GetMaxSerieDataCount(m_Series); axis.runtimeMaxValue = SeriesHelper.GetMaxSerieDataCount(m_Series) - 1;
return; return;
} }
float tempMinValue = 0; float tempMinValue = 0;

View File

@@ -108,6 +108,7 @@ namespace XCharts
m_StackSerieData.Clear(); m_StackSerieData.Clear();
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData); if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
float scaleWid = AxisHelper.GetDataWidth(xAxis, grid.runtimeWidth, showData.Count, dataZoom); float scaleWid = AxisHelper.GetDataWidth(xAxis, grid.runtimeWidth, showData.Count, dataZoom);
xAxis.runtimeScaleWidth = scaleWid;
float startX = grid.runtimeX + (xAxis.boundaryGap ? scaleWid / 2 : 0); float startX = grid.runtimeX + (xAxis.boundaryGap ? scaleWid / 2 : 0);
int maxCount = serie.maxShow > 0 ? int maxCount = serie.maxShow > 0 ?
(serie.maxShow > showData.Count ? showData.Count : serie.maxShow) (serie.maxShow > showData.Count ? showData.Count : serie.maxShow)

View File

@@ -27,7 +27,6 @@ namespace XCharts
visualMap.enable = false; visualMap.enable = false;
visualMap.show = false; visualMap.show = false;
visualMap.autoMinMax = true; visualMap.autoMinMax = true;
visualMap.direction = VisualMap.Direction.Y;
visualMap.inRange.Clear(); visualMap.inRange.Clear();
visualMap.inRange.Add(Color.blue); visualMap.inRange.Add(Color.blue);
visualMap.inRange.Add(Color.red); visualMap.inRange.Add(Color.red);