mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-16 13:30:10 +00:00
调整代码结构
This commit is contained in:
442
Scripts/UI/API/BaseChart_API.cs
Normal file
442
Scripts/UI/API/BaseChart_API.cs
Normal file
@@ -0,0 +1,442 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// The base class of all charts.
|
||||
/// 所有Chart的基类,不可直接使用。
|
||||
/// </summary>
|
||||
public partial class BaseChart
|
||||
{
|
||||
/// <summary>
|
||||
/// The title setting of chart.
|
||||
/// 标题组件
|
||||
/// </summary>
|
||||
public Title title { get { return m_Title; } }
|
||||
/// <summary>
|
||||
/// The legend setting of chart.
|
||||
/// 图例组件
|
||||
/// </summary>
|
||||
public Legend legend { get { return m_Legend; } }
|
||||
/// <summary>
|
||||
/// The tooltip setting of chart.
|
||||
/// 提示框组件
|
||||
/// </summary>
|
||||
public Tooltip tooltip { get { return m_Tooltip; } }
|
||||
/// <summary>
|
||||
/// The series setting of chart.
|
||||
/// 系列列表
|
||||
/// </summary>
|
||||
public Series series { get { return m_Series; } }
|
||||
/// <summary>
|
||||
/// Global parameter setting component.
|
||||
/// 全局设置组件。
|
||||
/// </summary>
|
||||
public Settings settings { get { return m_Settings; } }
|
||||
/// <summary>
|
||||
/// The width of chart.
|
||||
/// 图表的宽
|
||||
/// </summary>
|
||||
public float chartWidth { get { return m_ChartWidth; } }
|
||||
/// <summary>
|
||||
/// The height of chart.
|
||||
/// 图表的高
|
||||
/// </summary>
|
||||
public float chartHeight { get { return m_ChartHeight; } }
|
||||
/// <summary>
|
||||
/// The postion of pointer.
|
||||
/// 鼠标位置
|
||||
/// </summary>
|
||||
public Vector2 pointerPos { get; protected set; }
|
||||
/// <summary>
|
||||
/// 自定义绘制回调。
|
||||
/// </summary>
|
||||
public Action<VertexHelper> customDrawCallback { set { m_CustomDrawCallback = value; } }
|
||||
/// <summary>
|
||||
/// Set the size of chart.
|
||||
/// 设置图表的大小。
|
||||
/// </summary>
|
||||
/// <param name="width">width</param>
|
||||
/// <param name="height">height</param>
|
||||
public virtual void SetSize(float width, float height)
|
||||
{
|
||||
m_ChartWidth = width;
|
||||
m_ChartHeight = height;
|
||||
m_CheckWidth = width;
|
||||
m_CheckHeight = height;
|
||||
|
||||
rectTransform.sizeDelta = new Vector2(m_ChartWidth, m_ChartHeight);
|
||||
OnSizeChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all series and legend data.
|
||||
/// It just emptying all of serie's data without emptying the list of series.
|
||||
/// 清除所有数据,系列中只是移除数据,列表会保留。
|
||||
/// </summary>
|
||||
public virtual void ClearData()
|
||||
{
|
||||
m_Series.ClearData();
|
||||
m_Legend.ClearData();
|
||||
m_CheckAnimation = false;
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all data from series and legend.
|
||||
/// The series list is also cleared.
|
||||
/// 清除所有系列和图例数据,系列的列表也会被清除。
|
||||
/// </summary>
|
||||
public virtual void RemoveData()
|
||||
{
|
||||
m_Legend.ClearData();
|
||||
m_Series.RemoveAll();
|
||||
m_CheckAnimation = false;
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove legend and serie by name.
|
||||
/// 清除指定系列名称的数据。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
public virtual void RemoveData(string serieName)
|
||||
{
|
||||
m_Series.Remove(serieName);
|
||||
m_Legend.RemoveData(serieName);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a serie to serie list.
|
||||
/// 添加一个系列到系列列表中。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="type">the type of serie</param>
|
||||
/// <param name="show">whether to show this serie</param>
|
||||
/// <returns>the added serie</returns>
|
||||
public virtual Serie AddSerie(SerieType type, string serieName = null, bool show = true)
|
||||
{
|
||||
return m_Series.AddSerie(type, serieName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a data to serie.
|
||||
/// If serieName doesn't exist in legend,will be add to legend.
|
||||
/// 添加一个数据到指定的系列中。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="data">the data to add</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, float data, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieName, data, dataName);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a data to serie.
|
||||
/// 添加一个数据到指定的系列中。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="data">the data to add</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, float data, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieIndex, data, dataName);
|
||||
if (success)
|
||||
{
|
||||
RefreshChart();
|
||||
ReinitChartLabel();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an arbitray dimension data to serie,such as (x,y,z,...).
|
||||
/// 添加多维数据(x,y,z...)到指定的系列中。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="multidimensionalData">the (x,y,z,...) data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, List<float> multidimensionalData, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieName, multidimensionalData, dataName);
|
||||
if (success)
|
||||
{
|
||||
RefreshChart();
|
||||
ReinitChartLabel();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an arbitray dimension data to serie,such as (x,y,z,...).
|
||||
/// 添加多维数据(x,y,z...)到指定的系列中。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie,index starts at 0</param>
|
||||
/// <param name="multidimensionalData">the (x,y,z,...) data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, List<float> multidimensionalData, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieIndex, multidimensionalData, dataName);
|
||||
if (success)
|
||||
{
|
||||
RefreshChart();
|
||||
ReinitChartLabel();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a (x,y) data to serie.
|
||||
/// 添加(x,y)数据到指定系列中。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="xValue">x data</param>
|
||||
/// <param name="yValue">y data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, float xValue, float yValue, string dataName)
|
||||
{
|
||||
var success = m_Series.AddXYData(serieName, xValue, yValue, dataName);
|
||||
if (success)
|
||||
{
|
||||
RefreshChart();
|
||||
ReinitChartLabel();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a (x,y) data to serie.
|
||||
/// 添加(x,y)数据到指定系列中。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="xValue">x data</param>
|
||||
/// <param name="yValue">y data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, float xValue, float yValue, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddXYData(serieIndex, xValue, yValue, dataName);
|
||||
if (success)
|
||||
{
|
||||
RefreshChart();
|
||||
ReinitChartLabel();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data by serie name.
|
||||
/// 更新指定系列中的指定索引数据。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="dataIndex">the index of data</param>
|
||||
/// <param name="value">the data will be update</param>
|
||||
public virtual void UpdateData(string serieName, int dataIndex, float value)
|
||||
{
|
||||
m_Series.UpdateData(serieName, dataIndex, value);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data by serie index.
|
||||
/// 更新指定系列中的指定索引数据。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="dataIndex">the index of data</param>
|
||||
/// <param name="value">the data will be update</param>
|
||||
public virtual void UpdateData(int serieIndex, int dataIndex, float value)
|
||||
{
|
||||
m_Series.UpdateData(serieIndex, dataIndex, value);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data name.
|
||||
/// 更新指定系列中的指定索引数据名称。
|
||||
/// </summary>
|
||||
/// <param name="serieName"></param>
|
||||
/// <param name="dataIndex"></param>
|
||||
/// <param name="dataName"></param>
|
||||
public virtual void UpdateDataName(string serieName, int dataIndex, string dataName)
|
||||
{
|
||||
m_Series.UpdateDataName(serieName, dataIndex, dataName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data name.
|
||||
/// 更新指定系列中的指定索引数据名称。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex"></param>
|
||||
/// <param name="dataName"></param>
|
||||
/// <param name="dataIndex"></param>
|
||||
public virtual void UpdateDataName(int serieIndex, int dataIndex, string dataName)
|
||||
{
|
||||
m_Series.UpdateDataName(serieIndex, dataIndex, dataName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show serie.
|
||||
/// 设置指定系列是否显示。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(string serieName, bool active)
|
||||
{
|
||||
var serie = m_Series.GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
SetActive(serie.index, active);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show serie.
|
||||
/// 设置指定系列是否显示。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(int serieIndex, bool active)
|
||||
{
|
||||
m_Series.SetActive(serieIndex, active);
|
||||
var serie = m_Series.GetSerie(serieIndex);
|
||||
if (serie != null && !string.IsNullOrEmpty(serie.name))
|
||||
{
|
||||
var legendIndex = m_LegendRealShowName.IndexOf(serie.name);
|
||||
var bgColor1 = active ? m_ThemeInfo.GetColor(legendIndex) : m_ThemeInfo.legendUnableColor;
|
||||
m_Legend.UpdateButtonColor(serie.name, bgColor1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether serie is activated.
|
||||
/// 获取指定系列是否显示。
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(string serieName)
|
||||
{
|
||||
return m_Series.IsActive(serieName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether serie is activated.
|
||||
/// 获取指定系列是否显示。
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(int serieIndex)
|
||||
{
|
||||
return m_Series.IsActive(serieIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether serie is activated.
|
||||
/// 获得指定图例名字的系列是否显示。
|
||||
/// </summary>
|
||||
/// <param name="legendName"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool IsActiveByLegend(string legendName)
|
||||
{
|
||||
foreach (var serie in m_Series.list)
|
||||
{
|
||||
if (serie.show && legendName.Equals(serie.name))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var serieData in serie.data)
|
||||
{
|
||||
if (serieData.show && legendName.Equals(serieData.name))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redraw chart in next frame.
|
||||
/// 在下一帧刷新图表。
|
||||
/// </summary>
|
||||
public void RefreshChart()
|
||||
{
|
||||
m_RefreshChart = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新初始化Label。
|
||||
/// </summary>
|
||||
public void ReinitChartLabel()
|
||||
{
|
||||
m_ReinitLabel = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update chart theme.
|
||||
/// 切换图表主题。
|
||||
/// </summary>
|
||||
/// <param name="theme">theme</param>
|
||||
public void UpdateTheme(Theme theme)
|
||||
{
|
||||
m_ThemeInfo.theme = theme;
|
||||
OnThemeChanged();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether series animation enabel.
|
||||
/// 启用或关闭起始动画。
|
||||
/// </summary>
|
||||
/// <param name="flag"></param>
|
||||
public void AnimationEnable(bool flag)
|
||||
{
|
||||
m_Series.AnimationEnable(flag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start play animation.
|
||||
/// 开始初始动画。
|
||||
/// </summary>
|
||||
public void AnimationStart()
|
||||
{
|
||||
m_Series.AnimationStart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop play animation.
|
||||
/// 停止初始化动画。
|
||||
/// </summary>
|
||||
public void AnimationStop()
|
||||
{
|
||||
m_CheckAnimation = false;
|
||||
m_Series.AnimationStop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset animation to play.
|
||||
/// 重置初始动画,重新播放。
|
||||
/// </summary>
|
||||
public void AnimationReset()
|
||||
{
|
||||
m_CheckAnimation = false;
|
||||
m_Series.AnimationReset();
|
||||
RefreshChart();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/API/BaseChart_API.cs.meta
Normal file
11
Scripts/UI/API/BaseChart_API.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d078e85e5877d4d0f990c8dc43006247
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
359
Scripts/UI/API/ChartDrawer.cs
Normal file
359
Scripts/UI/API/ChartDrawer.cs
Normal file
@@ -0,0 +1,359 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public static class ChartDrawer
|
||||
{
|
||||
private static UIVertex[] vertex = new UIVertex[4];
|
||||
private static List<Vector3> s_CurvesPosList = new List<Vector3>();
|
||||
|
||||
public static void DrawArrow(VertexHelper vh, Vector3 startPos, Vector3 arrowPos, float width,
|
||||
float height, float offset, float dent, Color32 color)
|
||||
{
|
||||
var dir = (arrowPos - startPos).normalized;
|
||||
|
||||
var sharpPos = arrowPos + (offset + height / 2) * dir;
|
||||
var middle = sharpPos + (dent - height) * dir;
|
||||
var diff = Vector3.Cross(dir, Vector3.forward).normalized * width / 2;
|
||||
var left = sharpPos - height * dir + diff;
|
||||
var right = sharpPos - height * dir - diff;
|
||||
DrawTriangle(vh, middle, sharpPos, left, color);
|
||||
DrawTriangle(vh, middle, sharpPos, right, color);
|
||||
}
|
||||
|
||||
public static void DrawLine(VertexHelper vh, Vector3 p1, Vector3 p2, float size, Color32 color)
|
||||
{
|
||||
if (p1 == p2) return;
|
||||
Vector3 v = Vector3.Cross(p2 - p1, Vector3.forward).normalized * size;
|
||||
vertex[0].position = p1 - v;
|
||||
vertex[1].position = p2 - v;
|
||||
vertex[2].position = p2 + v;
|
||||
vertex[3].position = p1 + v;
|
||||
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
vertex[j].color = color;
|
||||
vertex[j].uv0 = Vector2.zero;
|
||||
}
|
||||
vh.AddUIVertexQuad(vertex);
|
||||
}
|
||||
|
||||
public static void DrawDashLine(VertexHelper vh, Vector3 p1, Vector3 p2, float size, Color32 color,
|
||||
float dashLen = 15f, float blankLen = 7f)
|
||||
{
|
||||
float dist = Vector3.Distance(p1, p2);
|
||||
if (dist < 0.1f) return;
|
||||
int segment = Mathf.CeilToInt(dist / (dashLen + blankLen));
|
||||
Vector3 dir = (p2 - p1).normalized;
|
||||
Vector3 sp = p1, np;
|
||||
for (int i = 1; i <= segment; i++)
|
||||
{
|
||||
np = p1 + dir * dist * i / segment;
|
||||
var dashep = np - dir * blankLen;
|
||||
DrawLine(vh, sp, dashep, size, color);
|
||||
sp = np;
|
||||
}
|
||||
DrawLine(vh, sp, p2, size, color);
|
||||
}
|
||||
public static void DrawDotLine(VertexHelper vh, Vector3 p1, Vector3 p2, float size, Color32 color,
|
||||
float dotLen = 5f, float blankLen = 5f)
|
||||
{
|
||||
float dist = Vector3.Distance(p1, p2);
|
||||
if (dist < 0.1f) return;
|
||||
int segment = Mathf.CeilToInt(dist / (dotLen + blankLen));
|
||||
Vector3 dir = (p2 - p1).normalized;
|
||||
Vector3 sp = p1, np;
|
||||
for (int i = 1; i <= segment; i++)
|
||||
{
|
||||
np = p1 + dir * dist * i / segment;
|
||||
var dashep = np - dir * blankLen;
|
||||
DrawLine(vh, sp, dashep, size, color);
|
||||
sp = np;
|
||||
}
|
||||
DrawLine(vh, sp, p2, size, color);
|
||||
}
|
||||
|
||||
public static void DrawDashDotLine(VertexHelper vh, Vector3 p1, Vector3 p2, float size, Color32 color,
|
||||
float dashLen = 15f, float blankDotLen = 15f)
|
||||
{
|
||||
float dist = Vector3.Distance(p1, p2);
|
||||
if (dist < 0.1f) return;
|
||||
int segment = Mathf.CeilToInt(dist / (dashLen + blankDotLen));
|
||||
Vector3 dir = (p2 - p1).normalized;
|
||||
Vector3 sp = p1, np;
|
||||
for (int i = 1; i <= segment; i++)
|
||||
{
|
||||
np = p1 + dir * dist * i / segment;
|
||||
var dashep = np - dir * blankDotLen;
|
||||
DrawLine(vh, sp, dashep, size, color);
|
||||
var dotsp = dashep + (blankDotLen - 2 * size) / 2 * dir;
|
||||
var dotep = dotsp + 2 * size * dir;
|
||||
DrawLine(vh, dotsp, dotep, size, color);
|
||||
sp = np;
|
||||
}
|
||||
DrawLine(vh, sp, p2, size, color);
|
||||
}
|
||||
|
||||
public static void DrawDashDotDotLine(VertexHelper vh, Vector3 p1, Vector3 p2, float size,
|
||||
Color32 color, float dashLen = 15f, float blankDotLen = 20f)
|
||||
{
|
||||
float dist = Vector3.Distance(p1, p2);
|
||||
if (dist < 0.1f) return;
|
||||
int segment = Mathf.CeilToInt(dist / (dashLen + blankDotLen));
|
||||
Vector3 dir = (p2 - p1).normalized;
|
||||
Vector3 sp = p1, np;
|
||||
for (int i = 1; i <= segment; i++)
|
||||
{
|
||||
np = p1 + dir * dist * i / segment;
|
||||
var dashep = np - dir * blankDotLen;
|
||||
DrawLine(vh, sp, dashep, size, color);
|
||||
var dotsp = dashep + (blankDotLen / 2 - 2 * size) / 2 * dir;
|
||||
var dotep = dotsp + 2 * size * dir;
|
||||
DrawLine(vh, dotsp, dotep, size, color);
|
||||
var dotsp2 = dashep + blankDotLen / 2 * dir;
|
||||
dotsp2 = dotsp2 + (blankDotLen / 4 - 2 * size) / 2 * dir;
|
||||
var dotep2 = dotsp2 + 2 * size * dir;
|
||||
DrawLine(vh, dotsp2, dotep2, size, color);
|
||||
sp = np;
|
||||
}
|
||||
DrawLine(vh, sp, p2, size, color);
|
||||
}
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p, float radius, Color32 color,
|
||||
bool vertical = true)
|
||||
{
|
||||
Vector3 p1, p2, p3, p4;
|
||||
if (vertical)
|
||||
{
|
||||
p1 = new Vector3(p.x + radius, p.y - radius);
|
||||
p2 = new Vector3(p.x - radius, p.y - radius);
|
||||
p3 = new Vector3(p.x - radius, p.y + radius);
|
||||
p4 = new Vector3(p.x + radius, p.y + radius);
|
||||
}
|
||||
else
|
||||
{
|
||||
p1 = new Vector3(p.x - radius, p.y - radius);
|
||||
p2 = new Vector3(p.x - radius, p.y + radius);
|
||||
p3 = new Vector3(p.x + radius, p.y + radius);
|
||||
p4 = new Vector3(p.x + radius, p.y - radius);
|
||||
}
|
||||
DrawPolygon(vh, p1, p2, p3, p4, color, color);
|
||||
}
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, float radius, Color32 color)
|
||||
{
|
||||
DrawPolygon(vh, p1, p2, radius, color, color);
|
||||
}
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, float radius, Color32 color, Color32 toColor)
|
||||
{
|
||||
var dir = (p2 - p1).normalized;
|
||||
var dirv = Vector3.Cross(dir, Vector3.forward).normalized;
|
||||
|
||||
var p3 = p1 + dirv * radius;
|
||||
var p4 = p1 - dirv * radius;
|
||||
var p5 = p2 - dirv * radius;
|
||||
var p6 = p2 + dirv * radius;
|
||||
DrawPolygon(vh, p3, p4, p5, p6, color, toColor);
|
||||
}
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p, float xRadius, float yRadius,
|
||||
Color32 color, bool vertical = true)
|
||||
{
|
||||
DrawPolygon(vh, p, xRadius, yRadius, color, color, vertical);
|
||||
}
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p, float xRadius, float yRadius,
|
||||
Color32 color, Color toColor, bool vertical = true)
|
||||
{
|
||||
Vector3 p1, p2, p3, p4;
|
||||
if (vertical)
|
||||
{
|
||||
p1 = new Vector3(p.x + xRadius, p.y - yRadius);
|
||||
p2 = new Vector3(p.x - xRadius, p.y - yRadius);
|
||||
p3 = new Vector3(p.x - xRadius, p.y + yRadius);
|
||||
p4 = new Vector3(p.x + xRadius, p.y + yRadius);
|
||||
}
|
||||
else
|
||||
{
|
||||
p1 = new Vector3(p.x - xRadius, p.y - yRadius);
|
||||
p2 = new Vector3(p.x - xRadius, p.y + yRadius);
|
||||
p3 = new Vector3(p.x + xRadius, p.y + yRadius);
|
||||
p4 = new Vector3(p.x + xRadius, p.y - yRadius);
|
||||
}
|
||||
|
||||
DrawPolygon(vh, p1, p2, p3, p4, color, toColor);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
|
||||
Color32 color)
|
||||
{
|
||||
DrawPolygon(vh, p1, p2, p3, p4, color, color);
|
||||
}
|
||||
|
||||
public static void DrawPolygon(VertexHelper vh, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4,
|
||||
Color32 startColor, Color32 toColor)
|
||||
{
|
||||
vertex[0].position = p1;
|
||||
vertex[1].position = p2;
|
||||
vertex[2].position = p3;
|
||||
vertex[3].position = p4;
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
vertex[j].color = j >= 2 ? toColor : startColor;
|
||||
vertex[j].uv0 = Vector2.zero;
|
||||
}
|
||||
vh.AddUIVertexQuad(vertex);
|
||||
}
|
||||
|
||||
public static void DrawBorder(VertexHelper vh, Vector3 p, float rectWidth, float rectHeight,
|
||||
float borderWidth, Color32 color)
|
||||
{
|
||||
var halfWid = rectWidth / 2;
|
||||
var halfHig = rectHeight / 2;
|
||||
var p1In = new Vector3(p.x - halfWid, p.y - halfHig);
|
||||
var p1Ot = new Vector3(p.x - halfWid - borderWidth, p.y - halfHig - borderWidth);
|
||||
var p2In = new Vector3(p.x - halfWid, p.y + halfHig);
|
||||
var p2Ot = new Vector3(p.x - halfWid - borderWidth, p.y + halfHig + borderWidth);
|
||||
var p3In = new Vector3(p.x + halfWid, p.y + halfHig);
|
||||
var p3Ot = new Vector3(p.x + halfWid + borderWidth, p.y + halfHig + borderWidth);
|
||||
var p4In = new Vector3(p.x + halfWid, p.y - halfHig);
|
||||
var p4Ot = new Vector3(p.x + halfWid + borderWidth, p.y - halfHig - borderWidth);
|
||||
DrawPolygon(vh, p1In, p1Ot, p2Ot, p2In, color);
|
||||
DrawPolygon(vh, p2In, p2Ot, p3Ot, p3In, color);
|
||||
DrawPolygon(vh, p3In, p3Ot, p4Ot, p4In, color);
|
||||
DrawPolygon(vh, p4In, p4Ot, p1Ot, p1In, color);
|
||||
}
|
||||
|
||||
public static void DrawTriangle(VertexHelper vh, Vector3 p1,
|
||||
Vector3 p2, Vector3 p3, Color32 color)
|
||||
{
|
||||
DrawTriangle(vh, p1, p2, p3, color, color, color);
|
||||
}
|
||||
|
||||
public static void DrawTriangle(VertexHelper vh, Vector3 p1,
|
||||
Vector3 p2, Vector3 p3, Color32 color, Color32 color2, Color32 color3)
|
||||
{
|
||||
UIVertex v1 = new UIVertex();
|
||||
v1.position = p1;
|
||||
v1.color = color;
|
||||
v1.uv0 = Vector3.zero;
|
||||
UIVertex v2 = new UIVertex();
|
||||
v2.position = p2;
|
||||
v2.color = color2;
|
||||
v2.uv0 = Vector3.zero;
|
||||
UIVertex v3 = new UIVertex();
|
||||
v3.position = p3;
|
||||
v3.color = color3;
|
||||
v3.uv0 = Vector3.zero;
|
||||
int startIndex = vh.currentVertCount;
|
||||
vh.AddVert(v1);
|
||||
vh.AddVert(v2);
|
||||
vh.AddVert(v3);
|
||||
vh.AddTriangle(startIndex, startIndex + 1, startIndex + 2);
|
||||
}
|
||||
|
||||
public static void DrawCricle(VertexHelper vh, Vector3 p, float radius, Color32 color,
|
||||
float smoothness = 2f)
|
||||
{
|
||||
DrawSector(vh, p, radius, color, 0, 360, smoothness);
|
||||
}
|
||||
|
||||
public static void DrawEmptyCricle(VertexHelper vh, Vector3 p, float radius, float tickness,
|
||||
Color32 color, Color emptyColor, float smoothness = 2f)
|
||||
{
|
||||
DrawDoughnut(vh, p, radius - tickness, radius, color, emptyColor, smoothness);
|
||||
}
|
||||
|
||||
public static void DrawSector(VertexHelper vh, Vector3 p, float radius, Color32 color,
|
||||
float startDegree, float toDegree, float smoothness = 2f)
|
||||
{
|
||||
int segments = (int)((2 * Mathf.PI * radius) / (smoothness < 0 ? 2f : smoothness));
|
||||
Vector3 p2, p3;
|
||||
float startAngle = startDegree * Mathf.Deg2Rad;
|
||||
float angle = (toDegree - startDegree) * Mathf.Deg2Rad / segments;
|
||||
p2 = new Vector3(p.x + radius * Mathf.Sin(startAngle), p.y + radius * Mathf.Cos(startAngle));
|
||||
for (int i = 0; i <= segments; i++)
|
||||
{
|
||||
float currAngle = startAngle + i * angle;
|
||||
p3 = new Vector3(p.x + radius * Mathf.Sin(currAngle),
|
||||
p.y + radius * Mathf.Cos(currAngle));
|
||||
DrawTriangle(vh, p, p2, p3, color);
|
||||
p2 = p3;
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawDoughnut(VertexHelper vh, Vector3 p, float insideRadius, float outsideRadius,
|
||||
Color32 color, Color emptyColor, float smoothness = 2f, float startDegree = 0, float toDegree = 360)
|
||||
{
|
||||
if (insideRadius <= 0)
|
||||
{
|
||||
DrawSector(vh, p, outsideRadius, color, startDegree, toDegree, smoothness);
|
||||
return;
|
||||
}
|
||||
Vector3 p1, p2, p3, p4;
|
||||
int segments = (int)((2 * Mathf.PI * outsideRadius) / (smoothness < 0 ? 2f : smoothness));
|
||||
float startAngle = startDegree * Mathf.Deg2Rad;
|
||||
float angle = (toDegree - startDegree) * Mathf.Deg2Rad / segments;
|
||||
p1 = new Vector3(p.x + insideRadius * Mathf.Sin(startAngle),
|
||||
p.y + insideRadius * Mathf.Cos(startAngle));
|
||||
p2 = new Vector3(p.x + outsideRadius * Mathf.Sin(startAngle),
|
||||
p.y + outsideRadius * Mathf.Cos(startAngle));
|
||||
for (int i = 0; i <= segments; i++)
|
||||
{
|
||||
float currAngle = startAngle + i * angle;
|
||||
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
|
||||
p.y + outsideRadius * Mathf.Cos(currAngle));
|
||||
p4 = new Vector3(p.x + insideRadius * Mathf.Sin(currAngle),
|
||||
p.y + insideRadius * Mathf.Cos(currAngle));
|
||||
if (emptyColor != Color.clear) DrawTriangle(vh, p, p1, p4, emptyColor);
|
||||
DrawPolygon(vh, p1, p2, p3, p4, color);
|
||||
p1 = p4;
|
||||
p2 = p3;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 画贝塞尔曲线
|
||||
/// </summary>
|
||||
/// <param name="vh"></param>
|
||||
/// <param name="sp">起始点</param>
|
||||
/// <param name="ep">结束点</param>
|
||||
/// <param name="cp1">控制点1</param>
|
||||
/// <param name="cp2">控制点2</param>
|
||||
/// <param name="lineWidth">曲线宽</param>
|
||||
/// <param name="lineColor">曲线颜色</param>
|
||||
public static void DrawCurves(VertexHelper vh, Vector3 sp, Vector3 ep, Vector3 cp1, Vector3 cp2,
|
||||
float lineWidth, Color lineColor, float smoothness)
|
||||
{
|
||||
var dist = Vector3.Distance(sp, ep);
|
||||
var segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness));
|
||||
ChartHelper.GetBezierList2(ref s_CurvesPosList, sp, ep, segment, cp1, cp2);
|
||||
if (s_CurvesPosList.Count > 1)
|
||||
{
|
||||
var start = s_CurvesPosList[0];
|
||||
var to = Vector3.zero;
|
||||
var dir = s_CurvesPosList[1] - start;
|
||||
var diff = Vector3.Cross(dir, Vector3.forward).normalized * lineWidth;
|
||||
var startUp = start - diff;
|
||||
var startDn = start + diff;
|
||||
for (int i = 1; i < s_CurvesPosList.Count; i++)
|
||||
{
|
||||
to = s_CurvesPosList[i];
|
||||
diff = Vector3.Cross(to - start, Vector3.forward).normalized * lineWidth;
|
||||
var toUp = to - diff;
|
||||
var toDn = to + diff;
|
||||
DrawPolygon(vh, startUp, toUp, toDn, startDn, lineColor);
|
||||
startUp = toUp;
|
||||
startDn = toDn;
|
||||
start = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/API/ChartDrawer.cs.meta
Normal file
11
Scripts/UI/API/ChartDrawer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a15bf61aa655d4175b71edd0b603036a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
158
Scripts/UI/API/CoordinateChart_API.cs
Normal file
158
Scripts/UI/API/CoordinateChart_API.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// The basic class of rectangular coordinate chart,such as LineChart,BarChart and ScatterChart.
|
||||
/// 直角坐标系类型图表的基类,如折线图LineChart,柱状图BarChart,散点图ScatterChart都属于这类型的图表。
|
||||
/// 不可用直接将CoordinateChart绑定到GameObject上。
|
||||
/// </summary>
|
||||
public partial class CoordinateChart
|
||||
{
|
||||
/// <summary>
|
||||
/// The lower left position x of coordinate system.
|
||||
/// 坐标系的左下角坐标X。
|
||||
/// </summary>
|
||||
public float coordinateX { get { return m_Grid.left; } }
|
||||
/// <summary>
|
||||
/// The lower left position y of coordinate system.
|
||||
/// 坐标系的左下角坐标Y。
|
||||
/// </summary>
|
||||
public float coordinateY { get { return m_Grid.bottom; } }
|
||||
/// <summary>
|
||||
/// the width of coordinate system。
|
||||
/// 坐标系的宽。
|
||||
/// </summary>
|
||||
public float coordinateWid { get { return chartWidth - m_Grid.left - m_Grid.right; } }
|
||||
/// <summary>
|
||||
/// the height of coordinate system。
|
||||
/// 坐标系的高。
|
||||
/// </summary>
|
||||
public float coordinateHig { get { return chartHeight - m_Grid.top - m_Grid.bottom; } }
|
||||
/// <summary>
|
||||
/// grid component.
|
||||
/// 网格组件。
|
||||
/// </summary>
|
||||
public Grid grid { get { return m_Grid; } }
|
||||
/// <summary>
|
||||
/// the x axises,xAxises[0] is the first x axis, xAxises[1] is the second x axis.
|
||||
/// 两个x轴。
|
||||
/// </summary>
|
||||
public List<XAxis> xAxises { get { return m_XAxises; } }
|
||||
/// <summary>
|
||||
/// the y axises, yAxises[0] is the first y axis, yAxises[1] is the second y axis.
|
||||
/// 两个y轴。
|
||||
/// </summary>
|
||||
public List<YAxis> yAxises { get { return m_YAxises; } }
|
||||
/// <summary>
|
||||
/// dataZoom component.
|
||||
/// 区域缩放组件。
|
||||
/// </summary>
|
||||
public DataZoom dataZoom { get { return m_DataZoom; } }
|
||||
/// <summary>
|
||||
/// visualMap component.
|
||||
/// 视觉映射组件。
|
||||
/// </summary>
|
||||
public VisualMap visualMap { get { return m_VisualMap; } }
|
||||
|
||||
|
||||
/// <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>
|
||||
/// Remove all data of axises.
|
||||
/// 清除所有x轴和y轴的类目数据。
|
||||
/// </summary>
|
||||
public void ClearAxisData()
|
||||
{
|
||||
foreach (var item in m_XAxises) item.data.Clear();
|
||||
foreach (var item in m_YAxises) item.data.Clear();
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
m_XAxises[xAxisIndex].AddData(category);
|
||||
m_XAxisChanged = true;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
m_YAxises[yAxisIndex].AddData(category);
|
||||
m_YAxisChanged = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// reutrn true when all the show axis is `Value` type.
|
||||
/// 纯数值坐标。
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool IsValue()
|
||||
{
|
||||
foreach (var axis in m_XAxises)
|
||||
{
|
||||
if (axis.show && !axis.IsValue()) return false;
|
||||
}
|
||||
foreach (var axis in m_YAxises)
|
||||
{
|
||||
if (axis.show && !axis.IsValue()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsCategory()
|
||||
{
|
||||
foreach (var axis in m_XAxises)
|
||||
{
|
||||
if (axis.show && !axis.IsCategory()) return false;
|
||||
}
|
||||
foreach (var axis in m_YAxises)
|
||||
{
|
||||
if (axis.show && !axis.IsCategory()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsInCooridate(Vector2 local)
|
||||
{
|
||||
if (local.x < coordinateX - 1 || local.x > coordinateX + coordinateWid + 1 ||
|
||||
local.y < coordinateY - 1 || local.y > coordinateY + coordinateHig + 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
Scripts/UI/API/CoordinateChart_API.cs.meta
Normal file
11
Scripts/UI/API/CoordinateChart_API.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 032e5d8f514854bf39f5fd7052119271
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user