mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-20 07:20:08 +00:00
0.2版本,重构代码,增加Editor
This commit is contained in:
154
Demo/Scripts/Demo.cs
Normal file
154
Demo/Scripts/Demo.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XCharts;
|
||||
|
||||
public class Demo : MonoBehaviour
|
||||
{
|
||||
private Theme m_SelectedTheme;
|
||||
private GameObject m_SelectedModule;
|
||||
|
||||
private GameObject m_LineChartModule;
|
||||
private GameObject m_BarChartModule;
|
||||
private GameObject m_PieChartModule;
|
||||
private GameObject m_RadarChartModule;
|
||||
private GameObject m_OtherModule;
|
||||
|
||||
private Button m_DefaultThemeButton;
|
||||
private Button m_LightThemeButton;
|
||||
private Button m_DarkThemeButton;
|
||||
|
||||
private Button m_LineChartButton;
|
||||
private Button m_BarChartButton;
|
||||
private Button m_PieChartButton;
|
||||
private Button m_RadarChartButton;
|
||||
private Button m_OtherButton;
|
||||
|
||||
private Text m_Title;
|
||||
private Color m_NormalColor;
|
||||
private Color m_SelectedColor;
|
||||
private Color m_HighlightColor;
|
||||
|
||||
private ScrollRect m_ScrollRect;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
m_SelectedTheme = Theme.Default;
|
||||
|
||||
m_NormalColor = ChartHelper.GetColor("#293C55FF");
|
||||
m_SelectedColor = ChartHelper.GetColor("#e43c59ff");
|
||||
m_HighlightColor = ChartHelper.GetColor("#0E151FFF");
|
||||
|
||||
m_ScrollRect = transform.Find("chart_detail").GetComponent<ScrollRect>();
|
||||
|
||||
m_LineChartModule = transform.Find("chart_detail/line_chart").gameObject;
|
||||
m_BarChartModule = transform.Find("chart_detail/bar_chart").gameObject;
|
||||
m_PieChartModule = transform.Find("chart_detail/pie_chart").gameObject;
|
||||
m_RadarChartModule = transform.Find("chart_detail/radar_chart").gameObject;
|
||||
m_OtherModule = transform.Find("chart_detail/other").gameObject;
|
||||
|
||||
m_Title = transform.Find("chart_title/Text").GetComponent<Text>();
|
||||
|
||||
InitChartButton();
|
||||
InitThemeButton();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void InitChartButton()
|
||||
{
|
||||
m_LineChartButton = transform.Find("chart_list/btn_linechart").GetComponent<Button>();
|
||||
m_BarChartButton = transform.Find("chart_list/btn_barchart").GetComponent<Button>();
|
||||
m_PieChartButton = transform.Find("chart_list/btn_piechart").GetComponent<Button>();
|
||||
m_RadarChartButton = transform.Find("chart_list/btn_radarchart").GetComponent<Button>();
|
||||
m_OtherButton = transform.Find("chart_list/btn_other").GetComponent<Button>();
|
||||
|
||||
m_LineChartButton.onClick.AddListener(delegate () { SelectedModule(m_LineChartModule); });
|
||||
m_BarChartButton.onClick.AddListener(delegate () { SelectedModule(m_BarChartModule); });
|
||||
m_PieChartButton.onClick.AddListener(delegate () { SelectedModule(m_PieChartModule); });
|
||||
m_RadarChartButton.onClick.AddListener(delegate () { SelectedModule(m_RadarChartModule); });
|
||||
m_OtherButton.onClick.AddListener(delegate () { SelectedModule(m_OtherModule); });
|
||||
|
||||
SelectedModule(m_LineChartModule);
|
||||
}
|
||||
|
||||
void InitThemeButton()
|
||||
{
|
||||
m_DefaultThemeButton = transform.Find("chart_theme/btn_default").GetComponent<Button>();
|
||||
m_LightThemeButton = transform.Find("chart_theme/btn_light").GetComponent<Button>();
|
||||
m_DarkThemeButton = transform.Find("chart_theme/btn_dark").GetComponent<Button>();
|
||||
|
||||
m_DefaultThemeButton.transform.Find("selected").gameObject.SetActive(m_SelectedTheme == Theme.Default);
|
||||
m_LightThemeButton.transform.Find("selected").gameObject.SetActive(m_SelectedTheme == Theme.Light);
|
||||
m_DarkThemeButton.transform.Find("selected").gameObject.SetActive(m_SelectedTheme == Theme.Dark);
|
||||
|
||||
m_DefaultThemeButton.onClick.AddListener(delegate () { SelecteTheme(Theme.Default); });
|
||||
m_LightThemeButton.onClick.AddListener(delegate () { SelecteTheme(Theme.Light); });
|
||||
m_DarkThemeButton.onClick.AddListener(delegate () { SelecteTheme(Theme.Dark); });
|
||||
|
||||
SelecteTheme(Theme.Default);
|
||||
}
|
||||
|
||||
|
||||
void SelectedModule(GameObject module)
|
||||
{
|
||||
m_SelectedModule = module;
|
||||
m_LineChartModule.SetActive(module == m_LineChartModule);
|
||||
m_BarChartModule.SetActive(module == m_BarChartModule);
|
||||
m_PieChartModule.SetActive(module == m_PieChartModule);
|
||||
m_RadarChartModule.SetActive(module == m_RadarChartModule);
|
||||
m_OtherModule.SetActive(module == m_OtherModule);
|
||||
|
||||
SetButtonColor(m_LineChartButton, m_SelectedModule, m_LineChartModule);
|
||||
SetButtonColor(m_BarChartButton, m_SelectedModule, m_BarChartModule);
|
||||
SetButtonColor(m_PieChartButton, m_SelectedModule, m_PieChartModule);
|
||||
SetButtonColor(m_RadarChartButton, m_SelectedModule, m_RadarChartModule);
|
||||
SetButtonColor(m_OtherButton, m_SelectedModule, m_OtherModule);
|
||||
|
||||
m_ScrollRect.content = m_SelectedModule.GetComponent<RectTransform>();
|
||||
|
||||
if (module == m_LineChartModule)
|
||||
{
|
||||
m_Title.text = "折线图 Line";
|
||||
}
|
||||
else if (module == m_BarChartModule)
|
||||
{
|
||||
m_Title.text = "柱状图 Bar";
|
||||
}
|
||||
else if (module == m_PieChartModule)
|
||||
{
|
||||
m_Title.text = "饼图 Pie";
|
||||
}
|
||||
else if (module == m_RadarChartModule)
|
||||
{
|
||||
m_Title.text = "雷达图 Radar";
|
||||
}
|
||||
else if (module == m_OtherModule)
|
||||
{
|
||||
m_Title.text = "其他";
|
||||
}
|
||||
}
|
||||
|
||||
void SetButtonColor(Button btn, GameObject selected, GameObject module)
|
||||
{
|
||||
var block = btn.colors;
|
||||
block.highlightedColor = selected == module ? m_SelectedColor : m_HighlightColor;
|
||||
block.normalColor = selected == module ? m_SelectedColor : m_NormalColor;
|
||||
btn.colors = block;
|
||||
}
|
||||
|
||||
void SelecteTheme(Theme theme)
|
||||
{
|
||||
m_SelectedTheme = theme;
|
||||
m_DefaultThemeButton.transform.Find("selected").gameObject.SetActive(m_SelectedTheme == Theme.Default);
|
||||
m_LightThemeButton.transform.Find("selected").gameObject.SetActive(m_SelectedTheme == Theme.Light);
|
||||
m_DarkThemeButton.transform.Find("selected").gameObject.SetActive(m_SelectedTheme == Theme.Dark);
|
||||
var charts = transform.GetComponentsInChildren<BaseChart>();
|
||||
foreach (var chart in charts)
|
||||
{
|
||||
chart.UpdateTheme(theme);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Demo/Scripts/Demo.cs.meta
Normal file
13
Demo/Scripts/Demo.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2cb45775d5ecfa4488a53912a49a832
|
||||
timeCreated: 1555862356
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
54
Demo/Scripts/Demo_Dynamic.cs
Normal file
54
Demo/Scripts/Demo_Dynamic.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XCharts;
|
||||
|
||||
public class Demo_Dynamic : MonoBehaviour
|
||||
{
|
||||
public int maxCacheDataNumber = 100;
|
||||
public float initDataTime = 2;
|
||||
|
||||
private CoordinateChart chart;
|
||||
private float updateTime;
|
||||
private float initTime;
|
||||
private int initCount;
|
||||
private int count;
|
||||
private bool isInited;
|
||||
private DateTime timeNow;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
chart = gameObject.GetComponentInChildren<CoordinateChart>();
|
||||
chart.xAxis.ClearData();
|
||||
chart.series.ClearData();
|
||||
chart.maxCacheDataNumber = maxCacheDataNumber;
|
||||
timeNow = DateTime.Now;
|
||||
initCount = maxCacheDataNumber;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (initCount>0)
|
||||
{
|
||||
int count = (int)(maxCacheDataNumber / initDataTime * Time.deltaTime);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
timeNow = timeNow.AddSeconds(-initCount);
|
||||
chart.AddXAxisData(timeNow.ToString("hh:mm:ss"));
|
||||
chart.AddData(0, UnityEngine.Random.Range(60, 150));
|
||||
initCount--;
|
||||
if (initCount <= 0) break;
|
||||
}
|
||||
chart.RefreshChart();
|
||||
}
|
||||
updateTime += Time.deltaTime;
|
||||
if (updateTime >= 1)
|
||||
{
|
||||
updateTime = 0;
|
||||
count++;
|
||||
chart.AddXAxisData(DateTime.Now.ToString("hh:mm:ss"));
|
||||
chart.AddData(0, UnityEngine.Random.Range(60, 150));
|
||||
chart.RefreshChart();
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Demo/Scripts/Demo_Dynamic.cs.meta
Normal file
13
Demo/Scripts/Demo_Dynamic.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c3d5e10d25691247b392b09eec051bf
|
||||
timeCreated: 1557485360
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
67
Demo/Scripts/Demo_LargeData.cs
Normal file
67
Demo/Scripts/Demo_LargeData.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
using XCharts;
|
||||
|
||||
public class Demo_LargeData : MonoBehaviour
|
||||
{
|
||||
public int maxCacheDataNumber = 3000;
|
||||
public float initDataTime = 5;
|
||||
|
||||
private CoordinateChart chart;
|
||||
private float initTime;
|
||||
private int initCount;
|
||||
private System.DateTime timeNow;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
chart = gameObject.GetComponentInChildren<CoordinateChart>();
|
||||
timeNow = System.DateTime.Now;
|
||||
chart.xAxis.ClearData();
|
||||
chart.series.ClearData();
|
||||
chart.maxCacheDataNumber = maxCacheDataNumber;
|
||||
initCount = maxCacheDataNumber;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (initCount>0)
|
||||
{
|
||||
int count = (int)(maxCacheDataNumber / initDataTime * Time.deltaTime);
|
||||
for(int i = 0; i < count; i++)
|
||||
{
|
||||
timeNow = timeNow.AddSeconds(-initCount);
|
||||
chart.AddXAxisData(timeNow.ToString("hh:mm:ss"));
|
||||
chart.AddData(0, UnityEngine.Random.Range(60, 150));
|
||||
initCount--;
|
||||
if (initCount <= 0) break;
|
||||
}
|
||||
chart.RefreshChart();
|
||||
}
|
||||
}
|
||||
|
||||
void GenerateData(int count, CoordinateChart chart)
|
||||
{
|
||||
var baseValue = Random.Range(0, 1000);
|
||||
var time = new System.DateTime(2011, 1, 1);
|
||||
var smallBaseValue = 0;
|
||||
|
||||
chart.xAxis.ClearData();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
chart.AddXAxisData(time.ToString("hh:mm:ss"));
|
||||
|
||||
smallBaseValue = i % 30 == 0
|
||||
? Random.Range(0, 700)
|
||||
: (smallBaseValue + Random.Range(0, 500) - 250);
|
||||
baseValue += Random.Range(0, 20) - 10;
|
||||
float value = Mathf.Max(
|
||||
0,
|
||||
Mathf.Round(baseValue + smallBaseValue) + 3000
|
||||
);
|
||||
//var index = i % 100;
|
||||
//var value = (Mathf.Sin(index / 5) * (index / 5 - 10) + index / 6) * 5;
|
||||
value = Mathf.Abs(value);
|
||||
chart.AddData(0, value);
|
||||
time = time.AddSeconds(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Demo/Scripts/Demo_LargeData.cs.meta
Normal file
13
Demo/Scripts/Demo_LargeData.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0b7f7cfd79a632a46bdc2aac41160f37
|
||||
timeCreated: 1557485007
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
28
Demo/Scripts/Demo_Test.cs
Normal file
28
Demo/Scripts/Demo_Test.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XCharts;
|
||||
|
||||
public class Demo_Test : MonoBehaviour
|
||||
{
|
||||
private CoordinateChart chart;
|
||||
private float time;
|
||||
private int count;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
chart = gameObject.GetComponentInChildren<CoordinateChart>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
time += Time.deltaTime;
|
||||
if (time >= 1)
|
||||
{
|
||||
time = 0;
|
||||
count++;
|
||||
chart.UpdateData(0, Random.Range(60, 150));
|
||||
chart.AddXAxisData("time" + count);
|
||||
chart.AddData(0, Random.Range(60, 150));
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Demo/Scripts/Demo_Test.cs.meta
Normal file
13
Demo/Scripts/Demo_Test.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79b834e804f7aa844bc2ad80cf2bda9f
|
||||
timeCreated: 1557366438
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user