mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-22 08:50:10 +00:00
增加SerieLabel的背景宽高、文字边距、文字旋转的配置
This commit is contained in:
@@ -739,9 +739,9 @@ namespace XCharts
|
||||
{
|
||||
var serieData = m_Data[index];
|
||||
serieData.name = name;
|
||||
if (serieData.label != null)
|
||||
if (serieData.labelText != null)
|
||||
{
|
||||
serieData.label.text = name == null ? "" : name;
|
||||
serieData.labelText.text = name == null ? "" : name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,7 +295,6 @@ namespace XCharts
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
var serie = m_Series.series[i];
|
||||
if (serie.type != SerieType.Pie && serie.type != SerieType.Line) continue;
|
||||
if (serie.type != SerieType.Pie && !serie.label.show) continue;
|
||||
for (int j = 0; j < serie.data.Count; j++)
|
||||
{
|
||||
@@ -313,11 +312,11 @@ namespace XCharts
|
||||
(Color)m_ThemeInfo.GetColor(i);
|
||||
}
|
||||
var backgroundColor = serie.label.backgroundColor;
|
||||
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font, color, backgroundColor,
|
||||
serie.label.fontSize, serie.label.fontStyle);
|
||||
serieData.label = labelObj.GetComponentInChildren<Text>();
|
||||
serieData.labelImage = labelObj.GetComponent<Image>();
|
||||
serieData.labelRect = labelObj.GetComponent<RectTransform>();
|
||||
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font,
|
||||
color, backgroundColor, serie.label.fontSize, serie.label.fontStyle, serie.label.rotate,
|
||||
serie.label.backgroundWidth,serie.label.backgroundHeight);
|
||||
var isAutoSize = serie.label.backgroundWidth == 0 || serie.label.backgroundHeight == 0;
|
||||
serieData.InitLabel(labelObj, isAutoSize, serie.label.paddingLeftRight, serie.label.paddingTopBottom);
|
||||
serieData.SetLabelActive(false);
|
||||
serieData.SetLabelText(serieData.name);
|
||||
count++;
|
||||
|
||||
@@ -17,6 +17,10 @@ namespace XCharts
|
||||
[SerializeField] private List<float> m_Data = new List<float>();
|
||||
|
||||
private bool m_Show = true;
|
||||
private GameObject m_Label;
|
||||
private bool m_LabelAutoSize;
|
||||
private float m_LabelPaddingLeftRight;
|
||||
private float m_LabelPaddingTopBottom;
|
||||
|
||||
/// <summary>
|
||||
/// the name of data item.
|
||||
@@ -47,9 +51,9 @@ namespace XCharts
|
||||
/// the label of data item.
|
||||
/// 该数据项的文本标签。
|
||||
/// </summary>
|
||||
public Text label { get; set; }
|
||||
public RectTransform labelRect { get; set; }
|
||||
public Image labelImage { get; set; }
|
||||
public Text labelText { get; private set; }
|
||||
public RectTransform labelRect { get; private set; }
|
||||
public Image labelImage { get; private set; }
|
||||
/// <summary>
|
||||
/// the maxinum value.
|
||||
/// 最大值。
|
||||
@@ -67,29 +71,44 @@ namespace XCharts
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public void InitLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, float paddingTopBottom)
|
||||
{
|
||||
m_Label = labelObj;
|
||||
m_LabelAutoSize = autoSize;
|
||||
m_LabelPaddingLeftRight = paddingLeftRight;
|
||||
m_LabelPaddingTopBottom = paddingTopBottom;
|
||||
labelText = labelObj.GetComponentInChildren<Text>();
|
||||
labelImage = labelObj.GetComponent<Image>();
|
||||
labelRect = labelObj.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
public void SetLabelActive(bool active)
|
||||
{
|
||||
if (labelImage)
|
||||
if (m_Label)
|
||||
{
|
||||
ChartHelper.SetActive(labelImage.gameObject, active);
|
||||
ChartHelper.SetActive(m_Label, active);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLabelText(string text)
|
||||
{
|
||||
if (label)
|
||||
if (labelText)
|
||||
{
|
||||
label.text = text;
|
||||
labelRect.sizeDelta = new Vector2(label.preferredWidth + 4,
|
||||
label.preferredHeight + 4);
|
||||
labelText.text = text;
|
||||
if (m_LabelAutoSize)
|
||||
{
|
||||
labelRect.sizeDelta = new Vector2(labelText.preferredWidth + m_LabelPaddingLeftRight * 2,
|
||||
labelText.preferredHeight + m_LabelPaddingTopBottom * 2);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLabelPosition(Vector3 position)
|
||||
{
|
||||
if (labelImage)
|
||||
if (m_Label)
|
||||
{
|
||||
labelImage.transform.localPosition = position;
|
||||
m_Label.transform.localPosition = position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +39,12 @@ namespace XCharts
|
||||
/// the left of symbol.
|
||||
/// 图形标志的左边。
|
||||
/// </summary>
|
||||
Left,
|
||||
//Left,
|
||||
/// <summary>
|
||||
/// the right of symbol.
|
||||
/// 图形标志的右边。
|
||||
/// </summary>
|
||||
Right,
|
||||
//Right,
|
||||
/// <summary>
|
||||
/// the bottom of symbol.
|
||||
/// 图形标志的底部。
|
||||
@@ -53,10 +53,14 @@ namespace XCharts
|
||||
}
|
||||
[SerializeField] private bool m_Show = false;
|
||||
[SerializeField] Position m_Position;
|
||||
[SerializeField] private float m_Distance;
|
||||
[SerializeField] private float m_Rotate;
|
||||
[SerializeField] private float m_Distance = 0;
|
||||
[SerializeField] private float m_Rotate = 0;
|
||||
[SerializeField] private float m_PaddingLeftRight = 2f;
|
||||
[SerializeField] private float m_PaddingTopBottom = 2f;
|
||||
[SerializeField] private Color m_Color;
|
||||
[SerializeField] private Color m_BackgroundColor;
|
||||
[SerializeField] private float m_BackgroundWidth = 0;
|
||||
[SerializeField] private float m_BackgroundHeight = 0;
|
||||
[SerializeField] private int m_FontSize = 18;
|
||||
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
|
||||
[SerializeField] private bool m_Line = true;
|
||||
@@ -83,13 +87,39 @@ namespace XCharts
|
||||
/// 自定义文字颜色,默认和系列的颜色一致。
|
||||
/// </summary>
|
||||
public Color color { get { return m_Color; } set { m_Color = value; } }
|
||||
/// <summary>
|
||||
/// the background color. If set as default, it means than don't show background.
|
||||
/// 标签的背景色,默认无颜色。
|
||||
/// </summary>
|
||||
public Color backgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; } }
|
||||
/// <summary>
|
||||
/// the width of background. If set as default value 0, it means than the background width auto set as the text width.
|
||||
/// 标签的背景宽度。一般不用指定,不指定时则自动是文字的宽度。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public float backgroundWidth { get { return m_BackgroundWidth; } set { m_BackgroundWidth = value; } }
|
||||
/// <summary>
|
||||
/// the height of background. If set as default value 0, it means than the background height auto set as the text height.
|
||||
/// 标签的背景高度。一般不用指定,不指定时则自动是文字的高度。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public float backgroundHeight { get { return m_BackgroundHeight; } set { m_BackgroundHeight = value; } }
|
||||
/// <summary>
|
||||
/// Rotate label.
|
||||
/// 标签旋转。
|
||||
/// </summary>
|
||||
public float rotate { get { return m_Rotate; } set { m_Rotate = value; } }
|
||||
/// <summary>
|
||||
/// the text padding of left and right. defaut:2.
|
||||
/// 左右边距。
|
||||
/// </summary>
|
||||
public float paddingLeftRight { get { return m_PaddingLeftRight; } set { m_PaddingLeftRight = value; } }
|
||||
/// <summary>
|
||||
/// the text padding of top and bottom. defaut:2.
|
||||
/// 上下边距。
|
||||
/// </summary>
|
||||
public float paddingTopBottom { get { return m_PaddingTopBottom; } set { m_PaddingTopBottom = value; } }
|
||||
/// <summary>
|
||||
/// font size.
|
||||
/// 文字的字体大小。
|
||||
/// </summary>
|
||||
|
||||
@@ -337,7 +337,7 @@ namespace XCharts
|
||||
private void DrawLabel(Serie serie, SerieData serieData, PieTempData tempData, Color serieColor,
|
||||
float currAngle, float offsetRadius, float insideRadius, float outsideRadius)
|
||||
{
|
||||
if (serieData.label == null) return;
|
||||
if (serieData.labelText == null) return;
|
||||
var isHighlight = (serieData.highlighted && serie.highlightLabel.show);
|
||||
if (serie.label.show || isHighlight)
|
||||
{
|
||||
@@ -366,9 +366,9 @@ namespace XCharts
|
||||
var fontStyle = isHighlight ? serie.highlightLabel.fontStyle : serie.label.fontStyle;
|
||||
float currRad = currAngle * Mathf.Deg2Rad;
|
||||
|
||||
serieData.label.color = color;
|
||||
serieData.label.fontSize = fontSize;
|
||||
serieData.label.fontStyle = fontStyle;
|
||||
serieData.labelText.color = color;
|
||||
serieData.labelText.fontSize = fontSize;
|
||||
serieData.labelText.fontStyle = fontStyle;
|
||||
|
||||
serieData.labelImage.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||
|
||||
@@ -388,7 +388,7 @@ namespace XCharts
|
||||
labelRadius = tempData.outsideRadius + serie.label.lineLength1;
|
||||
labelCenter = new Vector2(tempData.center.x + labelRadius * Mathf.Sin(currRad),
|
||||
tempData.center.y + labelRadius * Mathf.Cos(currRad));
|
||||
float labelWidth = serieData.label.preferredWidth;
|
||||
float labelWidth = serieData.labelText.preferredWidth;
|
||||
if (currAngle > 180)
|
||||
{
|
||||
serieData.labelImage.transform.localPosition = new Vector2(labelCenter.x - serie.label.lineLength2 - 5 - labelWidth / 2, labelCenter.y);
|
||||
|
||||
@@ -192,19 +192,21 @@ namespace XCharts
|
||||
}
|
||||
|
||||
public static GameObject AddSerieLabel(string name, Transform parent, Font font, Color textColor, Color backgroundColor,
|
||||
int fontSize, FontStyle fontStyle)
|
||||
int fontSize, FontStyle fontStyle, float rotate, float width, float height)
|
||||
{
|
||||
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 sizeDelta = new Vector2(50, fontSize + 2);
|
||||
var sizeDelta = (width != 0 && height != 0) ? new Vector2(width, height) : new Vector2(50, fontSize + 2);
|
||||
GameObject labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var img = GetOrAddComponent<Image>(labelObj);
|
||||
img.color = backgroundColor;
|
||||
img.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||
Text txt = AddTextObject("Text", labelObj.transform, font, textColor, TextAnchor.MiddleCenter,
|
||||
anchorMin, anchorMax, pivot, sizeDelta, fontSize, 0, fontStyle);
|
||||
txt.text = "Text";
|
||||
txt.transform.localPosition = new Vector2(0, 0);
|
||||
txt.transform.localEulerAngles = Vector3.zero;
|
||||
labelObj.transform.localPosition = Vector3.zero;
|
||||
return labelObj;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user