mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-20 07:20:08 +00:00
增加ScatterChart散点图
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,8 @@
|
||||
using UnityEngine.UI;
|
||||
using XCharts;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
[ExecuteInEditMode]
|
||||
public class Demo : MonoBehaviour
|
||||
{
|
||||
private Theme m_SelectedTheme;
|
||||
@@ -11,6 +13,7 @@ public class Demo : MonoBehaviour
|
||||
private GameObject m_BarChartModule;
|
||||
private GameObject m_PieChartModule;
|
||||
private GameObject m_RadarChartModule;
|
||||
private GameObject m_ScatterChartModule;
|
||||
private GameObject m_OtherModule;
|
||||
|
||||
private Button m_DefaultThemeButton;
|
||||
@@ -21,6 +24,7 @@ public class Demo : MonoBehaviour
|
||||
private Button m_BarChartButton;
|
||||
private Button m_PieChartButton;
|
||||
private Button m_RadarChartButton;
|
||||
private Button m_ScatterChartButton;
|
||||
private Button m_OtherButton;
|
||||
|
||||
private Text m_Title;
|
||||
@@ -45,12 +49,22 @@ public class Demo : MonoBehaviour
|
||||
m_BarChartModule = transform.Find("chart_detail/Viewport/bar_chart").gameObject;
|
||||
m_PieChartModule = transform.Find("chart_detail/Viewport/pie_chart").gameObject;
|
||||
m_RadarChartModule = transform.Find("chart_detail/Viewport/radar_chart").gameObject;
|
||||
m_ScatterChartModule = transform.Find("chart_detail/Viewport/scatter_chart").gameObject;
|
||||
m_OtherModule = transform.Find("chart_detail/Viewport/other").gameObject;
|
||||
m_Mark = transform.Find("chart_detail/Viewport").GetComponent<Mask>();
|
||||
m_Mark.enabled = true;
|
||||
|
||||
m_Title = transform.Find("chart_title/Text").GetComponent<Text>();
|
||||
|
||||
|
||||
// LineChart[] charts = m_LineChartModule.GetComponentsInChildren<LineChart>();
|
||||
// foreach(var chart in charts){
|
||||
// foreach(var serie in chart.series.series){
|
||||
// serie.symbol.type = SerieSymbolType.EmptyCircle;
|
||||
// serie.symbol.size = 4;
|
||||
// serie.symbol.selectedSize = 6;
|
||||
// }
|
||||
// }
|
||||
InitThemeButton();
|
||||
InitChartButton();
|
||||
}
|
||||
@@ -66,12 +80,14 @@ public class Demo : MonoBehaviour
|
||||
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_ScatterChartButton = transform.Find("chart_list/btn_scatterchart").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_ScatterChartButton.onClick.AddListener(delegate () { SelectedModule(m_ScatterChartModule); });
|
||||
m_OtherButton.onClick.AddListener(delegate () { SelectedModule(m_OtherModule); });
|
||||
|
||||
SelectedModule(m_LineChartModule);
|
||||
@@ -102,12 +118,14 @@ public class Demo : MonoBehaviour
|
||||
m_BarChartModule.SetActive(module == m_BarChartModule);
|
||||
m_PieChartModule.SetActive(module == m_PieChartModule);
|
||||
m_RadarChartModule.SetActive(module == m_RadarChartModule);
|
||||
m_ScatterChartModule.SetActive(module == m_ScatterChartModule);
|
||||
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_ScatterChartButton, m_SelectedModule, m_ScatterChartModule);
|
||||
SetButtonColor(m_OtherButton, m_SelectedModule, m_OtherModule);
|
||||
|
||||
m_ScrollRect.content = m_SelectedModule.GetComponent<RectTransform>();
|
||||
@@ -128,6 +146,10 @@ public class Demo : MonoBehaviour
|
||||
{
|
||||
m_Title.text = "雷达图 Radar";
|
||||
}
|
||||
else if (module == m_ScatterChartModule)
|
||||
{
|
||||
m_Title.text = "散点图 Scatter";
|
||||
}
|
||||
else if (module == m_OtherModule)
|
||||
{
|
||||
m_Title.text = "其他";
|
||||
|
||||
29
Demo/Scripts/Demo50_Scatter.cs
Normal file
29
Demo/Scripts/Demo50_Scatter.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XCharts;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
[ExecuteInEditMode]
|
||||
public class Demo50_Scatter : MonoBehaviour
|
||||
{
|
||||
private ScatterChart chart;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
chart = gameObject.GetComponent<ScatterChart>();
|
||||
if (chart == null) return;
|
||||
chart.series.SetSerieSymbolSizeCallback(SymbolSize,SymbolSelectedSize);
|
||||
}
|
||||
|
||||
float SymbolSize(List<float> data)
|
||||
{
|
||||
//return Mathf.Clamp(data[1] * 10,1,100);
|
||||
return (float)(Mathf.Sqrt(data[2]) / 6e2);
|
||||
}
|
||||
|
||||
float SymbolSelectedSize(List<float> data)
|
||||
{
|
||||
//return Mathf.Clamp(data[1] * 10,1,100);
|
||||
return (float)(Mathf.Sqrt(data[2]) / 5e2);
|
||||
}
|
||||
}
|
||||
11
Demo/Scripts/Demo50_Scatter.cs.meta
Normal file
11
Demo/Scripts/Demo50_Scatter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb4941eb0bbd14736b16b39d256255bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
101914
Demo/demo_xchart.unity
101914
Demo/demo_xchart.unity
File diff suppressed because it is too large
Load Diff
23
Scripts/Editor/ScatterChartEditor.cs
Normal file
23
Scripts/Editor/ScatterChartEditor.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using UnityEditor;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// Editor class used to edit UI ScatterChart.
|
||||
/// </summary>
|
||||
|
||||
[CustomEditor(typeof(ScatterChart), false)]
|
||||
public class ScatterChartEditor : CoordinateChartEditor
|
||||
{
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
m_Target = (ScatterChart)target;
|
||||
}
|
||||
|
||||
protected override void OnEndInspectorGUI()
|
||||
{
|
||||
base.OnEndInspectorGUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/Editor/ScatterChartEditor.cs.meta
Normal file
11
Scripts/Editor/ScatterChartEditor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3995bd8e5f80b49008624a5746622f68
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -24,9 +24,9 @@ public class ChartEditorHelper
|
||||
SerializedProperty stringDataProp = prop.FindPropertyRelative("m_JsonData");
|
||||
SerializedProperty needParseProp = prop.FindPropertyRelative("m_DataFromJson");
|
||||
float defalutX = drawRect.x;
|
||||
drawRect.x = EditorGUIUtility.labelWidth + 40;
|
||||
drawRect.width = EditorGUIUtility.currentViewWidth - EditorGUIUtility.labelWidth - 60;
|
||||
if (GUI.Button(drawRect, new GUIContent("Parse Json", "Parse data from input json")))
|
||||
drawRect.x = EditorGUIUtility.labelWidth + 15;
|
||||
drawRect.width = EditorGUIUtility.currentViewWidth - EditorGUIUtility.labelWidth - 35;
|
||||
if (GUI.Button(drawRect, new GUIContent("Parse JsonData", "Parse data from input json")))
|
||||
{
|
||||
showTextArea = !showTextArea;
|
||||
bool needParse = !showTextArea;
|
||||
|
||||
@@ -176,7 +176,9 @@ namespace XCharts
|
||||
{
|
||||
var xdata = serie.xData[n];
|
||||
var ydata = serie.yData[n];
|
||||
if (Mathf.Abs(xValue - xdata) / xRate < 5 && Mathf.Abs(yValue - ydata) / yRate < 5)
|
||||
var symbolSize = serie.symbol.GetSize(serie.data[n].data);
|
||||
if (Mathf.Abs(xValue - xdata) / xRate < symbolSize
|
||||
&& Mathf.Abs(yValue - ydata) / yRate < symbolSize)
|
||||
{
|
||||
m_Tooltip.dataIndex[i] = n;
|
||||
serie.selected = true;
|
||||
|
||||
@@ -47,11 +47,7 @@ namespace XCharts
|
||||
m_ItemWidth = 60.0f,
|
||||
m_ItemHeight = 20.0f,
|
||||
m_ItemGap = 5,
|
||||
m_ItemFontSize = 16,
|
||||
m_Data = new List<string>()
|
||||
{
|
||||
"serie1"
|
||||
}
|
||||
m_ItemFontSize = 16
|
||||
};
|
||||
legend.location.top = 30;
|
||||
return legend;
|
||||
|
||||
26
Scripts/UI/Internal/Scatter.cs
Normal file
26
Scripts/UI/Internal/Scatter.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
[System.Serializable]
|
||||
public class Scatter
|
||||
{
|
||||
[SerializeField] private string m_Name;
|
||||
[SerializeField] private float m_InsideRadius;
|
||||
[SerializeField] private float m_OutsideRadius;
|
||||
[SerializeField] private float m_TooltipExtraRadius;
|
||||
|
||||
public string name { get { return m_Name; } set { m_Name = value; } }
|
||||
|
||||
public static Scatter defaultScatter
|
||||
{
|
||||
get
|
||||
{
|
||||
var scatter = new Scatter
|
||||
{
|
||||
};
|
||||
return scatter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/Internal/Scatter.cs.meta
Normal file
11
Scripts/UI/Internal/Scatter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1dd59b655decf420ca27dfb742be4fdf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -283,8 +283,7 @@ namespace XCharts
|
||||
Color color = m_Radar.backgroundColorList[i % m_Radar.backgroundColorList.Count];
|
||||
outsideRadius = insideRadius + block;
|
||||
ChartHelper.DrawDoughnut(vh, p, insideRadius, outsideRadius, 0, 360, color);
|
||||
ChartHelper.DrawCicleNotFill(vh, p, outsideRadius, m_Radar.lineTickness,
|
||||
m_Radar.lineColor);
|
||||
ChartHelper.DrawCicleNotFill(vh, p, outsideRadius, m_Radar.lineTickness, m_Radar.lineColor);
|
||||
insideRadius = outsideRadius;
|
||||
}
|
||||
for (int j = 0; j <= indicatorNum; j++)
|
||||
|
||||
96
Scripts/UI/ScatterChart.cs
Normal file
96
Scripts/UI/ScatterChart.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
[AddComponentMenu("XCharts/ScatterChart", 17)]
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
[DisallowMultipleComponent]
|
||||
public class ScatterChart : CoordinateChart
|
||||
{
|
||||
[SerializeField] private Scatter m_Scatter = Scatter.defaultScatter;
|
||||
|
||||
public Scatter scatter { get { return m_Scatter; } }
|
||||
|
||||
#if UNITY_EDITOR
|
||||
protected override void Reset()
|
||||
{
|
||||
base.Reset();
|
||||
m_Scatter = Scatter.defaultScatter;
|
||||
m_Title.text = "ScatterChart";
|
||||
m_Tooltip.type = Tooltip.Type.None;
|
||||
m_XAxises[0].type = Axis.AxisType.Value;
|
||||
m_XAxises[0].boundaryGap = false;
|
||||
m_YAxises[1].type = Axis.AxisType.Value;
|
||||
m_XAxises[1].boundaryGap = false;
|
||||
RemoveData();
|
||||
AddSerie("serie1", SerieType.Scatter);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
AddXYData(0, Random.Range(10, 100), Random.Range(10, 100));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
protected override void DrawChart(VertexHelper vh)
|
||||
{
|
||||
base.DrawChart(vh);
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
var serie = m_Series.series[i];
|
||||
serie.index = i;
|
||||
var yAxis = m_YAxises[serie.axisIndex];
|
||||
var xAxis = m_XAxises[serie.axisIndex];
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
if (serie.dataCount <= 0 || !serie.show)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
int maxCount = maxShowDataNumber > 0 ?
|
||||
(maxShowDataNumber > serie.dataCount ? serie.dataCount : maxShowDataNumber)
|
||||
: serie.dataCount;
|
||||
int dataCount = (maxCount - minShowDataNumber);
|
||||
for (int n = minShowDataNumber; n < maxCount; n++)
|
||||
{
|
||||
float xValue, yValue;
|
||||
serie.GetXYData(n, m_DataZoom, out xValue, out yValue);
|
||||
float pX = coordinateX + m_Coordinate.tickness;
|
||||
float pY = coordinateY + m_Coordinate.tickness;
|
||||
float xDataHig = (xValue - xAxis.minValue) / (xAxis.maxValue - xAxis.minValue) * coordinateWid;
|
||||
float yDataHig = (yValue - yAxis.minValue) / (yAxis.maxValue - yAxis.minValue) * coordinateHig;
|
||||
var pos = new Vector3(pX + xDataHig, pY + yDataHig);
|
||||
var color = m_ThemeInfo.GetColor(serieNameCount);
|
||||
color.a = 200;
|
||||
var datas = serie.data[n].data;
|
||||
float symbolSize = 0;
|
||||
if (serie.selected && n == m_Tooltip.dataIndex[serie.axisIndex])
|
||||
{
|
||||
symbolSize = serie.symbol.GetSelectedSize(datas);
|
||||
}
|
||||
else
|
||||
{
|
||||
symbolSize = serie.symbol.GetSize(datas);
|
||||
}
|
||||
if (symbolSize > 100) symbolSize = 100;
|
||||
DrawSymbol(vh, serie.symbol.type, symbolSize, 3, pos, color);
|
||||
}
|
||||
if (vh.currentVertCount > 60000)
|
||||
{
|
||||
m_Large++;
|
||||
Debug.LogError("large:" + m_Large + "," + vh.currentVertCount);
|
||||
RefreshChart();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/ScatterChart.cs.meta
Normal file
11
Scripts/UI/ScatterChart.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf16aac0bd6c24a8da75846c34c5193e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
@@ -8,7 +9,7 @@ namespace XCharts
|
||||
{
|
||||
public static class ChartHelper
|
||||
{
|
||||
private static float CRICLE_SMOOTHNESS = 1f;
|
||||
public static float CRICLE_SMOOTHNESS = 2.5f;
|
||||
private static UIVertex[] vertex = new UIVertex[4];
|
||||
|
||||
public static void HideAllObject(GameObject obj, string match = null)
|
||||
@@ -240,13 +241,13 @@ namespace XCharts
|
||||
vh.AddTriangle(startIndex, startIndex + 1, startIndex + 2);
|
||||
}
|
||||
|
||||
public static void DrawCricle(VertexHelper vh, Vector3 p, float radius, Color32 color,
|
||||
int segments = 0, bool fill = true)
|
||||
public static void DrawCricle(VertexHelper vh, Vector3 p, float radius, Color32 color, int segments = 0)
|
||||
{
|
||||
if (segments <= 0)
|
||||
{
|
||||
segments = (int)((2 * Mathf.PI * radius) / CRICLE_SMOOTHNESS);
|
||||
}
|
||||
if (segments < 3) segments = 3;
|
||||
DrawSector(vh, p, radius, color, 0, 360, segments);
|
||||
}
|
||||
|
||||
@@ -450,12 +451,29 @@ namespace XCharts
|
||||
int startIndex = jsonData.IndexOf("[");
|
||||
int endIndex = jsonData.IndexOf("]");
|
||||
string temp = jsonData.Substring(startIndex + 1, endIndex - startIndex - 1);
|
||||
string[] datas = temp.Split(',');
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
Debug.LogError("temp:"+temp);
|
||||
if (temp.IndexOf("],") > -1 || temp.IndexOf("] ,") > -1)
|
||||
{
|
||||
list.Add(float.Parse(datas[i].Trim()));
|
||||
string[] datas = temp.Split(new string[] { "],", "] ," }, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
temp = datas[i];
|
||||
Debug.LogError("split:" + temp);
|
||||
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
return list;
|
||||
else
|
||||
{
|
||||
string[] datas = temp.Split(',');
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
list.Add(float.Parse(datas[i].Trim()));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static List<string> ParseStringFromString(string jsonData)
|
||||
|
||||
Reference in New Issue
Block a user