增加SerieLabel配置饼图标签,支持Center,inside,Outside等显示位置

This commit is contained in:
monitor1394
2019-07-29 00:25:10 +08:00
parent 0f25f4449b
commit 484178bbf9
11 changed files with 18946 additions and 188744 deletions

View File

@@ -18,6 +18,7 @@ namespace XCharts
{
private static readonly string s_TitleObjectName = "title";
private static readonly string s_LegendObjectName = "legend";
private static readonly string s_SerieLabelObjectName = "label";
[SerializeField] protected float m_ChartWidth;
[SerializeField] protected float m_ChartHeight;
@@ -39,6 +40,7 @@ namespace XCharts
[NonSerialized] private float m_CheckSerieCount = 0;
[NonSerialized] private List<string> m_CheckSerieName = new List<string>();
[NonSerialized] private bool m_RefreshChart = false;
[NonSerialized] private bool m_RefreshLabel = false;
protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } }
protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } }
@@ -61,11 +63,13 @@ namespace XCharts
m_CheckTheme = m_ThemeInfo.theme;
InitTitle();
InitLegend();
InitSerieLabel();
InitTooltip();
TransferOldVersionData();
}
protected override void Start() {
protected override void Start()
{
RefreshChart();
}
@@ -77,6 +81,7 @@ namespace XCharts
CheckLegend();
CheckTooltip();
CheckRefreshChart();
CheckRefreshLabel();
}
protected override void OnEnable()
@@ -248,6 +253,34 @@ namespace XCharts
}
}
private void InitSerieLabel()
{
ChartHelper.HideAllObject(transform, s_SerieLabelObjectName);
var labelObject = ChartHelper.AddObject(s_SerieLabelObjectName, transform, chartAnchorMin,
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
int count = 0;
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series.series[i];
if (serie.type != SerieType.Pie) continue;
for (int j = 0; j < serie.data.Count; j++)
{
var serieData = serie.data[j];
var textName = s_SerieLabelObjectName + "_" + i + "_" + j + "_" + serieData.name;
var color = (serie.label.position == SerieLabel.Position.Inside) ? Color.white :
(Color)m_ThemeInfo.GetColor(count++);
var anchorMin = new Vector2(0.5f, 0.5f);
var anchorMax = new Vector2(0.5f, 0.5f);
var pivot = new Vector2(0.5f, 0.5f);
serieData.label = ChartHelper.AddTextObject(textName, labelObject.transform,
m_ThemeInfo.font, color, TextAnchor.MiddleCenter, anchorMin, anchorMax, pivot,
new Vector2(50, serie.label.fontSize), serie.label.fontSize);
serieData.label.text = serieData.name;
serieData.label.gameObject.SetActive(true);
}
}
}
private void InitTooltip()
{
var tooltipObject = ChartHelper.AddObject("tooltip", transform, chartAnchorMin,
@@ -354,14 +387,14 @@ namespace XCharts
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
Input.mousePosition, canvas.worldCamera, out local))
{
if(m_Tooltip.IsActive()) RefreshChart();
if (m_Tooltip.IsActive()) RefreshChart();
m_Tooltip.SetActive(false);
return;
}
if (local.x < 0 || local.x > chartWidth ||
local.y < 0 || local.y > chartHeight)
{
if(m_Tooltip.IsActive()) RefreshChart();
if (m_Tooltip.IsActive()) RefreshChart();
m_Tooltip.SetActive(false);
return;
}
@@ -384,6 +417,20 @@ namespace XCharts
}
}
protected void CheckRefreshLabel()
{
if (m_RefreshLabel)
{
m_RefreshLabel = false;
OnRefreshLabel();
}
}
protected virtual void OnRefreshLabel()
{
}
protected virtual void OnSizeChanged()
{
InitTitle();
@@ -456,6 +503,7 @@ namespace XCharts
DrawBackground(vh);
DrawChart(vh);
DrawTooltip(vh);
m_RefreshLabel = true;
}
protected virtual void DrawChart(VertexHelper vh)

View File

@@ -39,7 +39,8 @@ namespace XCharts
[SerializeField] private float[] m_Center = new float[2] { 0.5f, 0.5f };
[SerializeField] private float[] m_Radius = new float[2] { 0, 80 };
#endregion
[SerializeField] private SerieLabel m_Label = new SerieLabel();
[SerializeField] private SerieLabel m_HighlightLabel = new SerieLabel();
[SerializeField] [Range(1, 6)] private int m_ShowDataDimension;
[SerializeField] private bool m_ShowDataName;
[FormerlySerializedAs("m_Data")]
@@ -58,7 +59,20 @@ namespace XCharts
public float space { get { return m_Space; } set { m_Space = value; } }
public float[] center { get { return m_Center; } set { m_Center = value; } }
public float[] radius { get { return m_Radius; } set { m_Radius = value; } }
/// <summary>
/// Text label of graphic element,to explain some data information about graphic item like value, name and so on.
/// 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
/// </summary>
public SerieLabel label { get { return m_Label; } set { m_Label = value; } }
public SerieLabel highlightLabel { get { return m_HighlightLabel; } set { m_HighlightLabel = value; } }
public List<float> yData { get { return m_YData; } }
public List<float> xData { get { return m_XData; } }
public List<SerieData> data { get { return m_Data; } }
/// <summary>
/// The index of serie,start at 0.
/// 系列的索引从0开始。
/// </summary>
public int index { get; set; }
/// <summary>
/// Whether the serie is highlighted.
@@ -66,11 +80,6 @@ namespace XCharts
/// </summary>
public bool highlighted { get; set; }
public int dataCount { get { return m_Data.Count; } }
public List<float> yData { get { return m_YData; } }
public List<float> xData { get { return m_XData; } }
public List<SerieData> data { get { return m_Data; } }
public int filterStart { get; set; }
public int filterEnd { get; set; }

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
@@ -36,5 +37,10 @@ namespace XCharts
/// Whether the data item is highlighted.该数据项是否被高亮,一般由鼠标悬停或图例悬停触发高亮。
/// </summary>
public bool highlighted { get; set; }
/// <summary>
/// the label of data item.该数据项的文本标签。
/// </summary>
/// <value></value>
public Text label { get; set; }
}
}

View File

@@ -0,0 +1,80 @@
using UnityEngine;
namespace XCharts
{
[System.Serializable]
/// <summary>
/// Text label of chart, to explain some data information about graphic item like value, name and so on.
/// 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
/// </summary>
public class SerieLabel
{
/// <summary>
/// The position of label.标签的位置。
/// </summary>
public enum Position
{
/// <summary>
/// Outside of sectors of pie chart, which relates to corresponding sector through visual guide line.
/// 饼图扇区外侧,通过视觉引导线连到相应的扇区。
/// </summary>
Outside,
/// <summary>
/// Inside the sectors of pie chart.饼图扇区内部。
/// </summary>
Inside,
/// <summary>
/// In the center of pie chart.在饼图中心位置。
Center,
Top,
Left,
Right,
Bottom,
}
[SerializeField] private bool m_Show = false;
[SerializeField] Position m_Position;
[SerializeField] private float m_Distance;
[SerializeField] private float m_Rotate;
[SerializeField] private Color m_Color;
[SerializeField] private int m_FontSize = 18;
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
[SerializeField] private bool m_Line = true;
[SerializeField] private float m_LineWidth = 1.0f;
[SerializeField] private float m_LineLength1 = 25f;
[SerializeField] private float m_LineLength2 = 15f;
/// <summary>
/// Whether the label is showed.是否显示文本标签。
/// </summary>
public bool show { get { return m_Show; } set { m_Show = value; } }
/// <summary>
/// The position of label.标签的位置。
/// </summary>
public Position position { get { return m_Position; } set { m_Position = value; } }
/// <summary>
/// Distance to the host graphic element. Works when position is Top,Left,Right,Bottom.
/// 距离图形元素的距离当position为TopLeftRightBottom时有效。
/// </summary>
public float distance { get { return m_Distance; } set { m_Distance = value; } }
/// <summary>
/// Text color,If set as default ,the color will assigned as series color.
/// 自定义文字颜色,默认和系列的颜色一致。
/// </summary>
public Color color { get { return m_Color; } set { m_Color = value; } }
/// <summary>
/// Rotate label.标签旋转。
/// </summary>
public float rotate { get { return m_Rotate; } set { m_Rotate = value; } }
/// <summary>
/// font size.文字的字体大小。
/// </summary>
public int fontSize { get { return m_FontSize; } set { m_FontSize = value; } }
/// <summary>
/// font style.文字的字体风格。
/// </summary>
public FontStyle fontStyle { get { return m_FontStyle; } set { m_FontStyle = value; } }
public bool line { get { return m_Line; } set { m_Line = value; } }
public float lineWidth { get { return m_LineWidth; } set { m_LineWidth = value; } }
public float lineLength1 { get { return m_LineLength1; } set { m_LineLength1 = value; } }
public float lineLength2 { get { return m_LineLength2; } set { m_LineLength2 = value; } }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ac43f23d6d8a143c6b200005689c17af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: