diff --git a/CHANGELOG.md b/CHANGELOG.md index 088a0726..d05e842b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.04.11) 增加`Check warning`检测功能 * (2020.04.09) 修复`Legend`初始化异常的问题 * (2020.04.08) 增加`PieChart`通过`ItemStyle`设置边框的支持 * (2020.03.29) 增加`Axis`的`ceilRate`设置最大最小值的取整倍率 diff --git a/Editor/BaseChartEditor.cs b/Editor/BaseChartEditor.cs index 81702b22..674198f2 100644 --- a/Editor/BaseChartEditor.cs +++ b/Editor/BaseChartEditor.cs @@ -6,6 +6,7 @@ /******************************************/ using UnityEditor; +using UnityEngine; namespace XCharts { @@ -32,6 +33,8 @@ namespace XCharts protected float m_DefaultLabelWidth; protected float m_DefaultFieldWidth; private int m_SeriesSize; + private Vector2 scrollPos; + private bool m_CheckWarning = false; protected virtual void OnEnable() { @@ -65,6 +68,7 @@ namespace XCharts OnMiddleInspectorGUI(); OnEndInspectorGUI(); + CheckWarning(); serializedObject.ApplyModifiedProperties(); } @@ -88,5 +92,50 @@ namespace XCharts protected virtual void OnEndInspectorGUI() { } + + + private void CheckWarning() + { + EditorGUILayout.Space(); + EditorGUILayout.Space(); + if (m_CheckWarning) + { + EditorGUILayout.BeginHorizontal(); + if (GUILayout.Button("Check warning")) + { + m_CheckWarning = true; + m_Target.CheckWarning(); + } + if (GUILayout.Button("Hide warning")) + { + m_CheckWarning = false; + } + EditorGUILayout.EndHorizontal(); + var version = string.Format("version:{0}_{1}\n", XChartsMgr.version, XChartsMgr.date); + EditorGUILayout.LabelField(version); + if (!string.IsNullOrEmpty(m_Target.warningInfo)) + { + var infos = m_Target.warningInfo.Split('\n'); + foreach (var info in infos) + { + EditorGUILayout.LabelField(info); + } + } + else + { + EditorGUILayout.LabelField("Perfect! No warning!"); + } + } + else + { + if (GUILayout.Button("Check warning")) + { + m_CheckWarning = true; + m_Target.CheckWarning(); + } + } + EditorGUILayout.Space(); + EditorGUILayout.Space(); + } } } \ No newline at end of file diff --git a/Runtime/API/BaseChart_API.cs b/Runtime/API/BaseChart_API.cs index 364ea749..1efcf1ba 100644 --- a/Runtime/API/BaseChart_API.cs +++ b/Runtime/API/BaseChart_API.cs @@ -67,6 +67,10 @@ namespace XCharts /// public Action customDrawCallback { set { m_CustomDrawCallback = value; } } /// + /// 警告信息。 + /// + public string warningInfo { get; protected set; } + /// /// Set the size of chart. /// 设置图表的大小。 /// @@ -637,5 +641,16 @@ namespace XCharts return np; } } + + /// + /// 检测警告信息。 + /// + /// + public string CheckWarning() + { + warningInfo = CheckHelper.CheckChart(this); + Debug.LogError(warningInfo); + return warningInfo; + } } } diff --git a/Runtime/Component/Main/Series.cs b/Runtime/Component/Main/Series.cs index c3ba17bf..fe44fb17 100644 --- a/Runtime/Component/Main/Series.cs +++ b/Runtime/Component/Main/Series.cs @@ -1053,6 +1053,33 @@ namespace XCharts return serieNameList; } + internal List GetLegalSerieNameList() + { + var list = new List(); + for (int n = 0; n < m_Series.Count; n++) + { + var serie = m_Series[n]; + switch (serie.type) + { + case SerieType.Pie: + case SerieType.Radar: + case SerieType.Ring: + for (int i = 0; i < serie.data.Count; i++) + { + var dataName = serie.data[i].name; + if (!string.IsNullOrEmpty(dataName) && IsLegalLegendName(dataName) && !list.Contains(dataName)) + list.Add(dataName); + } + break; + default: + if (!string.IsNullOrEmpty(serie.name) && !list.Contains(serie.name) && IsLegalLegendName(serie.name)) + list.Add(serie.name); + break; + } + } + return list; + } + /// /// 设置获得标志图形大小的回调 /// diff --git a/Runtime/Component/Main/Theme.cs b/Runtime/Component/Main/Theme.cs index 3dbf130f..68233dc1 100644 --- a/Runtime/Component/Main/Theme.cs +++ b/Runtime/Component/Main/Theme.cs @@ -1,4 +1,5 @@ -/******************************************/ +using System.Text; +/******************************************/ /* */ /* Copyright (c) 2018 monitor1394 */ /* https://github.com/monitor1394 */ @@ -292,6 +293,28 @@ namespace XCharts else return Color.clear; } + public void CheckWarning(StringBuilder sb) + { + if (m_Font == null && m_CustomFont == null) + { + sb.AppendFormat("warning:theme->font is null"); + } + if (m_ColorPalette.Length == 0 && m_CustomColorPalette.Count == 0) + { + sb.AppendFormat("warning:theme->colorPalette is empty"); + } + for (int i = 0; i < m_ColorPalette.Length; i++) + { + if (m_ColorPalette[i] != Color.clear && m_ColorPalette[i].a == 0) + sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i); + } + for (int i = 0; i < m_CustomColorPalette.Count; i++) + { + if (m_CustomColorPalette[i] != Color.clear && m_CustomColorPalette[i].a == 0) + sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i); + } + } + Dictionary _colorDic = new Dictionary(); /// /// Gets the hexadecimal color string of the specified index from the palette. diff --git a/Runtime/Helper/CheckHelper.cs b/Runtime/Helper/CheckHelper.cs new file mode 100644 index 00000000..b98da654 --- /dev/null +++ b/Runtime/Helper/CheckHelper.cs @@ -0,0 +1,133 @@ +using System.Linq; +/******************************************/ +/* */ +/* Copyright (c) 2018 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ +using System.Text; +using UnityEngine; +using UnityEngine.UI; + +namespace XCharts +{ + internal static class CheckHelper + { + private static bool IsColorAlphaZero(Color color) + { + return color != Color.clear && color.a == 0; + } + public static string CheckChart(BaseChart chart) + { + var sb = ChartHelper.sb; + sb.Length = 0; + //sb.AppendFormat("version:{0}_{1}\n", XChartsMgr.version, XChartsMgr.date); + CheckSize(chart, sb); + CheckTheme(chart, sb); + CheckTitle(chart, sb); + CheckLegend(chart, sb); + CheckGrid(chart, sb); + CheckSerie(chart, sb); + return sb.ToString(); + } + + private static void CheckSize(BaseChart chart, StringBuilder sb) + { + if (chart.chartWidth == 0 || chart.chartHeight == 0) + { + sb.Append("warning:chart width or height is 0\n"); + } + } + + private static void CheckTheme(BaseChart chart, StringBuilder sb) + { + var themeInfo = chart.themeInfo; + themeInfo.CheckWarning(sb); + } + + private static void CheckTitle(BaseChart chart, StringBuilder sb) + { + var title = chart.title; + if (!title.show) return; + if (string.IsNullOrEmpty(title.text)) sb.Append("warning:title->text is null\n"); + if (title.textStyle.color != Color.clear && title.textStyle.color.a == 0) + sb.Append("warning:title->textStyle->color alpha is 0\n"); + if (title.subTextStyle.color != Color.clear && title.subTextStyle.color.a == 0) + sb.Append("warning:title->subTextStyle->color alpha is 0\n"); + } + + private static void CheckLegend(BaseChart chart, StringBuilder sb) + { + var legend = chart.legend; + if (!legend.show) return; + if (legend.textStyle.color != Color.clear && legend.textStyle.color.a == 0) + sb.Append("warning:legend->textStyle->color alpha is 0\n"); + var serieNameList = chart.series.GetLegalSerieNameList(); + Debug.LogError("namelist:" + serieNameList.Count); + if (serieNameList.Count == 0) sb.Append("warning:legend need serie.name or serieData.name not empty\n"); + foreach (var category in legend.data) + { + if (!serieNameList.Contains(category)) + sb.AppendFormat("warning:legend [{0}] is invalid, must be one of serie.name or serieData.name\n", category); + } + } + + private static void CheckGrid(BaseChart chart, StringBuilder sb) + { + if (chart is CoordinateChart) + { + var grid = (chart as CoordinateChart).grid; + if (grid.left >= chart.chartWidth) + sb.Append("warning:grid->left > chartWidth\n"); + if (grid.right >= chart.chartWidth) + sb.Append("warning:grid->right > chartWidth\n"); + if (grid.top >= chart.chartHeight) + sb.Append("warning:grid->top > chartHeight\n"); + if (grid.bottom >= chart.chartHeight) + sb.Append("warning:grid->bottom > chartHeight\n"); + if (grid.left + grid.right >= chart.chartWidth) + sb.Append("warning:grid.left + grid.right > chartWidth\n"); + if (grid.top + grid.bottom >= chart.chartHeight) + sb.Append("warning:grid.top + grid.bottom > chartHeight\n"); + } + } + + private static void CheckSerie(BaseChart chart, StringBuilder sb) + { + foreach (var serie in chart.series.list) + { + if (IsColorAlphaZero(serie.itemStyle.color)) + sb.AppendFormat("warning:serie {0} itemStyle->color alpha is 0\n", serie.index); + if (serie.itemStyle.opacity == 0) + sb.AppendFormat("warning:serie {0} itemStyle->opacity is 0\n", serie.index); + if (serie.itemStyle.borderWidth != 0 && IsColorAlphaZero(serie.itemStyle.borderColor)) + sb.AppendFormat("warning:serie {0} itemStyle->borderColor alpha is 0\n", serie.index); + switch (serie.type) + { + case SerieType.Line: + if (serie.lineStyle.width == 0) + sb.AppendFormat("warning:serie {0} lineStyle->width is 0\n", serie.index); + if (serie.lineStyle.opacity == 0) + sb.AppendFormat("warning:serie {0} lineStyle->opacity is 0\n", serie.index); + if (serie.lineStyle.color != Color.clear && serie.lineStyle.color.a == 0) + sb.AppendFormat("warning:serie {0} lineStyle->color alpha is 0\n", serie.index); + break; + case SerieType.Bar: + if (serie.barWidth == 0) + sb.AppendFormat("warning:serie {0} barWidth is 0\n", serie.index); + break; + case SerieType.Pie: + if (serie.radius.Length >= 2 && serie.radius[1] == 0) + sb.AppendFormat("warning:serie {0} radius[1] is 0\n", serie.index); + break; + case SerieType.Scatter: + case SerieType.EffectScatter: + if (serie.symbol.type == SerieSymbolType.None) + sb.AppendFormat("warning:symbol type is None"); + break; + + } + } + } + } +} \ No newline at end of file diff --git a/Runtime/Helper/CheckHelper.cs.meta b/Runtime/Helper/CheckHelper.cs.meta new file mode 100644 index 00000000..56e1a2f0 --- /dev/null +++ b/Runtime/Helper/CheckHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 09a50ff0a7fdb4174b4dc2d28fc08b6a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Internal/BaseChart.cs b/Runtime/Internal/BaseChart.cs index b6097bec..8d859338 100644 --- a/Runtime/Internal/BaseChart.cs +++ b/Runtime/Internal/BaseChart.cs @@ -49,6 +49,7 @@ namespace XCharts [SerializeField] protected Settings m_Settings = new Settings(); [SerializeField] protected float m_Large = 1; [SerializeField] protected Action m_CustomDrawCallback; + [SerializeField] protected string m_DebugInfo = ""; [NonSerialized] private Theme m_CheckTheme = 0; [NonSerialized] private float m_CheckWidth = 0;