增加Serie采样类型sampleType的相关配置

This commit is contained in:
monitor1394
2019-09-30 12:43:26 +08:00
parent 91bee0fb81
commit 292d67fe49
3 changed files with 116 additions and 4 deletions

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.Serialization;
namespace XCharts
{
@@ -111,6 +110,33 @@ namespace XCharts
DashDotDot
}
/// <summary>
/// 采样类型
/// </summary>
public enum SampleType
{
/// <summary>
/// 取峰值。
/// </summary>
Peak,
/// <summary>
/// 取过滤点的平均值。
/// </summary>
Average,
/// <summary>
/// 取过滤点的最大值。
/// </summary>
Max,
/// <summary>
/// 取过滤点的最小值。
/// </summary>
Min,
/// <summary>
/// 取过滤点的和。
/// </summary>
Sum
}
/// <summary>
/// 系列。每个系列通过 type 决定自己的图表类型。
/// </summary>
@@ -130,6 +156,9 @@ namespace XCharts
[SerializeField] private SerieSymbol m_Symbol = new SerieSymbol();
[SerializeField] private LineType m_LineType = LineType.Normal;
[SerializeField] private float m_SampleDist = 0;
[SerializeField] private SampleType m_SampleType = SampleType.Average;
[SerializeField] private float m_SampleAverage = 0;
[SerializeField] private LineStyle m_LineStyle = new LineStyle();
[SerializeField] private float m_BarWidth = 0.6f;
[SerializeField] private float m_BarGap = 0.3f; // 30%
@@ -242,6 +271,16 @@ namespace XCharts
/// <value></value>
public float sampleDist { get { return m_SampleDist; } set { m_SampleDist = value < 0 ? 0 : value; } }
/// <summary>
/// the type of sample.
/// 采样类型。当sampleDist大于0时有效。
/// </summary>
public SampleType sampleType { get { return m_SampleType; } set { m_SampleType = value; } }
/// <summary>
/// 设定的采样平均值。当sampleType 为 Peak 时用于和过滤数据的平均值做对比是取最大值还是最小值。默认为0时会实时计算所有数据的平均值。
/// </summary>
/// <value></value>
public float sampleAverage { get { return m_SampleAverage; } set { m_SampleAverage = value; } }
/// <summary>
/// The style of line.
/// 线条样式。
/// </summary>

View File

@@ -101,6 +101,8 @@ namespace XCharts
if (sampleDist > 0) rate = (int)((maxCount - serie.minShow) / (coordinateWid / sampleDist));
if (rate < 1) rate = 1;
var includeLastData = false;
var totalAverage = serie.sampleAverage > 0 ? serie.sampleAverage :
DataAverage(ref showData, serie.sampleType, serie.minShow, maxCount, rate);
for (i = serie.minShow; i < maxCount; i += rate)
{
if (i == maxCount - 1) includeLastData = true;
@@ -108,7 +110,7 @@ namespace XCharts
{
for (int j = 0; j < rate; j++) seriesHig.Add(0);
}
float yValue = showData[i].data[1];
float yValue = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage, i);
seriesHig[i] += GetDataPoint(xAxis, yAxis, showData, yValue, startX, i, scaleWid, seriesHig[i], ref np);
serie.dataPoints.Add(np);
}
@@ -186,6 +188,71 @@ namespace XCharts
}
}
private float DataAverage(ref List<SerieData> showData, SampleType sampleType, int minCount, int maxCount, int rate)
{
var totalAverage = 0f;
if (rate > 1 && sampleType == SampleType.Peak)
{
var total = 0f;
for (int i = minCount; i < maxCount; i++)
{
total += showData[i].data[1];
}
totalAverage = total / (maxCount - minCount);
}
return totalAverage;
}
private float SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate,
int minCount, int maxCount, float totalAverage, int index)
{
if (rate <= 1 || index == minCount) return showData[index].data[1];
switch (sampleType)
{
case SampleType.Sum:
case SampleType.Average:
float total = 0;
for (int i = index; i > index - rate; i--)
{
total += showData[i].data[1];
}
if (sampleType == SampleType.Average) return total / rate;
else return total;
case SampleType.Max:
float max = float.MinValue;
for (int i = index; i > index - rate; i--)
{
var value = showData[i].data[1];
if (value > max) max = value;
}
return max;
case SampleType.Min:
float min = float.MaxValue;
for (int i = index; i > index - rate; i--)
{
var value = showData[i].data[1];
if (value < min) min = value;
}
return min;
case SampleType.Peak:
max = float.MinValue;
min = float.MaxValue;
total = 0;
for (int i = index; i > index - rate; i--)
{
var value = showData[i].data[1];
total += value;
if (value < min) min = value;
if (value > max) max = value;
}
var average = total / rate;
if (average >= totalAverage) return max;
else return min;
}
return showData[index].data[1];
}
private float GetDetailProgress(Serie serie, Axis axis, Vector3 lp, Vector3 np, Vector3 llp,
Vector3 nnp, bool fine)
{