mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-23 01:10:08 +00:00
增加Background背景组件
This commit is contained in:
@@ -34,12 +34,12 @@ namespace XCharts
|
||||
IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler,
|
||||
IDragHandler, IEndDragHandler, IScrollHandler
|
||||
{
|
||||
protected static readonly string s_BackgroundObjectName = "background";
|
||||
protected static readonly string s_TitleObjectName = "title";
|
||||
protected static readonly string s_SubTitleObjectName = "title_sub";
|
||||
protected static readonly string s_LegendObjectName = "legend";
|
||||
protected static readonly string s_SerieLabelObjectName = "label";
|
||||
protected static readonly string s_SerieTitleObjectName = "serie";
|
||||
protected static HideFlags s_HideFlags = HideFlags.HideAndDontSave;
|
||||
|
||||
[SerializeField] protected string m_ChartName;
|
||||
[SerializeField] protected float m_ChartWidth;
|
||||
@@ -48,10 +48,12 @@ namespace XCharts
|
||||
[SerializeField] protected float m_ChartY;
|
||||
[SerializeField] protected ThemeInfo m_ThemeInfo;
|
||||
[SerializeField] protected Title m_Title = Title.defaultTitle;
|
||||
[SerializeField] protected Background m_Background = Background.defaultBackground;
|
||||
[SerializeField] protected Legend m_Legend = Legend.defaultLegend;
|
||||
[SerializeField] protected Tooltip m_Tooltip = Tooltip.defaultTooltip;
|
||||
[SerializeField] protected Series m_Series = Series.defaultSeries;
|
||||
[SerializeField] protected Settings m_Settings = new Settings();
|
||||
[SerializeField] protected bool m_DebugMode = false;
|
||||
|
||||
protected Action<VertexHelper> m_OnCustomDrawCallback;
|
||||
protected Action<BaseChart, PointerEventData> m_OnPointerClick;
|
||||
@@ -79,16 +81,21 @@ namespace XCharts
|
||||
protected bool m_IsPlayingAnimation = false;
|
||||
protected List<string> m_LegendRealShowName = new List<string>();
|
||||
protected GameObject m_SerieLabelRoot;
|
||||
protected GameObject m_BackgroundRoot;
|
||||
protected bool m_ForceOpenRaycastTarget;
|
||||
protected bool m_IsControlledByLayout = false;
|
||||
|
||||
protected Vector2 chartAnchorMax { get { return m_ChartMinAnchor; } }
|
||||
protected Vector2 chartAnchorMin { get { return m_ChartMaxAnchor; } }
|
||||
protected Vector2 chartPivot { get { return m_ChartPivot; } }
|
||||
protected HideFlags chartHideFlags { get { return m_DebugMode ? HideFlags.None : HideFlags.HideInHierarchy; } }
|
||||
|
||||
private Theme m_CheckTheme = 0;
|
||||
private Vector3 m_LastLocalPosition;
|
||||
|
||||
protected virtual void InitComponent()
|
||||
{
|
||||
InitBackground();
|
||||
InitTitle();
|
||||
InitLegend();
|
||||
InitSerieLabel();
|
||||
@@ -102,8 +109,13 @@ namespace XCharts
|
||||
{
|
||||
m_ThemeInfo = ThemeInfo.Default;
|
||||
}
|
||||
if (transform.parent != null)
|
||||
{
|
||||
m_IsControlledByLayout = transform.parent.GetComponent<LayoutGroup>() != null;
|
||||
}
|
||||
raycastTarget = false;
|
||||
m_CheckTheme = m_ThemeInfo.theme;
|
||||
m_LastLocalPosition = transform.localPosition;
|
||||
UpdateSize();
|
||||
InitComponent();
|
||||
m_Series.AnimationReset();
|
||||
@@ -147,6 +159,12 @@ namespace XCharts
|
||||
if (m_ThemeInfo.vertsDirty) RefreshChart();
|
||||
m_ThemeInfo.ClearDirty();
|
||||
}
|
||||
if (m_Background.anyDirty)
|
||||
{
|
||||
if (m_Background.componentDirty) InitBackground();
|
||||
if (m_Background.vertsDirty) RefreshChart();
|
||||
m_Background.ClearDirty();
|
||||
}
|
||||
if (m_Title.anyDirty)
|
||||
{
|
||||
if (m_Title.componentDirty) InitTitle();
|
||||
@@ -215,6 +233,7 @@ namespace XCharts
|
||||
protected override void OnValidate()
|
||||
{
|
||||
m_ThemeInfo.SetAllDirty();
|
||||
m_Background.SetAllDirty();
|
||||
m_Title.SetAllDirty();
|
||||
m_Legend.SetAllDirty();
|
||||
m_Tooltip.SetAllDirty();
|
||||
@@ -231,6 +250,31 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private void InitBackground()
|
||||
{
|
||||
if (!m_Background.show || m_IsControlledByLayout)
|
||||
{
|
||||
if (m_BackgroundRoot)
|
||||
{
|
||||
m_BackgroundRoot.SetActive(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
var backgroundName = s_BackgroundObjectName + GetInstanceID();
|
||||
m_BackgroundRoot = ChartHelper.AddObject(backgroundName, transform.parent, m_ChartMinAnchor,
|
||||
m_ChartMaxAnchor, m_ChartPivot, m_ChartSizeDelta);
|
||||
//m_BackgroundRoot.hideFlags = chartHideFlags;
|
||||
var backgroundImage = ChartHelper.GetOrAddComponent<Image>(m_BackgroundRoot);
|
||||
var backgroundRect = m_BackgroundRoot.GetComponent<RectTransform>();
|
||||
backgroundRect.position = rectTransform.position;
|
||||
backgroundRect.SetSiblingIndex(rectTransform.GetSiblingIndex() - 1);
|
||||
|
||||
backgroundImage.sprite = m_Background.image;
|
||||
backgroundImage.type = m_Background.imageType;
|
||||
backgroundImage.color = m_Background.imageColor;
|
||||
m_BackgroundRoot.SetActive(m_Background.show);
|
||||
}
|
||||
|
||||
private void InitTitle()
|
||||
{
|
||||
m_Title.OnChanged();
|
||||
@@ -245,7 +289,7 @@ namespace XCharts
|
||||
var titleObject = ChartHelper.AddObject(s_TitleObjectName, transform, anchorMin, anchorMax,
|
||||
pivot, new Vector2(chartWidth, chartHeight));
|
||||
titleObject.transform.localPosition = titlePosition;
|
||||
titleObject.hideFlags = s_HideFlags;
|
||||
titleObject.hideFlags = chartHideFlags;
|
||||
ChartHelper.HideAllObject(titleObject);
|
||||
|
||||
var textFont = TitleHelper.GetTextFont(title, themeInfo);
|
||||
@@ -284,7 +328,7 @@ namespace XCharts
|
||||
var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax,
|
||||
pivot, new Vector2(chartWidth, chartHeight));
|
||||
legendObject.transform.localPosition = GetLegendPosition();
|
||||
legendObject.hideFlags = s_HideFlags;
|
||||
legendObject.hideFlags = chartHideFlags;
|
||||
SeriesHelper.UpdateSerieNameList(m_Series, ref m_LegendRealShowName);
|
||||
List<string> datas;
|
||||
if (m_Legend.show && m_Legend.data.Count > 0)
|
||||
@@ -379,7 +423,7 @@ namespace XCharts
|
||||
{
|
||||
m_SerieLabelRoot = ChartHelper.AddObject(s_SerieLabelObjectName, transform, m_ChartMinAnchor,
|
||||
m_ChartMaxAnchor, m_ChartPivot, m_ChartSizeDelta);
|
||||
m_SerieLabelRoot.hideFlags = s_HideFlags;
|
||||
m_SerieLabelRoot.hideFlags = chartHideFlags;
|
||||
SerieLabelPool.ReleaseAll(m_SerieLabelRoot.transform);
|
||||
int count = 0;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
@@ -431,7 +475,7 @@ namespace XCharts
|
||||
{
|
||||
var titleObject = ChartHelper.AddObject(s_SerieTitleObjectName, transform, m_ChartMinAnchor,
|
||||
m_ChartMaxAnchor, m_ChartPivot, new Vector2(chartWidth, chartHeight));
|
||||
titleObject.hideFlags = s_HideFlags;
|
||||
titleObject.hideFlags = chartHideFlags;
|
||||
ChartHelper.HideAllObject(titleObject);
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
@@ -465,7 +509,7 @@ namespace XCharts
|
||||
var tooltipObject = ChartHelper.AddObject("tooltip", transform, m_ChartMinAnchor,
|
||||
m_ChartMaxAnchor, m_ChartPivot, m_ChartSizeDelta);
|
||||
tooltipObject.transform.localPosition = Vector3.zero;
|
||||
tooltipObject.hideFlags = s_HideFlags;
|
||||
tooltipObject.hideFlags = chartHideFlags;
|
||||
DestroyImmediate(tooltipObject.GetComponent<Image>());
|
||||
var parent = tooltipObject.transform;
|
||||
var textStyle = m_Tooltip.textStyle;
|
||||
@@ -499,6 +543,11 @@ namespace XCharts
|
||||
{
|
||||
UpdateSize();
|
||||
}
|
||||
if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))
|
||||
{
|
||||
m_LastLocalPosition = transform.localPosition;
|
||||
OnLocalPositionChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSize()
|
||||
@@ -653,13 +702,20 @@ namespace XCharts
|
||||
|
||||
protected virtual void OnSizeChanged()
|
||||
{
|
||||
m_Background.SetAllDirty();
|
||||
m_Title.SetAllDirty();
|
||||
m_Legend.SetAllDirty();
|
||||
m_Tooltip.SetAllDirty();
|
||||
m_Series.SetLabelDirty();
|
||||
m_ReinitLabel = true;
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
protected virtual void OnLocalPositionChanged()
|
||||
{
|
||||
m_Background.SetAllDirty();
|
||||
}
|
||||
|
||||
protected virtual void OnThemeChanged()
|
||||
{
|
||||
}
|
||||
@@ -723,13 +779,14 @@ namespace XCharts
|
||||
Vector3 p2 = new Vector3(chartX + chartWidth, chartY + chartHeight);
|
||||
Vector3 p3 = new Vector3(chartX + chartWidth, chartY);
|
||||
Vector3 p4 = new Vector3(chartX, chartY);
|
||||
ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.backgroundColor);
|
||||
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout);
|
||||
ChartDrawer.DrawPolygon(vh, p1, p2, p3, p4, backgroundColor);
|
||||
}
|
||||
|
||||
public void DrawSymbol(VertexHelper vh, SerieSymbolType type, float symbolSize,
|
||||
float tickness, Vector3 pos, Color color, Color toColor, float gap, float[] cornerRadius)
|
||||
{
|
||||
var backgroundColor = m_ThemeInfo.backgroundColor;
|
||||
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout);
|
||||
var smoothness = m_Settings.cicleSmoothness;
|
||||
ChartDrawer.DrawSymbol(vh, type, symbolSize, tickness, pos, color, toColor, gap,
|
||||
cornerRadius, backgroundColor, smoothness);
|
||||
|
||||
@@ -156,7 +156,8 @@ namespace XCharts
|
||||
var cp2 = new Vector3(m_CoordinateX - yLineDiff, cpty);
|
||||
var cp3 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, cpty);
|
||||
var cp4 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_CoordinateY - xLineDiff);
|
||||
ChartDrawer.DrawPolygon(vh, cp1, cp2, cp3, cp4, m_ThemeInfo.backgroundColor);
|
||||
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout);
|
||||
ChartDrawer.DrawPolygon(vh, cp1, cp2, cp3, cp4, backgroundColor);
|
||||
|
||||
}
|
||||
else
|
||||
@@ -172,26 +173,27 @@ namespace XCharts
|
||||
var yLineDiff = yAxis0.axisLine.width;
|
||||
var xSplitDiff = xAxis0.splitLine.lineStyle.width;
|
||||
var ySplitDiff = yAxis0.splitLine.lineStyle.width;
|
||||
var backgroundColor = ThemeHelper.GetBackgroundColor(m_ThemeInfo, m_Background, m_IsControlledByLayout);
|
||||
var lp1 = new Vector3(m_ChartX, m_ChartY);
|
||||
var lp2 = new Vector3(m_ChartX, m_ChartY + chartHeight);
|
||||
var lp3 = new Vector3(m_CoordinateX - yLineDiff, m_ChartY + chartHeight);
|
||||
var lp4 = new Vector3(m_CoordinateX - yLineDiff, m_ChartY);
|
||||
ChartDrawer.DrawPolygon(vh, lp1, lp2, lp3, lp4, m_ThemeInfo.backgroundColor);
|
||||
ChartDrawer.DrawPolygon(vh, lp1, lp2, lp3, lp4, backgroundColor);
|
||||
var rp1 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_ChartY);
|
||||
var rp2 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_ChartY + chartHeight);
|
||||
var rp3 = new Vector3(m_ChartX + chartWidth, m_ChartY + chartHeight);
|
||||
var rp4 = new Vector3(m_ChartX + chartWidth, m_ChartY);
|
||||
ChartDrawer.DrawPolygon(vh, rp1, rp2, rp3, rp4, m_ThemeInfo.backgroundColor);
|
||||
ChartDrawer.DrawPolygon(vh, rp1, rp2, rp3, rp4, backgroundColor);
|
||||
var up1 = new Vector3(m_CoordinateX - yLineDiff, m_CoordinateY + m_CoordinateHeight + ySplitDiff);
|
||||
var up2 = new Vector3(m_CoordinateX - yLineDiff, m_ChartY + chartHeight);
|
||||
var up3 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_ChartY + chartHeight);
|
||||
var up4 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_CoordinateY + m_CoordinateHeight + ySplitDiff);
|
||||
ChartDrawer.DrawPolygon(vh, up1, up2, up3, up4, m_ThemeInfo.backgroundColor);
|
||||
ChartDrawer.DrawPolygon(vh, up1, up2, up3, up4, backgroundColor);
|
||||
var dp1 = new Vector3(m_CoordinateX - yLineDiff, m_ChartY);
|
||||
var dp2 = new Vector3(m_CoordinateX - yLineDiff, m_CoordinateY - xLineDiff);
|
||||
var dp3 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_CoordinateY - xLineDiff);
|
||||
var dp4 = new Vector3(m_CoordinateX + m_CoordinateWidth + xSplitDiff, m_ChartY);
|
||||
ChartDrawer.DrawPolygon(vh, dp1, dp2, dp3, dp4, m_ThemeInfo.backgroundColor);
|
||||
ChartDrawer.DrawPolygon(vh, dp1, dp2, dp3, dp4, backgroundColor);
|
||||
}
|
||||
|
||||
protected virtual void DrawSerie(VertexHelper vh)
|
||||
@@ -515,7 +517,7 @@ namespace XCharts
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
axisObj.transform.localPosition = Vector3.zero;
|
||||
axisObj.SetActive(yAxis.show && yAxis.axisLabel.show);
|
||||
axisObj.hideFlags = s_HideFlags;
|
||||
axisObj.hideFlags = chartHideFlags;
|
||||
ChartHelper.HideAllObject(axisObj);
|
||||
var labelColor = ChartHelper.IsClearColor(yAxis.axisLabel.color) ?
|
||||
(Color)m_ThemeInfo.axisTextColor :
|
||||
@@ -621,7 +623,7 @@ namespace XCharts
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
axisObj.transform.localPosition = Vector3.zero;
|
||||
axisObj.SetActive(xAxis.show && xAxis.axisLabel.show);
|
||||
axisObj.hideFlags = s_HideFlags;
|
||||
axisObj.hideFlags = chartHideFlags;
|
||||
ChartHelper.HideAllObject(axisObj);
|
||||
var labelColor = ChartHelper.IsClearColor(xAxis.axisLabel.color) ?
|
||||
(Color)m_ThemeInfo.axisTextColor :
|
||||
@@ -703,7 +705,7 @@ namespace XCharts
|
||||
var dataZoomObject = ChartHelper.AddObject(s_DefaultDataZoom, transform, chartAnchorMin,
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
dataZoomObject.transform.localPosition = Vector3.zero;
|
||||
dataZoomObject.hideFlags = s_HideFlags;
|
||||
dataZoomObject.hideFlags = chartHideFlags;
|
||||
ChartHelper.HideAllObject(dataZoomObject);
|
||||
var startLabel = ChartHelper.AddTextObject(s_DefaultDataZoom + "start",
|
||||
dataZoomObject.transform, m_ThemeInfo.font, m_ThemeInfo.dataZoomTextColor, TextAnchor.MiddleRight,
|
||||
|
||||
20
Runtime/Internal/Helper/ThemeHelper.cs
Normal file
20
Runtime/Internal/Helper/ThemeHelper.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
internal static class ThemeHelper
|
||||
{
|
||||
public static Color GetBackgroundColor(ThemeInfo themeInfo, Background background, bool m_IsControlledByLayout)
|
||||
{
|
||||
if (!m_IsControlledByLayout && background.show && background.hideThemeBackgroundColor) return Color.clear;
|
||||
else return themeInfo.backgroundColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Helper/ThemeHelper.cs.meta
Normal file
11
Runtime/Internal/Helper/ThemeHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5093880c2dbba4c01bb0231653ed3252
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user