增加AreaStyletoTop参数可设置折线图渐变色是到顶部还是到实际位置

This commit is contained in:
monitor1394
2022-12-08 07:32:33 +08:00
parent 927644fb10
commit d4a3886530
9 changed files with 79 additions and 59 deletions

View File

@@ -38,6 +38,7 @@ namespace XCharts.Runtime
[SerializeField] private Color32 m_ToColor;
[SerializeField][Range(0, 1)] private float m_Opacity = 0.6f;
[SerializeField][Since("v3.2.0")] private bool m_InnerFill;
[SerializeField][Since("v3.6.0")] private bool m_ToTop = true;
/// <summary>
/// Set this to false to prevent the areafrom showing.
@@ -93,6 +94,16 @@ namespace XCharts.Runtime
get { return m_InnerFill; }
set { if (PropertyUtil.SetStruct(ref m_InnerFill, value)) SetVerticesDirty(); }
}
/// <summary>
/// Whether to fill the gradient color to the top. The default is true, which means that the gradient color is filled to the top.
/// If it is false, the gradient color is filled to the actual position.
/// |渐变色是到顶部还是到实际位置。默认为true到顶部。
/// </summary>
public bool toTop
{
get { return m_ToTop; }
set { if (PropertyUtil.SetStruct(ref m_ToTop, value)) SetVerticesDirty(); }
}
public Color32 GetColor()
{

View File

@@ -25,8 +25,8 @@ namespace XCharts.Runtime
ThemeStyle theme, VisualMap visualMap, bool isY, Axis axis, Axis relativedAxis, GridCoord grid)
{
Color32 areaColor, areaToColor;
bool innerFill;
if (!SerieHelper.GetAreaColor(out areaColor, out areaToColor, out innerFill, serie, null, theme, serie.context.colorIndex))
bool innerFill, toTop;
if (!SerieHelper.GetAreaColor(out areaColor, out areaToColor, out innerFill, out toTop, serie, null, theme, serie.context.colorIndex))
{
return;
}
@@ -47,7 +47,8 @@ namespace XCharts.Runtime
visualMap,
axis,
relativedAxis,
grid);
grid,
toTop);
}
else
{
@@ -57,13 +58,14 @@ namespace XCharts.Runtime
gridXY + (isY ? grid.context.width : grid.context.height),
areaColor,
areaToColor,
visualMap);
visualMap,
toTop);
}
}
private static void DrawSerieLineNormalArea(VertexHelper vh, Serie serie, bool isY,
float zero, float min, float max, Color32 areaColor, Color32 areaToColor,
VisualMap visualMap, Axis axis, Axis relativedAxis, GridCoord grid)
VisualMap visualMap, Axis axis, Axis relativedAxis, GridCoord grid, bool toTop)
{
var points = serie.context.drawPoints;
var count = points.Count;
@@ -117,7 +119,7 @@ namespace XCharts.Runtime
if (UGLHelper.GetIntersection(lp, tp, zsp, zep, ref ip))
{
if (lerp)
AddVertToVertexHelperWithLerpColor(vh, ip, ip, color, toColor, isY, min, max, i > 0);
AddVertToVertexHelperWithLerpColor(vh, ip, ip, color, toColor, isY, min, max, i > 0, toTop);
else
{
if (lastDataIsIgnore)
@@ -133,7 +135,7 @@ namespace XCharts.Runtime
}
if (lerp)
AddVertToVertexHelperWithLerpColor(vh, tp, zp, color, toColor, isY, min, max, i > 0);
AddVertToVertexHelperWithLerpColor(vh, tp, zp, color, toColor, isY, min, max, i > 0, toTop);
else
{
if (lastDataIsIgnore)
@@ -152,7 +154,7 @@ namespace XCharts.Runtime
}
private static void DrawSerieLineStackArea(VertexHelper vh, Serie serie, Serie lastStackSerie, bool isY,
float zero, float min, float max, Color32 color, Color32 toColor, VisualMap visualMap)
float zero, float min, float max, Color32 color, Color32 toColor, VisualMap visualMap, bool toTop)
{
if (lastStackSerie == null)
return;
@@ -170,7 +172,7 @@ namespace XCharts.Runtime
var lbp = downPoints[0].position;
if (lerp)
AddVertToVertexHelperWithLerpColor(vh, ltp, lbp, color, toColor, isY, min, max, false);
AddVertToVertexHelperWithLerpColor(vh, ltp, lbp, color, toColor, isY, min, max, false, toTop);
else
UGL.AddVertToVertexHelper(vh, ltp, lbp, color, false);
@@ -216,7 +218,7 @@ namespace XCharts.Runtime
}
if (lerp)
AddVertToVertexHelperWithLerpColor(vh, tp, bp, color, toColor, isY, min, max, true);
AddVertToVertexHelperWithLerpColor(vh, tp, bp, color, toColor, isY, min, max, true, toTop);
else
UGL.AddVertToVertexHelper(vh, tp, bp, color, true);
u++;
@@ -234,12 +236,20 @@ namespace XCharts.Runtime
}
private static void AddVertToVertexHelperWithLerpColor(VertexHelper vh, Vector3 tp, Vector3 bp,
Color32 color, Color32 toColor, bool isY, float min, float max, bool needTriangle)
Color32 color, Color32 toColor, bool isY, float min, float max, bool needTriangle, bool toTop)
{
var range = max - min;
var color1 = Color32.Lerp(color, toColor, ((isY ? tp.x : tp.y) - min) / range);
var color2 = Color32.Lerp(color, toColor, ((isY ? bp.x : bp.y) - min) / range);
UGL.AddVertToVertexHelper(vh, tp, bp, color1, color2, needTriangle);
if (toTop)
{
var range = max - min;
var color1 = Color32.Lerp(color, toColor, ((isY ? tp.x : tp.y) - min) / range);
var color2 = Color32.Lerp(color, toColor, ((isY ? bp.x : bp.y) - min) / range);
UGL.AddVertToVertexHelper(vh, tp, bp, color1, color2, needTriangle);
}
else
{
UGL.AddVertToVertexHelper(vh, tp, bp, toColor, color, needTriangle);
}
}
internal static void DrawSerieLine(VertexHelper vh, ThemeStyle theme, Serie serie, VisualMap visualMap,

View File

@@ -542,16 +542,17 @@ namespace XCharts.Runtime
public static bool GetAreaColor(out Color32 color, out Color32 toColor,
Serie serie, SerieData serieData, ThemeStyle theme, int index)
{
bool fill;
return GetAreaColor(out color, out toColor, out fill, serie, serieData, theme, index);
bool fill, toTop;
return GetAreaColor(out color, out toColor, out fill, out toTop, serie, serieData, theme, index);
}
public static bool GetAreaColor(out Color32 color, out Color32 toColor, out bool innerFill,
Serie serie, SerieData serieData, ThemeStyle theme, int index)
out bool toTop, Serie serie, SerieData serieData, ThemeStyle theme, int index)
{
color = ChartConst.clearColor32;
toColor = ChartConst.clearColor32;
innerFill = false;
toTop = true;
var state = GetSerieState(serie, serieData);
var stateStyle = GetStateStyle(serie, serieData, state);
if (stateStyle == null)
@@ -559,6 +560,7 @@ namespace XCharts.Runtime
var areaStyle = GetAreaStyle(serie, serieData);
if (areaStyle == null || !areaStyle.show) return false;
innerFill = areaStyle.innerFill;
toTop = areaStyle.toTop;
GetColor(ref color, areaStyle.color, serie.itemStyle.color, areaStyle.opacity, theme, index);
GetColor(ref toColor, areaStyle.toColor, color, areaStyle.opacity, theme, index);
switch (state)
@@ -584,6 +586,7 @@ namespace XCharts.Runtime
if (stateStyle.areaStyle.show)
{
innerFill = stateStyle.areaStyle.innerFill;
toTop = stateStyle.areaStyle.toTop;
GetColor(ref color, stateStyle.areaStyle.color, stateStyle.itemStyle.color, stateStyle.areaStyle.opacity, theme, index);
GetColor(ref color, stateStyle.areaStyle.toColor, color, stateStyle.areaStyle.opacity, theme, index);
}