From 702b2a8c247109307095e0377075bd320eb88af3 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Wed, 11 Mar 2020 21:53:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96`LineChart`=E7=9A=84`label`?= =?UTF-8?q?=E5=81=8F=E7=A7=BB=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + Editor/PropertyDrawers/SerieDrawer.cs | 21 +++++++------------ Editor/Utility/ChartEditorHelper.cs | 29 ++++++++++++++++++++------- Runtime/Helper/SerieHelper.cs | 26 ++++++++++++++++++++++++ Runtime/Internal/CoordinateChart.cs | 12 ++++++++--- version.json | 4 ++-- 6 files changed, 67 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f46e2b1f..f7861b8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.03.11) 优化`LineChart`的`label`偏移显示 * (2020.03.11) 优化清空并重新添加数据后的自动刷新问题 * (2020.03.10) 增加`LineChart`的普通折线图可通过`ignore`参数设置忽略数据的支持 * (2020.03.09) 增加`BarChart`可通过`ItemStyle`配置边框的支持 diff --git a/Editor/PropertyDrawers/SerieDrawer.cs b/Editor/PropertyDrawers/SerieDrawer.cs index 746d932c..4a7bb76d 100644 --- a/Editor/PropertyDrawers/SerieDrawer.cs +++ b/Editor/PropertyDrawers/SerieDrawer.cs @@ -417,23 +417,17 @@ namespace XCharts } if (showDetail) { - EditorGUI.indentLevel++; + EditorGUI.indentLevel += 2; var m_Icon = serieData.FindPropertyRelative("m_IconStyle"); var m_EnableLabel = serieData.FindPropertyRelative("m_EnableLabel"); var m_Label = serieData.FindPropertyRelative("m_Label"); EditorGUI.PropertyField(drawRect, m_Icon); drawRect.y += EditorGUI.GetPropertyHeight(m_Icon); - EditorGUI.PropertyField(drawRect, m_EnableLabel); - drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; - if (m_EnableLabel.boolValue) - { - EditorGUI.indentLevel++; - EditorGUI.PropertyField(drawRect, m_Label); - drawRect.y += EditorGUI.GetPropertyHeight(m_Label); - EditorGUI.indentLevel--; - } + EditorGUI.PropertyField(drawRect, m_Label); + ChartEditorHelper.MakeBool(ref drawRect, m_EnableLabel, 1, "(enable)"); + drawRect.y += EditorGUI.GetPropertyHeight(m_Label); - EditorGUI.indentLevel--; + EditorGUI.indentLevel -= 2; } } @@ -537,10 +531,9 @@ namespace XCharts for (int i = 0; i < num; i++) { var item = m_Data.GetArrayElementAtIndex(i); - height += 1 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing; + //height += 1 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing; height += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("m_IconStyle")); - if (item.FindPropertyRelative("m_EnableLabel").boolValue) - height += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("m_Label")); + height += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("m_Label")); } } } diff --git a/Editor/Utility/ChartEditorHelper.cs b/Editor/Utility/ChartEditorHelper.cs index 8d19f9cf..0f59bf06 100644 --- a/Editor/Utility/ChartEditorHelper.cs +++ b/Editor/Utility/ChartEditorHelper.cs @@ -79,17 +79,32 @@ public class ChartEditorHelper float defaultX = drawRect.x; drawRect.width = EditorGUIUtility.labelWidth; moduleToggle = EditorGUI.Foldout(drawRect, moduleToggle, content, bold ? foldoutStyle : EditorStyles.foldout); - drawRect.x = EditorGUIUtility.labelWidth - (EditorGUI.indentLevel - 1) * 15 - 2; - drawRect.width = 40; - if (prop != null) - { - EditorGUI.PropertyField(drawRect, prop, GUIContent.none); - } + MakeBool(ref drawRect, prop); drawRect.width = defaultWidth; drawRect.x = defaultX; return moduleToggle; } + public static void MakeBool(ref Rect drawRect, SerializedProperty boolProp, int index = 0, string name = null) + { + float defaultWidth = drawRect.width; + float defaultX = drawRect.x; + drawRect.x = EditorGUIUtility.labelWidth - (EditorGUI.indentLevel - 1) * 15 - 2 + index * 30; + drawRect.width = 20 + EditorGUI.indentLevel * 20; + if (boolProp != null) + { + EditorGUI.PropertyField(drawRect, boolProp, GUIContent.none); + drawRect.x += 13; + drawRect.width = 200; + if (!string.IsNullOrEmpty(name)) + { + EditorGUI.LabelField(drawRect, name); + } + } + drawRect.width = defaultWidth; + drawRect.x = defaultX; + } + public static bool MakeFoldout(ref Rect drawRect, ref Dictionary moduleToggle, SerializedProperty prop, string moduleName, SerializedProperty showProp = null, bool bold = true) { @@ -114,7 +129,7 @@ public class ChartEditorHelper { if (showProp.propertyType == SerializedPropertyType.Boolean) { - drawRect.width = 80; + drawRect.width = 100; } else { diff --git a/Runtime/Helper/SerieHelper.cs b/Runtime/Helper/SerieHelper.cs index 74ef550e..739e88b7 100644 --- a/Runtime/Helper/SerieHelper.cs +++ b/Runtime/Helper/SerieHelper.cs @@ -46,5 +46,31 @@ namespace XCharts return color; } } + + public static bool IsDownPoint(Serie serie, int index) + { + var dataPoints = serie.dataPoints; + if (dataPoints.Count < 2) return false; + else if (index > 0 && index < dataPoints.Count - 1) + { + var lp = dataPoints[index - 1]; + var np = dataPoints[index + 1]; + var cp = dataPoints[index]; + var dot = Vector3.Cross(np - lp, cp - np); + return dot.z < 0; + } + else if (index == 0) + { + return dataPoints[0].y < dataPoints[1].y; + } + else if (index == dataPoints.Count - 1) + { + return dataPoints[index].y < dataPoints[index - 1].y; + } + else + { + return false; + } + } } } \ No newline at end of file diff --git a/Runtime/Internal/CoordinateChart.cs b/Runtime/Internal/CoordinateChart.cs index 5cbb5f57..eceb08ee 100644 --- a/Runtime/Internal/CoordinateChart.cs +++ b/Runtime/Internal/CoordinateChart.cs @@ -411,7 +411,13 @@ namespace XCharts sb.Length = 0; if (!isCartesian) { - sb.Append(tempAxis.GetData(index, m_DataZoom)); + var category = tempAxis.GetData(index, m_DataZoom); + if (!string.IsNullOrEmpty(category)) sb.Append(category); + else + { + m_Tooltip.SetActive(false); + return; + } } for (int i = 0; i < m_Series.Count; i++) { @@ -1498,7 +1504,6 @@ namespace XCharts if (j >= serie.dataPoints.Count) break; var serieData = serie.data[j]; var pos = serie.dataPoints[j]; - serieData.SetGameObjectPosition(serieData.labelPosition); serieData.UpdateIcon(); if (serie.show && serie.label.show && serieData.canShowLabel) @@ -1522,7 +1527,8 @@ namespace XCharts content = serie.label.GetFormatterContent(serie.name, serieData.name, value, total); } serieData.SetLabelActive(value != 0 && serieData.labelPosition != Vector3.zero); - serieData.SetLabelPosition(serie.label.offset); + var down = serie.type == SerieType.Line && SerieHelper.IsDownPoint(serie, j); + serieData.SetLabelPosition(down ? -serie.label.offset : serie.label.offset); if (serieData.SetLabelText(content)) RefreshChart(); } else diff --git a/version.json b/version.json index 33e1a940..642b3bd8 100644 --- a/version.json +++ b/version.json @@ -1,7 +1,7 @@ { "version": "1.2.0", - "date": "20200115", - "checkdate": "20200115", + "date": "20200311", + "checkdate": "20200311", "desc": "如果 XCharts 对您有帮助,希望您能在 Github 上点 Star 支持,非常感谢!", "homepage": "https://github.com/monitor1394/unity-ugui-XCharts" } \ No newline at end of file