重构LineChart和BarChart,移除Line和Bar组件,参数统一放到Serie中配置。

This commit is contained in:
monitor1394
2019-08-15 21:44:30 +08:00
parent 52ee1fe788
commit 3e506f9576
32 changed files with 13681 additions and 21508 deletions

View File

@@ -631,7 +631,7 @@ namespace XCharts
{
public XAxis Clone()
{
var axis = new XAxis();
var axis = XAxisPool.Get();
axis.show = show;
axis.type = type;
axis.min = min;
@@ -644,7 +644,7 @@ namespace XCharts
axis.axisName.Copy(axisName);
axis.axisLabel.Copy(axisLabel);
axis.data.Clear();
axis.data.Capacity = data.Count;
if (axis.data.Capacity < data.Count) axis.data.Capacity = data.Count;
foreach (var d in data) axis.data.Add(d);
return axis;
}
@@ -683,7 +683,7 @@ namespace XCharts
{
public YAxis Clone()
{
var axis = new YAxis();
var axis = YAxisPool.Get();
axis.show = show;
axis.type = type;
axis.min = min;
@@ -696,7 +696,7 @@ namespace XCharts
axis.axisName.Copy(axisName);
axis.axisLabel.Copy(axisLabel);
axis.data.Clear();
axis.data.Capacity = data.Count;
if (axis.data.Capacity < data.Count) axis.data.Capacity = data.Count;
foreach (var d in data) axis.data.Add(d);
return axis;
}

View File

@@ -1,45 +0,0 @@
using UnityEngine;
namespace XCharts
{
/// <summary>
/// bar component.global setting for bar chart.
/// 柱状图的全局配置组件。
/// </summary>
[System.Serializable]
public class Bar
{
[SerializeField] private bool m_InSameBar = false;
[SerializeField] private float m_BarWidth = 0.7f;
[SerializeField] private float m_Space = 10;
/// <summary>
/// Whether to draw all bar in the same bar,but not stacked.
/// 非堆叠同柱。多序列绘制在同一bar上但不堆叠而是覆盖绘制。
/// </summary>
public bool inSameBar { get { return m_InSameBar; } set { m_InSameBar = value; } }
/// <summary>
/// the width of bar.
/// 状态的宽度。
/// </summary>
public float barWidth { get { return m_BarWidth; } set { m_BarWidth = value; } }
/// <summary>
/// the space of bars.
/// 多柱状间的间距。
/// </summary>
public float space { get { return m_Space; } set { m_Space = value; } }
public static Bar defaultBar
{
get
{
return new Bar()
{
m_InSameBar = false,
m_BarWidth = 0.6f,
m_Space = 10
};
}
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 4f4f93f97ce4c4274a4153986a3e5752
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,86 +0,0 @@
using UnityEngine;
namespace XCharts
{
/// <summary>
/// The global settings of line chart.
/// LineChart的全局配置。
/// </summary>
[System.Serializable]
public class Line
{
/// <summary>
/// the type fo step line.
/// 阶梯线图类型。
/// </summary>
public enum StepType
{
/// <summary>
/// 当前点。
/// </summary>
Start,
/// <summary>
/// 当前点和下一个点的中间。
/// </summary>
Middle,
/// <summary>
/// 下一个拐点
/// </summary>
End
}
[SerializeField] private float m_Tickness;
[SerializeField] private bool m_Smooth;
[SerializeField] [Range(1f, 10f)] private float m_SmoothStyle;
[SerializeField] private bool m_Area;
[SerializeField] private bool m_Step;
[SerializeField] private StepType m_StepType;
/// <summary>
/// the tickness of lines.
/// 线条粗细。
/// </summary>
public float tickness { get { return m_Tickness; } set { m_Tickness = value; } }
/// <summary>
/// smoothness.
/// 平滑风格。
/// </summary>
public float smoothStyle { get { return m_SmoothStyle; } set { m_SmoothStyle = value; } }
/// <summary>
/// Whether the lines are displayed smoothly.
/// 是否平滑显示。
/// </summary>
public bool smooth { get { return m_Smooth; } set { m_Smooth = value; } }
/// <summary>
/// Whether to show area.
/// 是否显示区域填充颜色。
/// </summary>
public bool area { get { return m_Area; } set { m_Area = value; } }
/// <summary>
/// Whether to show as a step line.
/// 是否是阶梯线图。
/// </summary>
public bool step { get { return m_Step; } set { m_Step = value; } }
/// <summary>
/// the type of step line.
/// 阶梯线图类型。
/// </summary>
public StepType stepTpe { get { return m_StepType; } set { m_StepType = value; } }
public static Line defaultLine
{
get
{
var line = new Line
{
m_Tickness = 0.8f,
m_Smooth = false,
m_SmoothStyle = 2f,
m_Area = false,
m_Step = false,
m_StepType = StepType.Middle
};
return line;
}
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1a4ce8176d040474db7238ccc962c79f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -62,6 +62,39 @@ namespace XCharts
Area
}
/// <summary>
/// the type of line chart.
/// 折线图样式类型
/// </summary>
public enum LineType
{
/// <summary>
/// the normal line chart
/// 所有扇区圆心角相同,仅通过半径展现数据大小。
/// </summary>
Normal,
/// <summary>
/// the normal line chart
/// 平滑曲线。
/// </summary>
Smooth,
/// <summary>
/// step line.
/// 阶梯线图:当前点。
/// </summary>
StepStart,
/// <summary>
/// step line.
/// 阶梯线图:当前点和下一个点的中间。
/// </summary>
StepMiddle,
/// <summary>
/// step line.
/// 阶梯线图:下一个拐点。
/// </summary>
StepEnd
}
/// <summary>
/// 系列。每个系列通过 type 决定自己的图表类型。
/// </summary>
@@ -74,9 +107,14 @@ namespace XCharts
[SerializeField] private string m_Stack;
[SerializeField] [Range(0, 1)] private int m_AxisIndex = 0;
[SerializeField] private int m_RadarIndex = 0;
[SerializeField] private LineStyle m_LineStyle = new LineStyle();
[SerializeField] private AreaStyle m_AreaStyle = AreaStyle.defaultAreaStyle;
[SerializeField] private SerieSymbol m_Symbol = new SerieSymbol();
[SerializeField] private LineType m_LineType = LineType.Normal;
[SerializeField] private LineStyle m_LineStyle = new LineStyle();
[SerializeField] private float m_BarWidth = 0.6f;
[SerializeField] private float m_BarGap = 0.3f; // 30%
[SerializeField] private float m_BarCategoryGap = 0.2f; // 20%
#region PieChart field
[SerializeField] private bool m_ClickOffset = true;
[SerializeField] private RoseType m_RoseType = RoseType.None;
@@ -96,7 +134,8 @@ namespace XCharts
[NonSerialized] private int m_FilterStart;
[NonSerialized] private int m_FilterEnd;
[NonSerialized] private List<SerieData> m_FilterData;
[NonSerialized] private Dictionary<int, List<Vector3>> m_SmoothPoints = new Dictionary<int, List<Vector3>>();
[NonSerialized] private Dictionary<int, List<Vector3>> m_UpSmoothPoints = new Dictionary<int, List<Vector3>>();
[NonSerialized] private Dictionary<int, List<Vector3>> m_DownSmoothPoints = new Dictionary<int, List<Vector3>>();
[NonSerialized] private List<Vector3> m_DataPoints = new List<Vector3>();
/// <summary>
@@ -130,12 +169,6 @@ namespace XCharts
/// </summary>
public int radarIndex { get { return m_RadarIndex; } set { m_RadarIndex = value; } }
/// <summary>
/// The style of line.
/// 线条样式。
/// </summary>
/// <value></value>
public LineStyle lineStyle { get { return m_LineStyle; } set { m_LineStyle = value; } }
/// <summary>
/// The style of area.
/// 区域填充样式。
/// </summary>
@@ -147,30 +180,70 @@ namespace XCharts
/// </summary>
public SerieSymbol symbol { get { return m_Symbol; } set { m_Symbol = value; } }
/// <summary>
/// The type of line chart.
/// 折线图样式类型。
/// </summary>
/// <value></value>
public LineType lineType { get { return m_LineType; } set { m_LineType = value; } }
/// <summary>
/// The style of line.
/// 线条样式。
/// </summary>
/// <value></value>
public LineStyle lineStyle { get { return m_LineStyle; } set { m_LineStyle = value; } }
/// <summary>
/// The width of the bar. Adaptive when default 0.
/// 柱条的宽度,不设时自适应。支持设置成相对于类目宽度的百分比。
/// </summary>
/// <value></value>
public float barWidth { get { return m_BarWidth; } set { m_BarWidth = value; } }
/// <summary>
/// The gap between bars between different series, is a percent value like '0.3f' , which means 30% of the bar width, can be set as a fixed value.
/// <para>Set barGap as '-1' can overlap bars that belong to different series, which is useful when making a series of bar be background.
/// In a single coodinate system, this attribute is shared by multiple 'bar' series.
/// This attribute should be set on the last 'bar' series in the coodinate system,
/// then it will be adopted by all 'bar' series in the coordinate system.</para>
/// 不同系列的柱间距离。为百分比(如 '0.3f',表示柱子宽度的 30%
/// 如果想要两个系列的柱子重叠,可以设置 barGap 为 '-1f'。这在用柱子做背景的时候有用。
/// 在同一坐标系上,此属性会被多个 'bar' 系列共享。此属性应设置于此坐标系中最后一个 'bar' 系列上才会生效,并且是对此坐标系中所有 'bar' 系列生效。
/// </summary>
/// <value></value>
public float barGap { get { return m_BarGap; } set { m_BarGap = value; } }
/// <summary>
/// The bar gap of a single series, defaults to be 20% of the category gap, can be set as a fixed value.
/// In a single coodinate system, this attribute is shared by multiple 'bar' series.
/// This attribute should be set on the last 'bar' series in the coodinate system,
/// then it will be adopted by all 'bar' series in the coordinate system.
/// 同一系列的柱间距离默认为类目间距的20%,可设固定值。
/// 在同一坐标系上,此属性会被多个 'bar' 系列共享。此属性应设置于此坐标系中最后一个 'bar' 系列上才会生效,并且是对此坐标系中所有 'bar' 系列生效。
/// </summary>
/// <value></value>
public float barCategoryGap { get { return m_BarCategoryGap; } set { m_BarCategoryGap = value; } }
/// <summary>
/// Whether offset when mouse click pie chart item.
/// 鼠标点击时是否开启偏移一般用在PieChart图表中。
/// </summary>
public bool clickOffset { get { return m_ClickOffset; } set { m_ClickOffset = value; } }
public bool pieClickOffset { get { return m_ClickOffset; } set { m_ClickOffset = value; } }
/// <summary>
/// Whether to show as Nightingale chart.
/// 是否展示成南丁格尔图,通过半径区分数据大小。
/// </summary>
public RoseType roseType { get { return m_RoseType; } set { m_RoseType = value; } }
public RoseType pieRoseType { get { return m_RoseType; } set { m_RoseType = value; } }
/// <summary>
/// the space of pie chart item.
/// 饼图项间的空隙留白。
/// </summary>
public float space { get { return m_Space; } set { m_Space = value; } }
public float pieSpace { get { return m_Space; } set { m_Space = value; } }
/// <summary>
/// the center of pie chart.
/// 饼图的中心点。
/// </summary>
public float[] center { get { return m_Center; } set { m_Center = value; } }
public float[] pieCenter { get { return m_Center; } set { m_Center = value; } }
/// <summary>
/// the radius of pie chart.
/// 饼图的半径。radius[0]表示内径radius[1]表示外径。
/// </summary>
public float[] radius { get { return m_Radius; } set { m_Radius = value; } }
public float[] pieRadius { get { return m_Radius; } set { m_Radius = value; } }
/// <summary>
/// Text label of graphic element,to explain some data information about graphic item like value, name and so on.
/// 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
@@ -209,28 +282,49 @@ namespace XCharts
/// 数据项个数。
/// </summary>
public int dataCount { get { return m_Data.Count; } }
/// <summary>
/// 整个系列的每段曲线的点列表
/// </summary>
/// <value></value>
public Dictionary<int, List<Vector3>> smoothPoints { get { return m_SmoothPoints; } }
public List<Vector3> dataPoints { get { return m_DataPoints; } }
public List<Vector3> GetSmoothList(int dataIndex, int size = 100)
public List<Vector3> GetUpSmoothList(int dataIndex, int size = 100)
{
if (m_SmoothPoints.ContainsKey(dataIndex))
if (m_UpSmoothPoints.ContainsKey(dataIndex))
{
return m_SmoothPoints[dataIndex];
return m_UpSmoothPoints[dataIndex];
}
else
{
var list = new List<Vector3>(size);
m_SmoothPoints[dataIndex] = list;
m_UpSmoothPoints[dataIndex] = list;
return list;
}
}
public List<Vector3> GetDownSmoothList(int dataIndex, int size = 100)
{
if (m_DownSmoothPoints.ContainsKey(dataIndex))
{
return m_DownSmoothPoints[dataIndex];
}
else
{
var list = new List<Vector3>(size);
m_DownSmoothPoints[dataIndex] = list;
return list;
}
}
public void ClearSmoothList(int dataIndex)
{
if (m_UpSmoothPoints.ContainsKey(dataIndex))
{
m_UpSmoothPoints[dataIndex].Clear();
}
if (m_DownSmoothPoints.ContainsKey(dataIndex))
{
m_DownSmoothPoints[dataIndex].Clear();
}
}
/// <summary>
/// 维度Y对应数据中最大值。
/// </summary>
@@ -683,36 +777,81 @@ namespace XCharts
{
var color = areaStyle.color;
if (highlight) color *= color;
color.a *= areaStyle.opactiy;
color.a *= areaStyle.opacity;
return color;
}
else
{
var color = (Color)theme.GetColor(index);
if (highlight) color *= color;
color.a *= areaStyle.opactiy;
color.a *= areaStyle.opacity;
return color;
}
}
public Color GetAreaToColor(ThemeInfo theme, int index, bool highlight)
{
if (areaStyle.toColor != Color.clear)
{
var color = areaStyle.toColor;
if (highlight) color *= color;
color.a *= areaStyle.opacity;
return color;
}
else
{
return GetAreaColor(theme, index, highlight);
}
}
public Color GetLineColor(ThemeInfo theme, int index, bool highlight)
{
if (lineStyle.color != Color.clear)
{
var color = lineStyle.color;
if (highlight) color *= color;
color.a *= lineStyle.opactiy;
color.a *= lineStyle.opacity;
return color;
}
else
{
var color = (Color)theme.GetColor(index);
if (highlight) color *= color;
color.a *= lineStyle.opactiy;
color.a *= lineStyle.opacity;
return color;
}
}
public Color GetSymbolColor(ThemeInfo theme, int index, bool highlight)
{
if (symbol.color != Color.clear)
{
var color = symbol.color;
if (highlight) color *= color;
color.a *= symbol.opacity;
return color;
}
else
{
var color = (Color)theme.GetColor(index);
if (highlight) color *= color;
color.a *= symbol.opacity;
return color;
}
}
public float GetBarWidth(float categoryWidth)
{
if (m_BarWidth > 1) return m_BarWidth;
else return m_BarWidth * categoryWidth;
}
public float GetBarGap(float categoryWidth)
{
if (m_BarGap == -1) return 0;
else if (m_BarGap <= 1) return GetBarWidth(categoryWidth) * m_BarGap;
else return m_BarGap;
}
/// <summary>
/// 从json中导入数据
/// </summary>

View File

@@ -118,6 +118,51 @@ namespace XCharts
return null;
}
/// <summary>
/// 获得上一个同堆叠且显示的serie。
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Serie GetLastStackSerie(int index)
{
var serie = GetSerie(index);
return GetLastStackSerie(serie);
}
/// <summary>
/// 同堆叠的serie是否有渐变色的。
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
public bool IsAnyGradientSerie(string stack)
{
if (string.IsNullOrEmpty(stack)) return false;
foreach (var serie in m_Series)
{
if (serie.show && serie.areaStyle.show && stack.Equals(serie.stack))
{
if (serie.areaStyle.color != serie.areaStyle.toColor && serie.areaStyle.toColor != Color.clear) return true;
}
}
return false;
}
/// <summary>
/// 获得上一个同堆叠且显示的serie。
/// </summary>
/// <param name="serie"></param>
/// <returns></returns>
public Serie GetLastStackSerie(Serie serie)
{
if (serie == null || string.IsNullOrEmpty(serie.stack)) return null;
for (int i = serie.index - 1; i >= 0; i--)
{
var temp = m_Series[i];
if (temp.show && serie.stack.Equals(temp.stack)) return temp;
}
return null;
}
/// <summary>
/// 是否包含指定名字的系列
/// </summary>
@@ -605,19 +650,16 @@ namespace XCharts
return false;
}
public bool IsStack(string stackName)
public bool IsStack(string stackName, SerieType type)
{
_setForStack.Clear();
if (string.IsNullOrEmpty(stackName)) return false;
int count = 0;
foreach (var serie in m_Series)
{
if (string.IsNullOrEmpty(serie.stack)) continue;
if (_setForStack.Contains(serie.stack))
if (serie.show && serie.type == type)
{
if (serie.stack.Equals(stackName)) return true;
}
else
{
_setForStack.Add(serie.stack);
if (stackName.Equals(serie.stack)) count++;
if (count >= 2) return true;
}
}
return false;