增加LineChart的普通折线图可通过设置ingore参数过滤数据的支持

This commit is contained in:
monitor1394
2020-03-10 09:12:47 +08:00
parent d010c9d986
commit 705652d50c
9 changed files with 147 additions and 30 deletions

View File

@@ -254,6 +254,8 @@ namespace XCharts
[SerializeField] private bool m_ShowDataName;
[SerializeField] private bool m_ShowDataIcon;
[SerializeField] private bool m_Clip = true;
[SerializeField] private bool m_Ingore = true;
[SerializeField] private float m_IngoreValue = 0;
[SerializeField] private List<SerieData> m_Data = new List<SerieData>();
@@ -609,6 +611,22 @@ namespace XCharts
set { if (PropertyUtility.SetStruct(ref m_RoundCap, value)) SetVerticesDirty(); }
}
/// <summary>
/// 是否开启忽略数据。当为 true 时,数据值为 ingoreValue 时不进行绘制。
/// </summary>
public bool ingore
{
get { return m_Ingore; }
set { if (PropertyUtility.SetStruct(ref m_Ingore, value)) SetVerticesDirty(); }
}
/// <summary>
/// 忽略数据的默认值。当ingore为true才有效。
/// </summary>
public float ingoreValue
{
get { return m_IngoreValue; }
set { if (PropertyUtility.SetStruct(ref m_IngoreValue, value)) SetVerticesDirty(); }
}
/// <summary>
/// 仪表盘轴线。
/// </summary>
public GaugeAxis gaugeAxis
@@ -1538,6 +1556,11 @@ namespace XCharts
return false;
}
public bool IsIngoreValue(float value)
{
return m_Ingore && Mathf.Approximately(value, m_IngoreValue);
}
/// <summary>
/// 更新运行时中心点和半径
/// </summary>

View File

@@ -61,6 +61,7 @@ namespace XCharts
[SerializeField] private bool m_ForceENotation = false;
[SerializeField] private float m_PaddingLeftRight = 5f;
[SerializeField] private float m_PaddingTopBottom = 5f;
[SerializeField] private string m_IngoreDataDefaultContent = "-";
[SerializeField] private Sprite m_BackgroundImage;
[SerializeField] private TextStyle m_TextStyle = new TextStyle(18, FontStyle.Normal);
[SerializeField] private LineStyle m_LineStyle = new LineStyle(LineStyle.Type.Solid, 0.7f);
@@ -166,6 +167,10 @@ namespace XCharts
/// </summary>
public float paddingTopBottom { get { return m_PaddingTopBottom; } set { m_PaddingTopBottom = value; } }
/// <summary>
/// 被忽略数据的默认显示字符信息。
/// </summary>
public string ingoreDataDefaultContent { get { return m_IngoreDataDefaultContent; } set { m_IngoreDataDefaultContent = value; } }
/// <summary>
/// The image of icon.
/// 图标的图片。
/// </summary>

View File

@@ -421,6 +421,7 @@ namespace XCharts
string key = serie.name;
float xValue, yValue;
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
var isIngore = ChartHelper.IsIngore(serie.dataPoints[index]);
if (isCartesian)
{
var serieData = serie.GetSerieData(index, m_DataZoom);
@@ -433,10 +434,12 @@ namespace XCharts
}
else
{
var valueTxt = isIngore ? m_Tooltip.ingoreDataDefaultContent :
ChartCached.FloatToStr(yValue, 0, m_Tooltip.forceENotation);
sb.Append("\n")
.Append("<color=#").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
.Append(ChartCached.FloatToStr(yValue, 0, m_Tooltip.forceENotation));
.Append(valueTxt);
}
}
}
@@ -1438,7 +1441,8 @@ namespace XCharts
for (int j = 0; j < serie.data.Count; j++)
{
var serieData = serie.data[j];
if (serie.label.show || serieData.iconStyle.show)
var isIngore = ChartHelper.IsIngore(serie.dataPoints[j]);
if ((serie.label.show || serieData.iconStyle.show) && !isIngore)
{
var pos = serie.dataPoints[j];
var value = serieData.data[1];

View File

@@ -33,6 +33,7 @@ namespace XCharts
if (serie.lineArrow.position == LineArrow.Position.End && i == count - 1) continue;
}
Vector3 p = serie.dataPoints[i];
if (ChartHelper.IsIngore(p)) continue;
bool highlight = (m_Tooltip.show && m_Tooltip.IsSelected(i))
|| serie.data[i].highlighted || serie.highlighted;
float symbolSize = highlight ? serie.symbol.selectedSize : serie.symbol.size;
@@ -123,11 +124,18 @@ namespace XCharts
{
for (int j = 0; j < rate; j++) seriesHig.Add(0);
}
float yValue = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage,
i, dataChangeDuration, ref dataChanging);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np,
dataChangeDuration);
serie.dataPoints.Add(np);
if (serie.IsIngoreValue(showData[i].GetData(1)))
{
serie.dataPoints.Add(Vector3.zero);
}
else
{
float yValue = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage,
i, dataChangeDuration, ref dataChanging);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np,
dataChangeDuration);
serie.dataPoints.Add(np);
}
}
if (dataChanging)
{
@@ -137,10 +145,17 @@ namespace XCharts
{
i = maxCount - 1;
seriesHig.Add(0);
float yValue = showData[i].GetCurrData(1, dataChangeDuration);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np,
dataChangeDuration);
serie.dataPoints.Add(np);
if (serie.IsIngoreValue(showData[i].GetData(1)))
{
serie.dataPoints.Add(Vector3.zero);
}
else
{
float yValue = showData[i].GetCurrData(1, dataChangeDuration);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np,
dataChangeDuration);
serie.dataPoints.Add(np);
}
}
if (serie.dataPoints.Count <= 0)
{
@@ -156,8 +171,15 @@ namespace XCharts
if (serie.minShow > 0 && serie.minShow < showData.Count)
{
i = serie.minShow - 1;
float yValue = showData[i].GetCurrData(1, dataChangeDuration);
GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref firstLastPos, dataChangeDuration);
if (serie.IsIngoreValue(showData[i].GetData(1)))
{
serie.dataPoints.Add(Vector3.zero);
}
else
{
float yValue = showData[i].GetCurrData(1, dataChangeDuration);
GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref firstLastPos, dataChangeDuration);
}
}
else
{
@@ -166,8 +188,15 @@ namespace XCharts
if (serie.maxShow > 0 && serie.maxShow < showData.Count)
{
i = serie.maxShow;
float yValue = showData[i].GetCurrData(1, dataChangeDuration);
GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref lastNextPos, dataChangeDuration);
if (serie.IsIngoreValue(showData[i].GetData(1)))
{
serie.dataPoints.Add(Vector3.zero);
}
else
{
float yValue = showData[i].GetCurrData(1, dataChangeDuration);
GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, 0, ref lastNextPos, dataChangeDuration);
}
}
else
{
@@ -177,7 +206,8 @@ namespace XCharts
{
np = serie.dataPoints[i];
serie.ClearSmoothList(i);
if (!serie.animation.NeedAnimation(i)) break;
//if(np == Vector3.zero) continue;
//if (!serie.animation.NeedAnimation(i)) break;
bool isFinish = true;
if (serie.areaStyle.tooltipHighlight && m_Tooltip.show && i <= m_Tooltip.runtimeDataIndex[0])
{
@@ -192,21 +222,24 @@ namespace XCharts
switch (serie.lineType)
{
case LineType.Normal:
nnp = i < serie.dataPoints.Count - 1 ? serie.dataPoints[i + 1] : np;
nnp = GetNNPos(serie.dataPoints, i, np);
isFinish = DrawNormalLine(vh, serie, xAxis, lp, np, nnp, i, lineColor,
areaColor, areaToColor, zeroPos);
break;
case LineType.Smooth:
case LineType.SmoothDash:
llp = i > 1 ? serie.dataPoints[i - 2] : firstLastPos;
nnp = i < serie.dataPoints.Count - 1 ? serie.dataPoints[i + 1] : lastNextPos;
//llp = i > 1 ? serie.dataPoints[i - 2] : firstLastPos;
//nnp = i < serie.dataPoints.Count - 1 ? serie.dataPoints[i + 1] : lastNextPos;
llp = GetLLPos(serie.dataPoints, i, firstLastPos);
nnp = GetNNPos(serie.dataPoints, i, lastNextPos);
isFinish = DrawSmoothLine(vh, serie, xAxis, lp, np, llp, nnp, i,
lineColor, areaColor, areaToColor, isStack, zeroPos);
break;
case LineType.StepStart:
case LineType.StepMiddle:
case LineType.StepEnd:
nnp = i < serie.dataPoints.Count - 1 ? serie.dataPoints[i + 1] : np;
//nnp = i < serie.dataPoints.Count - 1 ? serie.dataPoints[i + 1] : np;
nnp = GetNNPos(serie.dataPoints, i, np);
isFinish = DrawStepLine(vh, serie, xAxis, lp, np, nnp, i, lineColor,
areaColor, areaToColor, zeroPos);
break;
@@ -218,7 +251,7 @@ namespace XCharts
break;
}
if (isFinish) serie.animation.SetDataFinish(i);
lp = np;
if (np != Vector3.zero) lp = np;
}
if (!serie.animation.IsFinish())
{
@@ -229,6 +262,37 @@ namespace XCharts
}
}
private Vector3 GetNNPos(List<Vector3> dataPoints, int index, Vector3 np)
{
int size = dataPoints.Count;
if (index >= size) return np;
for (int i = index + 1; i < size; i++)
{
if (dataPoints[i] != Vector3.zero) return dataPoints[i];
}
return np;
}
private Vector3 GetLastPos(List<Vector3> dataPoints, int index, Vector3 pos)
{
if (index <= 0) return pos;
for (int i = index - 1; i > 0; i--)
{
if (dataPoints[i] != Vector3.zero) return dataPoints[i];
}
return pos;
}
private Vector3 GetLLPos(List<Vector3> dataPoints, int index, Vector3 lp)
{
if (index <= 1) return lp;
for (int i = index - 2; i > 0; i--)
{
if (dataPoints[i] != Vector3.zero) return dataPoints[i];
}
return lp;
}
private float DataAverage(ref List<SerieData> showData, SampleType sampleType, int minCount, int maxCount, int rate)
{
var totalAverage = 0f;
@@ -303,8 +367,13 @@ namespace XCharts
}
private float GetDataPoint(Axis xAxis, Axis yAxis, List<SerieData> showData, float yValue, float startX, int i,
float scaleWid, float serieHig, ref Vector3 np, float duration)
float scaleWid, float serieHig, ref Vector3 np, float duration, bool isIngoreValue = false)
{
if (isIngoreValue)
{
np = Vector3.zero;
return 0;
}
float xDataHig, yDataHig;
float xMinValue = xAxis.GetCurrMinValue(duration);
float xMaxValue = xAxis.GetCurrMaxValue(duration);
@@ -355,7 +424,6 @@ namespace XCharts
else yDataHig = (yValue - yMinValue) / (yMaxValue - yMinValue) * coordinateHeight;
}
np = new Vector3(pX, pY + yDataHig);
}
return yDataHig;
}

View File

@@ -21,8 +21,15 @@ namespace XCharts
public static class ChartHelper
{
private static StringBuilder s_Builder = new StringBuilder();
private static Vector3 s_DefaultIngoreDataVector3 = Vector3.zero;
public static StringBuilder sb { get { return s_Builder; } }
public static Vector3 ingoreVector3 { get { return s_DefaultIngoreDataVector3; } }
public static bool IsIngore(Vector3 pos)
{
return pos == s_DefaultIngoreDataVector3;
}
public static string Cancat(string str1, string str2)
{
s_Builder.Length = 0;
@@ -93,12 +100,12 @@ namespace XCharts
#if UNITY_2019_1_OR_NEWER
#else
if (parent == null) return;
#if UNITY_EDITOR && UNITY_2018_3_OR_NEWER
#if UNITY_EDITOR && UNITY_2018_3_OR_NEWER
if (PrefabUtility.IsPartOfAnyPrefab(parent.gameObject))
{
return;
}
#endif
#endif
while (parent.childCount > 0)
{
var go = parent.GetChild(0);