diff --git a/Documentation~/en/api.md b/Documentation~/en/api.md index e648ee85..b02863f9 100644 --- a/Documentation~/en/api.md +++ b/Documentation~/en/api.md @@ -159,6 +159,7 @@ slug: /api - [MainComponentContext](#maincomponentcontext) - [MainComponentHandler](#maincomponenthandler) - [MainComponentHandler<T>](#maincomponenthandlert) +- [MainComponentHandler<Title>](#maincomponenthandlertitle) - [MarkArea](#markarea) - [MarkAreaData](#markareadata) - [MarkAreaType](#markareatype) @@ -257,6 +258,7 @@ slug: /api - [ThemeStyle](#themestyle) - [ThemeType](#themetype) - [Title](#title) +- [TitleHandler](#titlehandler) - [TitleStyle](#titlestyle) - [TitleTheme](#titletheme) - [Tooltip](#tooltip) @@ -2035,10 +2037,6 @@ public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount, int gridIndex) where T : Serie -### BaseChart.GetTitlePosition - -public Vector3 GetTitlePosition(Title title) - ### BaseChart.GetVisualMapOfSerie public VisualMap GetVisualMapOfSerie(Serie serie) @@ -2558,6 +2556,10 @@ public string warningInfo public string CheckWarning() 检测警告信息。 +### BaseGraph.GetTitlePosition + +public Vector3 GetTitlePosition(Title title) + ### BaseGraph.LocalPointToScreenPoint public Vector2 LocalPointToScreenPoint(Vector2 localPoint) @@ -2886,6 +2888,14 @@ public static string ColorToStr(Color color) public static string FloatToStr(double value, string numericFormatter = "F", int precision = 0) +### ChartCached.GetAxisLabelName + +public static string GetAxisLabelName(int index) + +### ChartCached.GetComponentObjectName + +public static string GetComponentObjectName(MainComponent component) + ### ChartCached.GetSerieLabelName public static string GetSerieLabelName(string prefix, int i, int j) @@ -2894,6 +2904,14 @@ public static string GetSerieLabelName(string prefix, int i, int j) public static string GetString(string prefix, int suffix) +### ChartCached.GetTypeName + +public static string GetTypeName(Type type) + +### ChartCached.GetTypeName<T> + +public static string GetTypeName<T>() + ### ChartCached.IntToStr public static string IntToStr(int value, string numericFormatter = "") @@ -5750,6 +5768,10 @@ class in XCharts.Runtime / Inherits from: [MainComponentHandler](#maincomponenth public T component +## MainComponentHandler<Title> + +class in / Subclasses: [TitleHandler](#titlehandler) + ## MarkArea class in XCharts.Runtime / Inherits from: [MainComponent](#maincomponent) @@ -8676,6 +8698,30 @@ public override void ClearComponentDirty() public void OnChanged() +## TitleHandler + +class in XCharts.Runtime / Inherits from: [MainComponentHandler<Title>](#maincomponenthandlertitle) + +### TitleHandler.AddSubTitleLabel + +public static ChartLabel AddSubTitleLabel(Transform parent, Title title, ComponentTheme componentTheme, BaseChart chart = null) + +### TitleHandler.AddTitleLabel + +public static ChartLabel AddTitleLabel(Transform parent, Title title, ComponentTheme componentTheme, BaseChart chart = null) + +### TitleHandler.AddTitleObject + +public static GameObject AddTitleObject(BaseGraph graph, Title title, ComponentTheme componentTheme, int titleSiblingIndex, string objectName = null) + +### TitleHandler.InitComponent + +public override void InitComponent() + +### TitleHandler.OnSerieDataUpdate + +public override void OnSerieDataUpdate(int serieIndex) + ## TitleStyle class in XCharts.Runtime / Inherits from: [LabelStyle](#labelstyle),[ISerieDataComponent](#iseriedatacomponent),[ISerieComponent](#iseriecomponent) diff --git a/Documentation~/zh/api.md b/Documentation~/zh/api.md index 2bf6a998..21992a61 100644 --- a/Documentation~/zh/api.md +++ b/Documentation~/zh/api.md @@ -159,6 +159,7 @@ slug: /api - [MainComponentContext](#maincomponentcontext) - [MainComponentHandler](#maincomponenthandler) - [MainComponentHandler<T>](#maincomponenthandlert) +- [MainComponentHandler<Title>](#maincomponenthandlertitle) - [MarkArea](#markarea) - [MarkAreaData](#markareadata) - [MarkAreaType](#markareatype) @@ -257,6 +258,7 @@ slug: /api - [ThemeStyle](#themestyle) - [ThemeType](#themetype) - [Title](#title) +- [TitleHandler](#titlehandler) - [TitleStyle](#titlestyle) - [TitleTheme](#titletheme) - [Tooltip](#tooltip) @@ -2035,10 +2037,6 @@ public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount, int gridIndex) where T : Serie -### BaseChart.GetTitlePosition - -public Vector3 GetTitlePosition(Title title) - ### BaseChart.GetVisualMapOfSerie public VisualMap GetVisualMapOfSerie(Serie serie) @@ -2558,6 +2556,10 @@ public string warningInfo public string CheckWarning() 检测警告信息。 +### BaseGraph.GetTitlePosition + +public Vector3 GetTitlePosition(Title title) + ### BaseGraph.LocalPointToScreenPoint public Vector2 LocalPointToScreenPoint(Vector2 localPoint) @@ -2886,6 +2888,14 @@ public static string ColorToStr(Color color) public static string FloatToStr(double value, string numericFormatter = "F", int precision = 0) +### ChartCached.GetAxisLabelName + +public static string GetAxisLabelName(int index) + +### ChartCached.GetComponentObjectName + +public static string GetComponentObjectName(MainComponent component) + ### ChartCached.GetSerieLabelName public static string GetSerieLabelName(string prefix, int i, int j) @@ -2894,6 +2904,14 @@ public static string GetSerieLabelName(string prefix, int i, int j) public static string GetString(string prefix, int suffix) +### ChartCached.GetTypeName + +public static string GetTypeName(Type type) + +### ChartCached.GetTypeName<T> + +public static string GetTypeName<T>() + ### ChartCached.IntToStr public static string IntToStr(int value, string numericFormatter = "") @@ -5750,6 +5768,10 @@ class in XCharts.Runtime / 继承自: [MainComponentHandler](#maincomponenthandl public T component +## MainComponentHandler<Title> + +class in / 子类: [TitleHandler](#titlehandler) + ## MarkArea class in XCharts.Runtime / 继承自: [MainComponent](#maincomponent) @@ -8676,6 +8698,30 @@ public override void ClearComponentDirty() public void OnChanged() +## TitleHandler + +class in XCharts.Runtime / 继承自: [MainComponentHandler<Title>](#maincomponenthandlertitle) + +### TitleHandler.AddSubTitleLabel + +public static ChartLabel AddSubTitleLabel(Transform parent, Title title, ComponentTheme componentTheme, BaseChart chart = null) + +### TitleHandler.AddTitleLabel + +public static ChartLabel AddTitleLabel(Transform parent, Title title, ComponentTheme componentTheme, BaseChart chart = null) + +### TitleHandler.AddTitleObject + +public static GameObject AddTitleObject(BaseGraph graph, Title title, ComponentTheme componentTheme, int titleSiblingIndex, string objectName = null) + +### TitleHandler.InitComponent + +public override void InitComponent() + +### TitleHandler.OnSerieDataUpdate + +public override void OnSerieDataUpdate(int serieIndex) + ## TitleStyle class in XCharts.Runtime / 继承自: [LabelStyle](#labelstyle),[ISerieDataComponent](#iseriedatacomponent),[ISerieComponent](#iseriecomponent) diff --git a/Runtime/Component/Title/Title.cs b/Runtime/Component/Title/Title.cs index 2ed68335..f0cf1239 100644 --- a/Runtime/Component/Title/Title.cs +++ b/Runtime/Component/Title/Title.cs @@ -8,7 +8,7 @@ namespace XCharts.Runtime /// ||标题组件,包含主标题和副标题。 /// [Serializable] - [ComponentHandler(typeof(TitleHander), true)] + [ComponentHandler(typeof(TitleHandler), true)] public class Title : MainComponent, IPropertyChanged { [SerializeField] private bool m_Show = true; diff --git a/Runtime/Component/Title/TitleHandler.cs b/Runtime/Component/Title/TitleHandler.cs index 9f789aba..90fb52b1 100644 --- a/Runtime/Component/Title/TitleHandler.cs +++ b/Runtime/Component/Title/TitleHandler.cs @@ -3,7 +3,7 @@ using UnityEngine; namespace XCharts.Runtime { [UnityEngine.Scripting.Preserve] - internal sealed class TitleHander : MainComponentHandler + public sealed class TitleHandler : MainComponentHandler<Title> { private static readonly string s_TitleObjectName = "title"; private static readonly string s_SubTitleObjectName = "title_sub"; @@ -14,50 +14,68 @@ namespace XCharts.Runtime { var title = component; title.painter = null; - title.refreshComponent = delegate() + title.refreshComponent = delegate () { title.OnChanged(); - var anchorMin = title.location.runtimeAnchorMin; - var anchorMax = title.location.runtimeAnchorMax; - var pivot = title.location.runtimePivot; - var objName = ChartCached.GetComponentObjectName(title); - var titleObject = ChartHelper.AddObject(objName, chart.transform, anchorMin, anchorMax, - pivot, chart.chartSizeDelta, -1, chart.childrenNodeNames); - title.gameObject = titleObject; - title.gameObject.transform.SetSiblingIndex(chart.m_PainterUpper.transform.GetSiblingIndex() + 1); - anchorMin = title.location.runtimeAnchorMin; - anchorMax = title.location.runtimeAnchorMax; - pivot = title.location.runtimePivot; - var fontSize = title.labelStyle.textStyle.GetFontSize(chart.theme.title); - ChartHelper.UpdateRectTransform(titleObject, anchorMin, anchorMax, pivot, new Vector2(chart.chartWidth, chart.chartHeight)); - var titlePosition = chart.GetTitlePosition(title); - var subTitlePosition = -new Vector3(0, fontSize + title.itemGap, 0); + var titleObject = AddTitleObject(chart, title, chart.theme.title, chart.m_PainterUpper.transform.GetSiblingIndex() + 1); - titleObject.transform.localPosition = titlePosition; - titleObject.hideFlags = chart.chartHideFlags; - ChartHelper.HideAllObject(titleObject); + m_LabelObject = AddTitleLabel(titleObject.transform, title, chart.theme.title, chart); + m_SubLabelObject = AddSubTitleLabel(titleObject.transform, title, chart.theme.subTitle, chart); - m_LabelObject = ChartHelper.AddChartLabel(s_TitleObjectName, titleObject.transform, title.labelStyle, chart.theme.title, - GetTitleText(title), Color.clear, title.location.runtimeTextAlignment); - m_LabelObject.SetActive(title.show && title.labelStyle.show, true); - - m_SubLabelObject = ChartHelper.AddChartLabel(s_SubTitleObjectName, titleObject.transform, title.subLabelStyle, chart.theme.subTitle, - GetSubTitleText(title), Color.clear, title.location.runtimeTextAlignment); - m_SubLabelObject.SetActive(title.show && title.subLabelStyle.show, true); - m_SubLabelObject.transform.localPosition = subTitlePosition + title.subLabelStyle.offset; }; title.refreshComponent(); } + public static GameObject AddTitleObject(BaseGraph graph, Title title, ComponentTheme componentTheme, int titleSiblingIndex, string objectName = null) + { + var anchorMin = title.location.runtimeAnchorMin; + var anchorMax = title.location.runtimeAnchorMax; + var pivot = title.location.runtimePivot; + var objName = objectName == null ? ChartCached.GetComponentObjectName(title) : objectName; + var titleObject = ChartHelper.AddObject(objName, graph.transform, anchorMin, anchorMax, + pivot, graph.graphSizeDelta, -1, graph.childrenNodeNames); + title.gameObject = titleObject; + title.gameObject.transform.SetSiblingIndex(titleSiblingIndex); + anchorMin = title.location.runtimeAnchorMin; + anchorMax = title.location.runtimeAnchorMax; + pivot = title.location.runtimePivot; + + ChartHelper.UpdateRectTransform(titleObject, anchorMin, anchorMax, pivot, new Vector2(graph.graphWidth, graph.graphHeight)); + var titlePosition = graph.GetTitlePosition(title); + titleObject.transform.localPosition = titlePosition; + titleObject.hideFlags = graph.chartHideFlags; + ChartHelper.HideAllObject(titleObject); + return titleObject; + } + + public static ChartLabel AddTitleLabel(Transform parent, Title title, ComponentTheme componentTheme, BaseChart chart = null) + { + var m_LabelObject = ChartHelper.AddChartLabel(s_TitleObjectName, parent, title.labelStyle, componentTheme, + GetTitleText(title, chart), Color.clear, title.location.runtimeTextAlignment); + m_LabelObject.SetActive(title.show && title.labelStyle.show, true); + return m_LabelObject; + } + + public static ChartLabel AddSubTitleLabel(Transform parent, Title title, ComponentTheme componentTheme, BaseChart chart = null) + { + var fontSize = title.labelStyle.textStyle.GetFontSize(componentTheme); + var subTitlePosition = -new Vector3(0, fontSize + title.itemGap, 0); + var m_SubLabelObject = ChartHelper.AddChartLabel(s_SubTitleObjectName, parent, title.subLabelStyle, componentTheme, + GetSubTitleText(title, chart), Color.clear, title.location.runtimeTextAlignment); + m_SubLabelObject.SetActive(title.show && title.subLabelStyle.show, true); + m_SubLabelObject.transform.localPosition = subTitlePosition + title.subLabelStyle.offset; + return m_SubLabelObject; + } + public override void OnSerieDataUpdate(int serieIndex) { if (m_LabelObject != null && FormatterHelper.NeedFormat(component.text)) - m_LabelObject.SetText(GetTitleText(component)); + m_LabelObject.SetText(GetTitleText(component, chart)); if (m_SubLabelObject != null && FormatterHelper.NeedFormat(component.subText)) - m_SubLabelObject.SetText(GetSubTitleText(component)); + m_SubLabelObject.SetText(GetSubTitleText(component, chart)); } - private string GetTitleText(Title title) + private static string GetTitleText(Title title, BaseChart chart) { if (FormatterHelper.NeedFormat(title.text)) { @@ -71,7 +89,7 @@ namespace XCharts.Runtime } } - private string GetSubTitleText(Title title) + private static string GetSubTitleText(Title title, BaseChart chart) { if (FormatterHelper.NeedFormat(title.subText)) { diff --git a/Runtime/Internal/BaseChart.API.cs b/Runtime/Internal/BaseChart.API.cs index 63cce895..bcd8e143 100644 --- a/Runtime/Internal/BaseChart.API.cs +++ b/Runtime/Internal/BaseChart.API.cs @@ -588,11 +588,6 @@ namespace XCharts.Runtime } } - public Vector3 GetTitlePosition(Title title) - { - return chartPosition + title.location.GetPosition(chartWidth, chartHeight); - } - public int GetLegendRealShowNameIndex(string name) { return m_LegendRealShowName.IndexOf(name); diff --git a/Runtime/Internal/BaseGraph.API.cs b/Runtime/Internal/BaseGraph.API.cs index 90c9adce..65aaeb13 100644 --- a/Runtime/Internal/BaseGraph.API.cs +++ b/Runtime/Internal/BaseGraph.API.cs @@ -219,5 +219,10 @@ namespace XCharts.Runtime yield return new WaitForEndOfFrame(); ChartHelper.SaveAsImage(rectTransform, canvas, imageType, path); } + + public Vector3 GetTitlePosition(Title title) + { + return graphPosition + title.location.GetPosition(graphWidth, graphHeight); + } } } \ No newline at end of file diff --git a/Runtime/Internal/Utilities/ChartCached.cs b/Runtime/Internal/Utilities/ChartCached.cs index 38ed92eb..aa2e6e97 100644 --- a/Runtime/Internal/Utilities/ChartCached.cs +++ b/Runtime/Internal/Utilities/ChartCached.cs @@ -200,7 +200,7 @@ namespace XCharts.Runtime return s_StringIntDict[prefix][suffix]; } - internal static string GetComponentObjectName(MainComponent component) + public static string GetComponentObjectName(MainComponent component) { Dictionary<int, string> dict; var type = component.GetType(); @@ -224,7 +224,7 @@ namespace XCharts.Runtime } } - internal static string GetAxisLabelName(int index) + public static string GetAxisLabelName(int index) { string name; if (!s_AxisLabelName.TryGetValue(index, out name)) @@ -239,12 +239,12 @@ namespace XCharts.Runtime } } - internal static string GetTypeName<T>() + public static string GetTypeName<T>() { return GetTypeName(typeof(T)); } - internal static string GetTypeName(Type type) + public static string GetTypeName(Type type) { if (s_TypeName.ContainsKey(type)) return s_TypeName[type]; else