From 3a41f1e00e2903e36454a7d38236df7dcb510a4c Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Tue, 28 Dec 2021 08:18:24 +0800 Subject: [PATCH] 3.0 - serie extra component --- Assets/XCharts/Editor/Series/BarEditor.cs | 5 - .../Editor/Series/CandlestickEditor.cs | 4 - .../Editor/Series/EffectScatterEditor.cs | 4 - Assets/XCharts/Editor/Series/HeatmapEditor.cs | 4 - Assets/XCharts/Editor/Series/LineEditor.cs | 7 - Assets/XCharts/Editor/Series/LiquidEditor.cs | 4 - Assets/XCharts/Editor/Series/PieEditor.cs | 4 - Assets/XCharts/Editor/Series/RadarEditor.cs | 7 +- Assets/XCharts/Editor/Series/RingEditor.cs | 6 +- Assets/XCharts/Editor/Series/ScatterEditor.cs | 6 +- .../XCharts/Editor/Series/SerieBaseEditor.cs | 2 +- Assets/XCharts/Editor/Series/SerieEditor.cs | 11 ++ .../XCharts/Editor/Series/SerieListEditor.cs | 27 +++- .../Examples/Runtime/Example10_LineChart.cs | 3 + .../Examples/Runtime/Example20_BarChart.cs | 1 + .../Examples/Runtime/Example30_PieChart.cs | 1 + .../Runtime/Example31_PieUpdateName.cs | 1 + .../Examples/Runtime/Example60_Heatmap.cs | 1 + .../Runtime/Component/Child/AreaStyle.cs | 21 +-- .../Runtime/Component/Child/Emphasis.cs | 2 +- .../Runtime/Component/Child/EndLabelStyle.cs | 11 ++ .../Component/Child/EndLabelStyle.cs.meta | 11 ++ .../Runtime/Component/Child/IconStyle.cs | 2 +- .../Runtime/Component/Child/LabelLine.cs | 2 +- .../Runtime/Component/Child/LabelStyle.cs | 6 +- .../Runtime/Component/Child/LineArrow.cs | 2 +- Assets/XCharts/Runtime/Helper/SerieHelper.cs | 21 ++- .../Runtime/Helper/SerieLabelHelper.cs | 16 +- Assets/XCharts/Runtime/Helper/SeriesHelper.cs | 2 +- .../SerieExtraComponentAttribute.cs | 74 ++++++++++ .../SerieExtraComponentAttribute.cs.meta | 11 ++ .../Runtime/Internal/BaseChart.Component.cs | 8 +- .../Internal/Misc/ISerieExtraComponent.cs | 12 ++ .../Misc/ISerieExtraComponent.cs.meta | 11 ++ .../Runtime/Internal/Object/ChartLabel.cs | 16 +- Assets/XCharts/Runtime/Serie/Bar/Bar.cs | 4 + .../Runtime/Serie/Candlestick/Candlestick.cs | 1 + Assets/XCharts/Runtime/Serie/Gauge.meta | 8 - .../XCharts/Runtime/Serie/Heatmap/Heatmap.cs | 11 +- .../Runtime/Serie/Heatmap/HeatmapHandler.cs | 17 ++- Assets/XCharts/Runtime/Serie/Line/Line.cs | 10 +- .../Serie/Line/LineHandler.GridCoord.cs | 9 +- .../XCharts/Runtime/Serie/Line/LineHelper.cs | 2 +- Assets/XCharts/Runtime/Serie/Liquid/Liquid.cs | 2 + Assets/XCharts/Runtime/Serie/Pie/Pie.cs | 1 + .../XCharts/Runtime/Serie/Pie/PieHandler.cs | 17 ++- Assets/XCharts/Runtime/Serie/Radar/Radar.cs | 1 + .../Runtime/Serie/Radar/RadarHandler.cs | 6 +- Assets/XCharts/Runtime/Serie/Ring/Ring.cs | 2 + .../XCharts/Runtime/Serie/Ring/RingHandler.cs | 13 +- .../Runtime/Serie/Scatter/EffectScatter.cs | 1 + .../XCharts/Runtime/Serie/Scatter/Scatter.cs | 1 + .../Runtime/Serie/Serie.ExtraComponent.cs | 139 ++++++++++++++++++ .../Serie/Serie.ExtraComponent.cs.meta | 11 ++ Assets/XCharts/Runtime/Serie/Serie.cs | 127 ++++++---------- Assets/XCharts/Runtime/Serie/SerieHandler.cs | 28 +++- .../Runtime/Utilities/ReflectionUtil.cs | 13 ++ 57 files changed, 505 insertions(+), 245 deletions(-) create mode 100644 Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs create mode 100644 Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs.meta create mode 100644 Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs create mode 100644 Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs.meta create mode 100644 Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs create mode 100644 Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs.meta delete mode 100644 Assets/XCharts/Runtime/Serie/Gauge.meta create mode 100644 Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs create mode 100644 Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs.meta diff --git a/Assets/XCharts/Editor/Series/BarEditor.cs b/Assets/XCharts/Editor/Series/BarEditor.cs index 524934f4..802f1ec3 100644 --- a/Assets/XCharts/Editor/Series/BarEditor.cs +++ b/Assets/XCharts/Editor/Series/BarEditor.cs @@ -39,12 +39,7 @@ namespace XCharts.Editor PropertyField("m_LargeThreshold"); PropertyField("m_Clip"); }); - PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/CandlestickEditor.cs b/Assets/XCharts/Editor/Series/CandlestickEditor.cs index 2c2f6c33..d4dde252 100644 --- a/Assets/XCharts/Editor/Series/CandlestickEditor.cs +++ b/Assets/XCharts/Editor/Series/CandlestickEditor.cs @@ -18,10 +18,6 @@ namespace XCharts.Editor PropertyField("m_LargeThreshold"); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/EffectScatterEditor.cs b/Assets/XCharts/Editor/Series/EffectScatterEditor.cs index 1a275290..8e38e34e 100644 --- a/Assets/XCharts/Editor/Series/EffectScatterEditor.cs +++ b/Assets/XCharts/Editor/Series/EffectScatterEditor.cs @@ -20,10 +20,6 @@ namespace XCharts.Editor PropertyField("m_Symbol"); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/HeatmapEditor.cs b/Assets/XCharts/Editor/Series/HeatmapEditor.cs index f7d2751d..bfc2570a 100644 --- a/Assets/XCharts/Editor/Series/HeatmapEditor.cs +++ b/Assets/XCharts/Editor/Series/HeatmapEditor.cs @@ -11,10 +11,6 @@ namespace XCharts.Editor PropertyField("m_IgnoreValue"); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/LineEditor.cs b/Assets/XCharts/Editor/Series/LineEditor.cs index bd547401..2217ad0e 100644 --- a/Assets/XCharts/Editor/Series/LineEditor.cs +++ b/Assets/XCharts/Editor/Series/LineEditor.cs @@ -36,14 +36,7 @@ namespace XCharts.Editor }); PropertyField("m_Symbol"); PropertyField("m_LineStyle"); - PropertyField("m_LineArrow"); - PropertyField("m_AreaStyle"); - PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/LiquidEditor.cs b/Assets/XCharts/Editor/Series/LiquidEditor.cs index 70b4dac3..9314d5c9 100644 --- a/Assets/XCharts/Editor/Series/LiquidEditor.cs +++ b/Assets/XCharts/Editor/Series/LiquidEditor.cs @@ -16,10 +16,6 @@ namespace XCharts.Editor PropertyField("m_WaveOffset"); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/PieEditor.cs b/Assets/XCharts/Editor/Series/PieEditor.cs index 4490dce9..2446a271 100644 --- a/Assets/XCharts/Editor/Series/PieEditor.cs +++ b/Assets/XCharts/Editor/Series/PieEditor.cs @@ -22,10 +22,6 @@ namespace XCharts.Editor }); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/RadarEditor.cs b/Assets/XCharts/Editor/Series/RadarEditor.cs index aa7bee6a..639872c4 100644 --- a/Assets/XCharts/Editor/Series/RadarEditor.cs +++ b/Assets/XCharts/Editor/Series/RadarEditor.cs @@ -9,15 +9,10 @@ namespace XCharts.Editor { PropertyField("m_RadarType"); PropertyField("m_RadarIndex"); + PropertyField("m_Symbol"); PropertyField("m_LineStyle"); - PropertyField("m_AreaStyle"); - PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/RingEditor.cs b/Assets/XCharts/Editor/Series/RingEditor.cs index 8077dddf..5f3cab3d 100644 --- a/Assets/XCharts/Editor/Series/RingEditor.cs +++ b/Assets/XCharts/Editor/Series/RingEditor.cs @@ -13,13 +13,9 @@ namespace XCharts.Editor PropertyField("m_RingGap"); PropertyField("m_RoundCap"); PropertyField("m_Clockwise"); - PropertyField("m_TitleStyle"); + PropertyField("m_TitleStyle"); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/ScatterEditor.cs b/Assets/XCharts/Editor/Series/ScatterEditor.cs index 1e6e05f6..26fb06b5 100644 --- a/Assets/XCharts/Editor/Series/ScatterEditor.cs +++ b/Assets/XCharts/Editor/Series/ScatterEditor.cs @@ -17,13 +17,9 @@ namespace XCharts.Editor PropertyField("m_YAxisIndex"); } PropertyField("m_Clip"); - PropertyField("m_Symbol"); + PropertyField("m_Symbol"); PropertyField("m_ItemStyle"); - PropertyField("m_IconStyle"); - PropertyField("m_Label"); - PropertyField("m_LabelLine"); - PropertyField("m_Emphasis"); PropertyField("m_Animation"); } } diff --git a/Assets/XCharts/Editor/Series/SerieBaseEditor.cs b/Assets/XCharts/Editor/Series/SerieBaseEditor.cs index 0a57ab28..237862d3 100644 --- a/Assets/XCharts/Editor/Series/SerieBaseEditor.cs +++ b/Assets/XCharts/Editor/Series/SerieBaseEditor.cs @@ -86,7 +86,7 @@ namespace XCharts.Editor return ObjectNames.NicifyVariableName(title); } - protected SerializedProperty FindProperty(string path) + internal SerializedProperty FindProperty(string path) { return baseProperty.FindPropertyRelative(path); } diff --git a/Assets/XCharts/Editor/Series/SerieEditor.cs b/Assets/XCharts/Editor/Series/SerieEditor.cs index 89dc436f..6b4bff76 100644 --- a/Assets/XCharts/Editor/Series/SerieEditor.cs +++ b/Assets/XCharts/Editor/Series/SerieEditor.cs @@ -29,6 +29,7 @@ namespace XCharts.Editor } } OnCustomInspectorGUI(); + OnExtraInspectorGUI(); PropertyFieldData(); --EditorGUI.indentLevel; } @@ -37,6 +38,16 @@ namespace XCharts.Editor { } + private void OnExtraInspectorGUI() + { + foreach (var kv in Serie.extraComponentFieldNameDict) + { + var prop = FindProperty(kv.Value); + if (prop.arraySize > 0) + PropertyField(prop.GetArrayElementAtIndex(0)); + } + } + private void PropertyFieldData() { m_DataFoldout = ChartEditorHelper.DrawHeader("Data", m_DataFoldout, false, null, null, diff --git a/Assets/XCharts/Editor/Series/SerieListEditor.cs b/Assets/XCharts/Editor/Series/SerieListEditor.cs index 4e1aa6e6..15a82962 100644 --- a/Assets/XCharts/Editor/Series/SerieListEditor.cs +++ b/Assets/XCharts/Editor/Series/SerieListEditor.cs @@ -6,7 +6,6 @@ using UnityEditor; using System.Linq; using System.Reflection; - namespace XCharts.Editor { public sealed class SerieListEditor @@ -151,7 +150,28 @@ namespace XCharts.Editor CovertSerie(editor.serie, type); })); } - + if (editor.serie.GetType().IsDefined(typeof(SerieExtraComponentAttribute), false)) + { + var attribute = editor.serie.GetType().GetAttribute(); + foreach (var type in attribute.types) + { + var size = editor.FindProperty(Serie.extraComponentFieldNameDict[type]).arraySize; + editor.menus.Add(new HeaderMenuInfo("Add " + type.Name, () => + { + editor.serie.AddExtraComponent(type); + RefreshEditors(); + }, size == 0)); + } + foreach (var type in attribute.types) + { + var size = editor.FindProperty(Serie.extraComponentFieldNameDict[type]).arraySize; + editor.menus.Add(new HeaderMenuInfo("Remove " + type.Name, () => + { + editor.serie.RemoveExtraComponent(type); + RefreshEditors(); + }, size > 0)); + } + } if (index < 0) m_Editors.Add(editor); else @@ -181,7 +201,8 @@ namespace XCharts.Editor RefreshEditors(); } - public void CloneSerie(Serie serie){ + public void CloneSerie(Serie serie) + { var newSerie = serie.Clone(); newSerie.serieName = chart.GenerateDefaultSerieName(); chart.InsertSerie(newSerie); diff --git a/Assets/XCharts/Examples/Runtime/Example10_LineChart.cs b/Assets/XCharts/Examples/Runtime/Example10_LineChart.cs index 1edf824e..53bf8009 100644 --- a/Assets/XCharts/Examples/Runtime/Example10_LineChart.cs +++ b/Assets/XCharts/Examples/Runtime/Example10_LineChart.cs @@ -116,6 +116,7 @@ namespace XCharts.Example { chart.GetChartComponent().subText = "AreaStyle 面积图"; + serie.AddExtraComponent<AreaStyle>(); serie.areaStyle.show = true; chart.RefreshChart(); yield return new WaitForSeconds(1f); @@ -144,6 +145,7 @@ namespace XCharts.Example IEnumerator LineArrowSettings() { chart.GetChartComponent<Title>().subText = "LineArrow 头部箭头"; + chart.GetSerie(0).AddExtraComponent<LineArrow>(); serie.lineArrow.show = true; serie.lineArrow.position = LineArrow.Position.Start; chart.RefreshChart(); @@ -205,6 +207,7 @@ namespace XCharts.Example IEnumerator LineLabelSettings() { chart.GetChartComponent<Title>().subText = "SerieLabel 文本标签"; + serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; serie.label.border = false; chart.RefreshChart(); diff --git a/Assets/XCharts/Examples/Runtime/Example20_BarChart.cs b/Assets/XCharts/Examples/Runtime/Example20_BarChart.cs index c4dc7003..cf81d124 100644 --- a/Assets/XCharts/Examples/Runtime/Example20_BarChart.cs +++ b/Assets/XCharts/Examples/Runtime/Example20_BarChart.cs @@ -140,6 +140,7 @@ namespace XCharts.Example serie.barPercentStack = true; + serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; serie.label.position = LabelStyle.Position.Center; serie.label.border = false; diff --git a/Assets/XCharts/Examples/Runtime/Example30_PieChart.cs b/Assets/XCharts/Examples/Runtime/Example30_PieChart.cs index 68d2db02..d9099dc0 100644 --- a/Assets/XCharts/Examples/Runtime/Example30_PieChart.cs +++ b/Assets/XCharts/Examples/Runtime/Example30_PieChart.cs @@ -83,6 +83,7 @@ namespace XCharts.Example { chart.GetChartComponent<Title>().subText = "显示文本标签"; + serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; chart.RefreshChart(); yield return new WaitForSeconds(1); diff --git a/Assets/XCharts/Examples/Runtime/Example31_PieUpdateName.cs b/Assets/XCharts/Examples/Runtime/Example31_PieUpdateName.cs index 54069a15..25048724 100644 --- a/Assets/XCharts/Examples/Runtime/Example31_PieUpdateName.cs +++ b/Assets/XCharts/Examples/Runtime/Example31_PieUpdateName.cs @@ -19,6 +19,7 @@ namespace XCharts.Example var serieIndex = 0; var serie = chart.GetSerie(serieIndex); if (serie == null) return; + serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; serie.label.position = LabelStyle.Position.Outside; } diff --git a/Assets/XCharts/Examples/Runtime/Example60_Heatmap.cs b/Assets/XCharts/Examples/Runtime/Example60_Heatmap.cs index 6775531d..648e5585 100644 --- a/Assets/XCharts/Examples/Runtime/Example60_Heatmap.cs +++ b/Assets/XCharts/Examples/Runtime/Example60_Heatmap.cs @@ -47,6 +47,7 @@ namespace XCharts.Example serie.itemStyle.borderColor = Color.clear; //设置高亮样式 + serie.AddExtraComponent<Emphasis>(); serie.emphasis.show = true; serie.emphasis.itemStyle.show = true; serie.emphasis.itemStyle.borderWidth = 1; diff --git a/Assets/XCharts/Runtime/Component/Child/AreaStyle.cs b/Assets/XCharts/Runtime/Component/Child/AreaStyle.cs index 42f957da..9323dec1 100644 --- a/Assets/XCharts/Runtime/Component/Child/AreaStyle.cs +++ b/Assets/XCharts/Runtime/Component/Child/AreaStyle.cs @@ -9,7 +9,7 @@ namespace XCharts /// 区域填充样式。 /// </summary> [System.Serializable] - public class AreaStyle : ChildComponent + public class AreaStyle : ChildComponent, ISerieExtraComponent { /// <summary> /// Origin position of area. @@ -33,11 +33,11 @@ namespace XCharts /// </summary> End } - [SerializeField] private bool m_Show; + [SerializeField] private bool m_Show = true; [SerializeField] private AreaOrigin m_Origin; [SerializeField] private Color32 m_Color; [SerializeField] private Color32 m_ToColor; - [SerializeField] [Range(0, 1)] private float m_Opacity; + [SerializeField] [Range(0, 1)] private float m_Opacity = 0.6f; [SerializeField] private bool m_TooltipHighlight; [SerializeField] private Color32 m_HighlightColor; [SerializeField] private Color32 m_HighlightToColor; @@ -114,21 +114,6 @@ namespace XCharts set { if (PropertyUtil.SetColor(ref m_HighlightToColor, value)) SetVerticesDirty(); } } - public static AreaStyle defaultAreaStyle - { - get - { - var area = new AreaStyle - { - m_Show = false, - m_Color = Color.clear, - m_ToColor = Color.clear, - m_Opacity = 0.6f - }; - return area; - } - } - public Color32 GetColor() { if (m_Opacity == 1) diff --git a/Assets/XCharts/Runtime/Component/Child/Emphasis.cs b/Assets/XCharts/Runtime/Component/Child/Emphasis.cs index 8d0151f9..b8115c62 100644 --- a/Assets/XCharts/Runtime/Component/Child/Emphasis.cs +++ b/Assets/XCharts/Runtime/Component/Child/Emphasis.cs @@ -8,7 +8,7 @@ namespace XCharts /// 高亮的图形样式和文本标签样式。 /// </summary> [System.Serializable] - public class Emphasis : ChildComponent, ISerieDataComponent + public class Emphasis : ChildComponent, ISerieExtraComponent, ISerieDataComponent { [SerializeField] private bool m_Show; [SerializeField] private LabelStyle m_Label = new LabelStyle(); diff --git a/Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs b/Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs new file mode 100644 index 00000000..7e562dca --- /dev/null +++ b/Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs @@ -0,0 +1,11 @@ + +using System; +using UnityEngine; + +namespace XCharts +{ + [System.Serializable] + public class EndLabelStyle : LabelStyle + { + } +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs.meta b/Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs.meta new file mode 100644 index 00000000..deca912f --- /dev/null +++ b/Assets/XCharts/Runtime/Component/Child/EndLabelStyle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b3ca55f3ab0314339ae171c8ac07c4e2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XCharts/Runtime/Component/Child/IconStyle.cs b/Assets/XCharts/Runtime/Component/Child/IconStyle.cs index c8141bbc..f97c2358 100644 --- a/Assets/XCharts/Runtime/Component/Child/IconStyle.cs +++ b/Assets/XCharts/Runtime/Component/Child/IconStyle.cs @@ -5,7 +5,7 @@ using UnityEngine.UI; namespace XCharts { [System.Serializable] - public class IconStyle : ChildComponent, ISerieDataComponent + public class IconStyle : ChildComponent, ISerieExtraComponent, ISerieDataComponent { public enum Layer { diff --git a/Assets/XCharts/Runtime/Component/Child/LabelLine.cs b/Assets/XCharts/Runtime/Component/Child/LabelLine.cs index 45b7999e..0856432e 100644 --- a/Assets/XCharts/Runtime/Component/Child/LabelLine.cs +++ b/Assets/XCharts/Runtime/Component/Child/LabelLine.cs @@ -5,7 +5,7 @@ using UnityEngine; namespace XCharts { [System.Serializable] - public class LabelLine : ChildComponent,ISerieDataComponent + public class LabelLine : ChildComponent, ISerieExtraComponent, ISerieDataComponent { /// <summary> /// 标签视觉引导线类型 diff --git a/Assets/XCharts/Runtime/Component/Child/LabelStyle.cs b/Assets/XCharts/Runtime/Component/Child/LabelStyle.cs index 963fd597..51761a8b 100644 --- a/Assets/XCharts/Runtime/Component/Child/LabelStyle.cs +++ b/Assets/XCharts/Runtime/Component/Child/LabelStyle.cs @@ -9,7 +9,7 @@ namespace XCharts /// 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。 /// </summary> [System.Serializable] - public class LabelStyle : ChildComponent,ISerieDataComponent + public class LabelStyle : ChildComponent, ISerieExtraComponent, ISerieDataComponent { /// <summary> /// The position of label. @@ -69,7 +69,7 @@ namespace XCharts End } - + [SerializeField] private bool m_Show = false; [SerializeField] Position m_Position = Position.Outside; [SerializeField] private Vector3 m_Offset; @@ -299,4 +299,6 @@ namespace XCharts } } } + + } diff --git a/Assets/XCharts/Runtime/Component/Child/LineArrow.cs b/Assets/XCharts/Runtime/Component/Child/LineArrow.cs index 1ed4176b..22c18f73 100644 --- a/Assets/XCharts/Runtime/Component/Child/LineArrow.cs +++ b/Assets/XCharts/Runtime/Component/Child/LineArrow.cs @@ -7,7 +7,7 @@ namespace XCharts /// <summary> /// </summary> [Serializable] - public class LineArrow : ChildComponent + public class LineArrow : ChildComponent, ISerieExtraComponent { public enum Position { diff --git a/Assets/XCharts/Runtime/Helper/SerieHelper.cs b/Assets/XCharts/Runtime/Helper/SerieHelper.cs index 067503a8..c4a4d80b 100644 --- a/Assets/XCharts/Runtime/Helper/SerieHelper.cs +++ b/Assets/XCharts/Runtime/Helper/SerieHelper.cs @@ -407,7 +407,7 @@ namespace XCharts { if (!serie.IsPerformanceMode() && serieData != null && serieData.emphasis != null && serieData.emphasis.show) return serieData.emphasis.itemStyle; - else if (serie.emphasis.show) return serie.emphasis.itemStyle; + else if (serie.emphasis != null && serie.emphasis.show) return serie.emphasis.itemStyle; else return null; } @@ -417,7 +417,7 @@ namespace XCharts { if (!serie.IsPerformanceMode() && serieData.emphasis != null && serieData.emphasis.show) return serieData.emphasis.label; - else if (serie.emphasis.show) return serie.emphasis.label; + else if (serie.emphasis != null && serie.emphasis.show) return serie.emphasis.label; else return serie.label; } else @@ -431,7 +431,7 @@ namespace XCharts { if (!serie.IsPerformanceMode() && serieData.emphasis != null && serieData.emphasis.show) return serieData.emphasis.label; - else if (serie.emphasis.show) return serie.emphasis.label; + else if (serie.emphasis != null && serie.emphasis.show) return serie.emphasis.label; else return null; } @@ -441,7 +441,7 @@ namespace XCharts { if (!serie.IsPerformanceMode() && serieData.emphasis != null && serieData.emphasis.show) return serieData.emphasis.labelLine; - else if (serie.emphasis.show) return serie.emphasis.labelLine; + else if (serie.emphasis != null && serie.emphasis.show) return serie.emphasis.labelLine; else return serie.labelLine; } else @@ -466,11 +466,16 @@ namespace XCharts public static Color32 GetAreaColor(Serie serie, ThemeStyle theme, int index, bool highlight) { var areaStyle = serie.areaStyle; - var color = !ChartHelper.IsClearColor(areaStyle.color) ? areaStyle.color : theme.GetColor(index); + if (areaStyle == null || !areaStyle.show) + return ColorUtil.clearColor32; + var color = !ChartHelper.IsClearColor(areaStyle.color) + ? areaStyle.color : theme.GetColor(index); if (highlight) { - if (!ChartHelper.IsClearColor(areaStyle.highlightColor)) color = areaStyle.highlightColor; - else color = ChartHelper.GetHighlightColor(color); + if (!ChartHelper.IsClearColor(areaStyle.highlightColor)) + color = areaStyle.highlightColor; + else + color = ChartHelper.GetHighlightColor(color); } ChartHelper.SetColorOpacity(ref color, areaStyle.opacity); return color; @@ -479,6 +484,8 @@ namespace XCharts public static Color32 GetAreaToColor(Serie serie, ThemeStyle theme, int index, bool highlight) { var areaStyle = serie.areaStyle; + if (areaStyle == null || !areaStyle.show) + return ColorUtil.clearColor32; if (!ChartHelper.IsClearColor(areaStyle.toColor)) { var color = areaStyle.toColor; diff --git a/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs b/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs index 871e0a67..684e28dd 100644 --- a/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs +++ b/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs @@ -8,7 +8,7 @@ namespace XCharts public static Color GetLabelColor(Serie serie, ThemeStyle theme, int index) { - if (!ChartHelper.IsClearColor(serie.label.textStyle.color)) + if (serie.label != null && !ChartHelper.IsClearColor(serie.label.textStyle.color)) { return serie.label.textStyle.color; } @@ -39,7 +39,7 @@ namespace XCharts { serieLabel = SerieHelper.GetSerieLabel(serie, serieData); } - var numericFormatter = serieLabel == null ? serie.label.numericFormatter : serieLabel.numericFormatter; + var numericFormatter = serieLabel == null ? "" : serieLabel.numericFormatter; var serieName = serie.serieName; var dataName = serieData != null ? serieData.name : null; if (serieLabel.formatterFunction != null) @@ -62,14 +62,16 @@ namespace XCharts var serieData = serie.GetSerieData(0); if (serieData == null) return; if (serieData.labelObject == null) return; + var label = SerieHelper.GetSerieLabel(serie, serieData); + if(label == null) return; var value = serieData.GetData(1); var total = serie.max; var content = SerieLabelHelper.GetFormatterContent(serie, serieData, value, total, null, Color.clear); serieData.labelObject.SetText(content); - serieData.labelObject.SetLabelPosition(serie.context.center + serie.label.offset); - if (!ChartHelper.IsClearColor(serie.label.textStyle.color)) + serieData.labelObject.SetLabelPosition(serie.context.center + label.offset); + if (!ChartHelper.IsClearColor(label.textStyle.color)) { - serieData.labelObject.label.SetColor(serie.label.textStyle.color); + serieData.labelObject.label.SetColor(label.textStyle.color); } } @@ -79,7 +81,7 @@ namespace XCharts { var serieData = serie.data[i]; var serieLabel = SerieHelper.GetSerieLabel(serie, serieData, serieData.context.highlight); - if (serieLabel.show && serieData.labelObject != null) + if (serieLabel != null && serieLabel.show && serieData.labelObject != null) { if (!serie.show || !serieData.show) { @@ -93,7 +95,7 @@ namespace XCharts serieData.labelObject.SetText(content); serieData.labelObject.SetTextColor(GetLabelColor(serie, theme, i)); - if (serie.label.position == LabelStyle.Position.Bottom) + if (serieLabel.position == LabelStyle.Position.Bottom) { var labelWidth = serieData.GetLabelWidth(); if (serie.clockwise) diff --git a/Assets/XCharts/Runtime/Helper/SeriesHelper.cs b/Assets/XCharts/Runtime/Helper/SeriesHelper.cs index 75638af2..3bb4db92 100644 --- a/Assets/XCharts/Runtime/Helper/SeriesHelper.cs +++ b/Assets/XCharts/Runtime/Helper/SeriesHelper.cs @@ -119,7 +119,7 @@ namespace XCharts if (string.IsNullOrEmpty(stack)) return false; foreach (var serie in series) { - if (serie.show && serie.areaStyle.show && stack.Equals(serie.stack)) + if (serie.show && serie.areaStyle != null && serie.areaStyle.show && stack.Equals(serie.stack)) { if (!ChartHelper.IsValueEqualsColor(serie.areaStyle.color, serie.areaStyle.toColor) && !ChartHelper.IsClearColor(serie.areaStyle.toColor)) diff --git a/Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs b/Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs new file mode 100644 index 00000000..69569244 --- /dev/null +++ b/Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; + +namespace XCharts +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public sealed class SerieExtraComponentAttribute : Attribute + { + public readonly List<Type> types = new List<Type>(); + + public SerieExtraComponentAttribute() + { + } + public SerieExtraComponentAttribute(Type type1) + { + types.Add(type1); + } + public SerieExtraComponentAttribute(Type type1, Type type2) + { + types.Add(type1); + types.Add(type2); + } + public SerieExtraComponentAttribute(Type type1, Type type2, Type type3) + { + types.Add(type1); + types.Add(type2); + types.Add(type3); + } + public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4) + { + types.Add(type1); + types.Add(type2); + types.Add(type3); + types.Add(type4); + } + public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5) + { + types.Add(type1); + types.Add(type2); + types.Add(type3); + types.Add(type4); + types.Add(type5); + } + public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6) + { + types.Add(type1); + types.Add(type2); + types.Add(type3); + types.Add(type4); + types.Add(type5); + types.Add(type6); + } + public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6, Type type7) + { + types.Add(type1); + types.Add(type2); + types.Add(type3); + types.Add(type4); + types.Add(type5); + types.Add(type6); + types.Add(type7); + } + + public bool Contains<T>() where T : ISerieExtraComponent + { + return Contains(typeof(T)); + } + + public bool Contains(Type type) + { + return types.Contains(type); + } + } +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs.meta b/Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs.meta new file mode 100644 index 00000000..99dd27c6 --- /dev/null +++ b/Assets/XCharts/Runtime/Internal/Attributes/SerieExtraComponentAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7d61861a0f45f43af8915ae23cc326e9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XCharts/Runtime/Internal/BaseChart.Component.cs b/Assets/XCharts/Runtime/Internal/BaseChart.Component.cs index 46723c4b..49e76e66 100644 --- a/Assets/XCharts/Runtime/Internal/BaseChart.Component.cs +++ b/Assets/XCharts/Runtime/Internal/BaseChart.Component.cs @@ -1,10 +1,4 @@ -/************************************************/ -/* */ -/* Copyright (c) 2018 - 2021 monitor1394 */ -/* https://github.component/monitor1394 */ -/* */ -/************************************************/ - + using UnityEngine; using System.Collections.Generic; using System; diff --git a/Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs b/Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs new file mode 100644 index 00000000..ffa77499 --- /dev/null +++ b/Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs @@ -0,0 +1,12 @@ + +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace XCharts +{ + public interface ISerieExtraComponent + { + bool show { get; set; } + } +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs.meta b/Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs.meta new file mode 100644 index 00000000..d9d9a3c9 --- /dev/null +++ b/Assets/XCharts/Runtime/Internal/Misc/ISerieExtraComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 20d76dbb8ca234b439951f6e72826c43 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XCharts/Runtime/Internal/Object/ChartLabel.cs b/Assets/XCharts/Runtime/Internal/Object/ChartLabel.cs index e7aa907c..af2932f7 100644 --- a/Assets/XCharts/Runtime/Internal/Object/ChartLabel.cs +++ b/Assets/XCharts/Runtime/Internal/Object/ChartLabel.cs @@ -50,18 +50,6 @@ namespace XCharts raycastTarget = false; } - // protected override void OnPopulateMesh(VertexHelper vh) - // { - // if (m_BackgroundColor != Color.clear || m_BackgroundImage != null) - // { - - // } - // else - // { - // vh.Clear(); - // } - // } - public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, float paddingTopBottom) { m_LabelAutoSize = autoSize; @@ -108,7 +96,9 @@ namespace XCharts public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null) { - if (m_IconImage == null) return; + if (m_IconImage == null || iconStyle == null) + return; + SetIconActive(iconStyle.show); if (iconStyle.show) { diff --git a/Assets/XCharts/Runtime/Serie/Bar/Bar.cs b/Assets/XCharts/Runtime/Serie/Bar/Bar.cs index e078176b..fa35b8c5 100644 --- a/Assets/XCharts/Runtime/Serie/Bar/Bar.cs +++ b/Assets/XCharts/Runtime/Serie/Bar/Bar.cs @@ -10,6 +10,10 @@ namespace XCharts [SerieConvert(typeof(Line),typeof(Pie))] [RequireChartComponent(typeof(GridCoord))] [DefaultAnimation(AnimationType.BottomToTop)] + [SerieExtraComponent( + typeof(LabelStyle), + typeof(IconStyle), + typeof(Emphasis))] public class Bar : Serie, INeedSerieContainer { public int containerIndex { get; internal set; } diff --git a/Assets/XCharts/Runtime/Serie/Candlestick/Candlestick.cs b/Assets/XCharts/Runtime/Serie/Candlestick/Candlestick.cs index 61c12617..dab5f453 100644 --- a/Assets/XCharts/Runtime/Serie/Candlestick/Candlestick.cs +++ b/Assets/XCharts/Runtime/Serie/Candlestick/Candlestick.cs @@ -6,6 +6,7 @@ namespace XCharts [System.Serializable] [SerieHandler(typeof(CandlestickHandler), true)] [DefaultAnimation(AnimationType.LeftToRight)] + [SerieExtraComponent()] public class Candlestick : Serie, INeedSerieContainer { public int containerIndex { get; internal set; } diff --git a/Assets/XCharts/Runtime/Serie/Gauge.meta b/Assets/XCharts/Runtime/Serie/Gauge.meta deleted file mode 100644 index 5adf9873..00000000 --- a/Assets/XCharts/Runtime/Serie/Gauge.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: e0eb5955894da4c8daac165364c5363a -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/XCharts/Runtime/Serie/Heatmap/Heatmap.cs b/Assets/XCharts/Runtime/Serie/Heatmap/Heatmap.cs index 8fa9b03d..d5b3a58d 100644 --- a/Assets/XCharts/Runtime/Serie/Heatmap/Heatmap.cs +++ b/Assets/XCharts/Runtime/Serie/Heatmap/Heatmap.cs @@ -6,6 +6,7 @@ namespace XCharts [System.Serializable] [SerieHandler(typeof(HeatmapHandler), true)] [DefaultAnimation(AnimationType.LeftToRight)] + [SerieExtraComponent(typeof(LabelStyle), typeof(Emphasis))] public class Heatmap : Serie, INeedSerieContainer { public int containerIndex { get; internal set; } @@ -16,10 +17,12 @@ namespace XCharts serie.itemStyle.show = true; serie.itemStyle.borderWidth = 1; serie.itemStyle.borderColor = Color.clear; - serie.emphasis.show = true; - serie.emphasis.itemStyle.show = true; - serie.emphasis.itemStyle.borderWidth = 1; - serie.emphasis.itemStyle.borderColor = Color.black; + + var emphasis = serie.AddExtraComponent<Emphasis>(); + emphasis.show = true; + emphasis.itemStyle.show = true; + emphasis.itemStyle.borderWidth = 1; + emphasis.itemStyle.borderColor = Color.black; } } } \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Serie/Heatmap/HeatmapHandler.cs b/Assets/XCharts/Runtime/Serie/Heatmap/HeatmapHandler.cs index e393091a..bcbbcdd6 100644 --- a/Assets/XCharts/Runtime/Serie/Heatmap/HeatmapHandler.cs +++ b/Assets/XCharts/Runtime/Serie/Heatmap/HeatmapHandler.cs @@ -96,6 +96,7 @@ namespace XCharts xAxis.boundaryGap = true; yAxis.boundaryGap = true; var visualMap = chart.GetVisualMapOfSerie(serie); + var emphasis = serie.emphasis; var xCount = xAxis.data.Count; var yCount = yAxis.data.Count; var xWidth = grid.context.width / xCount; @@ -153,7 +154,7 @@ namespace XCharts color = visualMap.GetColor(value); if (animationIndex >= 0 && i > animationIndex) continue; serieData.context.canShowLabel = true; - var emphasis = (serieData.context.highlight) + var highlight = (serieData.context.highlight) || visualMap.context.pointerIndex > 0; UGL.DrawRectangle(vh, pos, rectWid / 2, rectHig / 2, color); @@ -161,14 +162,14 @@ namespace XCharts { UGL.DrawBorder(vh, pos, rectWid, rectHig, borderWidth, borderColor, borderToColor); } - if (visualMap.hoverLink && emphasis && serie.emphasis.show - && serie.emphasis.itemStyle.borderWidth > 0) + if (visualMap.hoverLink && highlight && emphasis != null && emphasis.show + && emphasis.itemStyle.borderWidth > 0) { - var emphasisBorderWidth = serie.emphasis.itemStyle.borderWidth; - var emphasisBorderColor = serie.emphasis.itemStyle.opacity > 0 - ? serie.emphasis.itemStyle.borderColor : ChartConst.clearColor32; - var emphasisBorderToColor = serie.emphasis.itemStyle.opacity > 0 - ? serie.emphasis.itemStyle.borderToColor : ChartConst.clearColor32; + var emphasisBorderWidth = emphasis.itemStyle.borderWidth; + var emphasisBorderColor = emphasis.itemStyle.opacity > 0 + ? emphasis.itemStyle.borderColor : ChartConst.clearColor32; + var emphasisBorderToColor = emphasis.itemStyle.opacity > 0 + ? emphasis.itemStyle.borderToColor : ChartConst.clearColor32; UGL.DrawBorder(vh, pos, rectWid, rectHig, emphasisBorderWidth, emphasisBorderColor, emphasisBorderToColor); } diff --git a/Assets/XCharts/Runtime/Serie/Line/Line.cs b/Assets/XCharts/Runtime/Serie/Line/Line.cs index f79fea28..c198ada5 100644 --- a/Assets/XCharts/Runtime/Serie/Line/Line.cs +++ b/Assets/XCharts/Runtime/Serie/Line/Line.cs @@ -9,6 +9,14 @@ namespace XCharts [SerieConvert(typeof(Bar), typeof(Pie))] [CoordOptions(typeof(GridCoord), typeof(PolarCoord))] [DefaultAnimation(AnimationType.LeftToRight)] + [SerieExtraComponent( + typeof(LabelStyle), + typeof(LabelLine), + typeof(EndLabelStyle), + typeof(LineArrow), + typeof(AreaStyle), + typeof(IconStyle), + typeof(Emphasis))] public class Line : Serie, INeedSerieContainer { public int containerIndex { get; internal set; } @@ -17,8 +25,6 @@ namespace XCharts { var serie = chart.AddSerie<Line>(serieName); serie.symbol.show = true; - serie.label.offset = new Vector3(0, 15f, 0); - serie.label.autoOffset = true; for (int i = 0; i < 5; i++) { chart.AddData(serie.index, UnityEngine.Random.Range(10, 90)); diff --git a/Assets/XCharts/Runtime/Serie/Line/LineHandler.GridCoord.cs b/Assets/XCharts/Runtime/Serie/Line/LineHandler.GridCoord.cs index 5b8afdf9..e9ad4e15 100644 --- a/Assets/XCharts/Runtime/Serie/Line/LineHandler.GridCoord.cs +++ b/Assets/XCharts/Runtime/Serie/Line/LineHandler.GridCoord.cs @@ -184,6 +184,7 @@ namespace XCharts var clip = SeriesHelper.IsAnyClipSerie(chart.series); var theme = chart.theme; var interacting = false; + var lineArrow = serie.lineArrow; //var isY = ComponentHelper.IsAnyCategoryOfYAxis(chart.components); for (int i = 0; i < count; i++) @@ -202,11 +203,11 @@ namespace XCharts // { // continue; // } - if (serie.lineArrow.show) + if (lineArrow != null && lineArrow.show) { - if (serie.lineArrow.position == LineArrow.Position.Start && i == 0) + if (lineArrow.position == LineArrow.Position.Start && i == 0) continue; - if (serie.lineArrow.position == LineArrow.Position.End && i == count - 1) + if (lineArrow.position == LineArrow.Position.End && i == count - 1) continue; } @@ -246,7 +247,7 @@ namespace XCharts private void DrawLineArrow(VertexHelper vh, Serie serie) { - if (!serie.show || !serie.lineArrow.show) + if (!serie.show || serie.lineArrow == null || !serie.lineArrow.show) return; if (serie.context.dataPoints.Count < 2) diff --git a/Assets/XCharts/Runtime/Serie/Line/LineHelper.cs b/Assets/XCharts/Runtime/Serie/Line/LineHelper.cs index 6f3c5934..31df6a6c 100644 --- a/Assets/XCharts/Runtime/Serie/Line/LineHelper.cs +++ b/Assets/XCharts/Runtime/Serie/Line/LineHelper.cs @@ -25,7 +25,7 @@ namespace XCharts public static void DrawSerieLineArea(VertexHelper vh, Serie serie, Serie lastStackSerie, ThemeStyle theme, bool isY, Axis axis, Axis relativedAxis, GridCoord grid) { - if (!serie.areaStyle.show) + if (serie.areaStyle == null || !serie.areaStyle.show) return; var srcAreaColor = SerieHelper.GetAreaColor(serie, theme, serie.context.colorIndex, false); diff --git a/Assets/XCharts/Runtime/Serie/Liquid/Liquid.cs b/Assets/XCharts/Runtime/Serie/Liquid/Liquid.cs index c878ff7c..f5c77174 100644 --- a/Assets/XCharts/Runtime/Serie/Liquid/Liquid.cs +++ b/Assets/XCharts/Runtime/Serie/Liquid/Liquid.cs @@ -6,6 +6,7 @@ namespace XCharts [System.Serializable] [SerieHandler(typeof(LiquidHandler), true)] [RequireChartComponent(typeof(Vessel))] + [SerieExtraComponent()] public class Liquid : Serie, INeedSerieContainer { [SerializeField] private float m_WaveHeight = 10f; @@ -55,6 +56,7 @@ namespace XCharts var serie = chart.AddSerie<Liquid>(serieName); serie.min = 0; serie.max = 100; + serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; serie.label.textStyle.fontSize = 40; serie.label.formatter = "{d}%"; diff --git a/Assets/XCharts/Runtime/Serie/Pie/Pie.cs b/Assets/XCharts/Runtime/Serie/Pie/Pie.cs index 4b823a0b..c6d6111b 100644 --- a/Assets/XCharts/Runtime/Serie/Pie/Pie.cs +++ b/Assets/XCharts/Runtime/Serie/Pie/Pie.cs @@ -5,6 +5,7 @@ namespace XCharts [SerieConvert(typeof(Line), typeof(Bar))] [SerieHandler(typeof(PieHandler), true)] [DefaultAnimation(AnimationType.Clockwise)] + [SerieExtraComponent(typeof(LabelStyle), typeof(LabelLine), typeof(IconStyle), typeof(Emphasis))] public class Pie : Serie { public override bool useDataNameForColor { get { return true; } } diff --git a/Assets/XCharts/Runtime/Serie/Pie/PieHandler.cs b/Assets/XCharts/Runtime/Serie/Pie/PieHandler.cs index 4ea8e7e8..9da266a1 100644 --- a/Assets/XCharts/Runtime/Serie/Pie/PieHandler.cs +++ b/Assets/XCharts/Runtime/Serie/Pie/PieHandler.cs @@ -413,9 +413,9 @@ namespace XCharts { var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); var labelLine = SerieHelper.GetSerieLabelLine(serie, serieData); - if (serieLabel.show - && serieLabel.position == LabelStyle.Position.Outside - && labelLine.show) + if (serieLabel != null && serieLabel.show + && labelLine != null && labelLine.show + && serieLabel.position == LabelStyle.Position.Outside) { var insideRadius = serieData.context.insideRadius; var outSideRadius = serieData.context.outsideRadius; @@ -495,8 +495,9 @@ namespace XCharts private void DrawPieLabel(Serie serie, int dataIndex, SerieData serieData, Color serieColor) { if (serieData.labelObject == null) return; + var emphasis = serie.emphasis; var currAngle = serieData.context.halfAngle; - var isHighlight = (serieData.context.highlight && serie.emphasis.label.show); + var isHighlight = (serieData.context.highlight && emphasis != null && emphasis.label.show); var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); var labelLine = SerieHelper.GetSerieLabelLine(serie, serieData); var iconStyle = SerieHelper.GetIconStyle(serie, serieData); @@ -514,9 +515,9 @@ namespace XCharts Color color = serieColor; if (isHighlight) { - if (!ChartHelper.IsClearColor(serie.emphasis.label.textStyle.color)) + if (!ChartHelper.IsClearColor(emphasis.label.textStyle.color)) { - color = serie.emphasis.label.textStyle.color; + color = emphasis.label.textStyle.color; } } else if (!ChartHelper.IsClearColor(serieLabel.textStyle.color)) @@ -528,10 +529,10 @@ namespace XCharts color = isInsidePosition ? Color.white : serieColor; } var fontSize = isHighlight - ? serie.emphasis.label.textStyle.GetFontSize(chart.theme.common) + ? emphasis.label.textStyle.GetFontSize(chart.theme.common) : serieLabel.textStyle.GetFontSize(chart.theme.common); var fontStyle = isHighlight - ? serie.emphasis.label.textStyle.fontStyle + ? emphasis.label.textStyle.fontStyle : serieLabel.textStyle.fontStyle; serieData.labelObject.label.SetColor(color); diff --git a/Assets/XCharts/Runtime/Serie/Radar/Radar.cs b/Assets/XCharts/Runtime/Serie/Radar/Radar.cs index 5f14d613..854cfbde 100644 --- a/Assets/XCharts/Runtime/Serie/Radar/Radar.cs +++ b/Assets/XCharts/Runtime/Serie/Radar/Radar.cs @@ -7,6 +7,7 @@ namespace XCharts [System.Serializable] [SerieHandler(typeof(RadarHandler), true)] [RequireChartComponent(typeof(RadarCoord))] + [SerieExtraComponent(typeof(LabelStyle), typeof(LabelLine), typeof(AreaStyle), typeof(Emphasis))] public class Radar : Serie, INeedSerieContainer { public int containerIndex { get; internal set; } diff --git a/Assets/XCharts/Runtime/Serie/Radar/RadarHandler.cs b/Assets/XCharts/Runtime/Serie/Radar/RadarHandler.cs index 0e7875a7..6dbb446d 100644 --- a/Assets/XCharts/Runtime/Serie/Radar/RadarHandler.cs +++ b/Assets/XCharts/Runtime/Serie/Radar/RadarHandler.cs @@ -226,6 +226,8 @@ namespace XCharts serie.containerIndex = m_RadarCoord.index; serie.containterInstanceId = m_RadarCoord.instanceId; + var areaStyle = serie.areaStyle; + var startPoint = Vector3.zero; var toPoint = Vector3.zero; var firstPoint = Vector3.zero; @@ -281,7 +283,7 @@ namespace XCharts { toPoint = new Vector3(centerPos.x + radius * Mathf.Sin(currAngle), centerPos.y + radius * Mathf.Cos(currAngle)); - if (serie.areaStyle.show) + if (areaStyle != null && areaStyle.show) { UGL.DrawTriangle(vh, startPoint, toPoint, centerPos, areaColor, areaColor, areaToColor); } @@ -293,7 +295,7 @@ namespace XCharts } serieData.context.dataPoints.Add(startPoint); } - if (serie.areaStyle.show) + if (areaStyle != null && areaStyle.show) { UGL.DrawTriangle(vh, startPoint, firstPoint, centerPos, areaColor, areaColor, areaToColor); } diff --git a/Assets/XCharts/Runtime/Serie/Ring/Ring.cs b/Assets/XCharts/Runtime/Serie/Ring/Ring.cs index 6ec8dcc1..f7f8e5a9 100644 --- a/Assets/XCharts/Runtime/Serie/Ring/Ring.cs +++ b/Assets/XCharts/Runtime/Serie/Ring/Ring.cs @@ -5,6 +5,7 @@ namespace XCharts { [System.Serializable] [SerieHandler(typeof(RingHandler), true)] + [SerieExtraComponent(typeof(LabelStyle), typeof(Emphasis))] public class Ring : Serie { public override bool useDataNameForColor { get { return true; } } @@ -15,6 +16,7 @@ namespace XCharts serie.radius = new float[] { 0.3f, 0.35f }; serie.titleStyle.show = false; serie.titleStyle.textStyle.offset = new Vector2(0, 30); + serie.AddExtraComponent<LabelStyle>(); serie.label.show = true; serie.label.position = LabelStyle.Position.Center; serie.label.formatter = "{d:f0}%"; diff --git a/Assets/XCharts/Runtime/Serie/Ring/RingHandler.cs b/Assets/XCharts/Runtime/Serie/Ring/RingHandler.cs index 5d04a248..63b320b6 100644 --- a/Assets/XCharts/Runtime/Serie/Ring/RingHandler.cs +++ b/Assets/XCharts/Runtime/Serie/Ring/RingHandler.cs @@ -213,22 +213,23 @@ namespace XCharts private void UpateLabelPosition(Serie serie, SerieData serieData, int index, float startAngle, float toAngle, float centerRadius) { - if (!serie.label.show) return; + var label = serie.label; + if (label == null || !label.show) return; if (serieData.labelObject == null) return; - switch (serie.label.position) + switch (label.position) { case LabelStyle.Position.Center: - serieData.context.labelPosition = serie.context.center + serie.label.offset; + serieData.context.labelPosition = serie.context.center + label.offset; break; case LabelStyle.Position.Bottom: var px1 = Mathf.Sin(startAngle * Mathf.Deg2Rad) * centerRadius; var py1 = Mathf.Cos(startAngle * Mathf.Deg2Rad) * centerRadius; - var xDiff = serie.clockwise ? -serie.label.margin : serie.label.margin; + var xDiff = serie.clockwise ? -label.margin : label.margin; serieData.context.labelPosition = serie.context.center + new Vector3(px1 + xDiff, py1); break; case LabelStyle.Position.Top: - startAngle += serie.clockwise ? -serie.label.margin : serie.label.margin; - toAngle += serie.clockwise ? serie.label.margin : -serie.label.margin; + startAngle += serie.clockwise ? -label.margin : label.margin; + toAngle += serie.clockwise ? label.margin : -label.margin; var px2 = Mathf.Sin(toAngle * Mathf.Deg2Rad) * centerRadius; var py2 = Mathf.Cos(toAngle * Mathf.Deg2Rad) * centerRadius; serieData.context.labelPosition = serie.context.center + new Vector3(px2, py2); diff --git a/Assets/XCharts/Runtime/Serie/Scatter/EffectScatter.cs b/Assets/XCharts/Runtime/Serie/Scatter/EffectScatter.cs index d253660f..35398a7c 100644 --- a/Assets/XCharts/Runtime/Serie/Scatter/EffectScatter.cs +++ b/Assets/XCharts/Runtime/Serie/Scatter/EffectScatter.cs @@ -6,6 +6,7 @@ namespace XCharts [System.Serializable] [SerieHandler(typeof(EffectScatterHandler), true)] [CoordOptions(typeof(GridCoord), typeof(SingleAxisCoord))] + [SerieExtraComponent(typeof(LabelStyle), typeof(Emphasis))] public class EffectScatter : BaseScatter { public static void AddDefaultSerie(BaseChart chart, string serieName) diff --git a/Assets/XCharts/Runtime/Serie/Scatter/Scatter.cs b/Assets/XCharts/Runtime/Serie/Scatter/Scatter.cs index 87a1fbb9..5165cf3b 100644 --- a/Assets/XCharts/Runtime/Serie/Scatter/Scatter.cs +++ b/Assets/XCharts/Runtime/Serie/Scatter/Scatter.cs @@ -8,6 +8,7 @@ namespace XCharts [System.Serializable] [SerieHandler(typeof(ScatterHandler), true)] [CoordOptions(typeof(GridCoord), typeof(SingleAxisCoord))] + [SerieExtraComponent(typeof(LabelStyle), typeof(Emphasis))] public class Scatter : BaseScatter { public static void AddDefaultSerie(BaseChart chart, string serieName) diff --git a/Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs b/Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs new file mode 100644 index 00000000..b72d100b --- /dev/null +++ b/Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs @@ -0,0 +1,139 @@ +using UnityEngine; +using System.Collections.Generic; +using System; +using System.Reflection; + +namespace XCharts +{ + public partial class Serie + { + public static Dictionary<Type, string> extraComponentFieldNameDict = new Dictionary<Type, string> + { + {typeof(LabelStyle), "m_Labels"}, + {typeof(LabelLine), "m_LabelLines"}, + {typeof(EndLabelStyle), "m_EndLabels"}, + {typeof(LineArrow), "m_LineArrows"}, + {typeof(AreaStyle), "m_AreaStyles"}, + {typeof(IconStyle), "m_IconStyles"}, + {typeof(Emphasis), "m_Emphases"}, + }; + + [SerializeField] private List<LabelStyle> m_Labels = new List<LabelStyle>(); + [SerializeField] private List<LabelLine> m_LabelLines = new List<LabelLine>(); + [SerializeField] private List<EndLabelStyle> m_EndLabels = new List<EndLabelStyle>(); + [SerializeField] private List<LineArrow> m_LineArrows = new List<LineArrow>(); + [SerializeField] private List<AreaStyle> m_AreaStyles = new List<AreaStyle>(); + [SerializeField] private List<IconStyle> m_IconStyles = new List<IconStyle>(); + [SerializeField] private List<Emphasis> m_Emphases = new List<Emphasis>(); + + /// <summary> + /// The style of area. + /// 区域填充样式。 + /// </summary> + public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } } + /// <summary> + /// Text label of graphic element,to explain some data information about graphic item like value, name and so on. + /// 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。s + /// </summary> + public LabelStyle label { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } } + public LabelStyle endLabel { get { return m_EndLabels.Count > 0 ? m_EndLabels[0] : null; } } + /// <summary> + /// The line of label. + /// 标签上的视觉引导线。 + /// </summary> + public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } } + /// <summary> + /// The arrow of line. + /// 折线图的箭头。 + /// </summary> + public LineArrow lineArrow { get { return m_LineArrows.Count > 0 ? m_LineArrows[0] : null; } } + /// <summary> + /// 高亮的图形样式和文本标签样式。 + /// </summary> + public Emphasis emphasis { get { return m_Emphases.Count > 0 ? m_Emphases[0] : null; } } + /// <summary> + /// the icon of data. + /// 数据项图标样式。 + /// </summary> + public IconStyle iconStyle { get { return m_IconStyles.Count > 0 ? m_IconStyles[0] : null; } } + + public void RemoveAllExtraComponent() + { + var serieType = GetType(); + foreach (var kv in extraComponentFieldNameDict) + { + ReflectionUtil.InvokeListClear(this, serieType.GetField(kv.Value)); + } + SetAllDirty(); + } + + public T AddExtraComponent<T>() where T : ChildComponent + { + return AddExtraComponent(typeof(T)) as T; + } + + public ISerieExtraComponent AddExtraComponent(Type type) + { + if (GetType().IsDefined(typeof(SerieExtraComponentAttribute), false)) + { + var attr = GetType().GetAttribute<SerieExtraComponentAttribute>(); + if (attr.Contains(type)) + { + var fieldName = string.Empty; + if (extraComponentFieldNameDict.TryGetValue(type, out fieldName)) + { + var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); + if (ReflectionUtil.InvokeListCount(this, field) <= 0) + { + var extraComponent = Activator.CreateInstance(type) as ISerieExtraComponent; + ReflectionUtil.InvokeListAdd(this, field, extraComponent); + SetAllDirty(); + return extraComponent; + } + else + { + return ReflectionUtil.InvokeListGet<ISerieExtraComponent>(this, field, 0); + } + } + } + } + throw new System.Exception(string.Format("Serie {0} not support extra component: {1}", + GetType().Name, type.Name)); + } + + public void RemoveExtraComponent<T>() where T : ISerieExtraComponent + { + RemoveExtraComponent(typeof(T)); + } + + public void RemoveExtraComponent(Type type) + { + if (GetType().IsDefined(typeof(SerieExtraComponentAttribute), false)) + { + var attr = GetType().GetAttribute<SerieExtraComponentAttribute>(); + if (attr.Contains(type)) + { + var fieldName = string.Empty; + if (extraComponentFieldNameDict.TryGetValue(type, out fieldName)) + { + var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); + ReflectionUtil.InvokeListClear(this, field); + SetAllDirty(); + return; + } + } + } + throw new System.Exception(string.Format("Serie {0} not support extra component: {1}", + GetType().Name, type.Name)); + } + + private void RemoveExtraComponentList<T>(List<T> list) where T : ISerieExtraComponent + { + if (list.Count > 0) + { + list.Clear(); + SetAllDirty(); + } + } + } +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs.meta b/Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs.meta new file mode 100644 index 00000000..97d8db37 --- /dev/null +++ b/Assets/XCharts/Runtime/Serie/Serie.ExtraComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9c4f3a01039fd4e7fbf771a65ede0069 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XCharts/Runtime/Serie/Serie.cs b/Assets/XCharts/Runtime/Serie/Serie.cs index c83aa43e..ce089f92 100644 --- a/Assets/XCharts/Runtime/Serie/Serie.cs +++ b/Assets/XCharts/Runtime/Serie/Serie.cs @@ -76,7 +76,7 @@ namespace XCharts Capsule } - + /// <summary> /// 雷达图类型 @@ -159,7 +159,7 @@ namespace XCharts /// 系列。 /// </summary> [System.Serializable] - public class Serie : BaseSerie, IComparable + public partial class Serie : BaseSerie, IComparable { [SerializeField] private int m_Index; [SerializeField] private bool m_Show = true; @@ -177,16 +177,12 @@ namespace XCharts [SerializeField] protected int m_MinShow; [SerializeField] protected int m_MaxShow; [SerializeField] protected int m_MaxCache; - [SerializeField] private AreaStyle m_AreaStyle = AreaStyle.defaultAreaStyle; - [SerializeField] private SymbolStyle m_Symbol = new SymbolStyle(); [SerializeField] private float m_SampleDist = 0; [SerializeField] private SampleType m_SampleType = SampleType.Average; [SerializeField] private float m_SampleAverage = 0; [SerializeField] private LineType m_LineType = LineType.Normal; - [SerializeField] private LineStyle m_LineStyle = new LineStyle(); - [SerializeField] private BarType m_BarType = BarType.Normal; [SerializeField] private bool m_BarPercentStack = false; [SerializeField] private float m_BarWidth = 0.6f; @@ -211,14 +207,7 @@ namespace XCharts [SerializeField] private float m_Space; [SerializeField] private float[] m_Center = new float[2] { 0.5f, 0.45f }; [SerializeField] private float[] m_Radius = new float[2] { 0, 80 }; - [SerializeField] private LabelStyle m_Label = new LabelStyle(); - [SerializeField] private LabelLine m_LabelLine = new LabelLine(); - [SerializeField] private AnimationStyle m_Animation = new AnimationStyle(); - [SerializeField] private LineArrow m_LineArrow = new LineArrow(); - [SerializeField] private ItemStyle m_ItemStyle = new ItemStyle(); - [SerializeField] private Emphasis m_Emphasis = new Emphasis(); - [SerializeField] private TitleStyle m_TitleStyle = new TitleStyle(); - [SerializeField] private IconStyle m_IconStyle = new IconStyle(); + [SerializeField] [Range(1, 10)] private int m_ShowDataDimension; [SerializeField] private bool m_ShowDataName; [SerializeField] private bool m_ShowDataIcon; @@ -240,6 +229,18 @@ namespace XCharts [SerializeField] private float m_Top; [SerializeField] private float m_Bottom; [SerializeField] private bool m_InsertDataToHead; + + [SerializeField] private LineStyle m_LineStyle = new LineStyle(); + //[SerializeField] private AreaStyle m_AreaStyle = AreaStyle.defaultAreaStyle; + [SerializeField] private SymbolStyle m_Symbol = new SymbolStyle(); + //[SerializeField] private LabelStyle m_Label = new LabelStyle(); + //[SerializeField] private LabelLine m_LabelLine = new LabelLine(); + [SerializeField] private AnimationStyle m_Animation = new AnimationStyle(); + //[SerializeField] private LineArrow m_LineArrow = new LineArrow(); + [SerializeField] private ItemStyle m_ItemStyle = new ItemStyle(); + //[SerializeField] private Emphasis m_Emphasis = new Emphasis(); + [SerializeField] private TitleStyle m_TitleStyle = new TitleStyle(); + //[SerializeField] private IconStyle m_IconStyle = new IconStyle(); [SerializeField] private List<SerieData> m_Data = new List<SerieData>(); [NonSerialized] internal int m_FilterStart; @@ -398,15 +399,7 @@ namespace XCharts get { return m_MaxCache; } set { if (PropertyUtil.SetStruct(ref m_MaxCache, value < 0 ? 0 : value)) { SetVerticesDirty(); } } } - /// <summary> - /// The style of area. - /// 区域填充样式。 - /// </summary> - public AreaStyle areaStyle - { - get { return m_AreaStyle; } - set { if (PropertyUtil.SetClass(ref m_AreaStyle, value, true)) SetVerticesDirty(); } - } + /// <summary> /// the symbol of serie data item. /// 标记的图形。 @@ -703,25 +696,6 @@ namespace XCharts get { return m_RadarType; } set { if (PropertyUtil.SetStruct(ref m_RadarType, value)) SetVerticesDirty(); } } - - /// <summary> - /// Text label of graphic element,to explain some data information about graphic item like value, name and so on. - /// 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。 - /// </summary> - public LabelStyle label - { - get { return m_Label; } - set { if (PropertyUtil.SetClass(ref m_Label, value, true)) SetAllDirty(); } - } - /// <summary> - /// The line of label. - /// 标签上的视觉引导线。 - /// </summary> - public LabelLine labelLine - { - get { return m_LabelLine; } - set { if (PropertyUtil.SetClass(ref m_LabelLine, value, true)) SetAllDirty(); } - } /// <summary> /// The start animation. /// 起始动画。 @@ -732,15 +706,6 @@ namespace XCharts set { if (PropertyUtil.SetClass(ref m_Animation, value, true)) SetVerticesDirty(); } } /// <summary> - /// The arrow of line. - /// 折线图的箭头。 - /// </summary> - public LineArrow lineArrow - { - get { return m_LineArrow; } - set { if (PropertyUtil.SetClass(ref m_LineArrow, value, true)) SetVerticesDirty(); } - } - /// <summary> /// The style of data item. /// 图形样式。 /// </summary> @@ -750,14 +715,6 @@ namespace XCharts set { if (PropertyUtil.SetClass(ref m_ItemStyle, value, true)) SetVerticesDirty(); } } /// <summary> - /// 高亮的图形样式和文本标签样式。 - /// </summary> - public Emphasis emphasis - { - get { return m_Emphasis; } - set { if (PropertyUtil.SetClass(ref m_Emphasis, value, true)) SetVerticesDirty(); } - } - /// <summary> /// 标题样式。 /// </summary> public TitleStyle titleStyle @@ -766,15 +723,6 @@ namespace XCharts set { if (PropertyUtil.SetClass(ref m_TitleStyle, value, true)) SetAllDirty(); } } /// <summary> - /// the icon of data. - /// 数据项图标样式。 - /// </summary> - public IconStyle iconStyle - { - get { return m_IconStyle; } - set { if (PropertyUtil.SetClass(ref m_IconStyle, value, true)) SetAllDirty(); } - } - /// <summary> /// 数据项里的数据维数。 /// </summary> public int showDataDimension { get { return m_ShowDataDimension; } set { m_ShowDataDimension = value; } } @@ -813,7 +761,8 @@ namespace XCharts if (PropertyUtil.SetStruct(ref m_Large, value)) { SetAllDirty(); - label.SetComponentDirty(); + if (label != null) + label.SetComponentDirty(); } } } @@ -828,7 +777,8 @@ namespace XCharts if (PropertyUtil.SetStruct(ref m_LargeThreshold, value)) { SetAllDirty(); - label.SetComponentDirty(); + if (label != null) + label.SetComponentDirty(); } } } @@ -922,11 +872,11 @@ namespace XCharts return m_VertsDirty || symbol.vertsDirty || lineStyle.vertsDirty || - lineArrow.vertsDirty || + (lineArrow != null && lineArrow.vertsDirty) || itemStyle.vertsDirty || - areaStyle.vertsDirty || - label.vertsDirty || - emphasis.vertsDirty; + (areaStyle != null && areaStyle.vertsDirty) || + (label != null && label.vertsDirty) || + (emphasis != null && emphasis.vertsDirty); } } @@ -936,13 +886,18 @@ namespace XCharts base.ClearVerticesDirty(); symbol.ClearVerticesDirty(); lineStyle.ClearVerticesDirty(); - lineArrow.ClearVerticesDirty(); itemStyle.ClearVerticesDirty(); - areaStyle.ClearVerticesDirty(); - label.ClearVerticesDirty(); - emphasis.ClearVerticesDirty(); titleStyle.ClearVerticesDirty(); - iconStyle.ClearVerticesDirty(); + if (iconStyle != null) + iconStyle.ClearVerticesDirty(); + if (areaStyle != null) + areaStyle.ClearVerticesDirty(); + if (label != null) + label.ClearVerticesDirty(); + if (emphasis != null) + emphasis.ClearVerticesDirty(); + if (lineArrow != null) + lineArrow?.ClearVerticesDirty(); } public override void ClearComponentDirty() @@ -950,13 +905,19 @@ namespace XCharts base.ClearComponentDirty(); symbol.ClearComponentDirty(); lineStyle.ClearComponentDirty(); - lineArrow.ClearComponentDirty(); itemStyle.ClearComponentDirty(); - areaStyle.ClearComponentDirty(); - label.ClearComponentDirty(); emphasis.ClearComponentDirty(); titleStyle.ClearComponentDirty(); - iconStyle.ClearComponentDirty(); + if (iconStyle != null) + iconStyle.ClearComponentDirty(); + if (areaStyle != null) + areaStyle.ClearComponentDirty(); + if (label != null) + label.ClearComponentDirty(); + if (emphasis != null) + emphasis.ClearComponentDirty(); + if (lineArrow != null) + lineArrow.ClearComponentDirty(); } /// <summary> /// Whether the serie is highlighted. diff --git a/Assets/XCharts/Runtime/Serie/SerieHandler.cs b/Assets/XCharts/Runtime/Serie/SerieHandler.cs index 9005c362..0bef7cd6 100644 --- a/Assets/XCharts/Runtime/Serie/SerieHandler.cs +++ b/Assets/XCharts/Runtime/Serie/SerieHandler.cs @@ -63,7 +63,7 @@ namespace XCharts if (m_InitedLabel) InternalRefreshLabel(); } - if (serie.labelDirty || serie.label.componentDirty) + if (serie.label != null && (serie.labelDirty || serie.label.componentDirty)) { serie.labelDirty = false; serie.label.ClearComponentDirty(); @@ -172,13 +172,23 @@ namespace XCharts protected bool AddSerieLabel(GameObject serieLabelRoot, Serie serie, SerieData serieData, ref int count) { - if (serieLabelRoot == null) return false; - if (serie.IsPerformanceMode()) return false; + if (serieLabelRoot == null) + return false; + if (serie.IsPerformanceMode()) + return false; + if (count == -1) count = serie.dataCount; var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); + if (serieLabel == null) + return false; + var serieEmphasisLabel = SerieHelper.GetSerieEmphasisLabel(serie, serieData); var iconStyle = SerieHelper.GetIconStyle(serie, serieData); - if (!serieLabel.show && (serieEmphasisLabel == null || !serieEmphasisLabel.show) && !iconStyle.show) return false; + + if (!serieLabel.show && (serieEmphasisLabel == null || !serieEmphasisLabel.show) + && (iconStyle != null && !iconStyle.show)) + return false; + var textName = ChartCached.GetSerieLabelName(s_SerieLabelObjectName, serie.index, serieData.index); var color = Color.grey; if (serie.useDataNameForColor) @@ -191,14 +201,16 @@ namespace XCharts color = !ChartHelper.IsClearColor(serieLabel.textStyle.color) ? serieLabel.textStyle.color : (Color)chart.theme.GetColor(serie.index); } + var iconWidth = iconStyle != null ? iconStyle.width : 20; + var iconHeight = iconStyle != null ? iconStyle.height : 20; var labelObj = SerieLabelPool.Get(textName, serieLabelRoot.transform, serieLabel, color, - iconStyle.width, iconStyle.height, chart.theme); + iconWidth, iconHeight, chart.theme); var iconImage = labelObj.transform.Find("Icon").GetComponent<Image>(); var isAutoSize = serieLabel.backgroundWidth == 0 || serieLabel.backgroundHeight == 0; var item = ChartHelper.GetOrAddComponent<ChartLabel>(labelObj); item.SetLabel(labelObj, isAutoSize, serieLabel.paddingLeftRight, serieLabel.paddingTopBottom); item.SetIcon(iconImage); - item.SetIconActive(iconStyle.show); + item.SetIconActive(iconStyle != null && iconStyle.show); item.color = serieLabel.textStyle.backgroundColor; serieData.labelObject = item; @@ -251,7 +263,7 @@ namespace XCharts var isIgnore = serie.IsIgnoreIndex(serieData.index); serieData.labelObject.SetPosition(serieData.context.position); serieData.labelObject.UpdateIcon(iconStyle); - if (serie.show && serieLabel.show && serieData.context.canShowLabel && !isIgnore) + if (serie.show && serieLabel != null && serieLabel.show && serieData.context.canShowLabel && !isIgnore) { var value = serieData.GetData(1); var content = SerieLabelHelper.GetFormatterContent(serie, serieData, value, total, @@ -259,7 +271,7 @@ namespace XCharts var invert = serieLabel.autoOffset && serie is Line && SerieHelper.IsDownPoint(serie, serieData.index) - && !serie.areaStyle.show; + && (serie.areaStyle == null || !serie.areaStyle.show); SerieLabelHelper.ResetLabel(serieData.labelObject.label, serieLabel, chart.theme, colorIndex); serieData.SetLabelActive(!isIgnore); serieData.labelObject.SetPosition(serieData.context.position + (invert ? -serieLabel.offset : serieLabel.offset)); diff --git a/Assets/XCharts/Runtime/Utilities/ReflectionUtil.cs b/Assets/XCharts/Runtime/Utilities/ReflectionUtil.cs index 5ab32767..0be701e5 100644 --- a/Assets/XCharts/Runtime/Utilities/ReflectionUtil.cs +++ b/Assets/XCharts/Runtime/Utilities/ReflectionUtil.cs @@ -12,6 +12,11 @@ namespace XCharts var method = list.GetType().GetMethod("Clear"); method.Invoke(list, new object[] { }); } + public static int InvokeListCount(object obj, FieldInfo field) + { + var list = field.GetValue(obj); + return (int)list.GetType().GetProperty("Count").GetValue(list); + } public static void InvokeListAdd(object obj, FieldInfo field, object item) { @@ -20,6 +25,14 @@ namespace XCharts method.Invoke(list, new object[] { item }); } + public static T InvokeListGet<T>(object obj, FieldInfo field, int i) + { + var list = field.GetValue(obj); + var item = list.GetType().GetProperty("Item").GetValue(list, new object[] { i }); + return (T)item; + } + + public static void InvokeListAddTo<T>(object obj, FieldInfo field, Action<T> callback) { var list = field.GetValue(obj);