优化LineChartlabel偏移显示

This commit is contained in:
monitor1394
2020-03-11 21:53:45 +08:00
parent 4606d65a5c
commit 702b2a8c24
6 changed files with 67 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
# 更新日志 # 更新日志
* (2020.03.11) 优化`LineChart``label`偏移显示
* (2020.03.11) 优化清空并重新添加数据后的自动刷新问题 * (2020.03.11) 优化清空并重新添加数据后的自动刷新问题
* (2020.03.10) 增加`LineChart`的普通折线图可通过`ignore`参数设置忽略数据的支持 * (2020.03.10) 增加`LineChart`的普通折线图可通过`ignore`参数设置忽略数据的支持
* (2020.03.09) 增加`BarChart`可通过`ItemStyle`配置边框的支持 * (2020.03.09) 增加`BarChart`可通过`ItemStyle`配置边框的支持

View File

@@ -417,23 +417,17 @@ namespace XCharts
} }
if (showDetail) if (showDetail)
{ {
EditorGUI.indentLevel++; EditorGUI.indentLevel += 2;
var m_Icon = serieData.FindPropertyRelative("m_IconStyle"); var m_Icon = serieData.FindPropertyRelative("m_IconStyle");
var m_EnableLabel = serieData.FindPropertyRelative("m_EnableLabel"); var m_EnableLabel = serieData.FindPropertyRelative("m_EnableLabel");
var m_Label = serieData.FindPropertyRelative("m_Label"); var m_Label = serieData.FindPropertyRelative("m_Label");
EditorGUI.PropertyField(drawRect, m_Icon); EditorGUI.PropertyField(drawRect, m_Icon);
drawRect.y += EditorGUI.GetPropertyHeight(m_Icon); drawRect.y += EditorGUI.GetPropertyHeight(m_Icon);
EditorGUI.PropertyField(drawRect, m_EnableLabel); EditorGUI.PropertyField(drawRect, m_Label);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; ChartEditorHelper.MakeBool(ref drawRect, m_EnableLabel, 1, "(enable)");
if (m_EnableLabel.boolValue) drawRect.y += EditorGUI.GetPropertyHeight(m_Label);
{
EditorGUI.indentLevel++;
EditorGUI.PropertyField(drawRect, m_Label);
drawRect.y += EditorGUI.GetPropertyHeight(m_Label);
EditorGUI.indentLevel--;
}
EditorGUI.indentLevel--; EditorGUI.indentLevel -= 2;
} }
} }
@@ -537,10 +531,9 @@ namespace XCharts
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{ {
var item = m_Data.GetArrayElementAtIndex(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")); 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"));
} }
} }
} }

View File

@@ -79,17 +79,32 @@ public class ChartEditorHelper
float defaultX = drawRect.x; float defaultX = drawRect.x;
drawRect.width = EditorGUIUtility.labelWidth; drawRect.width = EditorGUIUtility.labelWidth;
moduleToggle = EditorGUI.Foldout(drawRect, moduleToggle, content, bold ? foldoutStyle : EditorStyles.foldout); moduleToggle = EditorGUI.Foldout(drawRect, moduleToggle, content, bold ? foldoutStyle : EditorStyles.foldout);
drawRect.x = EditorGUIUtility.labelWidth - (EditorGUI.indentLevel - 1) * 15 - 2; MakeBool(ref drawRect, prop);
drawRect.width = 40;
if (prop != null)
{
EditorGUI.PropertyField(drawRect, prop, GUIContent.none);
}
drawRect.width = defaultWidth; drawRect.width = defaultWidth;
drawRect.x = defaultX; drawRect.x = defaultX;
return moduleToggle; 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<string, bool> moduleToggle, SerializedProperty prop, public static bool MakeFoldout(ref Rect drawRect, ref Dictionary<string, bool> moduleToggle, SerializedProperty prop,
string moduleName, SerializedProperty showProp = null, bool bold = true) string moduleName, SerializedProperty showProp = null, bool bold = true)
{ {
@@ -114,7 +129,7 @@ public class ChartEditorHelper
{ {
if (showProp.propertyType == SerializedPropertyType.Boolean) if (showProp.propertyType == SerializedPropertyType.Boolean)
{ {
drawRect.width = 80; drawRect.width = 100;
} }
else else
{ {

View File

@@ -46,5 +46,31 @@ namespace XCharts
return color; 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;
}
}
} }
} }

View File

@@ -411,7 +411,13 @@ namespace XCharts
sb.Length = 0; sb.Length = 0;
if (!isCartesian) 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++) for (int i = 0; i < m_Series.Count; i++)
{ {
@@ -1498,7 +1504,6 @@ namespace XCharts
if (j >= serie.dataPoints.Count) break; if (j >= serie.dataPoints.Count) break;
var serieData = serie.data[j]; var serieData = serie.data[j];
var pos = serie.dataPoints[j]; var pos = serie.dataPoints[j];
serieData.SetGameObjectPosition(serieData.labelPosition); serieData.SetGameObjectPosition(serieData.labelPosition);
serieData.UpdateIcon(); serieData.UpdateIcon();
if (serie.show && serie.label.show && serieData.canShowLabel) if (serie.show && serie.label.show && serieData.canShowLabel)
@@ -1522,7 +1527,8 @@ namespace XCharts
content = serie.label.GetFormatterContent(serie.name, serieData.name, value, total); content = serie.label.GetFormatterContent(serie.name, serieData.name, value, total);
} }
serieData.SetLabelActive(value != 0 && serieData.labelPosition != Vector3.zero); 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(); if (serieData.SetLabelText(content)) RefreshChart();
} }
else else

View File

@@ -1,7 +1,7 @@
{ {
"version": "1.2.0", "version": "1.2.0",
"date": "20200115", "date": "20200311",
"checkdate": "20200115", "checkdate": "20200311",
"desc": "如果 XCharts 对您有帮助,希望您能在 Github 上点 Star 支持,非常感谢!", "desc": "如果 XCharts 对您有帮助,希望您能在 Github 上点 Star 支持,非常感谢!",
"homepage": "https://github.com/monitor1394/unity-ugui-XCharts" "homepage": "https://github.com/monitor1394/unity-ugui-XCharts"
} }