mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-22 17:00:08 +00:00
3.0 - unitypackage
This commit is contained in:
238
Editor/Windows/PraseExternalDataEditor.cs
Normal file
238
Editor/Windows/PraseExternalDataEditor.cs
Normal file
@@ -0,0 +1,238 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts.Editor
|
||||
{
|
||||
public class PraseExternalDataEditor : UnityEditor.EditorWindow
|
||||
{
|
||||
private static BaseChart s_Chart;
|
||||
private static Serie s_Serie;
|
||||
private static Axis s_Axis;
|
||||
private static PraseExternalDataEditor window;
|
||||
private static string inputJsonText = "";
|
||||
|
||||
public static void ShowWindow()
|
||||
{
|
||||
window = GetWindow<PraseExternalDataEditor>();
|
||||
window.titleContent = new GUIContent("PraseExternalData");
|
||||
window.minSize = new Vector2(450, 550);
|
||||
window.Focus();
|
||||
window.Show();
|
||||
}
|
||||
|
||||
public static void UpdateData(BaseChart chart, Serie serie, Axis axis)
|
||||
{
|
||||
s_Chart = chart;
|
||||
s_Serie = serie;
|
||||
s_Axis = axis;
|
||||
inputJsonText = UnityEngine.GUIUtility.systemCopyBuffer;
|
||||
}
|
||||
|
||||
void OnInspectorUpdate()
|
||||
{
|
||||
Repaint();
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (s_Chart == null)
|
||||
{
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
EditorGUILayout.LabelField("Input external data (echarts data):");
|
||||
inputJsonText = EditorGUILayout.TextArea(inputJsonText, GUILayout.Height(400));
|
||||
if (GUILayout.Button("Add"))
|
||||
{
|
||||
if (s_Serie != null)
|
||||
{
|
||||
if (!ParseArrayData(s_Serie, inputJsonText))
|
||||
{
|
||||
if (ParseJsonData(s_Serie, inputJsonText))
|
||||
inputJsonText = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
inputJsonText = "";
|
||||
}
|
||||
}
|
||||
else if (s_Axis != null)
|
||||
{
|
||||
if (!ParseArrayData(s_Axis, inputJsonText))
|
||||
{
|
||||
if (ParseJsonData(s_Axis, inputJsonText))
|
||||
inputJsonText = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
inputJsonText = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ParseArrayData(Axis axis, string arrayData)
|
||||
{
|
||||
arrayData = arrayData.Trim();
|
||||
if (!arrayData.StartsWith("data: Array")) return false;
|
||||
axis.data.Clear();
|
||||
var list = arrayData.Split('\n');
|
||||
for (int i = 1; i < list.Length; i++)
|
||||
{
|
||||
var temp = list[i].Split(':');
|
||||
if (temp.Length == 2)
|
||||
{
|
||||
var category = temp[1].Replace("\"", "").Trim();
|
||||
axis.data.Add(category);
|
||||
}
|
||||
}
|
||||
axis.SetAllDirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool ParseArrayData(Serie serie, string arrayData)
|
||||
{
|
||||
arrayData = arrayData.Trim();
|
||||
if (!arrayData.StartsWith("data: Array")) return false;
|
||||
serie.ClearData();
|
||||
var list = arrayData.Split('\n');
|
||||
for (int i = 1; i < list.Length; i++)
|
||||
{
|
||||
var temp = list[i].Split(':');
|
||||
if (temp.Length == 2)
|
||||
{
|
||||
var strvalue = temp[1].Replace("\"", "").Trim();
|
||||
var value = 0d;
|
||||
var flag = double.TryParse(strvalue, out value);
|
||||
if (flag)
|
||||
{
|
||||
serie.AddYData(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
serie.SetAllDirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool ParseJsonData(Axis axis, string jsonData)
|
||||
{
|
||||
if (!CheckJsonData(ref jsonData)) return false;
|
||||
axis.data.Clear();
|
||||
string[] datas = jsonData.Split(',');
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
var txt = datas[i].Trim().Replace("[", "").Replace("]", "");
|
||||
var value = 0d;
|
||||
if (!double.TryParse(txt, out value))
|
||||
axis.data.Add(txt.Replace("\'", "").Replace("\"", ""));
|
||||
}
|
||||
axis.SetAllDirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从json中导入数据
|
||||
/// </summary>
|
||||
/// <param name="jsonData"></param>
|
||||
private static bool ParseJsonData(Serie serie, string jsonData)
|
||||
{
|
||||
if (!CheckJsonData(ref jsonData)) return false;
|
||||
serie.ClearData();
|
||||
if (jsonData.IndexOf("],") > -1 || jsonData.IndexOf("] ,") > -1)
|
||||
{
|
||||
string[] datas = jsonData.Split(new string[] { "],", "] ," }, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
var data = datas[i].Replace("[", "").Replace("]", "").Split(new char[] { '[', ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var serieData = new SerieData();
|
||||
double value = 0;
|
||||
if (data.Length == 2 && !double.TryParse(data[0], out value))
|
||||
{
|
||||
double.TryParse(data[1], out value);
|
||||
serieData.data = new List<double>() { i, value };
|
||||
serieData.name = data[0].Replace("\"", "").Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < data.Length; j++)
|
||||
{
|
||||
var txt = data[j].Trim().Replace("]", "");
|
||||
var flag = double.TryParse(txt, out value);
|
||||
if (flag)
|
||||
{
|
||||
serieData.data.Add(value);
|
||||
}
|
||||
else serieData.name = txt.Replace("\"", "").Trim();
|
||||
}
|
||||
}
|
||||
serie.AddSerieData(serieData);
|
||||
}
|
||||
}
|
||||
else if (jsonData.IndexOf("value") > -1 && jsonData.IndexOf("name") > -1)
|
||||
{
|
||||
string[] datas = jsonData.Split(new string[] { "},", "} ,", "}" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
var arr = datas[i].Replace("{", "").Split(',');
|
||||
var serieData = new SerieData();
|
||||
foreach (var a in arr)
|
||||
{
|
||||
if (a.StartsWith("value:"))
|
||||
{
|
||||
double value = double.Parse(a.Substring(6, a.Length - 6));
|
||||
serieData.data = new List<double>() { i, value };
|
||||
}
|
||||
else if (a.StartsWith("name:"))
|
||||
{
|
||||
string name = a.Substring(6, a.Length - 6 - 1);
|
||||
serieData.name = name;
|
||||
}
|
||||
else if (a.StartsWith("selected:"))
|
||||
{
|
||||
string selected = a.Substring(9, a.Length - 9);
|
||||
serieData.selected = bool.Parse(selected);
|
||||
}
|
||||
}
|
||||
serie.AddSerieData(serieData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] datas = jsonData.Split(',');
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
double value;
|
||||
var flag = double.TryParse(datas[i].Trim(), out value);
|
||||
if (flag)
|
||||
{
|
||||
var serieData = new SerieData();
|
||||
serieData.data = new List<double>() { i, value };
|
||||
serie.AddSerieData(serieData);
|
||||
}
|
||||
}
|
||||
}
|
||||
serie.SetAllDirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool CheckJsonData(ref string jsonData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(jsonData)) return false;
|
||||
jsonData = jsonData.Replace("\r\n", "");
|
||||
jsonData = jsonData.Replace(" ", "");
|
||||
jsonData = jsonData.Replace("\n", "");
|
||||
int startIndex = jsonData.IndexOf("[");
|
||||
int endIndex = jsonData.LastIndexOf("]");
|
||||
if (startIndex == -1 || endIndex == -1)
|
||||
{
|
||||
Debug.LogError("json data need include in [ ]");
|
||||
return false;
|
||||
}
|
||||
jsonData = jsonData.Substring(startIndex + 1, endIndex - startIndex - 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Editor/Windows/PraseExternalDataEditor.cs.meta
Normal file
11
Editor/Windows/PraseExternalDataEditor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b41bbccd77d88460aba5bcf81b4920ce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
62
Editor/Windows/XCSettingsEditor.cs
Normal file
62
Editor/Windows/XCSettingsEditor.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts.Editor
|
||||
{
|
||||
[CustomEditor(typeof(XCSettings))]
|
||||
public class XCSettingsEditor : UnityEditor.Editor
|
||||
{
|
||||
internal class Styles
|
||||
{
|
||||
public static readonly GUIContent defaultFontAssetLabel = new GUIContent("Default Font Asset", "The Font Asset that will be assigned by default to newly created text objects when no Font Asset is specified.");
|
||||
public static readonly GUIContent defaultFontAssetPathLabel = new GUIContent("Path: Resources/", "The relative path to a Resources folder where the Font Assets and Material Presets are located.\nExample \"Fonts & Materials/\"");
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
class XCResourceImporterProvider : SettingsProvider
|
||||
{
|
||||
XCResourcesImporter m_ResourceImporter;
|
||||
|
||||
public XCResourceImporterProvider()
|
||||
: base("Project/XCharts", SettingsScope.Project)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnGUI(string searchContext)
|
||||
{
|
||||
if (m_ResourceImporter == null)
|
||||
m_ResourceImporter = new XCResourcesImporter();
|
||||
|
||||
m_ResourceImporter.OnGUI();
|
||||
}
|
||||
|
||||
public override void OnDeactivate()
|
||||
{
|
||||
if (m_ResourceImporter != null)
|
||||
m_ResourceImporter.OnDestroy();
|
||||
}
|
||||
|
||||
static UnityEngine.Object GetSettings()
|
||||
{
|
||||
return Resources.Load<XCSettings>("XCSettings");
|
||||
}
|
||||
|
||||
[SettingsProviderGroup]
|
||||
static SettingsProvider[] CreateXCSettingsProvider()
|
||||
{
|
||||
var providers = new List<SettingsProvider> { new XCResourceImporterProvider() };
|
||||
|
||||
if (GetSettings() != null)
|
||||
{
|
||||
var provider = new AssetSettingsProvider("Project/XCharts/Settings", GetSettings);
|
||||
provider.PopulateSearchKeywordsFromGUIContentProperties<XCSettingsEditor.Styles>();
|
||||
providers.Add(provider);
|
||||
}
|
||||
|
||||
return providers.ToArray();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
11
Editor/Windows/XCSettingsEditor.cs.meta
Normal file
11
Editor/Windows/XCSettingsEditor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a1acb5e9cc3740aabbaaccd4ec9b8b8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
211
Editor/Windows/XChartsEditor.cs
Normal file
211
Editor/Windows/XChartsEditor.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace XCharts.Editor
|
||||
{
|
||||
public class XChartsEditor : UnityEditor.Editor
|
||||
{
|
||||
private static Transform GetParent()
|
||||
{
|
||||
GameObject selectObj = Selection.activeGameObject;
|
||||
if (selectObj == null)
|
||||
{
|
||||
var canvas = GameObject.FindObjectOfType<Canvas>();
|
||||
if (canvas != null) return canvas.transform;
|
||||
else
|
||||
{
|
||||
var canvasObject = new GameObject();
|
||||
canvasObject.name = "Canvas";
|
||||
canvas = canvasObject.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
canvasObject.AddComponent<CanvasScaler>();
|
||||
canvasObject.AddComponent<GraphicRaycaster>();
|
||||
var eventSystem = new GameObject();
|
||||
eventSystem.name = "EventSystem";
|
||||
eventSystem.AddComponent<EventSystem>();
|
||||
eventSystem.AddComponent<StandaloneInputModule>();
|
||||
return canvas.transform;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return selectObj.transform;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetName(Transform parent, string name)
|
||||
{
|
||||
if (parent.Find(name) == null) return name;
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
var newName = string.Format("{0} ({1})", name, i);
|
||||
if (parent.Find(newName) == null)
|
||||
{
|
||||
return newName;
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public static T AddChart<T>(string chartName) where T : BaseChart
|
||||
{
|
||||
var parent = GetParent();
|
||||
if (parent == null) return null;
|
||||
var chart = new GameObject();
|
||||
chart.name = GetName(parent, chartName);
|
||||
var t = chart.AddComponent<T>();
|
||||
chart.transform.SetParent(parent);
|
||||
chart.transform.localScale = Vector3.one;
|
||||
chart.transform.localPosition = Vector3.zero;
|
||||
var rect = chart.GetComponent<RectTransform>();
|
||||
rect.anchorMin = new Vector2(0.5f, 0.5f);
|
||||
rect.anchorMax = new Vector2(0.5f, 0.5f);
|
||||
rect.pivot = new Vector2(0.5f, 0.5f);
|
||||
Selection.activeGameObject = chart;
|
||||
EditorUtility.SetDirty(chart);
|
||||
return t;
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/EmptyChart", priority = 43)]
|
||||
[MenuItem("GameObject/XCharts/EmptyChart", priority = 43)]
|
||||
public static void AddBaseChart()
|
||||
{
|
||||
var chart = AddChart<BaseChart>("EmptyChart");
|
||||
chart.GetChartComponent<Title>().text = "EmptyChart";
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/LineChart", priority = 44)]
|
||||
[MenuItem("GameObject/XCharts/LineChart", priority = 44)]
|
||||
public static void AddLineChart()
|
||||
{
|
||||
AddChart<LineChart>("LineChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/BarChart", priority = 45)]
|
||||
[MenuItem("GameObject/XCharts/BarChart", priority = 45)]
|
||||
public static void AddBarChart()
|
||||
{
|
||||
AddChart<BarChart>("BarChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/PieChart", priority = 46)]
|
||||
[MenuItem("GameObject/XCharts/PieChart", priority = 46)]
|
||||
public static void AddPieChart()
|
||||
{
|
||||
AddChart<PieChart>("PieChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/RadarChart", priority = 47)]
|
||||
[MenuItem("GameObject/XCharts/RadarChart", priority = 47)]
|
||||
public static void AddRadarChart()
|
||||
{
|
||||
AddChart<RadarChart>("RadarChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/ScatterChart", priority = 48)]
|
||||
[MenuItem("GameObject/XCharts/ScatterChart", priority = 48)]
|
||||
public static void AddScatterChart()
|
||||
{
|
||||
AddChart<ScatterChart>("ScatterChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/HeatmapChart", priority = 49)]
|
||||
[MenuItem("GameObject/XCharts/HeatmapChart", priority = 49)]
|
||||
public static void AddHeatmapChart()
|
||||
{
|
||||
AddChart<HeatmapChart>("HeatmapChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/RingChart", priority = 51)]
|
||||
[MenuItem("GameObject/XCharts/RingChart", priority = 51)]
|
||||
public static void AddRingChart()
|
||||
{
|
||||
AddChart<RingChart>("RingChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/LiquidChart", priority = 53)]
|
||||
[MenuItem("GameObject/XCharts/LiquidChart", priority = 53)]
|
||||
public static void AddLiquidChart()
|
||||
{
|
||||
AddChart<LiquidChart>("LiquidChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/CandlestickChart", priority = 54)]
|
||||
[MenuItem("GameObject/XCharts/CandlestickChart", priority = 54)]
|
||||
public static void CandlestickChart()
|
||||
{
|
||||
AddChart<CandlestickChart>("CandlestickChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/ParallelChart", priority = 55)]
|
||||
[MenuItem("GameObject/XCharts/ParallelChart", priority = 55)]
|
||||
public static void ParallelChart()
|
||||
{
|
||||
AddChart<ParallelChart>("ParallelChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/SimplifiedLineChart", priority = 56)]
|
||||
[MenuItem("GameObject/XCharts/SimplifiedLineChart", priority = 56)]
|
||||
public static void SimplifiedLineChart()
|
||||
{
|
||||
AddChart<SimplifiedLineChart>("SimplifiedLineChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/SimplifiedBarChart", priority = 57)]
|
||||
[MenuItem("GameObject/XCharts/SimplifiedBarChart", priority = 57)]
|
||||
public static void SimplifiedBarChart()
|
||||
{
|
||||
AddChart<SimplifiedBarChart>("SimplifiedBarChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/SimplifiedCandlestickChart", priority = 58)]
|
||||
[MenuItem("GameObject/XCharts/SimplifiedCandlestickChart", priority = 58)]
|
||||
public static void SimplifiedCandlestickChart()
|
||||
{
|
||||
AddChart<SimplifiedCandlestickChart>("SimplifiedCandlestickChart");
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/Themes Reload")]
|
||||
public static void ReloadTheme()
|
||||
{
|
||||
XCThemeMgr.ReloadThemeList();
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/TextMeshPro Enable")]
|
||||
public static void EnableTextMeshPro()
|
||||
{
|
||||
if (!XChartsMgr.IsExistTMPAssembly())
|
||||
{
|
||||
Debug.LogError("TextMeshPro is not in the project, please import TextMeshPro package first.");
|
||||
return;
|
||||
}
|
||||
XChartsMgr.EnableTextMeshPro();
|
||||
XChartsMgr.ModifyTMPRefence();
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/TextMeshPro Disable")]
|
||||
public static void DisableTextMeshPro()
|
||||
{
|
||||
XChartsMgr.ModifyTMPRefence(true);
|
||||
XChartsMgr.DisableTextMeshPro();
|
||||
}
|
||||
|
||||
[MenuItem("XCharts/Theme/Export Default Theme")]
|
||||
public static void ExportDefaultTheme()
|
||||
{
|
||||
var profile = ScriptableObject.CreateInstance<Theme>();
|
||||
profile.name = "Default";
|
||||
profile.themeName = "Default";
|
||||
profile.themeType = ThemeType.Default;
|
||||
|
||||
var themeName = XCSettings.THEME_ASSET_NAME_PREFIX + profile.themeName;
|
||||
var path = XCSettings.THEME_ASSET_FOLDER + "/" + themeName + ".asset";
|
||||
AssetDatabase.CreateAsset(profile, path);
|
||||
AssetDatabase.SaveAssets();
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Editor/Windows/XChartsEditor.cs.meta
Normal file
11
Editor/Windows/XChartsEditor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 941beb76fdaa64a27a2df6561893157e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user