Files
XCharts/Runtime/API/CoordinateChart_API.cs

302 lines
9.9 KiB
C#
Raw Normal View History

2021-01-11 08:54:28 +08:00
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using System.Collections.Generic;
using UnityEngine;
namespace XCharts
{
/// <summary>
/// The basic class of rectangular coordinate chartsuch as LineChart,BarChart and ScatterChart.
/// 直角坐标系类型图表的基类如折线图LineChart柱状图BarChart散点图ScatterChart都属于这类型的图表。
/// 不可用直接将CoordinateChart绑定到GameObject上。
/// </summary>
public partial class CoordinateChart
{
/// <summary>
/// grid component.
/// 网格组件。
/// </summary>
2021-01-11 08:54:28 +08:00
public Grid grid { get { return m_Grids.Count > 0 ? m_Grids[0] : null; } }
public List<Grid> grids { get { return m_Grids; } }
/// <summary>
2021-01-11 08:54:28 +08:00
/// the x AxesxAxes[0] is the first x axis, xAxes[1] is the second x axis.
/// 两个x轴。
/// </summary>
2021-01-11 08:54:28 +08:00
public List<XAxis> xAxes { get { return m_XAxes; } }
/// <summary>
2021-01-11 08:54:28 +08:00
/// the y Axes, yAxes[0] is the first y axis, yAxes[1] is the second y axis.
/// 两个y轴。
/// </summary>
2021-01-11 08:54:28 +08:00
public List<YAxis> yAxes { get { return m_YAxes; } }
/// <summary>
/// dataZoom component.
/// 区域缩放组件。
/// </summary>
2021-01-11 08:54:28 +08:00
public DataZoom dataZoom { get { return m_DataZooms.Count > 0 ? m_DataZooms[0] : null; } }
2019-10-14 07:45:56 +08:00
/// <summary>
/// visualMap component.
/// 视觉映射组件。
/// </summary>
2021-01-11 08:54:28 +08:00
public VisualMap visualMap { get { return m_VisualMaps.Count > 0 ? m_VisualMaps[0] : null; } }
2019-10-16 13:01:43 +08:00
/// <summary>
/// X轴
/// </summary>
2021-01-11 08:54:28 +08:00
public XAxis xAxis0 { get { return m_XAxes.Count > 0 ? m_XAxes[0] : null; } }
2019-10-16 13:01:43 +08:00
/// <summary>
/// X轴
/// </summary>
2021-01-11 08:54:28 +08:00
public XAxis xAxis1 { get { return m_XAxes.Count > 1 ? m_XAxes[1] : null; } }
2019-10-16 13:01:43 +08:00
/// <summary>
/// Y轴
/// </summary>
2021-01-11 08:54:28 +08:00
public YAxis yAxis0 { get { return m_YAxes.Count > 0 ? m_YAxes[0] : null; } }
2019-10-16 13:01:43 +08:00
/// <summary>
/// Y轴
/// </summary>
2021-01-11 08:54:28 +08:00
public YAxis yAxis1 { get { return m_YAxes.Count > 1 ? m_YAxes[1] : null; } }
/// <summary>
/// Remove all data from series,legend and axis.
/// It just emptying all of serie's data without emptying the list of series.
/// 清空所有图例,系列和坐标轴类目数据。系列中指示清空系列中的数据,会保留系列列表。
/// </summary>
public override void ClearData()
{
base.ClearData();
ClearAxisData();
}
/// <summary>
/// Remove all data from series,legend and axis.
/// The series list is also cleared.
/// 清空所有图例,系列和坐标轴类目数据。系列的列表也会被清空。
/// </summary>
public override void RemoveData()
{
base.RemoveData();
ClearAxisData();
}
/// <summary>
2021-01-11 08:54:28 +08:00
/// Remove all data of Axes.
/// 清除所有x轴和y轴的类目数据。
/// </summary>
public void ClearAxisData()
{
2021-03-25 12:55:52 +08:00
foreach (var axis in m_XAxes)
{
axis.data.Clear();
axis.SetAllDirty();
}
foreach (var axis in m_YAxes)
{
axis.data.Clear();
axis.SetAllDirty();
}
}
/// <summary>
/// Add a category data to xAxis.
/// 添加一个类目数据到指定的x轴。
/// </summary>
/// <param name="category">the category data</param>
/// <param name="xAxisIndex">which xAxis should category add to</param>
public void AddXAxisData(string category, int xAxisIndex = 0)
{
2021-01-11 08:54:28 +08:00
m_XAxes[xAxisIndex].AddData(category);
}
/// <summary>
/// Add a category data to yAxis.
/// 添加一个类目数据到指定的y轴。
/// </summary>
/// <param name="category">the category data</param>
/// <param name="yAxisIndex">which yAxis should category add to</param>
public void AddYAxisData(string category, int yAxisIndex = 0)
{
2021-01-11 08:54:28 +08:00
m_YAxes[yAxisIndex].AddData(category);
}
/// <summary>
/// reutrn true when all the show axis is `Value` type.
2020-01-15 19:41:21 +08:00
/// 纯数值坐标轴(数值轴或对数轴)。
/// </summary>
public bool IsValue()
{
2021-01-11 08:54:28 +08:00
foreach (var axis in m_XAxes)
{
2020-01-15 19:41:21 +08:00
if (axis.show && !axis.IsValue() && !axis.IsLog()) return false;
}
2021-01-11 08:54:28 +08:00
foreach (var axis in m_YAxes)
{
2020-01-15 19:41:21 +08:00
if (axis.show && !axis.IsValue() && !axis.IsLog()) return false;
}
return true;
}
2020-05-16 20:33:01 +08:00
/// <summary>
/// 纯类目轴。
/// </summary>
public bool IsCategory()
{
2021-01-11 08:54:28 +08:00
foreach (var axis in m_XAxes)
2019-10-14 07:45:56 +08:00
{
if (axis.show && !axis.IsCategory()) return false;
}
2021-01-11 08:54:28 +08:00
foreach (var axis in m_YAxes)
2019-10-14 07:45:56 +08:00
{
if (axis.show && !axis.IsCategory()) return false;
}
return true;
}
2020-05-16 20:33:01 +08:00
/// <summary>
/// 坐标是否在坐标轴内。
/// </summary>
2021-01-11 08:54:28 +08:00
public bool IsInGrid(Grid grid, Vector2 local)
{
2021-01-11 08:54:28 +08:00
return IsInGrid(grid, local.x, local.y);
}
2021-01-11 08:54:28 +08:00
public bool IsInGrid(Grid grid, Vector3 local)
{
2021-01-11 08:54:28 +08:00
return IsInGrid(grid, local.x, local.y);
}
2021-01-11 08:54:28 +08:00
public bool IsInGrid(Grid grid, float x, float y)
{
2021-01-11 08:54:28 +08:00
if (x < grid.runtimeX - 1 || x > grid.runtimeX + grid.runtimeWidth + 1 ||
y < grid.runtimeY - 1 || y > grid.runtimeY + grid.runtimeHeight + 1)
{
return false;
}
return true;
}
2019-10-16 13:01:43 +08:00
2021-01-11 08:54:28 +08:00
public bool IsInAnyGrid(Vector2 local)
{
foreach (var grid in m_Grids)
{
if (IsInGrid(grid, local)) return true;
}
return false;
}
public Grid GetGrid(Vector2 local)
{
for (int i = 0; i < m_Grids.Count; i++)
{
var grid = m_Grids[i];
grid.index = i;
if (IsInGrid(grid, local)) return grid;
}
return null;
}
2019-10-16 13:01:43 +08:00
/// <summary>
/// 在下一帧刷新DataZoom
/// </summary>
public void RefreshDataZoom()
{
RefreshDataZoomLabel();
}
/// <summary>
/// 立即刷新数值坐标轴的最大最小值
/// </summary>
public void RefreshAxisMinMaxValue()
{
CheckMinMaxValue();
}
2021-01-11 08:54:28 +08:00
public Vector3 ClampInGrid(Grid grid, Vector3 pos)
{
2021-01-11 08:54:28 +08:00
if (IsInGrid(grid, pos)) return pos;
else
{
// var pos = new Vector3(pos.x, pos.y);
2021-01-11 08:54:28 +08:00
if (pos.x < grid.runtimeX) pos.x = grid.runtimeX;
if (pos.x > grid.runtimeX + grid.runtimeWidth) pos.x = grid.runtimeX + grid.runtimeWidth;
if (pos.y < grid.runtimeY) pos.y = grid.runtimeY;
if (pos.y > grid.runtimeY + grid.runtimeHeight) pos.y = grid.runtimeY + grid.runtimeHeight;
return pos;
}
}
/// <summary>
/// 转换X轴和Y轴的配置
/// </summary>
/// <param name="index">坐标轴索引0或1</param>
public void CovertXYAxis(int index)
{
if (index >= 0 && index <= 1)
{
2021-01-11 08:54:28 +08:00
var xAxis = m_XAxes[index];
var yAxis = m_YAxes[index];
var tempX = m_XAxes[index].Clone();
xAxis.Copy(m_YAxes[index]);
2020-04-18 23:38:42 +08:00
yAxis.Copy(tempX);
xAxis.runtimeZeroXOffset = 0;
xAxis.runtimeZeroYOffset = 0;
yAxis.runtimeZeroXOffset = 0;
yAxis.runtimeZeroYOffset = 0;
xAxis.runtimeMinValue = 0;
xAxis.runtimeMaxValue = 0;
yAxis.runtimeMinValue = 0;
yAxis.runtimeMaxValue = 0;
RefreshChart();
}
}
/// <summary>
/// 更新坐标系原点和宽高
/// </summary>
public void UpdateCoordinate()
{
2021-01-11 08:54:28 +08:00
foreach (var grid in m_Grids)
{
grid.UpdateRuntimeData(m_ChartX, m_ChartY, m_ChartWidth, m_ChartHeight);
}
foreach (var dataZoom in m_DataZooms)
{
dataZoom.UpdateRuntimeData(m_ChartX, m_ChartY, m_ChartWidth, m_ChartHeight);
}
}
/// <summary>
2020-05-16 20:33:01 +08:00
/// 设置可缓存的最大数据量。当数据量超过该值时,会自动删除第一个值再加入最新值。
/// </summary>
public void SetMaxCache(int maxCache)
{
foreach (var serie in m_Series.list) serie.maxCache = maxCache;
2021-01-11 08:54:28 +08:00
foreach (var axis in m_XAxes) axis.maxCache = maxCache;
foreach (var axis in m_YAxes) axis.maxCache = maxCache;
}
public Grid GetGrid(int index)
{
if (index >= 0 && index < m_Grids.Count) return m_Grids[index];
else return null;
}
public XAxis GetXAxis(int index)
{
if (index >= 0 && index < m_XAxes.Count) return m_XAxes[index];
else return null;
}
public YAxis GetYAxis(int index)
{
if (index >= 0 && index < m_YAxes.Count) return m_YAxes[index];
else return null;
}
}
}