mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-24 09:50:15 +00:00
3.0 - guage chart
This commit is contained in:
@@ -5,7 +5,7 @@ namespace XCharts
|
||||
{
|
||||
[System.Serializable]
|
||||
[SerieHandler(typeof(RingHandler), true)]
|
||||
[SerieExtraComponent(typeof(LabelStyle), typeof(Emphasis))]
|
||||
[SerieExtraComponent(typeof(LabelStyle), typeof(TitleStyle), typeof(Emphasis))]
|
||||
public class Ring : Serie
|
||||
{
|
||||
public override bool useDataNameForColor { get { return true; } }
|
||||
@@ -14,13 +14,17 @@ namespace XCharts
|
||||
var serie = chart.AddSerie<Ring>(serieName);
|
||||
serie.roundCap = true;
|
||||
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}%";
|
||||
serie.label.textStyle.fontSize = 28;
|
||||
|
||||
serie.AddExtraComponent<TitleStyle>();
|
||||
serie.titleStyle.show = false;
|
||||
serie.titleStyle.textStyle.offset = new Vector2(0, 30);
|
||||
|
||||
var value = Random.Range(30, 90);
|
||||
var max = 100;
|
||||
chart.AddData(serie.index, value, max, "data1");
|
||||
|
||||
@@ -247,12 +247,12 @@ namespace XCharts
|
||||
case LabelStyle.Position.Bottom:
|
||||
var px1 = Mathf.Sin(startAngle * Mathf.Deg2Rad) * centerRadius;
|
||||
var py1 = Mathf.Cos(startAngle * Mathf.Deg2Rad) * centerRadius;
|
||||
var xDiff = serie.clockwise ? -label.margin : label.margin;
|
||||
var xDiff = serie.clockwise ? -label.distance : label.distance;
|
||||
serieData.context.labelPosition = serie.context.center + new Vector3(px1 + xDiff, py1);
|
||||
break;
|
||||
case LabelStyle.Position.Top:
|
||||
startAngle += serie.clockwise ? -label.margin : label.margin;
|
||||
toAngle += serie.clockwise ? label.margin : -label.margin;
|
||||
startAngle += serie.clockwise ? -label.distance : label.distance;
|
||||
toAngle += serie.clockwise ? label.distance : -label.distance;
|
||||
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);
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace XCharts
|
||||
{typeof(AreaStyle), "m_AreaStyles"},
|
||||
{typeof(IconStyle), "m_IconStyles"},
|
||||
{typeof(Emphasis), "m_Emphases"},
|
||||
{typeof(TitleStyle), "m_TitleStyles"},
|
||||
};
|
||||
|
||||
[SerializeField] private List<LabelStyle> m_Labels = new List<LabelStyle>();
|
||||
@@ -24,6 +25,7 @@ namespace XCharts
|
||||
[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<TitleStyle> m_TitleStyles = new List<TitleStyle>();
|
||||
[SerializeField] private List<Emphasis> m_Emphases = new List<Emphasis>();
|
||||
|
||||
/// <summary>
|
||||
@@ -56,6 +58,11 @@ namespace XCharts
|
||||
/// 数据项图标样式。
|
||||
/// </summary>
|
||||
public IconStyle iconStyle { get { return m_IconStyles.Count > 0 ? m_IconStyles[0] : null; } }
|
||||
/// <summary>
|
||||
/// the icon of data.
|
||||
/// 数据项标题样式。
|
||||
/// </summary>
|
||||
public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
|
||||
|
||||
public void RemoveAllExtraComponent()
|
||||
{
|
||||
|
||||
@@ -235,7 +235,6 @@ namespace XCharts
|
||||
[SerializeField] private SymbolStyle m_Symbol = new SymbolStyle();
|
||||
[SerializeField] private AnimationStyle m_Animation = new AnimationStyle();
|
||||
[SerializeField] private ItemStyle m_ItemStyle = new ItemStyle();
|
||||
[SerializeField] private TitleStyle m_TitleStyle = new TitleStyle();
|
||||
[SerializeField] private List<SerieData> m_Data = new List<SerieData>();
|
||||
|
||||
[NonSerialized] internal int m_FilterStart;
|
||||
@@ -710,14 +709,6 @@ namespace XCharts
|
||||
set { if (PropertyUtil.SetClass(ref m_ItemStyle, value, true)) SetVerticesDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// 标题样式。
|
||||
/// </summary>
|
||||
public TitleStyle titleStyle
|
||||
{
|
||||
get { return m_TitleStyle; }
|
||||
set { if (PropertyUtil.SetClass(ref m_TitleStyle, value, true)) SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// 数据项里的数据维数。
|
||||
/// </summary>
|
||||
public int showDataDimension { get { return m_ShowDataDimension; } set { m_ShowDataDimension = value; } }
|
||||
@@ -879,18 +870,25 @@ namespace XCharts
|
||||
itemStyle.vertsDirty ||
|
||||
(areaStyle != null && areaStyle.vertsDirty) ||
|
||||
(label != null && label.vertsDirty) ||
|
||||
(emphasis != null && emphasis.vertsDirty);
|
||||
(emphasis != null && emphasis.vertsDirty) ||
|
||||
(titleStyle != null && titleStyle.vertsDirty);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool componentDirty { get { return m_ComponentDirty || titleStyle.componentDirty; } }
|
||||
public override bool componentDirty
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_ComponentDirty
|
||||
|| (titleStyle != null && titleStyle.componentDirty);
|
||||
}
|
||||
}
|
||||
public override void ClearVerticesDirty()
|
||||
{
|
||||
base.ClearVerticesDirty();
|
||||
symbol.ClearVerticesDirty();
|
||||
lineStyle.ClearVerticesDirty();
|
||||
itemStyle.ClearVerticesDirty();
|
||||
titleStyle.ClearVerticesDirty();
|
||||
if (iconStyle != null)
|
||||
iconStyle.ClearVerticesDirty();
|
||||
if (areaStyle != null)
|
||||
@@ -901,6 +899,8 @@ namespace XCharts
|
||||
emphasis.ClearVerticesDirty();
|
||||
if (lineArrow != null)
|
||||
lineArrow.ClearVerticesDirty();
|
||||
if (titleStyle != null)
|
||||
titleStyle.ClearVerticesDirty();
|
||||
}
|
||||
|
||||
public override void ClearComponentDirty()
|
||||
@@ -909,8 +909,6 @@ namespace XCharts
|
||||
symbol.ClearComponentDirty();
|
||||
lineStyle.ClearComponentDirty();
|
||||
itemStyle.ClearComponentDirty();
|
||||
emphasis.ClearComponentDirty();
|
||||
titleStyle.ClearComponentDirty();
|
||||
if (iconStyle != null)
|
||||
iconStyle.ClearComponentDirty();
|
||||
if (areaStyle != null)
|
||||
@@ -921,6 +919,8 @@ namespace XCharts
|
||||
emphasis.ClearComponentDirty();
|
||||
if (lineArrow != null)
|
||||
lineArrow.ClearComponentDirty();
|
||||
if (titleStyle != null)
|
||||
titleStyle.ClearComponentDirty();
|
||||
}
|
||||
|
||||
public override void SetAllDirty()
|
||||
|
||||
@@ -39,11 +39,11 @@ namespace XCharts
|
||||
/// <summary>
|
||||
/// 内半径
|
||||
/// </summary>
|
||||
public float insideRadius { get; internal set; }
|
||||
public float insideRadius { get; set; }
|
||||
/// <summary>
|
||||
/// 外半径
|
||||
/// </summary>
|
||||
public float outsideRadius { get; internal set; }
|
||||
public float outsideRadius { get; set; }
|
||||
/// <summary>
|
||||
/// 最大值
|
||||
/// </summary>
|
||||
|
||||
@@ -25,12 +25,14 @@ namespace XCharts
|
||||
[SerializeField] private List<IconStyle> m_IconStyles = new List<IconStyle>();
|
||||
[SerializeField] private List<LineStyle> m_LineStyles = new List<LineStyle>();
|
||||
[SerializeField] private List<AreaStyle> m_AreaStyles = new List<AreaStyle>();
|
||||
[SerializeField] private List<TitleStyle> m_TitleStyles = new List<TitleStyle>();
|
||||
[SerializeField] private List<double> m_Data = new List<double>();
|
||||
[SerializeField] private List<int> m_Children = new List<int>();
|
||||
|
||||
[NonSerialized] public SerieDataContext context = new SerieDataContext();
|
||||
[NonSerialized] public InteractData interact = new InteractData();
|
||||
public ChartLabel labelObject { get; set; }
|
||||
public ChartLabel titleObject { get; set; }
|
||||
|
||||
private bool m_Show = true;
|
||||
|
||||
@@ -81,6 +83,7 @@ namespace XCharts
|
||||
public SymbolStyle symbol { get { return m_Symbols.Count > 0 ? m_Symbols[0] : null; } }
|
||||
public LineStyle lineStyle { get { return m_LineStyles.Count > 0 ? m_LineStyles[0] : null; } }
|
||||
public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } }
|
||||
public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
|
||||
/// <summary>
|
||||
/// 是否忽略数据。当为 true 时,数据不进行绘制。
|
||||
/// </summary>
|
||||
@@ -130,6 +133,7 @@ namespace XCharts
|
||||
m_Symbols.Clear();
|
||||
m_LineStyles.Clear();
|
||||
m_AreaStyles.Clear();
|
||||
m_TitleStyles.Clear();
|
||||
}
|
||||
|
||||
public T GetOrAddComponent<T>() where T : ChildComponent
|
||||
@@ -183,6 +187,12 @@ namespace XCharts
|
||||
m_AreaStyles.Add(new AreaStyle() { show = true });
|
||||
return m_AreaStyles[0] as T;
|
||||
}
|
||||
else if (type == typeof(TitleStyle))
|
||||
{
|
||||
if (m_TitleStyles.Count == 0)
|
||||
m_TitleStyles.Add(new TitleStyle() { show = true });
|
||||
return m_TitleStyles[0] as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.Exception("SerieData not support component:" + type);
|
||||
@@ -199,6 +209,7 @@ namespace XCharts
|
||||
m_Emphases.Clear();
|
||||
m_LineStyles.Clear();
|
||||
m_AreaStyles.Clear();
|
||||
m_TitleStyles.Clear();
|
||||
}
|
||||
|
||||
public void RemoveComponent<T>() where T : ISerieDataComponent
|
||||
@@ -220,6 +231,8 @@ namespace XCharts
|
||||
m_LineStyles.Clear();
|
||||
else if (type == typeof(AreaStyle))
|
||||
m_AreaStyles.Clear();
|
||||
else if (type == typeof(TitleStyle))
|
||||
m_TitleStyles.Clear();
|
||||
else
|
||||
throw new System.Exception("SerieData not support component:" + type);
|
||||
}
|
||||
@@ -427,6 +440,11 @@ namespace XCharts
|
||||
m_PolygonPoints.Add(p3);
|
||||
m_PolygonPoints.Add(p4);
|
||||
}
|
||||
public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 p5)
|
||||
{
|
||||
SetPolygon(p1, p2, p3, p4);
|
||||
m_PolygonPoints.Add(p5);
|
||||
}
|
||||
|
||||
public bool IsInPolygon(Vector2 p)
|
||||
{
|
||||
|
||||
@@ -45,7 +45,8 @@ namespace XCharts
|
||||
public abstract class SerieHandler<T> : SerieHandler where T : Serie
|
||||
{
|
||||
private static readonly string s_SerieLabelObjectName = "label";
|
||||
private static readonly string s_SerieTitleObjectName = "serie";
|
||||
private static readonly string s_SerieTitleObjectName = "title";
|
||||
private static readonly string s_SerieRootObjectName = "serie";
|
||||
protected GameObject m_SerieRoot;
|
||||
protected bool m_InitedLabel;
|
||||
protected bool m_NeedInitComponent;
|
||||
@@ -82,7 +83,7 @@ namespace XCharts
|
||||
serie.label.ClearComponentDirty();
|
||||
InitSerieLabel();
|
||||
}
|
||||
if (serie.titleDirty || serie.titleStyle.componentDirty)
|
||||
if (serie.titleStyle != null && (serie.titleDirty || serie.titleStyle.componentDirty))
|
||||
{
|
||||
serie.titleDirty = false;
|
||||
serie.titleStyle.ClearComponentDirty();
|
||||
@@ -169,7 +170,7 @@ namespace XCharts
|
||||
private void InitRoot()
|
||||
{
|
||||
if (m_SerieRoot != null) return;
|
||||
var objName = s_SerieTitleObjectName + "_" + serie.index;
|
||||
var objName = s_SerieRootObjectName + "_" + serie.index;
|
||||
m_SerieRoot = ChartHelper.AddObject(objName, chart.transform, chart.chartMinAnchor,
|
||||
chart.chartMaxAnchor, chart.chartPivot, chart.chartSizeDelta);
|
||||
m_SerieRoot.hideFlags = chart.chartHideFlags;
|
||||
@@ -198,6 +199,7 @@ namespace XCharts
|
||||
count++;
|
||||
}
|
||||
}
|
||||
RefreshLabelInternal();
|
||||
}
|
||||
|
||||
protected bool AddSerieLabel(GameObject serieLabelRoot, Serie serie, SerieData serieData, ref int count)
|
||||
@@ -231,7 +233,10 @@ namespace XCharts
|
||||
item.SetLabel(labelObj, isAutoSize, serieLabel.paddingLeftRight, serieLabel.paddingTopBottom);
|
||||
item.SetIcon(iconImage);
|
||||
item.SetIconActive(iconStyle != null && iconStyle.show);
|
||||
item.color = serieLabel.textStyle.backgroundColor;
|
||||
if (serieLabel.textStyle.autoBackgroundColor)
|
||||
item.color = chart.theme.GetColor(serieData.index);
|
||||
else
|
||||
item.color = serieLabel.textStyle.backgroundColor;
|
||||
serieData.labelObject = item;
|
||||
|
||||
foreach (var data in serieData.children)
|
||||
@@ -247,28 +252,35 @@ namespace XCharts
|
||||
{
|
||||
if (m_SerieRoot == null)
|
||||
InitRoot();
|
||||
var textStyle = serie.titleStyle.textStyle;
|
||||
var titleColor = ChartHelper.IsClearColor(textStyle.color)
|
||||
? chart.theme.GetColor(serie.index)
|
||||
: (Color32)textStyle.color;
|
||||
var serieTitleRoot = ChartHelper.AddObject(s_SerieTitleObjectName, m_SerieRoot.transform,
|
||||
chart.chartMinAnchor, chart.chartMaxAnchor, chart.chartPivot, chart.chartSizeDelta);
|
||||
serieTitleRoot.hideFlags = chart.chartHideFlags;
|
||||
SerieLabelPool.ReleaseAll(serieTitleRoot.transform);
|
||||
ChartHelper.RemoveComponent<Text>(serieTitleRoot);
|
||||
|
||||
SerieHelper.UpdateCenter(serie, chart.chartPosition, chart.chartWidth, chart.chartHeight);
|
||||
var anchorMin = new Vector2(0.5f, 0.5f);
|
||||
var anchorMax = new Vector2(0.5f, 0.5f);
|
||||
var pivot = new Vector2(0.5f, 0.5f);
|
||||
var fontSize = 10;
|
||||
var sizeDelta = new Vector2(50, fontSize + 2);
|
||||
var txt = ChartHelper.AddTextObject("title", m_SerieRoot.transform, anchorMin, anchorMax,
|
||||
pivot, sizeDelta, textStyle, chart.theme.common);
|
||||
txt.SetText("");
|
||||
txt.SetColor(titleColor);
|
||||
txt.SetLocalPosition(Vector2.zero);
|
||||
txt.SetLocalEulerAngles(Vector2.zero);
|
||||
txt.SetActive(serie.titleStyle.show);
|
||||
serie.titleStyle.runtimeText = txt;
|
||||
serie.titleStyle.UpdatePosition(serie.context.center);
|
||||
var serieData = serie.GetSerieData(0);
|
||||
if (serieData != null)
|
||||
for (int i = 0; i < serie.dataCount; i++)
|
||||
{
|
||||
txt.SetText(serieData.name);
|
||||
var serieData = serie.data[i];
|
||||
var titleStyle = SerieHelper.GetTitleStyle(serie, serieData);
|
||||
if (titleStyle == null) continue;
|
||||
var color = chart.GetLegendRealShowNameColor(serieData.name);
|
||||
var label = ChartHelper.AddDefaultChartLabel("title_" + i, serieTitleRoot.transform, anchorMin, anchorMax,
|
||||
pivot, sizeDelta, titleStyle.textStyle, chart.theme.common, serieData.name);
|
||||
serieData.titleObject = label;
|
||||
label.SetActive(titleStyle.show);
|
||||
var labelPosition = GetSerieDataTitlePosition(serieData, titleStyle);
|
||||
var offset = titleStyle.GetOffset(serie.context.insideRadius);
|
||||
label.SetPosition(labelPosition + offset);
|
||||
if (titleStyle.textStyle.autoBackgroundColor)
|
||||
label.color = color;
|
||||
else
|
||||
label.color = titleStyle.textStyle.backgroundColor;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +291,7 @@ namespace XCharts
|
||||
|
||||
var colorIndex = chart.GetLegendRealShowNameIndex(serie.legendName);
|
||||
var total = serie.GetDataTotal(defaultDimension);
|
||||
var isNeedInvertPositionSerie = serie is Line;
|
||||
var dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
|
||||
|
||||
foreach (var serieData in serie.data)
|
||||
{
|
||||
@@ -292,7 +304,6 @@ namespace XCharts
|
||||
var isIgnore = serie.IsIgnoreIndex(serieData.index, defaultDimension);
|
||||
var currLabel = isHighlight && emphasisLabel != null ? emphasisLabel : serieLabel;
|
||||
|
||||
serieData.labelObject.SetPosition(serieData.context.position);
|
||||
serieData.labelObject.UpdateIcon(iconStyle);
|
||||
if (serie.show
|
||||
&& currLabel != null
|
||||
@@ -300,17 +311,12 @@ namespace XCharts
|
||||
&& serieData.context.canShowLabel
|
||||
&& !isIgnore)
|
||||
{
|
||||
var value = serieData.GetData(defaultDimension);
|
||||
var content = serie.useDataNameForColor && string.IsNullOrEmpty(currLabel.formatter)
|
||||
? serieData.name
|
||||
//var value = serieData.GetData(defaultDimension);
|
||||
var value = serieData.GetCurrData(defaultDimension, dataChangeDuration);
|
||||
var content = string.IsNullOrEmpty(currLabel.formatter)
|
||||
? ChartCached.NumberToStr(value, serieLabel.numericFormatter)
|
||||
: SerieLabelHelper.GetFormatterContent(serie, serieData, value, total,
|
||||
currLabel, chart.theme.GetColor(colorIndex));
|
||||
|
||||
var invert = currLabel.autoOffset
|
||||
&& isNeedInvertPositionSerie
|
||||
&& SerieHelper.IsDownPoint(serie, serieData.index)
|
||||
&& (serie.areaStyle == null || !serie.areaStyle.show);
|
||||
var labelPosition = GetSerieDataLabelPosition(serieData, currLabel);
|
||||
var isInsidePosition = currLabel.position == LabelStyle.Position.Inside;
|
||||
|
||||
//text color
|
||||
@@ -332,9 +338,9 @@ namespace XCharts
|
||||
}
|
||||
SerieLabelHelper.ResetLabel(serieData.labelObject.label, currLabel, chart.theme, textColor, rotate);
|
||||
serieData.SetLabelActive(!isIgnore);
|
||||
serieData.labelObject.SetPosition(labelPosition
|
||||
+ (invert ? -currLabel.offset : currLabel.offset));
|
||||
|
||||
serieData.labelObject.SetText(content);
|
||||
UpdateLabelPosition(serieData, currLabel);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -343,11 +349,29 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLabelPosition(SerieData serieData, LabelStyle currLabel)
|
||||
{
|
||||
var isNeedInvertPositionSerie = serie is Line;
|
||||
var invert = currLabel.autoOffset
|
||||
&& isNeedInvertPositionSerie
|
||||
&& SerieHelper.IsDownPoint(serie, serieData.index)
|
||||
&& (serie.areaStyle == null || !serie.areaStyle.show);
|
||||
var labelPosition = GetSerieDataLabelPosition(serieData, currLabel);
|
||||
var offset = currLabel.GetOffset(serie.context.insideRadius);
|
||||
serieData.labelObject.SetPosition(labelPosition
|
||||
+ (invert ? -offset : offset));
|
||||
}
|
||||
|
||||
public virtual Vector3 GetSerieDataLabelPosition(SerieData serieData, LabelStyle label)
|
||||
{
|
||||
return serieData.context.position;
|
||||
}
|
||||
|
||||
public virtual Vector3 GetSerieDataTitlePosition(SerieData serieData, TitleStyle titleStyle)
|
||||
{
|
||||
return serieData.context.position;
|
||||
}
|
||||
|
||||
protected void UpdateCoordSerieParams(ref List<SerieParams> paramList, ref string title,
|
||||
int dataIndex, bool showCategory, string category, string marker,
|
||||
string itemFormatter, string numericFormatter)
|
||||
|
||||
Reference in New Issue
Block a user