├── .gitignore ├── Documents~ └── imgs │ └── example_line_chart_2d.png ├── Editor.meta ├── Editor ├── GBG.EditorDataChart.Editor.asmdef ├── GBG.EditorDataChart.Editor.asmdef.meta ├── LineChart2D.meta └── LineChart2D │ ├── DataList.cs │ ├── DataList.cs.meta │ ├── DataListLabel.cs │ ├── DataListLabel.cs.meta │ ├── LineChart2DDrawer.cs │ ├── LineChart2DDrawer.cs.meta │ ├── LineChart2DWindow.cs │ └── LineChart2DWindow.cs.meta ├── LICENSE ├── LICENSE.meta ├── README.md ├── README.md.meta ├── README_CN.md ├── README_CN.md.meta ├── package.json └── package.json.meta /.gitignore: -------------------------------------------------------------------------------- 1 | # This .gitignore file should be placed at the root of your Unity project directory 2 | # 3 | # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore 4 | # 5 | /[Ll]ibrary/ 6 | /[Tt]emp/ 7 | /[Oo]bj/ 8 | /[Bb]uild/ 9 | /[Bb]uilds/ 10 | /[Ll]ogs/ 11 | /[Mm]emoryCaptures/ 12 | 13 | # Asset meta data should only be ignored when the corresponding asset is also ignored 14 | !/[Aa]ssets/**/*.meta 15 | 16 | # Uncomment this line if you wish to ignore the asset store tools plugin 17 | # /[Aa]ssets/AssetStoreTools* 18 | 19 | # Autogenerated Jetbrains Rider plugin 20 | [Aa]ssets/Plugins/Editor/JetBrains* 21 | 22 | # Visual Studio cache directory 23 | .vs/ 24 | 25 | # Gradle cache directory 26 | .gradle/ 27 | 28 | # Autogenerated VS/MD/Consulo solution and project files 29 | ExportedObj/ 30 | .consulo/ 31 | *.csproj 32 | *.unityproj 33 | *.sln 34 | *.suo 35 | *.tmp 36 | *.user 37 | *.userprefs 38 | *.pidb 39 | *.booproj 40 | *.svd 41 | *.pdb 42 | *.mdb 43 | *.opendb 44 | *.VC.db 45 | 46 | # Unity3D generated meta files 47 | *.pidb.meta 48 | *.pdb.meta 49 | *.mdb.meta 50 | 51 | # Unity3D generated file on crash reports 52 | sysinfo.txt 53 | 54 | # Builds 55 | *.apk 56 | *.unitypackage 57 | 58 | # Crashlytics generated file 59 | crashlytics-build.properties 60 | 61 | -------------------------------------------------------------------------------- /Documents~/imgs/example_line_chart_2d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SolarianZ/UnityEditorDataChartTool/374dbc1e6f051b3b9c2c023997c202ac9683b705/Documents~/imgs/example_line_chart_2d.png -------------------------------------------------------------------------------- /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b0baeb05d487e3846bd26fbbcb3cca4f 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor/GBG.EditorDataChart.Editor.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "GBG.EditorDataChart.Editor", 3 | "rootNamespace": "GBG.EditorDataChart.Editor", 4 | "references": [], 5 | "includePlatforms": [ 6 | "Editor" 7 | ], 8 | "excludePlatforms": [], 9 | "allowUnsafeCode": false, 10 | "overrideReferences": false, 11 | "precompiledReferences": [], 12 | "autoReferenced": true, 13 | "defineConstraints": [], 14 | "versionDefines": [], 15 | "noEngineReferences": false 16 | } -------------------------------------------------------------------------------- /Editor/GBG.EditorDataChart.Editor.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8592187cbb728b048b656f7e8352a180 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor/LineChart2D.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 42407335f5dd0eb439f8e872ca9fd4fe 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor/LineChart2D/DataList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using UnityEditor; 4 | using UnityEngine; 5 | 6 | namespace GBG.EditorDataChart.Editor.LineChart2D 7 | { 8 | internal class DataList 9 | { 10 | public string Category { get; } 11 | 12 | public bool Enabled { get; set; } = true; 13 | 14 | public Color Color { get; set; } = EditorGUIUtility.isProSkin ? Color.white : Color.black; 15 | 16 | public int Count => _dataList.Count; 17 | 18 | public Vector2 this[int index] 19 | { 20 | get => _dataList[index]; 21 | set => _dataList[index] = value; 22 | } 23 | 24 | private readonly List _dataList = new(); 25 | 26 | 27 | public DataList(string category, Color? color = null) 28 | { 29 | Category = category; 30 | if (color != null) 31 | { 32 | Color = color.Value; 33 | } 34 | } 35 | 36 | public void Add(Vector2 data) 37 | { 38 | _dataList.Add(data); 39 | } 40 | 41 | public void RemoveAt(int index) 42 | { 43 | _dataList.RemoveAt(index); 44 | } 45 | 46 | public void Clear() 47 | { 48 | _dataList.Clear(); 49 | } 50 | 51 | public int FindIndex(Predicate match) 52 | { 53 | return _dataList.FindIndex(match); 54 | } 55 | 56 | public int FindLastIndex(Predicate match) 57 | { 58 | return _dataList.FindLastIndex(match); 59 | } 60 | 61 | public List.Enumerator GetEnumerator() 62 | { 63 | return _dataList.GetEnumerator(); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /Editor/LineChart2D/DataList.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8f8ee196fe7f4eceabfabbb1614ae583 3 | timeCreated: 1669017187 -------------------------------------------------------------------------------- /Editor/LineChart2D/DataListLabel.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor.UIElements; 2 | using UnityEngine.UIElements; 3 | 4 | namespace GBG.EditorDataChart.Editor.LineChart2D 5 | { 6 | internal class DataListLabel : VisualElement 7 | { 8 | private DataList _target; 9 | 10 | private readonly Toggle _enabled; 11 | 12 | private readonly ColorField _colorField; 13 | 14 | private readonly Label _countLabel; 15 | 16 | private readonly Label _categoryLabel; 17 | 18 | 19 | public DataListLabel() 20 | { 21 | style.flexDirection = FlexDirection.Row; 22 | style.alignItems = Align.Center; 23 | 24 | _enabled = new Toggle(); 25 | _enabled.RegisterValueChangedCallback(evt => 26 | { 27 | if (_target != null) { _target.Enabled = evt.newValue; } 28 | }); 29 | Add(_enabled); 30 | 31 | _colorField = new ColorField 32 | { 33 | style = { flexShrink = 1, width = 16, height = 16, }, showEyeDropper = false, 34 | }; 35 | _colorField.RegisterValueChangedCallback(evt => 36 | { 37 | if (_target != null) { _target.Color = evt.newValue; } 38 | }); 39 | Add(_colorField); 40 | 41 | _countLabel = new Label(); 42 | Add(_countLabel); 43 | 44 | _categoryLabel = new Label(); 45 | Add(_categoryLabel); 46 | } 47 | 48 | public void SetTarget(DataList target) 49 | { 50 | _target = target; 51 | Refresh(); 52 | } 53 | 54 | public void Refresh() 55 | { 56 | if (_target == null) 57 | { 58 | _enabled.SetValueWithoutNotify(true); 59 | _colorField.SetValueWithoutNotify(default); 60 | _categoryLabel.text = null; 61 | _countLabel.text = null; 62 | } 63 | else 64 | { 65 | _enabled.SetValueWithoutNotify(_target.Enabled); 66 | _colorField.SetValueWithoutNotify(_target.Color); 67 | _countLabel.text = $"[{_target.Count.ToString()}]\t"; 68 | _categoryLabel.text = _target.Category; 69 | } 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /Editor/LineChart2D/DataListLabel.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c80a3788290a4a71a4e7e84629071ad9 3 | timeCreated: 1669017168 -------------------------------------------------------------------------------- /Editor/LineChart2D/LineChart2DDrawer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using UnityEditor; 5 | using UnityEngine; 6 | using UnityEngine.Assertions; 7 | using UnityEngine.UIElements; 8 | 9 | namespace GBG.EditorDataChart.Editor.LineChart2D 10 | { 11 | internal struct LineChartScale2D 12 | { 13 | public bool Enabled; 14 | 15 | public float XLength; 16 | 17 | public float YMin; 18 | 19 | public float YMax; 20 | 21 | 22 | public LineChartScale2D(float xLength, float yMin, float yMax, bool enabled) 23 | { 24 | XLength = xLength; 25 | YMin = yMin; 26 | YMax = yMax; 27 | Enabled = enabled; 28 | } 29 | 30 | public void OverrideDataRange(ref float xMin, out float xMax, out float yMin, out float yMax) 31 | { 32 | Assert.IsTrue(XLength > 0, $"{nameof(XLength)}({XLength:F5}) <= 0."); 33 | Assert.IsTrue(YMin < YMax, $"{nameof(YMin)}({YMin:F5}) >= {nameof(YMax)}({YMax:F5})."); 34 | 35 | xMax = xMin + XLength; 36 | yMin = YMin; 37 | yMax = YMax; 38 | } 39 | 40 | public override string ToString() 41 | { 42 | var state = Enabled ? "Enabled" : "Disabled"; 43 | return $"{state}, {nameof(XLength)}={XLength:F5}, {nameof(YMin)}={YMin:F5}, {nameof(YMax)}={YMax:F5}."; 44 | } 45 | } 46 | 47 | internal class LineChart2DDrawer : VisualElement 48 | { 49 | public byte PointRadius { get; set; } = 2; 50 | 51 | public ushort StartIndex { get; set; } = 0; 52 | 53 | public ushort EndIndex { get; set; } = ushort.MaxValue; 54 | 55 | public ref LineChartScale2D FixedScale => ref _fixedScale; 56 | 57 | private LineChartScale2D _fixedScale; 58 | 59 | private readonly List _dataTable; 60 | 61 | private Rect _chartBounds; 62 | 63 | private Rect _dataBounds; 64 | 65 | private GUIStyle _leftAlignedLabelStyle; 66 | 67 | private GUIStyle _rightAlignedLabelStyle; 68 | 69 | private GUIStyle _centerAlignedLabelStyle; 70 | 71 | 72 | public LineChart2DDrawer(List dataTable) 73 | { 74 | _dataTable = dataTable; 75 | 76 | style.flexGrow = 1; 77 | style.flexShrink = 0; 78 | 79 | var chartContainer = new IMGUIContainer(DrawLineChart2D) 80 | { 81 | style = 82 | { 83 | flexGrow = 1, 84 | marginLeft = 20, 85 | marginRight = 20, 86 | marginTop = 20, 87 | marginBottom = 20, 88 | } 89 | }; 90 | chartContainer.RegisterCallback(evt => { _chartBounds = evt.newRect; }); 91 | Add(chartContainer); 92 | } 93 | 94 | public Rect GetDataBounds() 95 | { 96 | var hasPoint = false; 97 | float xMin = default; 98 | float xMax = default; 99 | float yMin = default; 100 | float yMax = default; 101 | 102 | foreach (var dataList in _dataTable) 103 | { 104 | var dataCount = dataList.Count; 105 | Assert.IsTrue(StartIndex <= EndIndex); 106 | 107 | for (var i = StartIndex; i < dataCount && i <= EndIndex; i++) 108 | { 109 | var point = dataList[i]; 110 | if (!hasPoint || xMin > point.x) xMin = point.x; 111 | if (!hasPoint || xMax < point.x) xMax = point.x; 112 | if (!hasPoint || yMin > point.y) yMin = point.y; 113 | if (!hasPoint || yMax < point.y) yMax = point.y; 114 | hasPoint = true; 115 | } 116 | } 117 | 118 | if (!hasPoint) 119 | { 120 | return Rect.zero; 121 | } 122 | 123 | // Ensure the width of bounds is not zero 124 | if (Mathf.Approximately(xMin, xMax)) 125 | { 126 | xMax = xMin + 1; 127 | } 128 | 129 | // Ensure the height of bounds is not zero 130 | if (Mathf.Approximately(yMin, yMax)) 131 | { 132 | yMax = yMin + 1; 133 | } 134 | 135 | if (FixedScale.Enabled) 136 | { 137 | FixedScale.OverrideDataRange(ref xMin, out xMax, out yMin, out yMax); 138 | } 139 | 140 | return Rect.MinMaxRect(xMin, yMin, xMax, yMax); 141 | } 142 | 143 | 144 | private void DrawLineChart2D() 145 | { 146 | var baseColor = GetBaseColor(); 147 | var originalGuiColor = GUI.color; 148 | PrepareLabelStyles(); 149 | 150 | // Find min and max values of x and y 151 | _dataBounds = GetDataBounds(); 152 | 153 | // Draw axes 154 | Handles.color = baseColor; 155 | 156 | // X axis 157 | var xAxisStart = TransformPoint(new Vector2(_dataBounds.xMin, _dataBounds.yMin)); 158 | var xAxisEnd = TransformPoint(new Vector2(_dataBounds.xMax, _dataBounds.yMin)); 159 | Handles.DrawLine(xAxisStart, xAxisEnd); 160 | 161 | // X axis labels 162 | var axisLabelSize = new Vector2(200, 20); 163 | GUI.color = baseColor; 164 | var xStartLabelPos = new Rect(xAxisStart, axisLabelSize); 165 | GUI.Label(xStartLabelPos, _dataBounds.xMin.ToString(CultureInfo.InvariantCulture), _leftAlignedLabelStyle); 166 | var xEndLabelPos = new Rect(xAxisEnd - new Vector2(axisLabelSize.x, 0), axisLabelSize); 167 | GUI.Label(xEndLabelPos, _dataBounds.xMax.ToString(CultureInfo.InvariantCulture), _rightAlignedLabelStyle); 168 | GUI.color = originalGuiColor; 169 | 170 | // Y axis 171 | var yAxisStart = TransformPoint(new Vector2(_dataBounds.xMin, _dataBounds.yMin)); 172 | var yAxisEnd = TransformPoint(new Vector2(_dataBounds.xMin, _dataBounds.yMax)); 173 | Handles.DrawLine(yAxisStart, yAxisEnd); 174 | 175 | // Y axis labels 176 | GUI.color = baseColor; 177 | var yStartLabelPos = new Rect(yAxisStart - new Vector2(0, axisLabelSize.x), axisLabelSize); 178 | GUIUtility.RotateAroundPivot(90, yStartLabelPos.position); 179 | GUI.Label(yStartLabelPos, _dataBounds.yMin.ToString(CultureInfo.InvariantCulture), _rightAlignedLabelStyle); 180 | GUIUtility.RotateAroundPivot(-90, yStartLabelPos.position); 181 | var yEndLabelPos = new Rect(yAxisEnd, axisLabelSize); 182 | GUIUtility.RotateAroundPivot(90, yEndLabelPos.position); 183 | GUI.Label(yEndLabelPos, _dataBounds.yMax.ToString(CultureInfo.InvariantCulture), _leftAlignedLabelStyle); 184 | GUIUtility.RotateAroundPivot(-90, yEndLabelPos.position); 185 | GUI.color = originalGuiColor; 186 | 187 | // Draw data lines 188 | var hover = false; 189 | var hoverData = Vector2.zero; 190 | var hoverColor = Color.black; 191 | foreach (var dataList in _dataTable) 192 | { 193 | if (!dataList.Enabled || dataList.Count == 0) 194 | { 195 | continue; 196 | } 197 | 198 | var normal = Vector3.forward; 199 | Handles.color = dataList.Color; 200 | 201 | var mousePos = Event.current.mousePosition; 202 | var firstPoint = TransformPoint(dataList[0]); 203 | var expandedPointRadius = PointRadius * 1.5f; 204 | if (!hover && (mousePos - firstPoint).sqrMagnitude <= expandedPointRadius * expandedPointRadius) 205 | { 206 | hover = true; 207 | hoverData = dataList[0]; 208 | hoverColor = dataList.Color; 209 | Handles.DrawSolidDisc(firstPoint, normal, expandedPointRadius); 210 | } 211 | else 212 | { 213 | Handles.DrawSolidDisc(firstPoint, normal, PointRadius); 214 | } 215 | 216 | for (var i = StartIndex + 1; i < dataList.Count && i <= EndIndex; i++) 217 | { 218 | // Line segment 219 | var lineStart = TransformPoint(dataList[i - 1]); 220 | var lineEnd = TransformPoint(dataList[i]); 221 | Handles.DrawLine(lineStart, lineEnd); 222 | 223 | // Expand the size of mouse hovered point 224 | if (!hover && (mousePos - lineEnd).sqrMagnitude <= expandedPointRadius * expandedPointRadius) 225 | { 226 | hover = true; 227 | hoverData = dataList[i]; 228 | hoverColor = dataList.Color; 229 | Handles.DrawSolidDisc(lineEnd, normal, expandedPointRadius); 230 | } 231 | else 232 | { 233 | Handles.DrawSolidDisc(lineEnd, normal, PointRadius); 234 | } 235 | } 236 | } 237 | 238 | // Draw value of mouse hovered point 239 | if (hover) 240 | { 241 | var hoverLabelPos = (xAxisStart + xAxisEnd) / 2 - new Vector2(axisLabelSize.x / 2, 0); 242 | var hoverLabelRectPos = new Rect(hoverLabelPos, axisLabelSize); 243 | GUI.color = hoverColor; 244 | GUI.Label(hoverLabelRectPos, $"({hoverData.x:F5}, {hoverData.y:F5})", _centerAlignedLabelStyle); 245 | GUI.color = originalGuiColor; 246 | } 247 | } 248 | 249 | private void PrepareLabelStyles() 250 | { 251 | if (_leftAlignedLabelStyle == null) 252 | { 253 | _leftAlignedLabelStyle = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleLeft }; 254 | } 255 | 256 | if (_rightAlignedLabelStyle == null) 257 | { 258 | _rightAlignedLabelStyle = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleRight }; 259 | } 260 | 261 | if (_centerAlignedLabelStyle == null) 262 | { 263 | _centerAlignedLabelStyle = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter }; 264 | } 265 | } 266 | 267 | /// 268 | /// Transform data point to window gui position. 269 | /// 270 | /// 271 | /// 272 | /// 273 | private Vector2 TransformPoint(Vector2 point, bool lockAspect = false) 274 | { 275 | var xScale = _chartBounds.width / _dataBounds.width; 276 | var yScale = _chartBounds.height / _dataBounds.height; 277 | var scale = lockAspect ? Vector2.one * Math.Min(xScale, yScale) : new Vector2(xScale, yScale); 278 | scale.y *= -1; 279 | var offset = point - _dataBounds.center; 280 | offset.Scale(scale); 281 | 282 | // Don't use windowBounds.center, transform point to window center 283 | var windowCenter = _chartBounds.size / 2; 284 | var windowSpacePoint = windowCenter + offset; 285 | 286 | return windowSpacePoint; 287 | } 288 | 289 | private Color GetBaseColor() 290 | { 291 | return EditorGUIUtility.isProSkin ? Color.white : Color.black; 292 | } 293 | } 294 | } -------------------------------------------------------------------------------- /Editor/LineChart2D/LineChart2DDrawer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 709598fc776c48a7a3eadf822b86da13 3 | timeCreated: 1669017178 -------------------------------------------------------------------------------- /Editor/LineChart2D/LineChart2DWindow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using UnityEditor; 5 | using UnityEditor.UIElements; 6 | using UnityEngine; 7 | using UnityEngine.UIElements; 8 | 9 | namespace GBG.EditorDataChart.Editor.LineChart2D 10 | { 11 | public class LineChart2DWindow : EditorWindow 12 | { 13 | // ReSharper disable once Unity.IncorrectMethodSignature 14 | [MenuItem("Tools/Bamboo/Editor Data Chart/Line Chart 2D")] 15 | public static LineChart2DWindow Open() 16 | { 17 | return GetWindow("Line Chart 2D"); 18 | } 19 | 20 | public static LineChart2DWindow Open(string title) 21 | { 22 | var window = Open(); 23 | window.titleContent = new GUIContent(title); 24 | return window; 25 | } 26 | 27 | 28 | private readonly List _dataTable = new(); 29 | 30 | private ToolbarToggle _lockScaleToggle; 31 | 32 | private SliderInt _chartPointRadiusSlider; 33 | 34 | private ListView _categoryListView; 35 | 36 | private LineChart2DDrawer _chartDrawer; 37 | 38 | private MinMaxSlider _dataRangeSlider; 39 | 40 | 41 | #region Data Management 42 | 43 | public void SetColor(string category, Color color) 44 | { 45 | DataList dataList; 46 | var newDataList = false; 47 | var dataListIndex = _dataTable.FindIndex(list => list.Category == category); 48 | if (dataListIndex < 0) 49 | { 50 | newDataList = true; 51 | dataListIndex = _dataTable.Count; 52 | dataList = new DataList(category); 53 | _dataTable.Add(dataList); 54 | } 55 | else 56 | { 57 | dataList = _dataTable[dataListIndex]; 58 | } 59 | 60 | dataList.Color = color; 61 | 62 | if (!newDataList) 63 | { 64 | _categoryListView.RefreshItem(dataListIndex); 65 | } 66 | } 67 | 68 | public void AddData(string category, Vector2 data) 69 | { 70 | DataList dataList; 71 | var newDataList = false; 72 | var dataListIndex = _dataTable.FindIndex(list => list.Category == category); 73 | if (dataListIndex < 0) 74 | { 75 | newDataList = true; 76 | dataListIndex = _dataTable.Count; 77 | dataList = new DataList(category); 78 | _dataTable.Add(dataList); 79 | } 80 | else 81 | { 82 | dataList = _dataTable[dataListIndex]; 83 | } 84 | 85 | dataList.Add(data); 86 | if (dataList.Count > 1 && dataList[dataList.Count - 2].x >= data.x) 87 | { 88 | Debug.LogWarning($"X value not in ascending order, category={category}, " + 89 | $"index={dataList.Count - 1}, value=({data.x:F5}, {data.y:F5})."); 90 | } 91 | 92 | if (newDataList) 93 | { 94 | _categoryListView.RefreshItems(); 95 | } 96 | else 97 | { 98 | _categoryListView.RefreshItem(dataListIndex); 99 | } 100 | 101 | OnDataListChanged(dataList.Count, false); 102 | } 103 | 104 | public void AddData(string category, float x, float y) 105 | { 106 | AddData(category, new Vector2(x, y)); 107 | } 108 | 109 | public bool RemoveData(string category, int index) 110 | { 111 | var dataListIndex = _dataTable.FindIndex(list => list.Category == category); 112 | if (dataListIndex < 0) 113 | { 114 | return false; 115 | } 116 | 117 | var dataList = _dataTable[dataListIndex]; 118 | dataList.RemoveAt(index); 119 | if (_dataTable[dataListIndex].Count == 0) 120 | { 121 | _dataTable.RemoveAt(dataListIndex); 122 | _categoryListView.RefreshItems(); 123 | } 124 | else 125 | { 126 | _categoryListView.RefreshItem(dataListIndex); 127 | } 128 | 129 | OnDataListChanged(dataList.Count, true); 130 | 131 | return true; 132 | } 133 | 134 | public bool ClearData(string category) 135 | { 136 | var dataListIndex = _dataTable.FindIndex(list => list.Category == category); 137 | if (dataListIndex < 0) 138 | { 139 | return false; 140 | } 141 | 142 | _dataTable.RemoveAt(dataListIndex); 143 | OnDataListChanged(0, true); 144 | 145 | return true; 146 | } 147 | 148 | public void ClearAllData() 149 | { 150 | _dataTable.Clear(); 151 | OnDataListChanged(0, true); 152 | } 153 | 154 | public int FindDataIndex(string category, Predicate match) 155 | { 156 | var dataList = _dataTable.FirstOrDefault(list => list.Category == category); 157 | return dataList?.FindIndex(match) ?? -1; 158 | } 159 | 160 | public int FindDataLastIndex(string category, Predicate match) 161 | { 162 | var dataList = _dataTable.FirstOrDefault(list => list.Category == category); 163 | return dataList?.FindLastIndex(match) ?? -1; 164 | } 165 | 166 | private void OnDataListChanged(int dataCount, bool isDataRemoved) 167 | { 168 | var oldHighLimit = (int)_dataRangeSlider.highLimit; 169 | var newHighLimit = oldHighLimit; 170 | if (isDataRemoved || dataCount < 1) 171 | { 172 | var maxCount = 0; 173 | foreach (var dataList in _dataTable) 174 | { 175 | if (maxCount < dataList.Count) 176 | { 177 | maxCount = dataList.Count; 178 | } 179 | } 180 | 181 | newHighLimit = maxCount > 0 ? maxCount - 1 : 0; 182 | } 183 | else if (oldHighLimit < dataCount - 1) 184 | { 185 | newHighLimit = dataCount - 1; 186 | } 187 | 188 | var maxValue = _dataRangeSlider.maxValue; 189 | _dataRangeSlider.highLimit = newHighLimit; 190 | 191 | if (maxValue > newHighLimit) 192 | { 193 | _dataRangeSlider.maxValue = newHighLimit; 194 | } 195 | else if (Mathf.RoundToInt(maxValue) == oldHighLimit) 196 | { 197 | _dataRangeSlider.maxValue = newHighLimit; 198 | } 199 | } 200 | 201 | #endregion 202 | 203 | 204 | #region Chart Management 205 | 206 | public void SetChartScale(float xValueLength, float yMinValue, float yMaxValue) 207 | { 208 | _chartDrawer.FixedScale = new LineChartScale2D(xValueLength, yMinValue, yMaxValue, true); 209 | _lockScaleToggle.SetValueWithoutNotify(true); 210 | } 211 | 212 | public void RemoveChartScale() 213 | { 214 | _chartDrawer.FixedScale.Enabled = false; 215 | _lockScaleToggle.SetValueWithoutNotify(false); 216 | } 217 | 218 | #endregion 219 | 220 | 221 | #region Lifecycle 222 | 223 | private void OnEnable() 224 | { 225 | rootVisualElement.style.paddingBottom = 8; 226 | 227 | var toolbar = CreateToolbar(); 228 | rootVisualElement.Add(toolbar); 229 | 230 | _categoryListView = new ListView 231 | { 232 | showBorder = true, 233 | reorderable = false, 234 | fixedItemHeight = 20, 235 | itemsSource = _dataTable, 236 | makeItem = MakeDataListViewItem, 237 | bindItem = BindDataListViewItem, 238 | }; 239 | // rootVisualElement.Add(_dataListView); 240 | 241 | _chartDrawer = new LineChart2DDrawer(_dataTable); 242 | _chartDrawer.PointRadius = (byte)_chartPointRadiusSlider.value; 243 | rootVisualElement.Add(_chartDrawer); 244 | 245 | _dataRangeSlider = new MinMaxSlider("Range", 0, 1, 0, 1); 246 | _dataRangeSlider.labelElement.style.minWidth = 55; 247 | _dataRangeSlider.RegisterValueChangedCallback(OnDataRangeChanged); 248 | rootVisualElement.Add(_dataRangeSlider); 249 | } 250 | 251 | private void Update() 252 | { 253 | Repaint(); 254 | } 255 | 256 | private void ShowButton(Rect position) 257 | { 258 | if (GUI.Button(position, EditorGUIUtility.IconContent("_Help"), GUI.skin.FindStyle("IconButton"))) 259 | { 260 | Application.OpenURL("https://github.com/SolarianZ/UnityEditorDataChartTool"); 261 | } 262 | } 263 | 264 | private VisualElement MakeDataListViewItem() 265 | { 266 | return new DataListLabel(); 267 | } 268 | 269 | private void BindDataListViewItem(VisualElement element, int index) 270 | { 271 | var dataListLabel = (DataListLabel)element; 272 | dataListLabel.SetTarget(_dataTable[index]); 273 | } 274 | 275 | private void OnDataRangeChanged(ChangeEvent evt) 276 | { 277 | var oldStartIndex = _chartDrawer.StartIndex; 278 | var oldEndIndex = _chartDrawer.EndIndex; 279 | //var isLenghtChanged = 280 | CalcIndices(oldStartIndex, oldEndIndex, evt.newValue.x, evt.newValue.y, 281 | out var newStartIndex, out var newEndIndex); 282 | _dataRangeSlider.SetValueWithoutNotify(new Vector2(newStartIndex, newEndIndex)); 283 | 284 | if (newStartIndex == oldStartIndex && newEndIndex == oldEndIndex) 285 | { 286 | return; 287 | } 288 | 289 | // Let users choose when to lock state from toolbar 290 | // if (isLenghtChanged) 291 | // { 292 | // _chartDrawer.FixedScale.Enabled = false; 293 | // } 294 | // else 295 | // { 296 | // // Translate indices, should lock scale 297 | // var dataRange = _chartDrawer.GetDataBounds(); 298 | // _chartDrawer.FixedScale = new LineChartScale2D(dataRange.width, dataRange.yMin, dataRange.yMax, true); 299 | // } 300 | 301 | _chartDrawer.StartIndex = (ushort)newStartIndex; 302 | _chartDrawer.EndIndex = (ushort)newEndIndex; 303 | _dataRangeSlider.label = $"Range[{newStartIndex.ToString()},{newEndIndex.ToString()}]"; 304 | 305 | 306 | // When x and y close to 0.5f, Mathf.RoundToInt() may floors one of them and ceils another 307 | // ReSharper disable once UnusedLocalFunctionReturnValue 308 | bool CalcIndices(int internalOldStartIndex, int internalOldEndIndex, float newStartValue, float newEndValue, 309 | out int internalNewStartIndex, out int internalNewEndIndex) 310 | { 311 | internalNewStartIndex = (ushort)Mathf.RoundToInt(newStartValue); 312 | internalNewEndIndex = (ushort)Mathf.RoundToInt(newEndValue); 313 | 314 | var oldLength = internalOldEndIndex - internalOldStartIndex; 315 | var newLength = newEndValue - newStartValue; 316 | var isLenghtChanged = Mathf.Abs(oldLength - newLength) > 0.6f; 317 | if (!isLenghtChanged) // Translate indices 318 | { 319 | internalNewEndIndex = (ushort)(internalNewStartIndex + oldLength); 320 | } 321 | 322 | return isLenghtChanged; 323 | } 324 | } 325 | 326 | #endregion 327 | 328 | 329 | #region Toolbar 330 | 331 | private Toolbar CreateToolbar() 332 | { 333 | var toolbar = new Toolbar(); 334 | 335 | var categoryToggle = new ToolbarToggle { text = "Categories", }; 336 | categoryToggle.RegisterValueChangedCallback(OnToolbarCategoryToggleChanged); 337 | toolbar.Add(categoryToggle); 338 | 339 | _lockScaleToggle = new ToolbarToggle { text = "Lock Scale" }; 340 | _lockScaleToggle.RegisterValueChangedCallback(OnToolbarLockScaleToggleChanged); 341 | toolbar.Add(_lockScaleToggle); 342 | 343 | _chartPointRadiusSlider = new SliderInt("Radius", 1, 10) { value = 2, }; 344 | _chartPointRadiusSlider.labelElement.style.minWidth = 45; 345 | _chartPointRadiusSlider.Q(className: "unity-base-slider__drag-container").style.width = 40; 346 | // _chartPointRadiusSlider.Q(className: "unity-base-slider__input").style.width = 40; 347 | // _chartPointRadiusSlider.Q(className: "unity-base-slider__text-field").style.width = 22; 348 | _chartPointRadiusSlider.RegisterValueChangedCallback(OnToolbarChartPointRadiusSliderChanged); 349 | toolbar.Add(_chartPointRadiusSlider); 350 | UpdateToolbarChartPointRadiusSliderTitle(); 351 | 352 | return toolbar; 353 | } 354 | 355 | private void OnToolbarCategoryToggleChanged(ChangeEvent evt) 356 | { 357 | if (evt.newValue) 358 | { 359 | rootVisualElement.Insert(1, _categoryListView); 360 | } 361 | else 362 | { 363 | rootVisualElement.Remove(_categoryListView); 364 | } 365 | } 366 | 367 | private void OnToolbarLockScaleToggleChanged(ChangeEvent evt) 368 | { 369 | if (evt.newValue) 370 | { 371 | var dataRange = _chartDrawer.GetDataBounds(); 372 | _chartDrawer.FixedScale = new LineChartScale2D(dataRange.width, dataRange.yMin, dataRange.yMax, true); 373 | } 374 | else 375 | { 376 | _chartDrawer.FixedScale.Enabled = false; 377 | } 378 | } 379 | 380 | private void OnToolbarChartPointRadiusSliderChanged(ChangeEvent evt) 381 | { 382 | _chartDrawer.PointRadius = (byte)evt.newValue; 383 | UpdateToolbarChartPointRadiusSliderTitle(); 384 | } 385 | 386 | private void UpdateToolbarChartPointRadiusSliderTitle() 387 | { 388 | _chartPointRadiusSlider.label = $"Radius({_chartPointRadiusSlider.value})"; 389 | } 390 | 391 | #endregion 392 | } 393 | } -------------------------------------------------------------------------------- /Editor/LineChart2D/LineChart2DWindow.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4271cf87360022d428c2076c7c6d0fe9 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Qiuyu ZHANG 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f633682abc2936041a483bc836920e6c 3 | DefaultImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity Editor Data Chart Tool 2 | 3 | Draw data chart in Unity Editor. 4 | 5 | ![Example](./Documents~/imgs/example_line_chart_2d.png) 6 | 7 | [中文](./README_CN.md) 8 | 9 | 10 | ## Supported Unity Version 11 | 12 | Unity 2021.3 and later. 13 | 14 | 15 | ## Installation 16 | 17 | [![openupm](https://img.shields.io/npm/v/com.greenbamboogames.editordatachart?label=openupm®istry_uri=https://package.openupm.com)](https://openupm.com/packages/com.greenbamboogames.editordatachart/) 18 | 19 | Install this package via [OpenUPM](https://openupm.com/packages/com.greenbamboogames.editordatachart) . 20 | 21 | 22 | ## API 23 | 24 | - class LineChart2DWindow 25 | - static void Open(string title): Open data chart window. 26 | - void SetColor(string category, Color color): Set the color of the category. 27 | - void AddData(string category, Vector2 data): Add data to category. 28 | - bool RemoveData(string category, int index): Remove data from category. 29 | - bool ClearData(string category): Clear data of category(will remove category). 30 | - void ClearAllData(): Clear all categories(will remove all categories). 31 | - int FindDataIndex(string category, Predicate match): Find the index of the data in category. 32 | - int FindDataLastIndex(string category, Predicate match): Find the last index of the data in category. 33 | - void SetChartScale(float xValueLength, float yMinValue, float yMaxValue): Set visible value range of the chart. 34 | - void RemoveChartScale(): Remove visible value range of the chart. 35 | 36 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6c92a1b93334caa488b1f289745e4488 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # Unity Editor数据图表工具 2 | 3 | 在Unity Editor绘制数据图表。 4 | 5 | ![Example](./Documents~/imgs/example_line_chart_2d.png) 6 | 7 | [English](./README.md) 8 | 9 | 10 | ## 支持的Unity版本 11 | 12 | Unity 2021.3 或更新版本。 13 | 14 | 15 | ## 安装 16 | 17 | [![openupm](https://img.shields.io/npm/v/com.greenbamboogames.editordatachart?label=openupm®istry_uri=https://package.openupm.com)](https://openupm.com/packages/com.greenbamboogames.editordatachart/) 18 | 19 | 从 [OpenUPM](https://openupm.com/packages/com.greenbamboogames.editordatachart) 安装。 20 | 21 | 22 | ## API 23 | 24 | - class LineChart2DWindow 25 | - static void Open(string title): 打开数据图表窗口。 26 | - void SetColor(string category, Color color): 设置类别颜色。 27 | - void AddData(string category, Vector2 data): 向类别中添加数据。 28 | - bool RemoveData(string category, int index): 从类别中移除数据。 29 | - bool ClearData(string category): 清空类别中的数据(将会删除此类别)。 30 | - void ClearAllData(): 清空所有类别中的数据(将会删除所有类别)。 31 | - int FindDataIndex(string category, Predicate match): 在类别中查找指定数据的索引。 32 | - int FindDataLastIndex(string category, Predicate match): 在类别中查找指定数据的最后一个索引。 33 | - void SetChartScale(float xValueLength, float yMinValue, float yMaxValue): 设置图表的显示数值范围。 34 | - void RemoveChartScale(): 移除图表的显示数值范围。 35 | 36 | -------------------------------------------------------------------------------- /README_CN.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5fa79c3ed6237184fbad43cca85a31fa 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.greenbamboogames.editordatachart", 3 | "version": "1.0.1", 4 | "displayName": "Editor Data Chart!", 5 | "description": "Draw data chart in Unity Editor.", 6 | "unity": "2021.3", 7 | "documentationUrl": "https://github.com/SolarianZ/UnityEditorDataChartTool/blob/main/README.md", 8 | "changelogUrl": "https://github.com/SolarianZ/UnityEditorDataChartTool/releases", 9 | "licensesUrl": "https://github.com/SolarianZ/UnityEditorDataChartTool/blob/main/LICENSE", 10 | "keywords": [ 11 | "Data", 12 | "Chart" 13 | ], 14 | "author": { 15 | "name": "ZQY", 16 | "email": "vdergow@hotmail.com", 17 | "url": "https://github.com/SolarianZ" 18 | }, 19 | "type": "tool", 20 | "hideInEditor": false 21 | } -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 35f3b294cdb4d8345838c451ba17b92e 3 | PackageManifestImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | --------------------------------------------------------------------------------