UI update 02

This commit is contained in:
FloatGaming
2026-06-30 21:21:30 +08:00
parent b2a1e307a4
commit f62f643cfc
1666 changed files with 211081 additions and 22799 deletions
@@ -0,0 +1,313 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[RequireComponent(typeof(LineChart))]
public class Example00_CheatSheet : MonoBehaviour
{
private LineChart chart;
private float speed = 100f;
private void OnEnable()
{
StartCoroutine(CheatSheet());
}
IEnumerator CheatSheet()
{
StartCoroutine(InitChart());
while (true)
{
StartCoroutine(ComponentTitle());
yield return new WaitForSeconds(2);
StartCoroutine(ComponentAxis());
yield return new WaitForSeconds(2);
StartCoroutine(ComponentGrid());
yield return new WaitForSeconds(2);
StartCoroutine(ComponentSerie());
yield return new WaitForSeconds(4);
StartCoroutine(ComponentLegend());
yield return new WaitForSeconds(4);
StartCoroutine(ComponentTheme());
yield return new WaitForSeconds(4);
StartCoroutine(ComponentDataZoom());
yield return new WaitForSeconds(5);
StartCoroutine(ComponentVisualMap());
yield return new WaitForSeconds(3);
}
}
IEnumerator InitChart()
{
chart = gameObject.GetComponent<LineChart>();
chart.EnsureChartComponent<Title>().show = true;
chart.EnsureChartComponent<Title>().text = "术语解析-组件";
var grid = chart.EnsureChartComponent<GridCoord>();
grid.bottom = 30;
grid.right = 30;
grid.left = 50;
grid.top = 80;
chart.RemoveChartComponent<VisualMap>();
chart.RemoveData();
chart.AddSerie<Bar>("Bar");
chart.AddSerie<Line>("Line");
for (int i = 0; i < 8; i++)
{
chart.AddXAxisData("x" + (i + 1));
chart.AddData(0, Random.Range(10, 100));
chart.AddData(1, Random.Range(30, 100));
}
yield return null;
}
IEnumerator ComponentTitle()
{
chart.EnsureChartComponent<Title>().text = "术语解析 - 组件";
chart.EnsureChartComponent<Title>().subText = "Title 标题:可指定主标题和子标题";
chart.EnsureChartComponent<XAxis>().show = true;
chart.EnsureChartComponent<YAxis>().show = true;
chart.EnsureChartComponent<Legend>().show = false;
chart.series[0].show = false;
chart.series[1].show = false;
for (int i = 0; i < 4; i++)
{
chart.EnsureChartComponent<Title>().show = !chart.EnsureChartComponent<Title>().show;
chart.RefreshChart();
yield return new WaitForSeconds(0.2f);
}
chart.EnsureChartComponent<Title>().show = true;
chart.RefreshChart();
}
IEnumerator ComponentAxis()
{
chart.EnsureChartComponent<Title>().subText = "Axis 坐标轴:配置X和Y轴的轴线、刻度、标签等样式外观配置";
chart.series[0].show = false;
chart.series[1].show = false;
var xAxis = chart.EnsureChartComponent<XAxis>();
var yAxis = chart.EnsureChartComponent<YAxis>();
for (int i = 0; i < 4; i++)
{
xAxis.show = !xAxis.show;
yAxis.show = !yAxis.show;
chart.RefreshChart();
yield return new WaitForSeconds(0.2f);
}
xAxis.show = true;
yAxis.show = true;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
}
IEnumerator ComponentGrid()
{
chart.EnsureChartComponent<Title>().subText = "Grid 网格:调整坐标系边距和颜色等";
var grid = chart.EnsureChartComponent<GridCoord>();
for (int i = 0; i < 4; i++)
{
grid.backgroundColor = i % 2 == 0 ? Color.clear : Color.grey;
chart.RefreshChart();
yield return new WaitForSeconds(0.2f);
}
grid.backgroundColor = Color.clear;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
}
IEnumerator ComponentSerie()
{
chart.EnsureChartComponent<Title>().subText = "Serie 系列:调整坐标系边距和颜色等";
chart.series[0].show = true;
chart.series[1].show = true;
chart.AnimationReset();
chart.RefreshChart();
yield return new WaitForSeconds(1.2f);
for (int i = 0; i < 4; i++)
{
chart.series[0].show = !chart.series[0].show;
chart.series[1].show = !chart.series[1].show;
chart.RefreshChart();
yield return new WaitForSeconds(0.2f);
}
chart.series[0].show = true;
chart.series[1].show = true;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
}
IEnumerator ComponentLegend()
{
chart.EnsureChartComponent<Title>().subText = "Legend 图例:展示不同系列的名字和颜色,可控制系列显示等";
var legend = chart.EnsureChartComponent<Legend>();
legend.show = true;
var grid = chart.EnsureChartComponent<GridCoord>();
grid.top = 80;
legend.location.top = 50;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
for (int i = 0; i < 4; i++)
{
legend.show = !legend.show;
chart.RefreshChart();
yield return new WaitForSeconds(0.2f);
}
legend.show = true;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
chart.ClickLegendButton(0, "Line", false);
yield return new WaitForSeconds(0.2f);
chart.ClickLegendButton(0, "Line", true);
yield return new WaitForSeconds(0.5f);
chart.ClickLegendButton(1, "Bar", false);
yield return new WaitForSeconds(0.2f);
chart.ClickLegendButton(1, "Bar", true);
yield return new WaitForSeconds(0.5f);
}
IEnumerator ComponentTheme()
{
chart.EnsureChartComponent<Title>().subText = "Theme 主题:可从全局上配置图表的颜色、字体等效果,支持默认主题切换";
yield return new WaitForSeconds(1f);
chart.EnsureChartComponent<Title>().subText = "Theme 主题:Light主题";
chart.UpdateTheme(ThemeType.Light);
yield return new WaitForSeconds(1f);
chart.EnsureChartComponent<Title>().subText = "Theme 主题:Dark主题";
chart.UpdateTheme(ThemeType.Dark);
yield return new WaitForSeconds(1f);
chart.EnsureChartComponent<Title>().subText = "Theme 主题:Default主题";
chart.UpdateTheme(ThemeType.Default);
yield return new WaitForSeconds(1f);
}
IEnumerator ComponentDataZoom()
{
chart.EnsureChartComponent<Title>().subText = "DataZoom 区域缩放:可通过拖、拽、缩小、放大来观察细节数据";
var grid = chart.EnsureChartComponent<GridCoord>();
grid.bottom = 70;
var dataZoom = chart.EnsureChartComponent<DataZoom>();
dataZoom.enable = true;
dataZoom.supportInside = true;
dataZoom.supportSlider = true;
dataZoom.start = 0;
dataZoom.end = 100;
chart.RefreshChart();
for (int i = 0; i < 4; i++)
{
dataZoom.supportSlider = !dataZoom.supportSlider;
chart.RefreshChart();
yield return new WaitForSeconds(0.2f);
}
dataZoom.supportSlider = true;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
while (dataZoom.start < 40)
{
dataZoom.start += speed * Time.deltaTime * 0.8f;
chart.RefreshDataZoom();
chart.RefreshChart();
yield return null;
}
while (dataZoom.end > 60)
{
dataZoom.end -= speed * Time.deltaTime * 0.8f;
chart.RefreshDataZoom();
chart.RefreshChart();
yield return null;
}
while (dataZoom.start > 0)
{
dataZoom.start -= speed * Time.deltaTime * 0.8f;
dataZoom.end -= speed * Time.deltaTime * 0.8f;
chart.RefreshDataZoom();
chart.RefreshChart();
yield return null;
}
while (dataZoom.end < 100)
{
dataZoom.start += speed * Time.deltaTime * 0.8f;
dataZoom.end += speed * Time.deltaTime * 0.8f;
chart.RefreshDataZoom();
chart.RefreshChart();
yield return null;
}
while (dataZoom.start > 0 || dataZoom.end < 100)
{
dataZoom.start -= speed * Time.deltaTime * 0.8f;
dataZoom.end += speed * Time.deltaTime * 0.8f;
chart.RefreshDataZoom();
chart.RefreshChart();
yield return null;
}
}
IEnumerator ComponentVisualMap()
{
chart.EnsureChartComponent<Title>().subText = "VisualMap 视觉映射:可从全局上配置图表的颜色、字体等效果,支持默认主题切换";
var visualMap = chart.EnsureChartComponent<VisualMap>();
visualMap.show = true;
visualMap.showUI = true;
visualMap.orient = Orient.Vertical;
visualMap.calculable = true;
visualMap.min = 0;
visualMap.max = 100;
visualMap.range[0] = 0;
visualMap.range[1] = 100;
var colors = new List<string>
{
"#313695",
"#4575b4",
"#74add1",
"#abd9e9",
"#e0f3f8",
"#ffffbf",
"#fee090",
"#fdae61",
"#f46d43",
"#d73027",
"#a50026"
};
visualMap.AddColors(colors);
var grid = chart.EnsureChartComponent<GridCoord>();
grid.left = 80;
grid.bottom = 100;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
while (visualMap.rangeMin < 40)
{
visualMap.rangeMin += speed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
while (visualMap.rangeMax > 60)
{
visualMap.rangeMax -= speed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
while (visualMap.rangeMin > 0 || visualMap.rangeMax < 100)
{
visualMap.rangeMin -= speed * Time.deltaTime;
visualMap.rangeMax += speed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 677b2673e728a4e308f26a5a9b236277
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,238 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XCharts.Runtime;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Example
{
[DisallowMultipleComponent]
[RequireComponent(typeof(BaseChart))]
public class Example01_RandomData : MonoBehaviour
{
public bool loopAdd = false;
public float loopAddTime = 1f;
public bool loopUpdate = false;
public float loopUpadteTime = 1f;
public int maxCache = 0;
public bool insertDataToHead = false;
BaseChart chart;
float lastAddTime;
float lastUpdateTime;
int dataCount;
int lastMaxCache = 0;
bool lastInsertDataToHead = false;
void Awake()
{
chart = gameObject.GetComponent<BaseChart>();
chart.onInit = () =>
{
dataCount = chart.GetSerie(0).dataCount;
SetMaxCache(maxCache);
SetInsertDataToHead(insertDataToHead);
lastMaxCache = maxCache;
lastInsertDataToHead = insertDataToHead;
};
}
void SetMaxCache(int maxCache)
{
chart.SetMaxCache(maxCache);
}
void SetInsertDataToHead(bool insertDataToHead)
{
foreach (var serie in chart.series)
serie.insertDataToHead = insertDataToHead;
var coms = chart.GetChartComponents<XAxis>();
if (coms != null)
{
foreach (var com in coms)
{
var axis = com as XAxis;
if (axis.type == Axis.AxisType.Category)
{
axis.insertDataToHead = insertDataToHead;
Debug.LogError("axis:" + axis + "," + insertDataToHead);
}
}
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddData();
}
else if (Input.GetKeyDown(KeyCode.U))
{
UpdateData();
}
else if (Input.GetKeyDown(KeyCode.C))
{
chart.ClearData();
}
if (lastMaxCache != maxCache)
{
lastMaxCache = maxCache;
SetMaxCache(maxCache);
}
if (lastInsertDataToHead != insertDataToHead)
{
lastInsertDataToHead = insertDataToHead;
SetInsertDataToHead(insertDataToHead);
}
lastAddTime += Time.deltaTime;
if (loopAdd && lastAddTime >= loopAddTime)
{
lastAddTime = 0;
AddData();
}
lastUpdateTime += Time.deltaTime;
if (loopUpdate && lastUpdateTime >= loopUpadteTime)
{
lastUpdateTime = 0;
UpdateData();
}
}
void AddData()
{
if (chart is HeatmapChart)
{
var xAxis = chart.GetChartComponent<XAxis>();
var yAxis = chart.GetChartComponent<YAxis>();
if (xAxis != null && yAxis != null)
{
chart.AddXAxisData((xAxis.GetAddedDataCount() + 1).ToString());
for (int i = 0; i < yAxis.data.Count; i++)
{
chart.AddData(0, xAxis.GetAddedDataCount() - 1, i, Random.Range(10, 90));
}
}
return;
}
else
{
AddXAxisData();
var xAxis = chart.GetChartComponent<XAxis>();
foreach (var serie in chart.series)
{
AddSerieRandomData(serie, xAxis);
}
}
}
void AddXAxisData()
{
var xAxes = chart.GetChartComponents<XAxis>();
foreach (var com in xAxes)
{
var xAxis = com as XAxis;
if (xAxis.type == Axis.AxisType.Category)
{
chart.AddXAxisData("x" + (xAxis.GetAddedDataCount() + 1), xAxis.index);
}
}
}
void UpdateData()
{
foreach (var serie in chart.series)
{
UpdateSerieRandomData(serie);
}
}
void AddSerieRandomData(Serie serie, XAxis xAxis)
{
if (serie is Line || serie is Bar || serie is Scatter || serie is EffectScatter)
{
if (xAxis.type == Axis.AxisType.Category)
{
chart.AddData(serie.index, Random.Range(10, 90), "data" + serie.dataCount);
}
else
{
if (serie is Line)
chart.AddData(serie.index, dataCount++, Random.Range(10, 90), "data" + serie.dataCount);
else
chart.AddData(serie.index, Random.Range(10, 90), Random.Range(10, 90), "data" + serie.dataCount);
}
}
else if (serie is Ring)
{
chart.AddData(serie.index, Random.Range(10, 90), 100, "data" + serie.dataCount);
}
else if (serie is Radar)
{
var list = new System.Collections.Generic.List<double>();
for (int i = 0; i < 5; i++)
list.Add(Random.Range(10, 90));
chart.AddData(serie.index, list, "data" + serie.dataCount);
}
else if (serie is Candlestick)
{
var open = Random.Range(20, 60);
var close = Random.Range(40, 90);
var lowest = Random.Range(0, 50);
var heighest = Random.Range(50, 100);
chart.AddData(serie.index, serie.dataCount, open, close, lowest, heighest);
}
else if (serie is Heatmap)
{
var yAxis = chart.GetChartComponent<YAxis>(serie.yAxisIndex);
for (int i = 0; i < yAxis.data.Count; i++)
{
chart.AddData(serie.index, xAxis.GetAddedDataCount() - 1, i, Random.Range(0, 150));
}
}
else
{
chart.AddData(serie.index, Random.Range(10, 90), "data" + serie.dataCount);
}
}
void UpdateSerieRandomData(Serie serie)
{
var index = Random.Range(0, serie.dataCount);
if (serie is Ring)
{
chart.UpdateData(serie.index, index, 0, Random.Range(10, 90));
}
else if (serie is Radar)
{
var dimension = Random.Range(0, 5);
chart.UpdateData(serie.index, index, dimension, Random.Range(10, 90));
}
else if (serie is Heatmap)
{
var xAxis = chart.GetChartComponent<XAxis>();
var yAxis = chart.GetChartComponent<YAxis>();
var xIndex = Random.Range(0, xAxis.data.Count);
var yIndex = Random.Range(0, yAxis.data.Count);
chart.UpdateData(serie.index, xIndex, yIndex, Random.Range(10, 90));
}
else if (serie is Candlestick)
{
var open = Random.Range(20, 60);
var close = Random.Range(40, 90);
var lowest = Random.Range(0, 50);
var heighest = Random.Range(50, 100);
chart.UpdateData(serie.index, index, new List<double> { open, close, lowest, heighest });
}
else
{
chart.UpdateData(serie.index, index, Random.Range(10, 90));
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c7cdc29e9a8040fdbc7100c3325e9ba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,113 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XCharts.Runtime;
using XUGL;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[RequireComponent(typeof(BaseChart))]
public class Example02_ChartEvent : MonoBehaviour
{
BaseChart chart;
void Awake()
{
chart = gameObject.GetComponent<BaseChart>();
chart.onPointerEnter = OnPointerEnter;
chart.onPointerExit = OnPointerExit;
chart.onPointerDown = OnPointerDown;
chart.onPointerUp = OnPointerUp;
chart.onPointerClick = OnPointerClick;
chart.onScroll = OnScroll;
chart.onSerieClick = OnSerieClick;
chart.onSerieEnter = OnSerieEnter;
chart.onSerieExit = OnSerieExit;
chart.onDraw = OnDraw;
chart.onDrawBeforeSerie = OnDrawBeforeSerie;
chart.onDrawAfterSerie = OnDrawAfterSerie;
chart.onDrawTop = OnDrawTop;
}
void OnPointerEnter(PointerEventData eventData, BaseGraph chart)
{
Debug.Log("enter:" + chart);
}
void OnPointerExit(PointerEventData eventData, BaseGraph chart)
{
Debug.Log("exit:" + chart);
}
void OnPointerDown(PointerEventData eventData, BaseGraph chart)
{
Debug.Log("down:" + chart);
}
void OnPointerUp(PointerEventData eventData, BaseGraph chart)
{
Debug.Log("up:" + chart);
}
void OnPointerClick(PointerEventData eventData, BaseGraph chart)
{
Debug.Log("click:" + chart);
}
void OnScroll(PointerEventData eventData, BaseGraph chart)
{
Debug.Log("scroll:" + chart);
}
void OnSerieClick(SerieEventData data)
{
Debug.Log("OnSerieClick: " + data.serieIndex + " " + data.dataIndex + " " + data.dimension + " " + data.value);
}
void OnSerieEnter(SerieEventData data)
{
Debug.Log("OnSerieEnter: " + data.serieIndex + " " + data.dataIndex + " " + data.dimension + " " + data.value);
}
void OnSerieExit(SerieEventData data)
{
Debug.Log("OnSerieExit: " + data.serieIndex + " " + data.dataIndex + " " + data.dimension + " " + data.value);
}
void OnDraw(VertexHelper vh)
{
//Debug.Log("OnDraw");
}
void OnDrawBeforeSerie(VertexHelper vh, Serie serie)
{
//Debug.Log("OnDrawBeforeSerie: " + serie.index);
}
void OnDrawAfterSerie(VertexHelper vh, Serie serie)
{
//Debug.Log("OnDrawAfterSerie: " + serie.index);
if (serie.index != 0) return;
var dataPoints = serie.context.dataPoints;
if (dataPoints.Count > 4)
{
var pos = dataPoints[3];
var grid = chart.GetChartComponent<GridCoord>();
var zeroPos = new Vector3(grid.context.x, grid.context.y);
var startPos = new Vector3(pos.x, zeroPos.y);
var endPos = new Vector3(pos.x, zeroPos.y + grid.context.height);
UGL.DrawLine(vh, startPos, endPos, chart.theme.serie.lineWidth, Color.blue);
UGL.DrawCricle(vh, pos, 5, Color.blue);
}
}
void OnDrawTop(VertexHelper vh)
{
//Debug.Log("OnDrawTop");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c549dc496cd86467e8286252906562cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,38 @@
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example03_ChartAnimation : MonoBehaviour
{
BaseChart chart;
void Awake()
{
chart = gameObject.GetComponent<BaseChart>();
if (chart == null)
{
chart = gameObject.AddComponent<BarChart>();
chart.Init();
}
var serie = chart.GetSerie(0);
serie.animation.enable = true;
//自定义每个数据项的渐入延时
serie.animation.fadeIn.delayFunction = CustomFadeInDelay;
//自定义每个数据项的渐入时长
serie.animation.fadeIn.durationFunction = CustomFadeInDuration;
}
float CustomFadeInDelay(int dataIndex)
{
return dataIndex * 1000;
}
float CustomFadeInDuration(int dataIndex)
{
return dataIndex * 1000 + 1000;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6258ca3b055714eac92804f501011b53
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,50 @@
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example04_DataZoom : MonoBehaviour
{
BaseChart chart;
void Awake()
{
chart = gameObject.GetComponent<BaseChart>();
if (chart == null) return;
var dataZoom = chart.GetChartComponent<DataZoom>();
if (dataZoom == null) return;
dataZoom.marqueeStyle.onStart = OnMarqueeStart;
dataZoom.marqueeStyle.onEnd = OnMarqueeEnd;
dataZoom.marqueeStyle.onGoing = OnMarquee;
}
void OnMarqueeStart(DataZoom dataZoom)
{
//Debug.Log("OnMarqueeStart:" + dataZoom);
}
void OnMarquee(DataZoom dataZoom)
{
//Debug.Log("OnMarquee:" + dataZoom);
}
void OnMarqueeEnd(DataZoom dataZoom)
{
//Debug.Log("OnMarqueeEnd:" + dataZoom);
var serie = chart.GetSerie(0);
foreach (var serieData in serie.data)
{
if (dataZoom.IsInMarqueeArea(serieData))
{
serieData.EnsureComponent<ItemStyle>().color = Color.red;
}
else
{
serieData.EnsureComponent<ItemStyle>().color = Color.clear;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f2cc0ca220d904377984528de6214b97
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,95 @@
using UnityEngine;
using XCharts.Runtime;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example05_DynamicChart : MonoBehaviour
{
BaseChart chart;
void Awake() { }
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
AddPieChart("Dynamic PieChart");
}
if (Input.GetKeyDown(KeyCode.L))
{
AddLineChart("Dynamic LineChart");
}
}
GameObject CreateChartObject(string chartName)
{
for (int i = transform.childCount - 1; i >= 0; i--)
{
Destroy(transform.GetChild(i).gameObject);
}
var chartObject = new GameObject();
chartObject.name = chartName;
chartObject.transform.SetParent(transform);
chartObject.transform.localScale = Vector3.one;
chartObject.transform.localPosition = Vector3.zero;
return chartObject;
}
void AddPieChart(string chartName)
{
var chartObject = CreateChartObject(chartName);
var chart = chartObject.AddComponent<PieChart>();
chart.SetSize(580, 300);
chart.EnsureChartComponent<Title>().show = true;
chart.EnsureChartComponent<Title>().text = chartName;
chart.EnsureChartComponent<Tooltip>().show = true;
chart.EnsureChartComponent<Legend>().show = true;
chart.RemoveData();
chart.AddSerie<Pie>();
for (int i = 0; i < 3; i++)
{
chart.AddData(0, Random.Range(10, 20), "pie" + (i + 1));
}
}
void AddLineChart(string chartName)
{
var chartObject = CreateChartObject(chartName);
var chart = chartObject.AddComponent<PieChart>();
chart.SetSize(580, 300);
chart.EnsureChartComponent<Title>().show = true;
chart.EnsureChartComponent<Title>().text = chartName;
chart.EnsureChartComponent<Legend>().show = false;
var tooltip = chart.EnsureChartComponent<Tooltip>();
tooltip.trigger = Tooltip.Trigger.Axis;
var xAxis = chart.EnsureChartComponent<XAxis>();
var yAxis = chart.EnsureChartComponent<YAxis>();
xAxis.splitNumber = 10;
xAxis.boundaryGap = true;
xAxis.show = true;
yAxis.show = true;
xAxis.type = Axis.AxisType.Category;
yAxis.type = Axis.AxisType.Value;
chart.RemoveData();
chart.AddSerie<Line>();
for (int i = 0; i < 10; i++)
{
chart.AddXAxisData("x" + (i + 1));
chart.AddData(0, Random.Range(10, 100));
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c3dbcd4fb120c4508b7bba52b41fbdb9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,260 @@
using System.Collections;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example10_LineChart : MonoBehaviour
{
private LineChart chart;
private Serie serie;
private int m_DataNum = 8;
private void OnEnable()
{
StartCoroutine(PieDemo());
}
IEnumerator PieDemo()
{
while (true)
{
StartCoroutine(AddSimpleLine());
yield return new WaitForSeconds(2);
StartCoroutine(ChangeLineType());
yield return new WaitForSeconds(8);
StartCoroutine(LineAreaStyleSettings());
yield return new WaitForSeconds(5);
StartCoroutine(LineArrowSettings());
yield return new WaitForSeconds(2);
StartCoroutine(LineSymbolSettings());
yield return new WaitForSeconds(7);
StartCoroutine(LineLabelSettings());
yield return new WaitForSeconds(3);
StartCoroutine(LineMutilSerie());
yield return new WaitForSeconds(5);
}
}
IEnumerator AddSimpleLine()
{
chart = gameObject.GetComponent<LineChart>();
if (chart == null){
chart = gameObject.AddComponent<LineChart>();
chart.Init();
}
chart.GetChartComponent<Title>().text = "LineChart - 折线图";
chart.GetChartComponent<Title>().subText = "普通折线图";
var yAxis = chart.GetChartComponent<YAxis>();
yAxis.minMaxType = Axis.AxisMinMaxType.Custom;
yAxis.min = 0;
yAxis.max = 100;
chart.RemoveData();
serie = chart.AddSerie<Line>("Line");
for (int i = 0; i < m_DataNum; i++)
{
chart.AddXAxisData("x" + (i + 1));
chart.AddData(0, UnityEngine.Random.Range(30, 90));
}
yield return new WaitForSeconds(1);
}
IEnumerator ChangeLineType()
{
chart.GetChartComponent<Title>().subText = "LineTyle - 曲线图";
serie.lineType = LineType.Smooth;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "LineTyle - 阶梯线图";
serie.lineType = LineType.StepStart;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.lineType = LineType.StepMiddle;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.lineType = LineType.StepEnd;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "LineTyle - 虚线";
serie.lineStyle.type = LineStyle.Type.Dashed;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "LineTyle - 点线";
serie.lineStyle.type = LineStyle.Type.Dotted;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "LineTyle - 点划线";
serie.lineStyle.type = LineStyle.Type.DashDot;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "LineTyle - 双点划线";
serie.lineStyle.type = LineStyle.Type.DashDotDot;
chart.RefreshChart();
serie.lineType = LineType.Normal;
chart.RefreshChart();
}
IEnumerator LineAreaStyleSettings()
{
chart.GetChartComponent<Title>().subText = "AreaStyle 面积图";
serie.EnsureComponent<AreaStyle>();
serie.areaStyle.show = true;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
chart.GetChartComponent<Title>().subText = "AreaStyle 面积图";
serie.lineType = LineType.Smooth;
serie.areaStyle.show = true;
chart.RefreshChart();
yield return new WaitForSeconds(1f);
chart.GetChartComponent<Title>().subText = "AreaStyle 面积图 - 调整透明度";
while (serie.areaStyle.opacity > 0.4)
{
serie.areaStyle.opacity -= 0.6f * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "AreaStyle 面积图 - 渐变";
serie.areaStyle.toColor = Color.white;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
IEnumerator LineArrowSettings()
{
chart.GetChartComponent<Title>().subText = "LineArrow 头部箭头";
chart.GetSerie(0).EnsureComponent<LineArrow>();
serie.lineArrow.show = true;
serie.lineArrow.position = LineArrow.Position.Start;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "LineArrow 尾部箭头";
serie.lineArrow.position = LineArrow.Position.End;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.lineArrow.show = false;
}
/// <summary>
/// SerieSymbol 相关设置
/// </summary>
/// <returns></returns>
IEnumerator LineSymbolSettings()
{
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记";
while (serie.symbol.size < 5)
{
serie.symbol.size += 2.5f * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记 - 空心圆";
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记 - 实心圆";
serie.symbol.type = SymbolType.Circle;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记 - 三角形";
serie.symbol.type = SymbolType.Triangle;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记 - 正方形";
serie.symbol.type = SymbolType.Rect;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记 - 菱形";
serie.symbol.type = SymbolType.Diamond;
chart.RefreshChart();
yield return new WaitForSeconds(1);
chart.GetChartComponent<Title>().subText = "SerieSymbol 图形标记";
serie.symbol.type = SymbolType.EmptyCircle;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
/// <summary>
/// SerieLabel相关配置
/// </summary>
/// <returns></returns>
IEnumerator LineLabelSettings()
{
chart.GetChartComponent<Title>().subText = "SerieLabel 文本标签";
serie.EnsureComponent<LabelStyle>();
chart.RefreshChart();
while (serie.label.offset[1] < 20)
{
serie.label.offset = new Vector3(serie.label.offset.x, serie.label.offset.y + 20f * Time.deltaTime);
chart.RefreshChart();
yield return null;
}
yield return new WaitForSeconds(1);
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.label.textStyle.color = Color.white;
serie.label.background.color = Color.grey;
serie.labelDirty = true;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.label.show = false;
chart.RefreshChart();
}
/// <summary>
/// 添加多条线图
/// </summary>
/// <returns></returns>
IEnumerator LineMutilSerie()
{
chart.GetChartComponent<Title>().subText = "多系列";
var serie2 = chart.AddSerie<Line>("Line2");
serie2.lineType = LineType.Normal;
for (int i = 0; i < m_DataNum; i++)
{
chart.AddData(1, UnityEngine.Random.Range(30, 90));
}
yield return new WaitForSeconds(1);
var serie3 = chart.AddSerie<Line>("Line3");
serie3.lineType = LineType.Normal;
for (int i = 0; i < m_DataNum; i++)
{
chart.AddData(2, UnityEngine.Random.Range(30, 90));
}
yield return new WaitForSeconds(1);
var yAxis = chart.GetChartComponent<YAxis>();
yAxis.minMaxType = Axis.AxisMinMaxType.Default;
chart.GetChartComponent<Title>().subText = "多系列 - 堆叠";
serie.stack = "samename";
serie2.stack = "samename";
serie3.stack = "samename";
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6155c7e0df4504ebfaf0c671ae200197
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,62 @@
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example11_AddSinCurve : MonoBehaviour
{
private float time;
public int angle;
private LineChart chart;
void Awake()
{
chart = gameObject.GetComponent<LineChart>();
if (chart == null)
{
chart = gameObject.AddComponent<LineChart>();
chart.Init();
}
chart.EnsureChartComponent<Title>().show = true;
chart.EnsureChartComponent<Title>().text = "Sin Curve";
chart.EnsureChartComponent<Tooltip>().show = true;
chart.EnsureChartComponent<Legend>().show = false;
var xAxis = chart.EnsureChartComponent<XAxis>();
var yAxis = chart.EnsureChartComponent<YAxis>();
xAxis.show = true;
yAxis.show = true;
xAxis.type = Axis.AxisType.Value;
yAxis.type = Axis.AxisType.Value;
xAxis.boundaryGap = false;
xAxis.maxCache = 0;
chart.series[0].maxCache = 0;
chart.RemoveData();
var serie = chart.AddSerie<Line>();
serie.symbol.show = false;
serie.lineType = LineType.Normal;
for (angle = 0; angle < 1080; angle++)
{
float xvalue = Mathf.PI / 180 * angle;
float yvalue = Mathf.Sin(xvalue);
chart.AddData(0, xvalue, yvalue);
}
}
void Update()
{
if (angle > 3000) return;
angle++;
float xvalue = Mathf.PI / 180 * angle;
float yvalue = Mathf.Sin(xvalue);
chart.AddData(0, xvalue, yvalue);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b380753d3cb4149c4a3a65a1816e0cc7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,41 @@
using UnityEngine;
using UnityEngine.UI;
using XCharts.Runtime;
using XUGL;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example12_CustomDrawing : MonoBehaviour
{
LineChart chart;
void Awake()
{
chart = gameObject.GetComponent<LineChart>();
if (chart == null) return;
chart.onDraw = delegate(VertexHelper vh) { };
// or
chart.onDrawBeforeSerie = delegate(VertexHelper vh, Serie serie) { };
// or
chart.onDrawAfterSerie = delegate(VertexHelper vh, Serie serie)
{
if (serie.index != 0) return;
var dataPoints = serie.context.dataPoints;
if (dataPoints.Count > 0)
{
var pos = dataPoints[3];
var grid = chart.GetChartComponent<GridCoord>();
var zeroPos = new Vector3(grid.context.x, grid.context.y);
var startPos = new Vector3(pos.x, zeroPos.y);
var endPos = new Vector3(pos.x, zeroPos.y + grid.context.height);
UGL.DrawLine(vh, startPos, endPos, chart.theme.serie.lineWidth, Color.blue);
UGL.DrawCricle(vh, pos, 5, Color.blue);
}
};
// or
chart.onDrawTop = delegate(VertexHelper vh) { };
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: da550ad36be5f442e96ad021cc10ca68
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,61 @@
using UnityEngine;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example13_LineSimple : MonoBehaviour
{
void Awake()
{
AddData();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddData();
}
}
void AddData()
{
var chart = gameObject.GetComponent<LineChart>();
if (chart == null)
{
chart = gameObject.AddComponent<LineChart>();
chart.Init();
}
chart.EnsureChartComponent<Title>().show = true;
chart.EnsureChartComponent<Title>().text = "Line Simple";
chart.EnsureChartComponent<Tooltip>().show = true;
chart.EnsureChartComponent<Legend>().show = false;
var xAxis = chart.EnsureChartComponent<XAxis>();
var yAxis = chart.EnsureChartComponent<YAxis>();
xAxis.show = true;
yAxis.show = true;
xAxis.type = Axis.AxisType.Category;
yAxis.type = Axis.AxisType.Value;
xAxis.splitNumber = 10;
xAxis.boundaryGap = true;
chart.RemoveData();
chart.AddSerie<Line>();
chart.AddSerie<Line>();
for (int i = 0; i < 20; i++)
{
chart.AddXAxisData("x" + i);
chart.AddData(0, Random.Range(10, 20));
chart.AddData(1, Random.Range(10, 20));
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c6d0f65efd8e14ebdafa172e0ccbd562
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,158 @@
using System.Collections;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example20_BarChart : MonoBehaviour
{
private BarChart chart;
private Serie serie, serie2;
private int m_DataNum = 5;
private void OnEnable()
{
StartCoroutine(PieDemo());
}
IEnumerator PieDemo()
{
while (true)
{
StartCoroutine(AddSimpleBar());
yield return new WaitForSeconds(2);
StartCoroutine(BarMutilSerie());
yield return new WaitForSeconds(3);
StartCoroutine(ZebraBar());
yield return new WaitForSeconds(3);
StartCoroutine(SameBarAndNotStack());
yield return new WaitForSeconds(3);
StartCoroutine(SameBarAndStack());
yield return new WaitForSeconds(3);
StartCoroutine(SameBarAndPercentStack());
yield return new WaitForSeconds(10);
}
}
IEnumerator AddSimpleBar()
{
chart = gameObject.GetComponent<BarChart>();
if (chart == null)
{
chart = gameObject.AddComponent<BarChart>();
chart.Init();
}
chart.EnsureChartComponent<Title>().text = "BarChart - 柱状图";
chart.EnsureChartComponent<Title>().subText = "普通柱状图";
var yAxis = chart.EnsureChartComponent<YAxis>();
yAxis.minMaxType = Axis.AxisMinMaxType.Default;
chart.RemoveData();
serie = chart.AddSerie<Bar>("Bar1");
for (int i = 0; i < m_DataNum; i++)
{
chart.AddXAxisData("x" + (i + 1));
chart.AddData(0, UnityEngine.Random.Range(30, 90));
}
yield return new WaitForSeconds(1);
}
IEnumerator BarMutilSerie()
{
chart.EnsureChartComponent<Title>().subText = "多条柱状图";
float now = serie.barWidth - 0.35f;
while (serie.barWidth > 0.35f)
{
serie.barWidth -= now * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
serie2 = chart.AddSerie<Bar>("Bar2");
serie2.lineType = LineType.Normal;
serie2.barWidth = 0.35f;
for (int i = 0; i < m_DataNum; i++)
{
chart.AddData(1, UnityEngine.Random.Range(20, 90));
}
yield return new WaitForSeconds(1);
}
IEnumerator ZebraBar()
{
chart.EnsureChartComponent<Title>().subText = "斑马柱状图";
serie.barType = BarType.Zebra;
serie2.barType = BarType.Zebra;
serie.barZebraWidth = serie.barZebraGap = 4;
serie2.barZebraWidth = serie2.barZebraGap = 4;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
IEnumerator SameBarAndNotStack()
{
chart.EnsureChartComponent<Title>().subText = "非堆叠同柱";
serie.barType = serie2.barType = BarType.Normal;
serie.stack = "";
serie2.stack = "";
serie.barGap = -1;
serie2.barGap = -1;
yield return new WaitForSeconds(1);
}
IEnumerator SameBarAndStack()
{
chart.EnsureChartComponent<Title>().subText = "堆叠同柱";
serie.barType = serie2.barType = BarType.Normal;
serie.stack = "samename";
serie2.stack = "samename";
yield return new WaitForSeconds(1);
float now = 0.6f - serie.barWidth;
while (serie.barWidth < 0.6f)
{
serie.barWidth += now * Time.deltaTime;
serie2.barWidth += now * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
serie.barWidth = serie2.barWidth;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
IEnumerator SameBarAndPercentStack()
{
chart.EnsureChartComponent<Title>().subText = "百分比堆叠同柱";
serie.barType = serie2.barType = BarType.Normal;
serie.stack = "samename";
serie2.stack = "samename";
serie.barPercentStack = true;
if (null == serie.label)
{
serie.EnsureComponent<LabelStyle>();
}
serie.label.show = true;
serie.label.position = LabelStyle.Position.Center;
serie.label.textStyle.color = Color.white;
serie.label.formatter = "{d:f0}%";
if (null == serie2.label)
{
serie2.EnsureComponent<LabelStyle>();
}
serie2.label.show = true;
serie2.label.position = LabelStyle.Position.Center;
serie2.label.textStyle.color = Color.white;
serie2.label.formatter = "{d:f0}%";
serie2.labelDirty = true;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 03916f7ca858b446883197ae17e50f16
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,46 @@
using System.Collections;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example21_BarRace : MonoBehaviour
{
private BarChart chart;
private float lastTime;
void Awake()
{
chart = gameObject.GetComponent<BarChart>();
chart.ClearData();
for (int i = 0; i < 5; i++)
{
chart.AddYAxisData("y" + i);
chart.AddData(0, Random.Range(0, 200));
}
}
void Update()
{
if (Time.time - lastTime >= 3f)
{
lastTime = Time.time;
UpdateData();
}
}
void UpdateData()
{
var serie = chart.GetSerie(0);
for (int i = 0; i < serie.dataCount; i++)
{
if (Random.Range(0, 1f) > 0.9f)
chart.UpdateData(0, i, chart.GetData(0, i) + Mathf.Round(Random.Range(0, 2000)));
else
chart.UpdateData(0, i, chart.GetData(0, i) + Mathf.Round(Random.Range(0, 200)));
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9842ca7fe07044666950b6f53ef65fdb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,207 @@
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example30_PieChart : MonoBehaviour
{
private PieChart chart;
private Serie serie, serie1;
private float m_RadiusSpeed = 100f;
private float m_CenterSpeed = 1f;
private void OnEnable()
{
StartCoroutine(PieDemo());
}
IEnumerator PieDemo()
{
while (true)
{
StartCoroutine(PieAdd());
yield return new WaitForSeconds(2);
StartCoroutine(PieShowLabel());
yield return new WaitForSeconds(4);
StartCoroutine(Doughnut());
yield return new WaitForSeconds(3);
StartCoroutine(DoublePie());
yield return new WaitForSeconds(2);
StartCoroutine(RosePie());
yield return new WaitForSeconds(5);
}
}
IEnumerator PieAdd()
{
chart = gameObject.GetComponent<PieChart>();
if (chart == null)
{
chart = gameObject.AddComponent<PieChart>();
chart.Init();
}
yield return null;
chart.GetChartComponent<Title>().text = "PieChart - 饼图";
chart.GetChartComponent<Title>().subText = "基础饼图";
var legend = chart.EnsureChartComponent<Legend>();
legend.show = true;
legend.location.align = Location.Align.TopLeft;
legend.location.top = 60;
legend.location.left = 2;
legend.itemWidth = 70;
legend.itemHeight = 20;
legend.orient = Orient.Vertical;
chart.RemoveData();
serie = chart.AddSerie<Pie>("访问来源");
serie.radius[0] = 0;
serie.radius[1] = 110;
serie.center[0] = 0.5f;
serie.center[1] = 0.4f;
chart.AddData(0, 335, "直接访问");
chart.AddData(0, 310, "邮件营销");
chart.AddData(0, 243, "联盟广告");
chart.AddData(0, 135, "视频广告");
chart.AddData(0, 1548, "搜索引擎");
chart.onSerieClick = delegate (SerieEventData data)
{
};
yield return new WaitForSeconds(1);
}
IEnumerator PieShowLabel()
{
chart.EnsureChartComponent<Title>().subText = "显示文本标签";
serie.EnsureComponent<LabelStyle>();
serie.label.show = true;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.labelLine.lineType = LabelLine.LineType.Curves;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.labelLine.lineType = LabelLine.LineType.HorizontalLine;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.labelLine.lineType = LabelLine.LineType.BrokenLine;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.labelLine.show = false;
chart.RefreshChart();
}
IEnumerator Doughnut()
{
chart.EnsureChartComponent<Title>().subText = "圆环图";
serie.radius[0] = 2f;
while (serie.radius[0] < serie.radius[1] * 0.7f)
{
serie.radius[0] += m_RadiusSpeed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
serie.gap = 1f;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.data[0].selected = true;
chart.RefreshChart();
yield return new WaitForSeconds(1);
serie.gap = 0f;
serie.data[0].selected = false;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
IEnumerator DoublePie()
{
chart.EnsureChartComponent<Title>().subText = "多图组合";
serie1 = chart.AddSerie<Pie>("访问来源2");
chart.AddData(1, 335, "直达");
chart.AddData(1, 679, "营销广告");
chart.AddData(1, 1548, "搜索引擎");
serie1.radius[0] = 0;
serie1.radius[1] = 2f;
serie1.center[0] = 0.5f;
serie1.center[1] = 0.4f;
chart.RefreshChart();
while (serie1.radius[1] < serie.radius[0] * 0.75f)
{
serie1.radius[1] += m_RadiusSpeed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
if (null == serie.label)
{
serie.EnsureComponent<LabelStyle>();
}
if (null == serie1.label)
{
serie1.EnsureComponent<LabelStyle>();
}
serie1.label.show = true;
serie1.label.position = LabelStyle.Position.Inside;
serie1.label.textStyle.color = Color.white;
serie1.label.textStyle.fontSize = 14;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
IEnumerator RosePie()
{
chart.EnsureChartComponent<Title>().subText = "玫瑰图";
chart.EnsureChartComponent<Legend>().show = false;
serie1.ClearData();
serie.ClearData();
serie1.radius = serie.radius = new float[2] { 0, 80 };
serie1.label.position = LabelStyle.Position.Outside;
serie1.labelLine.lineType = LabelLine.LineType.Curves;
serie1.label.textStyle.color = Color.clear;
for (int i = 0; i < 2; i++)
{
chart.AddData(i, 10, "rose1");
chart.AddData(i, 5, "rose2");
chart.AddData(i, 15, "rose3");
chart.AddData(i, 25, "rose4");
chart.AddData(i, 20, "rose5");
chart.AddData(i, 35, "rose6");
chart.AddData(i, 30, "rose7");
chart.AddData(i, 40, "rose8");
}
while (serie.center[0] > 0.25f || serie1.center[0] < 0.7f)
{
if (serie.center[0] > 0.25f) serie.center[0] -= m_CenterSpeed * Time.deltaTime;
if (serie1.center[0] < 0.7f) serie1.center[0] += m_CenterSpeed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
yield return new WaitForSeconds(1);
while (serie.radius[0] > 3f)
{
serie.radius[0] -= m_RadiusSpeed * Time.deltaTime;
serie1.radius[0] -= m_RadiusSpeed * Time.deltaTime;
chart.RefreshChart();
yield return null;
}
serie.radius[0] = 0;
serie1.radius[0] = 0;
serie.pieRoseType = RoseType.Area;
serie1.pieRoseType = RoseType.Radius;
chart.RefreshChart();
yield return new WaitForSeconds(1);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0b8649d38981b4b5bbdf16e8f303fa1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,77 @@
using UnityEngine;
using XCharts.Runtime;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example31_PieUpdateName : MonoBehaviour
{
PieChart chart;
void Awake()
{
chart = gameObject.GetComponent<PieChart>();
if (chart == null)
{
chart = gameObject.AddComponent<PieChart>();
chart.Init();
}
var serieIndex = 0;
var serie = chart.GetSerie(serieIndex);
if (serie == null) return;
serie.EnsureComponent<LabelStyle>();
serie.label.show = true;
serie.label.position = LabelStyle.Position.Outside;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ClearAndAddData();
//UpdateDataName();
//UpdateDataName();
}
}
void UpdateDataName()
{
var serieIndex = 0;
var serie = chart.GetSerie(serieIndex);
if (serie == null) return;
for (int i = 0; i < serie.dataCount; i++)
{
var value = Random.Range(10, 100);
chart.UpdateData(serieIndex, i, value);
chart.UpdateDataName(serieIndex, i, "value=" + value);
}
}
void ResetSameName()
{
var serieIndex = 0;
var serie = chart.GetSerie(serieIndex);
if (serie == null) return;
for (int i = 0; i < serie.dataCount; i++)
{
chart.UpdateDataName(serieIndex, i, "piename");
}
}
void ClearAndAddData()
{
var serieIndex = 0;
var serie = chart.GetSerie(serieIndex);
if (serie == null) return;
int count = serie.dataCount;
serie.ClearData();
for (int i = 0; i < count; i++)
{
chart.AddData(0, Random.Range(0, 100), "pie" + i);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 41195ee7a652f4ef79c22c365d314621
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+139
View File
@@ -0,0 +1,139 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
public class Example40_Radar : MonoBehaviour
{
private RadarChart chart;
private Serie serie, serie1;
void Awake()
{
LoopDemo();
}
private void OnEnable()
{
LoopDemo();
}
void LoopDemo()
{
StopAllCoroutines();
StartCoroutine(RadarDemo());
}
IEnumerator RadarDemo()
{
StartCoroutine(RadarAdd());
yield return new WaitForSeconds(2);
StartCoroutine(RadarUpdate());
yield return new WaitForSeconds(2);
StartCoroutine(RadarAddMultiple());
yield return new WaitForSeconds(2);
LoopDemo();
}
IEnumerator RadarAdd()
{
chart = gameObject.GetComponent<RadarChart>();
if (chart == null)
{
chart = gameObject.AddComponent<RadarChart>();
chart.Init();
}
chart.RemoveChartComponents<RadarCoord>();
chart.RemoveData();
chart.GetChartComponent<Title>().text = "RadarChart - 雷达图";
chart.GetChartComponent<Title>().subText = "";
var legend = chart.GetChartComponent<Legend>();
legend.show = true;
legend.location.align = Location.Align.TopLeft;
legend.location.top = 60;
legend.location.left = 2;
legend.itemWidth = 70;
legend.itemHeight = 20;
legend.orient = Orient.Vertical;
var radarCoord = chart.AddChartComponent<RadarCoord>();
radarCoord.shape = RadarCoord.Shape.Polygon;
radarCoord.center[0] = 0.5f;
radarCoord.center[1] = 0.4f;
radarCoord.radius = 0.4f;
radarCoord.AddIndicator("indicator1", 0, 100);
radarCoord.AddIndicator("indicator2", 0, 100);
radarCoord.AddIndicator("indicator3", 0, 100);
radarCoord.AddIndicator("indicator4", 0, 100);
radarCoord.AddIndicator("indicator5", 0, 100);
serie = chart.AddSerie<Radar>("test");
serie.radarIndex = 0;
chart.AddData(0, new List<double> { 10, 20, 60, 40, 20 }, "data1");
chart.AddData(0, new List<double> { 40, 60, 90, 80, 70 }, "data2");
yield return new WaitForSeconds(1);
}
IEnumerator RadarUpdate()
{
var radarCoord = chart.GetChartComponent<RadarCoord>();
radarCoord.UpdateIndicator(0, "new1", 0, 100);
chart.UpdateData(0, 0, new List<double> { 15, 30, 50, 60, 50 });
chart.UpdateDataName(0, 0, "new1");
yield return new WaitForSeconds(1);
}
IEnumerator RadarAddMultiple()
{
chart.RemoveChartComponents<RadarCoord>();
chart.RemoveData();
chart.GetChartComponent<Title>().text = "RadarChart - 多雷达图";
chart.GetChartComponent<Title>().subText = "";
var legend = chart.GetChartComponent<Legend>();
legend.show = true;
legend.location.align = Location.Align.TopLeft;
legend.location.top = 60;
legend.location.left = 2;
legend.itemWidth = 70;
legend.itemHeight = 20;
legend.orient = Orient.Vertical;
var radarCoord = chart.AddChartComponent<RadarCoord>();
radarCoord.shape = RadarCoord.Shape.Polygon;
radarCoord.center[0] = 0.25f;
radarCoord.center[1] = 0.4f;
radarCoord.radius = 0.25f;
for (int i = 1; i <= 5; i++)
{
radarCoord.AddIndicator("radar1" + i, 0, 100);
}
var radarCoord2 = chart.AddChartComponent<RadarCoord>();
radarCoord2.shape = RadarCoord.Shape.Polygon;
radarCoord2.center[0] = 0.75f;
radarCoord2.center[1] = 0.4f;
radarCoord2.radius = 0.25f;
for (int i = 1; i <= 5; i++)
{
radarCoord2.AddIndicator("radar2" + i, 0, 100);
}
serie = chart.AddSerie<Radar>("test1");
serie.radarIndex = 0;
chart.AddData(0, new List<double> { 10, 20, 60, 40, 20 }, "data1");
serie1 = chart.AddSerie<Radar>("test2");
serie1.radarIndex = 1;
chart.AddData(1, new List<double> { 10, 20, 60, 40, 20 }, "data2");
yield return new WaitForSeconds(1);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 95a60d7e7a0fc41ecaec5f48823b70bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,76 @@
using UnityEngine;
using XCharts.Runtime;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example41_RadarUpdate : MonoBehaviour
{
RadarChart chart;
int count = 0;
double max = 0;
void Awake()
{
chart = gameObject.GetComponent<RadarChart>();
if (chart == null)
{
chart = gameObject.AddComponent<RadarChart>();
chart.Init();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
UpdateData();
count++;
}
UpdateMax();
}
void UpdateData()
{
var serieIndex = 0;
var serie = chart.GetSerie(serieIndex);
if (serie == null) return;
if (serie.radarType == RadarType.Multiple)
{
for (int i = 0; i < serie.dataCount; i++)
{
var serieData = serie.GetSerieData(i);
for (int j = 0; j < serieData.data.Count; j++)
{
var value = Random.Range(10, 100);
chart.UpdateData(serieIndex, i, j, value);
}
}
}
else
{
for (int i = 0; i < serie.dataCount; i++)
{
var value = Random.Range(10, 100);
chart.UpdateData(serieIndex, i, value);
}
}
chart.GetChartComponent<Title>().subText = "max:" + serie.context.dataMax;
}
void UpdateMax()
{
var serieIndex = 0;
var serie = chart.GetSerie(serieIndex);
if (serie == null) return;
if (serie.context.dataMax != max)
{
chart.GetChartComponent<Title>().subText = "max:" + serie.context.dataMax;
max = serie.context.dataMax;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7a2ad6907bd5045ec920b4f0e359535e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example50_Scatter : MonoBehaviour
{
private ScatterChart chart;
void Awake()
{
chart = gameObject.GetComponent<ScatterChart>();
if (chart == null) return;
foreach (var serie in chart.series)
{
serie.symbol.sizeFunction = SymbolSize;
}
}
float SymbolSize(float defaultSize, SerieData serieData)
{
return defaultSize;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5e6c9b864ab644b45ae93df3878ab1dd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,112 @@
using System.Collections.Generic;
using UnityEngine;
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example60_Heatmap : MonoBehaviour
{
private HeatmapChart chart;
void Awake()
{
chart = gameObject.GetComponent<HeatmapChart>();
if (chart == null)
{
chart = gameObject.AddComponent<HeatmapChart>();
chart.Init();
}
chart.GetChartComponent<Title>().text = "HeatmapChart";
chart.GetChartComponent<Tooltip>().type = Tooltip.Type.None;
var grid = chart.GetChartComponent<GridCoord>();
grid.left = 100;
grid.right = 60;
grid.bottom = 60;
var xAxis = chart.GetChartComponent<XAxis>();
var yAxis = chart.GetChartComponent<YAxis>();
//目前只支持Category
xAxis.type = Axis.AxisType.Category;
yAxis.type = Axis.AxisType.Category;
xAxis.boundaryGap = true;
xAxis.boundaryGap = true;
xAxis.splitNumber = 10;
yAxis.splitNumber = 10;
//清空数据重新添加
chart.RemoveData();
var serie = chart.AddSerie<Heatmap>("serie1");
//设置样式
serie.itemStyle.show = true;
serie.itemStyle.borderWidth = 1;
serie.itemStyle.borderColor = Color.clear;
//设置高亮样式
var emphasisStyle = serie.EnsureComponent<EmphasisStyle>();
emphasisStyle.itemStyle.show = true;
emphasisStyle.itemStyle.borderWidth = 1;
emphasisStyle.itemStyle.borderColor = Color.black;
//设置视觉映射组件
var visualMap = chart.GetChartComponent<VisualMap>();
visualMap.max = 10;
visualMap.range[0] = 0f;
visualMap.range[1] = 10f;
visualMap.orient = Orient.Vertical;
visualMap.calculable = true;
visualMap.location.align = Location.Align.BottomLeft;
visualMap.location.bottom = 100;
visualMap.location.left = 30;
//清空颜色重新添加
var heatmapGridWid = 10f;
int xSplitNumber = (int) (grid.context.width / heatmapGridWid);
int ySplitNumber = (int) (grid.context.height / heatmapGridWid);
var colors = new List<string>
{
"#313695",
"#4575b4",
"#74add1",
"#abd9e9",
"#e0f3f8",
"#ffffbf",
"#fee090",
"#fdae61",
"#f46d43",
"#d73027",
"#a50026"
};
visualMap.AddColors(colors);
//添加xAxis的数据
for (int i = 0; i < xSplitNumber; i++)
{
chart.AddXAxisData((i + 1).ToString());
}
//添加yAxis的数据
for (int i = 0; i < ySplitNumber; i++)
{
chart.AddYAxisData((i + 1).ToString());
}
for (int i = 0; i < xSplitNumber; i++)
{
for (int j = 0; j < ySplitNumber; j++)
{
var value = 0f;
var rate = Random.Range(0, 101);
if (rate > 70) value = Random.Range(8f, 10f);
else value = Random.Range(1f, 8f);
var list = new List<double> { i, j, value };
//至少是一个三位数据:(x,y,value)
chart.AddData(0, list);
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e702e0ac05be84dbe9622180d4f6ef71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,55 @@
using UnityEngine;
using XCharts.Runtime;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example80_Polar : MonoBehaviour
{
private BaseChart chart;
private float updateTime;
void Awake()
{
chart = gameObject.GetComponent<BaseChart>();
if (chart == null)
{
chart = gameObject.AddComponent<BaseChart>();
chart.Init();
}
chart.EnsureChartComponent<PolarCoord>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddData();
}
}
void AddData()
{
chart.RemoveData();
chart.GetChartComponent<Tooltip>().type = Tooltip.Type.Cross;
var angleAxis = chart.GetChartComponent<AngleAxis>();
angleAxis.type = Axis.AxisType.Value;
angleAxis.minMaxType = Axis.AxisMinMaxType.Custom;
angleAxis.min = 0;
angleAxis.max = 360;
angleAxis.startAngle = Random.Range(0, 90);
chart.AddSerie<Line>("line1");
var rate = Random.Range(1, 4);
for (int i = 0; i <= 360; i++)
{
var t = i / 180f * Mathf.PI;
var r = Mathf.Sin(2 * t) * Mathf.Cos(2 * t) * rate;
chart.AddData(0, Mathf.Abs(r), i);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ca29783da761a4e0e9c5204d5b24b610
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,69 @@
using UnityEngine;
using XCharts.Runtime;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example90_Candlestick : MonoBehaviour
{
private CandlestickChart chart;
private float updateTime;
public int dataCount = 100;
void Awake()
{
chart = gameObject.GetComponent<CandlestickChart>();
if (chart == null)
{
chart = gameObject.AddComponent<CandlestickChart>();
chart.Init();
}
AddData();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddData();
}
}
void AddData()
{
chart.ClearData();
var xValue = System.DateTime.Now;
var baseValue = Random.Range(0f, 1f) * 12000;
var boxVals = new float[4];
var dayRange = 12;
for (int i = 0; i < dataCount; i++)
{
baseValue = baseValue + Random.Range(0f, 1f) * 30 - 10;
for (int j = 0; j < 4; j++)
{
boxVals[j] = (Random.Range(0f, 1f) - 0.5f) * dayRange + baseValue;
}
System.Array.Sort(boxVals);
var openIdx = Mathf.RoundToInt(Random.Range(0f, 1f) * 3);
var closeIdx = Mathf.RoundToInt(Random.Range(0f, 1f) * 2);
if (openIdx == closeIdx)
{
closeIdx++;
}
//var volumn = boxVals[3]*(1000+Random.Range(0f,1f) * 500);
var open = boxVals[openIdx];
var close = boxVals[closeIdx];
var lowest = boxVals[0];
var heighest = boxVals[3];
chart.AddXAxisData(i.ToString());
chart.AddData(0, i, open, close, lowest, heighest);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 69c7f3bf337c843888b4a7031eef1027
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+47
View File
@@ -0,0 +1,47 @@
using UnityEngine;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
using XCharts.Runtime;
namespace XCharts.Example
{
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Example_Test : MonoBehaviour
{
BaseChart chart;
void Awake()
{
chart = gameObject.GetComponent<BaseChart>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddData();
}
else if (Input.GetKeyDown(KeyCode.R))
{
chart.AnimationReset();
chart.AnimationFadeIn();
}
else if (Input.GetKeyDown(KeyCode.U))
{
chart.UpdateData(0, 2, 99);
}
else if (Input.GetKeyDown(KeyCode.C))
{
chart.UpdateData(0, 2, 22);
}
}
void AddData()
{
chart.AnimationReset();
chart.AnimationFadeOut();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bac63bf58d06d47be8e1759189fbd9ed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,15 @@
{
"name": "XCharts.Examples",
"references": [
"XCharts.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": []
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 9ca8daef375784f86b76407e76c9045a
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: