优化Background组件的生效条件,需要有单独的父节点

This commit is contained in:
monitor1394
2020-05-31 07:43:29 +08:00
parent 73919ab32a
commit 2de64580ec
12 changed files with 96 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
# 更新日志 # 更新日志
* (2020.05.31) 优化`Background`组件的生效条件,需要有单独的父节点(升级前需要自己处理旧的背景节点)
* (2020.05.30) 优化`PieChart`支持设置`ignoreValue`不显示指定数据 * (2020.05.30) 优化`PieChart`支持设置`ignoreValue`不显示指定数据
* (2020.05.30) 修复`RadarChart``Circle`时不绘制`SplitArea`的问题 * (2020.05.30) 修复`RadarChart``Circle`时不绘制`SplitArea`的问题
* (2020.05.30) 优化`RadarChart`在设置`max``0`时可自动刷新最大值 * (2020.05.30) 优化`RadarChart`在设置`max``0`时可自动刷新最大值

View File

@@ -346,13 +346,14 @@
## `Background` ## `Background`
背景组件。 背景组件。
由于框架的局限性,背景组件`chart`受上层布局控制时不适用。因为背景组件节点和`chart`节点是同一级的。 由于框架的局限性,背景组件使用有以下两个限制:
自动布局下的一种解决方案是,可以将`chart`节点再包一层`parent` 1`chart`的父节点不能有布局控制类组件
背景组件的开启需要通过接口来开启:`BaseChart.EnableBackground(bool flag)` 2`chart`的父节点只能有当前`chart`一个子节点。
背景组件的开启需要通过接口来开启:`BaseChart.EnableBackground(bool flag)`
相关参数: 相关参数:
* `show`:是否显示启用背景组件。注意背景组件在`chart`受上层布局控制时不适用 * `show`:是否显示启用背景组件。但能否激活背景组件还要受其他条件限制
* `image`:背景图。 * `image`:背景图。
* `imageType`:背景图填充类型。 * `imageType`:背景图填充类型。
* `imageColor`背景图颜色。默认`white` * `imageColor`背景图颜色。默认`white`

View File

@@ -28,6 +28,7 @@
[QA 22如何做成预设](#如何做成预设) [QA 22如何做成预设](#如何做成预设)
[QA 23如何在图表上画点画线等自定义内容](#如何在图表上画点画线等自定义内容) [QA 23如何在图表上画点画线等自定义内容](#如何在图表上画点画线等自定义内容)
[QA 24如何实现心电图类似的数据移动效果](#如何实现心电图类似的数据移动效果) [QA 24如何实现心电图类似的数据移动效果](#如何实现心电图类似的数据移动效果)
[QA 25如何使用背景组件有什么条件限制](#如何使用背景组件?有什么条件限制)
## 如何调整坐标轴与背景的边距 ## 如何调整坐标轴与背景的边距
@@ -125,6 +126,11 @@
参考Example目录下的`Example_Dynamic.cs`。主要通过设置`maxCache`参数实现。axis和serie都设置相同的maxCache。maxCache可固定数据个数当数据超过设定时会先删除第一个在添加新数据实现数据移动效果。 参考Example目录下的`Example_Dynamic.cs`。主要通过设置`maxCache`参数实现。axis和serie都设置相同的maxCache。maxCache可固定数据个数当数据超过设定时会先删除第一个在添加新数据实现数据移动效果。
## 如何使用背景组件?有什么条件限制
设置background组件的show为true但不一定就能激活背景组件。由于框架的局限性背景组件有两个前提条件一是图表的父节点不能用布局控制因为背景组件和图表的节点关系是并行的用了布局控制背景组件的位置就无法控制。二是图表的父节点只能有图表自己一个子节点这是方便管理背景组件节点的需要要不然并行关系的原因容易错乱对不上。另外调整图表的层级关系时最好先隐藏背景组件这是会自动删除关联的背景组件节点。
[返回首页](https://github.com/monitor1394/unity-ugui-XCharts) [返回首页](https://github.com/monitor1394/unity-ugui-XCharts)
[XChartsAPI接口](XChartsAPI.md) [XChartsAPI接口](XChartsAPI.md)
[XCharts配置项手册](XCharts配置项手册.md) [XCharts配置项手册](XCharts配置项手册.md)

View File

@@ -7,6 +7,7 @@
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using System.Text;
namespace XCharts namespace XCharts
{ {
@@ -38,6 +39,7 @@ namespace XCharts
private int m_SeriesSize; private int m_SeriesSize;
private Vector2 scrollPos; private Vector2 scrollPos;
private bool m_CheckWarning = false; private bool m_CheckWarning = false;
private StringBuilder sb = new StringBuilder();
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
@@ -90,7 +92,9 @@ namespace XCharts
var m_Show = m_Background.FindPropertyRelative("m_Show"); var m_Show = m_Background.FindPropertyRelative("m_Show");
if (m_Show.boolValue && !m_Target.CanShowBackgroundComponent()) if (m_Show.boolValue && !m_Target.CanShowBackgroundComponent())
{ {
EditorGUILayout.HelpBox("can't show background component:chart is control by LayoutGroup.", MessageType.Warning); var msg = "The background component cannot be activated because chart is controlled by LayoutGroup,"
+ " or its parent have more than one child.";
EditorGUILayout.HelpBox(msg, MessageType.Error);
} }
EditorGUILayout.PropertyField(m_Title, true); EditorGUILayout.PropertyField(m_Title, true);
EditorGUILayout.PropertyField(m_Legend, true); EditorGUILayout.PropertyField(m_Legend, true);
@@ -112,7 +116,7 @@ namespace XCharts
private void CheckWarning() private void CheckWarning()
{ {
if (GUILayout.Button("Check Update ")) if (GUILayout.Button("Check XCharts Update "))
{ {
CheckVersionEditor.ShowWindow(); CheckVersionEditor.ShowWindow();
} }
@@ -129,19 +133,19 @@ namespace XCharts
m_CheckWarning = false; m_CheckWarning = false;
} }
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
EditorGUILayout.LabelField("version:" + XChartsMgr.Instance.nowVersion); sb.Length = 0;
sb.AppendFormat("version:{0}", XChartsMgr.Instance.nowVersion);
if (!string.IsNullOrEmpty(m_Target.warningInfo)) if (!string.IsNullOrEmpty(m_Target.warningInfo))
{ {
var infos = m_Target.warningInfo.Split('\n'); sb.AppendLine();
foreach (var info in infos) sb.Append(XChartsMgr.Instance.nowVersion);
{
EditorGUILayout.LabelField(info);
}
} }
else else
{ {
EditorGUILayout.LabelField("Perfect! No warning!"); sb.AppendLine();
sb.Append("Perfect! No warning!");
} }
EditorGUILayout.HelpBox(sb.ToString(), MessageType.Warning);
} }
else else
{ {

View File

@@ -413,7 +413,10 @@ namespace XCharts
if (fieldCount <= 1) if (fieldCount <= 1)
{ {
while (2 > data.arraySize) while (2 > data.arraySize)
{
data.InsertArrayElementAtIndex(data.arraySize); data.InsertArrayElementAtIndex(data.arraySize);
data.GetArrayElementAtIndex(data.arraySize - 1).floatValue = 0;
}
SerializedProperty element = data.GetArrayElementAtIndex(1); SerializedProperty element = data.GetArrayElementAtIndex(1);
if (showSelected) if (showSelected)
{ {
@@ -449,6 +452,7 @@ namespace XCharts
while (i > data.arraySize - 1) while (i > data.arraySize - 1)
{ {
data.InsertArrayElementAtIndex(data.arraySize); data.InsertArrayElementAtIndex(data.arraySize);
data.GetArrayElementAtIndex(data.arraySize - 1).floatValue = 0;
} }
drawRect.x = startX + i * xWid; drawRect.x = startX + i * xWid;
drawRect.width = dataWid + 40; drawRect.width = dataWid + 40;

View File

@@ -101,6 +101,7 @@ namespace XCharts
/// 警告信息。 /// 警告信息。
/// </summary> /// </summary>
public string warningInfo { get; protected set; } public string warningInfo { get; protected set; }
public bool isControlledByLayout { get { return m_IsControlledByLayout; } }
/// <summary> /// <summary>
/// 强制开启鼠标事件检测。 /// 强制开启鼠标事件检测。
/// </summary> /// </summary>
@@ -733,7 +734,7 @@ namespace XCharts
/// <returns></returns> /// <returns></returns>
public bool CanShowBackgroundComponent() public bool CanShowBackgroundComponent()
{ {
return !m_IsControlledByLayout; return !m_IsControlledByLayout && m_Background.runtimeActive;
} }
/// <summary> /// <summary>
@@ -744,7 +745,9 @@ namespace XCharts
{ {
if (flag && !CanShowBackgroundComponent()) if (flag && !CanShowBackgroundComponent())
{ {
Debug.LogError("can't show background component:chart is controlled by LayoutGroup."); var msg = "The background component cannot be activated because chart is controlled by LayoutGroup,"
+ " or its parent have more than one child.";
Debug.LogError(msg);
return; return;
} }
m_Background.show = flag; m_Background.show = flag;

View File

@@ -14,9 +14,9 @@ namespace XCharts
{ {
/// <summary> /// <summary>
/// 背景组件。 /// 背景组件。
/// 由于框架的局限性,背景组件在chart受上层布局控制时不适用。因为背景组件节点和chart节点是同一级的。 /// 由于框架的局限性,背景组件使用有以下两个限制:
/// 自动布局下的一种解决方案是可以将chart节点再包一层parent /// 1chart的父节点不能有布局控制类组件
/// 要处理这个问题底层框架要大改了,目前暂时不打算改 /// 2chart的父节点只能有当前chart一个子节点
/// 背景组件的开启需要通过接口来开启BaseChart.EnableBackground(bool flag) /// 背景组件的开启需要通过接口来开启BaseChart.EnableBackground(bool flag)
/// </summary> /// </summary>
[Serializable] [Serializable]
@@ -33,7 +33,7 @@ namespace XCharts
[SerializeField] private bool m_HideThemeBackgroundColor = true; [SerializeField] private bool m_HideThemeBackgroundColor = true;
/// <summary> /// <summary>
/// 是否启用背景组件。注意背景组件在chart受上层布局控制时不适用 /// 是否启用背景组件。但能否激活背景组件还要受其他条件限制
/// </summary> /// </summary>
public bool show public bool show
{ {
@@ -112,6 +112,11 @@ namespace XCharts
set { if (PropertyUtility.SetStruct(ref m_HideThemeBackgroundColor, value)) SetVerticesDirty(); } set { if (PropertyUtility.SetStruct(ref m_HideThemeBackgroundColor, value)) SetVerticesDirty(); }
} }
/// <summary>
/// 是否已激活
/// </summary>
public bool runtimeActive{get;internal set;}
public static Background defaultBackground public static Background defaultBackground
{ {
get get

View File

@@ -109,6 +109,14 @@ namespace XCharts
sb.AppendFormat("warning:serie {0} itemStyle->opacity is 0\n", serie.index); sb.AppendFormat("warning:serie {0} itemStyle->opacity is 0\n", serie.index);
if (serie.itemStyle.borderWidth != 0 && IsColorAlphaZero(serie.itemStyle.borderColor)) if (serie.itemStyle.borderWidth != 0 && IsColorAlphaZero(serie.itemStyle.borderColor))
sb.AppendFormat("warning:serie {0} itemStyle->borderColor alpha is 0\n", serie.index); sb.AppendFormat("warning:serie {0} itemStyle->borderColor alpha is 0\n", serie.index);
if (serie.dataCount > 0)
{
var dataCount = serie.GetSerieData(0).data.Count;
if (serie.showDataDimension != dataCount)
{
sb.AppendFormat("warning:serie {0} serieData.data.count[{1}] not match showDataDimension[{2}]\n", serie.index, dataCount, serie.showDataDimension);
}
}
switch (serie.type) switch (serie.type)
{ {
case SerieType.Line: case SerieType.Line:
@@ -132,7 +140,6 @@ namespace XCharts
if (serie.symbol.type == SerieSymbolType.None) if (serie.symbol.type == SerieSymbolType.None)
sb.AppendFormat("warning:symbol type is None"); sb.AppendFormat("warning:symbol type is None");
break; break;
} }
} }
} }

View File

@@ -258,15 +258,35 @@ namespace XCharts
private void InitBackground() private void InitBackground()
{ {
if (!m_Background.show || m_IsControlledByLayout) int childCount = transform.parent.childCount;
if (childCount > 2) m_Background.runtimeActive = false;
else if (childCount == 1) m_Background.runtimeActive = true;
else
{ {
if (m_BackgroundRoot) m_Background.runtimeActive = false;
for (int i = 0; i < childCount; i++)
{ {
m_BackgroundRoot.SetActive(false); if (transform.parent.GetChild(i).name.StartsWith(s_BackgroundObjectName))
{
m_Background.runtimeActive = true;
break;
}
} }
}
if (!m_Background.runtimeActive || m_IsControlledByLayout)
{
//find old gameobject and delete
var objName = s_BackgroundObjectName + GetInstanceID();
ChartHelper.DestoryGameObject(transform.parent, objName);
ChartHelper.DestoryGameObject(m_BackgroundRoot);
return; return;
} }
var backgroundName = s_BackgroundObjectName + GetInstanceID(); if (!m_Background.show)
{
ChartHelper.DestoryGameObject(m_BackgroundRoot);
return;
}
var backgroundName = s_BackgroundObjectName;
m_BackgroundRoot = ChartHelper.AddObject(backgroundName, transform.parent, m_ChartMinAnchor, m_BackgroundRoot = ChartHelper.AddObject(backgroundName, transform.parent, m_ChartMinAnchor,
m_ChartMaxAnchor, m_ChartPivot, m_ChartSizeDelta); m_ChartMaxAnchor, m_ChartPivot, m_ChartSizeDelta);
m_BackgroundRoot.hideFlags = chartHideFlags; m_BackgroundRoot.hideFlags = chartHideFlags;
@@ -777,14 +797,14 @@ namespace XCharts
Vector3 p2 = new Vector3(chartX + chartWidth, chartY + chartHeight); Vector3 p2 = new Vector3(chartX + chartWidth, chartY + chartHeight);
Vector3 p3 = new Vector3(chartX + chartWidth, chartY); Vector3 p3 = new Vector3(chartX + chartWidth, chartY);
Vector3 p4 = new Vector3(chartX, chartY); Vector3 p4 = new Vector3(chartX, chartY);
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout); var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background);
ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor); ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
} }
public void DrawSymbol(VertexHelper vh, SerieSymbolType type, float symbolSize, public void DrawSymbol(VertexHelper vh, SerieSymbolType type, float symbolSize,
float tickness, Vector3 pos, Color color, Color toColor, float gap, float[] cornerRadius) float tickness, Vector3 pos, Color color, Color toColor, float gap, float[] cornerRadius)
{ {
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout); var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background);
var smoothness = m_Settings.cicleSmoothness; var smoothness = m_Settings.cicleSmoothness;
ChartDrawer.DrawSymbol(vh, type, symbolSize, tickness, pos, color, toColor, gap, ChartDrawer.DrawSymbol(vh, type, symbolSize, tickness, pos, color, toColor, gap,
cornerRadius, backgroundColor, smoothness); cornerRadius, backgroundColor, smoothness);

View File

@@ -156,9 +156,8 @@ namespace XCharts
var cp2 = new Vector3(m_CoordinateX - yLineDiff, cpty); var cp2 = new Vector3(m_CoordinateX - yLineDiff, cpty);
var cp3 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, cpty); var cp3 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, cpty);
var cp4 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_CoordinateY - xLineDiff); var cp4 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_CoordinateY - xLineDiff);
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout); var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background);
ChartDrawer.DrawPolygon(vh, cp1, cp2, cp3, cp4, backgroundColor); ChartDrawer.DrawPolygon(vh, cp1, cp2, cp3, cp4, backgroundColor);
} }
else else
{ {
@@ -173,7 +172,7 @@ namespace XCharts
var yLineDiff = yAxis0.axisLine.width; var yLineDiff = yAxis0.axisLine.width;
var xSplitDiff = xAxis0.splitLine.lineStyle.width; var xSplitDiff = xAxis0.splitLine.lineStyle.width;
var ySplitDiff = yAxis0.splitLine.lineStyle.width; var ySplitDiff = yAxis0.splitLine.lineStyle.width;
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout); var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background);
var lp1 = new Vector3(m_ChartX, m_ChartY); var lp1 = new Vector3(m_ChartX, m_ChartY);
var lp2 = new Vector3(m_ChartX, m_ChartY + chartHeight); var lp2 = new Vector3(m_ChartX, m_ChartY + chartHeight);
var lp3 = new Vector3(m_CoordinateX - yLineDiff, m_ChartY + chartHeight); var lp3 = new Vector3(m_CoordinateX - yLineDiff, m_ChartY + chartHeight);

View File

@@ -11,9 +11,9 @@ namespace XCharts
{ {
internal static class ThemeHelper internal static class ThemeHelper
{ {
public static Color GetBackgroundColor(ThemeInfo themeInfo, Background background, bool m_IsControlledByLayout) public static Color GetBackgroundColor(ThemeInfo themeInfo, Background background)
{ {
if (!m_IsControlledByLayout && background.show && background.hideThemeBackgroundColor) return Color.clear; if (background.show && background.runtimeActive && background.hideThemeBackgroundColor) return Color.clear;
else return themeInfo.backgroundColor; else return themeInfo.backgroundColor;
} }
} }

View File

@@ -112,6 +112,21 @@ namespace XCharts
} }
} }
public static void DestoryGameObject(Transform parent, string childName)
{
if (parent == null) return;
var go = parent.Find(childName);
if (go != null)
{
GameObject.DestroyImmediate(go.gameObject);
}
}
public static void DestoryGameObject(GameObject go)
{
if (go != null) GameObject.DestroyImmediate(go);
}
public static string GetFullName(Transform transform) public static string GetFullName(Transform transform)
{ {
string name = transform.name; string name = transform.name;