mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-24 09:50:15 +00:00
增加LineChart可通过VisualMap或ItemStyle配置渐变#78
This commit is contained in:
@@ -764,11 +764,6 @@ namespace XCharts
|
||||
private void CheckMinMaxValue()
|
||||
{
|
||||
if (m_XAxises == null || m_YAxises == null) return;
|
||||
if (IsCategory())
|
||||
{
|
||||
m_CheckMinMaxValue = true;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < m_XAxises.Count; i++)
|
||||
{
|
||||
UpdateAxisMinMaxValue(i, m_XAxises[i]);
|
||||
@@ -781,7 +776,14 @@ namespace XCharts
|
||||
|
||||
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis, bool updateChart = true)
|
||||
{
|
||||
if (axis.IsCategory() || !axis.show) return;
|
||||
if (!axis.show) return;
|
||||
if (axis.IsCategory())
|
||||
{
|
||||
m_CheckMinMaxValue = true;
|
||||
axis.runtimeMinValue = 0;
|
||||
axis.runtimeMaxValue = SeriesHelper.GetMaxSerieDataCount(m_Series);
|
||||
return;
|
||||
}
|
||||
float tempMinValue = 0;
|
||||
float tempMaxValue = 0;
|
||||
|
||||
@@ -864,10 +866,12 @@ namespace XCharts
|
||||
DrawGrid(vh);
|
||||
for (int i = 0; i < m_XAxises.Count; i++)
|
||||
{
|
||||
m_XAxises[i].index = i;
|
||||
DrawXAxisSplit(vh, i, m_XAxises[i]);
|
||||
}
|
||||
for (int i = 0; i < m_YAxises.Count; i++)
|
||||
{
|
||||
m_YAxises[i].index = i;
|
||||
DrawYAxisSplit(vh, i, m_YAxises[i]);
|
||||
}
|
||||
for (int i = 0; i < m_XAxises.Count; i++)
|
||||
@@ -1494,8 +1498,7 @@ namespace XCharts
|
||||
|
||||
if (serie.type == SerieType.Heatmap)
|
||||
{
|
||||
dimension = m_VisualMap.enable && m_VisualMap.dimension > 0 ? m_VisualMap.dimension - 1 :
|
||||
serieData.data.Count - 1;
|
||||
dimension = VisualMapHelper.GetDimension(m_VisualMap, serieData.data.Count);
|
||||
}
|
||||
|
||||
SerieLabelHelper.ResetLabel(serieData, serieLabel, themeInfo, i);
|
||||
|
||||
@@ -145,8 +145,7 @@ namespace XCharts
|
||||
var dataIndex = i * yCount + j;
|
||||
if (dataIndex >= dataList.Count) continue;
|
||||
var serieData = dataList[dataIndex];
|
||||
var dimension = m_VisualMap.enable && m_VisualMap.dimension > 0 ? m_VisualMap.dimension - 1 :
|
||||
serieData.data.Count - 1;
|
||||
var dimension = VisualMapHelper.GetDimension(m_VisualMap, serieData.data.Count);
|
||||
if (serie.IsIgnoreIndex(dataIndex, dimension))
|
||||
{
|
||||
serie.dataPoints.Add(Vector3.zero);
|
||||
|
||||
@@ -220,6 +220,7 @@ namespace XCharts
|
||||
{
|
||||
lastNextPos = endPos;
|
||||
}
|
||||
VisualMapHelper.AutoSetLineMinMax(visualMap, serie, xAxis, yAxis);
|
||||
for (i = startIndex + 1; i < serie.dataPoints.Count; i++)
|
||||
{
|
||||
np = serie.dataPoints[i];
|
||||
@@ -641,10 +642,11 @@ namespace XCharts
|
||||
|
||||
private Vector3 stPos1, stPos2, lastDir, lastDnPos;
|
||||
private bool lastIsDown;
|
||||
private bool DrawNormalLine(VertexHelper vh, Serie serie, Axis axis, Vector3 lp,
|
||||
Vector3 np, Vector3 nnp, int dataIndex, Color lineColor, Color areaColor, Color areaToColor,
|
||||
private bool DrawNormalLine(VertexHelper vh, Serie serie, Axis axis, Vector3 lp, Vector3 np, Vector3 nnp,
|
||||
int dataIndex, Color lineColor, Color areaColor, Color areaToColor,
|
||||
Vector3 zeroPos, int startIndex = 0)
|
||||
{
|
||||
var defaultLineColor = lineColor;
|
||||
var isSecond = dataIndex == startIndex + 1;
|
||||
var isTheLastPos = np == nnp;
|
||||
bool isYAxis = axis is YAxis;
|
||||
@@ -715,6 +717,7 @@ namespace XCharts
|
||||
if (serie.animation.CheckDetailBreak(cp, isYAxis)) isBreak = true;
|
||||
var tp1 = cp - dir1v * serie.lineStyle.width;
|
||||
var tp2 = cp + dir1v * serie.lineStyle.width;
|
||||
CheckLineGradientColor(cp, serie.itemStyle, axis, defaultLineColor, ref lineColor);
|
||||
if (isDown)
|
||||
{
|
||||
if (!isBreak)
|
||||
@@ -849,7 +852,7 @@ namespace XCharts
|
||||
if (lastSerie != null)
|
||||
{
|
||||
var lastSmoothPoints = lastSerie.GetUpSmoothList(dataIndex);
|
||||
DrawStackArea(vh, serie, axis, smoothDownPoints, lastSmoothPoints, lineColor, areaColor, areaToColor);
|
||||
DrawStackArea(vh, serie, axis, smoothDownPoints, lastSmoothPoints, areaColor, areaToColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -985,6 +988,14 @@ namespace XCharts
|
||||
return !isBreak;
|
||||
}
|
||||
|
||||
private void CheckLineGradientColor(Vector3 cp, ItemStyle itemStyle, Axis axis, Color defaultLineColor, ref Color lineColor)
|
||||
{
|
||||
if (VisualMapHelper.IsNeedGradient(m_VisualMap))
|
||||
lineColor = VisualMapHelper.GetLineGradientColor(m_VisualMap, cp, this, axis, defaultLineColor);
|
||||
else if (itemStyle.IsNeedGradient())
|
||||
lineColor = VisualMapHelper.GetItemStyleGradientColor(itemStyle, cp, this, axis, defaultLineColor);
|
||||
}
|
||||
|
||||
private bool IsInRightOrUp(bool isYAxis, bool isLastDown, Vector3 dnPos, Vector3 checkPos)
|
||||
{
|
||||
if ((isLastDown && ((isYAxis && checkPos.y <= dnPos.y) || (!isYAxis && checkPos.x <= dnPos.x))) ||
|
||||
@@ -1082,6 +1093,7 @@ namespace XCharts
|
||||
Vector3 np, Vector3 llp, Vector3 nnp, int dataIndex, Color lineColor, Color areaColor,
|
||||
Color areaToColor, bool isStack, Vector3 zeroPos, int startIndex = 0)
|
||||
{
|
||||
var defaultLineColor = lineColor;
|
||||
bool isYAxis = xAxis is YAxis;
|
||||
var lineWidth = serie.lineStyle.width;
|
||||
var smoothPoints = serie.GetUpSmoothList(dataIndex);
|
||||
@@ -1096,6 +1108,7 @@ namespace XCharts
|
||||
{
|
||||
start = bezierPoints[i];
|
||||
to = bezierPoints[i + 1];
|
||||
CheckLineGradientColor(start, serie.itemStyle, xAxis, defaultLineColor, ref lineColor);
|
||||
CheckClipAndDrawLine(vh, start, to, lineWidth, lineColor, serie.clip);
|
||||
}
|
||||
return true;
|
||||
@@ -1118,6 +1131,7 @@ namespace XCharts
|
||||
{
|
||||
if (!serie.animation.IsInFadeOut())
|
||||
{
|
||||
CheckLineGradientColor(lp, serie.itemStyle, xAxis, defaultLineColor, ref lineColor);
|
||||
CheckClipAndDrawTriangle(vh, smoothStartPosUp, startUp, lp, lineColor, serie.clip);
|
||||
CheckClipAndDrawTriangle(vh, smoothStartPosDn, startDn, lp, lineColor, serie.clip);
|
||||
}
|
||||
@@ -1146,6 +1160,7 @@ namespace XCharts
|
||||
diff = dir1v * lineWidth;
|
||||
toUp = to - diff;
|
||||
toDn = to + diff;
|
||||
CheckLineGradientColor(to, serie.itemStyle, xAxis, defaultLineColor, ref lineColor);
|
||||
if (isYAxis) CheckClipAndDrawPolygon(vh, startDn, toDn, toUp, startUp, lineColor, serie.clip);
|
||||
else CheckClipAndDrawPolygon(vh, startUp, toUp, toDn, startDn, lineColor, serie.clip);
|
||||
smoothPoints.Add(toUp);
|
||||
@@ -1209,7 +1224,7 @@ namespace XCharts
|
||||
if (lastSerie != null)
|
||||
{
|
||||
var lastSmoothPoints = lastSerie.GetUpSmoothList(dataIndex);
|
||||
DrawStackArea(vh, serie, xAxis, smoothDownPoints, lastSmoothPoints, lineColor, areaColor, areaToColor);
|
||||
DrawStackArea(vh, serie, xAxis, smoothDownPoints, lastSmoothPoints, areaColor, areaToColor);
|
||||
}
|
||||
}
|
||||
return isFinish;
|
||||
@@ -1241,7 +1256,7 @@ namespace XCharts
|
||||
}
|
||||
|
||||
private void DrawStackArea(VertexHelper vh, Serie serie, Axis axis, List<Vector3> smoothPoints,
|
||||
List<Vector3> lastSmoothPoints, Color lineColor, Color areaColor, Color areaToColor)
|
||||
List<Vector3> lastSmoothPoints, Color areaColor, Color areaToColor)
|
||||
{
|
||||
if (!serie.areaStyle.show || lastSmoothPoints.Count <= 0) return;
|
||||
Vector3 start, to;
|
||||
|
||||
@@ -470,5 +470,15 @@ namespace XCharts
|
||||
maxValue = max > 1 ? Mathf.CeilToInt(max) : max;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetMaxSerieDataCount(Series series)
|
||||
{
|
||||
int max = 0;
|
||||
foreach (var serie in series.list)
|
||||
{
|
||||
if (serie.dataCount > max) max = serie.dataCount;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Runtime/Internal/Helper/VisualMapHelper.cs
Normal file
130
Runtime/Internal/Helper/VisualMapHelper.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public static class VisualMapHelper
|
||||
{
|
||||
public static void AutoSetLineMinMax(VisualMap visualMap, Serie serie, XAxis xAxis, YAxis yAxis)
|
||||
{
|
||||
if (!IsNeedGradient(visualMap) || !visualMap.autoMinMax) return;
|
||||
float min = 0;
|
||||
float max = 0;
|
||||
switch (visualMap.direction)
|
||||
{
|
||||
case VisualMap.Direction.Default:
|
||||
case VisualMap.Direction.X:
|
||||
min = xAxis.IsCategory() ? 0 : xAxis.runtimeMinValue;
|
||||
max = xAxis.IsCategory() ? serie.dataCount : xAxis.runtimeMaxValue;
|
||||
SetMinMax(visualMap, min, max);
|
||||
break;
|
||||
case VisualMap.Direction.Y:
|
||||
min = yAxis.IsCategory() ? 0 : yAxis.runtimeMinValue;
|
||||
max = yAxis.IsCategory() ? serie.dataCount : yAxis.runtimeMaxValue;
|
||||
SetMinMax(visualMap, min, max);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetMinMax(VisualMap visualMap, float min, float max)
|
||||
{
|
||||
if (visualMap.enable && (visualMap.min != min || visualMap.max != max))
|
||||
{
|
||||
//Debug.LogError("minmax:"+min+","+max);
|
||||
if (max >= min)
|
||||
{
|
||||
visualMap.min = min;
|
||||
visualMap.max = max;
|
||||
//Debug.LogError("minmax2222:"+visualMap.min+","+visualMap.max);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("SetMinMax:max < min:" + min + "," + max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void GetLineGradientColor(VisualMap visualMap, float xValue, float yValue,
|
||||
out Color startColor, out Color toColor)
|
||||
{
|
||||
startColor = Color.clear;
|
||||
toColor = Color.clear;
|
||||
switch (visualMap.direction)
|
||||
{
|
||||
case VisualMap.Direction.Default:
|
||||
case VisualMap.Direction.X:
|
||||
startColor = visualMap.IsPiecewise() ? visualMap.GetColor(xValue) : visualMap.GetColor(xValue - 1);
|
||||
toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(xValue);
|
||||
break;
|
||||
case VisualMap.Direction.Y:
|
||||
startColor = visualMap.IsPiecewise() ? visualMap.GetColor(yValue) : visualMap.GetColor(yValue - 1);
|
||||
toColor = visualMap.IsPiecewise() ? startColor : visualMap.GetColor(yValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Color GetLineGradientColor(VisualMap visualMap, Vector3 pos, CoordinateChart chart, Axis axis, Color defaultColor)
|
||||
{
|
||||
float value = 0;
|
||||
switch (visualMap.direction)
|
||||
{
|
||||
case VisualMap.Direction.Default:
|
||||
case VisualMap.Direction.X:
|
||||
var min = axis.runtimeMinValue;
|
||||
var max = axis.runtimeMaxValue;
|
||||
value = min + (pos.x - chart.coordinateX) / chart.coordinateWidth * (max - min);
|
||||
break;
|
||||
case VisualMap.Direction.Y:
|
||||
if (axis is YAxis)
|
||||
{
|
||||
var yAxis = chart.xAxises[axis.index];
|
||||
min = yAxis.runtimeMinValue;
|
||||
max = yAxis.runtimeMaxValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
var yAxis = chart.yAxises[axis.index];
|
||||
min = yAxis.runtimeMinValue;
|
||||
max = yAxis.runtimeMaxValue;
|
||||
}
|
||||
value = min + (pos.y - chart.coordinateY) / chart.coordinateHeight * (max - min);
|
||||
break;
|
||||
}
|
||||
var color = visualMap.GetColor(value);
|
||||
if (ChartHelper.IsClearColor(color)) return defaultColor;
|
||||
else return color;
|
||||
}
|
||||
|
||||
internal static Color GetItemStyleGradientColor(ItemStyle itemStyle, Vector3 pos, CoordinateChart chart, Axis axis, Color defaultColor)
|
||||
{
|
||||
var min = axis.runtimeMinValue;
|
||||
var max = axis.runtimeMaxValue;
|
||||
var value = min + (pos.x - chart.coordinateX) / chart.coordinateWidth * (max - min);
|
||||
var rate = (value - min) / (max - min);
|
||||
var color = itemStyle.GetGradientColor(rate, defaultColor);
|
||||
if (ChartHelper.IsClearColor(color)) return defaultColor;
|
||||
else return color;
|
||||
}
|
||||
|
||||
public static bool IsNeedGradient(VisualMap visualMap)
|
||||
{
|
||||
if (!visualMap.enable || visualMap.inRange.Count <= 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int GetDimension(VisualMap visualMap, int serieDataCount)
|
||||
{
|
||||
var dimension = visualMap.enable && visualMap.dimension >= 0 ? visualMap.dimension : serieDataCount - 1;
|
||||
if (dimension > serieDataCount - 1) dimension = serieDataCount - 1;
|
||||
return dimension;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user