mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-30 05:08:48 +00:00
增加SerieLabel的背景宽高、文字边距、文字旋转的配置
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -17,8 +17,12 @@ namespace XCharts
|
|||||||
SerializedProperty m_Position = prop.FindPropertyRelative("m_Position");
|
SerializedProperty m_Position = prop.FindPropertyRelative("m_Position");
|
||||||
SerializedProperty m_Distance = prop.FindPropertyRelative("m_Distance");
|
SerializedProperty m_Distance = prop.FindPropertyRelative("m_Distance");
|
||||||
SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate");
|
SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate");
|
||||||
|
SerializedProperty m_PaddingLeftRight = prop.FindPropertyRelative("m_PaddingLeftRight");
|
||||||
|
SerializedProperty m_PaddingTopBottom = prop.FindPropertyRelative("m_PaddingTopBottom");
|
||||||
SerializedProperty m_Color = prop.FindPropertyRelative("m_Color");
|
SerializedProperty m_Color = prop.FindPropertyRelative("m_Color");
|
||||||
SerializedProperty m_BackgroundColor = prop.FindPropertyRelative("m_BackgroundColor");
|
SerializedProperty m_BackgroundColor = prop.FindPropertyRelative("m_BackgroundColor");
|
||||||
|
SerializedProperty m_BackgroundWidth = prop.FindPropertyRelative("m_BackgroundWidth");
|
||||||
|
SerializedProperty m_BackgroundHeight = prop.FindPropertyRelative("m_BackgroundHeight");
|
||||||
SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize");
|
SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize");
|
||||||
SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle");
|
SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle");
|
||||||
SerializedProperty m_Line = prop.FindPropertyRelative("m_Line");
|
SerializedProperty m_Line = prop.FindPropertyRelative("m_Line");
|
||||||
@@ -39,8 +43,16 @@ namespace XCharts
|
|||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
EditorGUI.PropertyField(drawRect, m_BackgroundColor);
|
EditorGUI.PropertyField(drawRect, m_BackgroundColor);
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
|
EditorGUI.PropertyField(drawRect, m_BackgroundWidth);
|
||||||
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
|
EditorGUI.PropertyField(drawRect, m_BackgroundHeight);
|
||||||
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
EditorGUI.PropertyField(drawRect, m_Rotate);
|
EditorGUI.PropertyField(drawRect, m_Rotate);
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
|
EditorGUI.PropertyField(drawRect, m_PaddingLeftRight);
|
||||||
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
|
EditorGUI.PropertyField(drawRect, m_PaddingTopBottom);
|
||||||
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
EditorGUI.PropertyField(drawRect, m_FontSize);
|
EditorGUI.PropertyField(drawRect, m_FontSize);
|
||||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||||
EditorGUI.PropertyField(drawRect, m_FontStyle);
|
EditorGUI.PropertyField(drawRect, m_FontStyle);
|
||||||
@@ -62,7 +74,7 @@ namespace XCharts
|
|||||||
float height = 0;
|
float height = 0;
|
||||||
if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop))
|
if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop))
|
||||||
{
|
{
|
||||||
height += 12 * EditorGUIUtility.singleLineHeight + 11 * EditorGUIUtility.standardVerticalSpacing;
|
height += 16 * EditorGUIUtility.singleLineHeight + 15 * EditorGUIUtility.standardVerticalSpacing;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -739,9 +739,9 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
var serieData = m_Data[index];
|
var serieData = m_Data[index];
|
||||||
serieData.name = name;
|
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++)
|
for (int i = 0; i < m_Series.Count; i++)
|
||||||
{
|
{
|
||||||
var serie = m_Series.series[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;
|
if (serie.type != SerieType.Pie && !serie.label.show) continue;
|
||||||
for (int j = 0; j < serie.data.Count; j++)
|
for (int j = 0; j < serie.data.Count; j++)
|
||||||
{
|
{
|
||||||
@@ -313,11 +312,11 @@ namespace XCharts
|
|||||||
(Color)m_ThemeInfo.GetColor(i);
|
(Color)m_ThemeInfo.GetColor(i);
|
||||||
}
|
}
|
||||||
var backgroundColor = serie.label.backgroundColor;
|
var backgroundColor = serie.label.backgroundColor;
|
||||||
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font, color, backgroundColor,
|
var labelObj = ChartHelper.AddSerieLabel(textName, labelObject.transform, m_ThemeInfo.font,
|
||||||
serie.label.fontSize, serie.label.fontStyle);
|
color, backgroundColor, serie.label.fontSize, serie.label.fontStyle, serie.label.rotate,
|
||||||
serieData.label = labelObj.GetComponentInChildren<Text>();
|
serie.label.backgroundWidth,serie.label.backgroundHeight);
|
||||||
serieData.labelImage = labelObj.GetComponent<Image>();
|
var isAutoSize = serie.label.backgroundWidth == 0 || serie.label.backgroundHeight == 0;
|
||||||
serieData.labelRect = labelObj.GetComponent<RectTransform>();
|
serieData.InitLabel(labelObj, isAutoSize, serie.label.paddingLeftRight, serie.label.paddingTopBottom);
|
||||||
serieData.SetLabelActive(false);
|
serieData.SetLabelActive(false);
|
||||||
serieData.SetLabelText(serieData.name);
|
serieData.SetLabelText(serieData.name);
|
||||||
count++;
|
count++;
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ namespace XCharts
|
|||||||
[SerializeField] private List<float> m_Data = new List<float>();
|
[SerializeField] private List<float> m_Data = new List<float>();
|
||||||
|
|
||||||
private bool m_Show = true;
|
private bool m_Show = true;
|
||||||
|
private GameObject m_Label;
|
||||||
|
private bool m_LabelAutoSize;
|
||||||
|
private float m_LabelPaddingLeftRight;
|
||||||
|
private float m_LabelPaddingTopBottom;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the name of data item.
|
/// the name of data item.
|
||||||
@@ -47,9 +51,9 @@ namespace XCharts
|
|||||||
/// the label of data item.
|
/// the label of data item.
|
||||||
/// 该数据项的文本标签。
|
/// 该数据项的文本标签。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Text label { get; set; }
|
public Text labelText { get; private set; }
|
||||||
public RectTransform labelRect { get; set; }
|
public RectTransform labelRect { get; private set; }
|
||||||
public Image labelImage { get; set; }
|
public Image labelImage { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the maxinum value.
|
/// the maxinum value.
|
||||||
/// 最大值。
|
/// 最大值。
|
||||||
@@ -67,29 +71,44 @@ namespace XCharts
|
|||||||
else return 0;
|
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)
|
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)
|
public void SetLabelText(string text)
|
||||||
{
|
{
|
||||||
if (label)
|
if (labelText)
|
||||||
{
|
{
|
||||||
label.text = text;
|
labelText.text = text;
|
||||||
labelRect.sizeDelta = new Vector2(label.preferredWidth + 4,
|
if (m_LabelAutoSize)
|
||||||
label.preferredHeight + 4);
|
{
|
||||||
|
labelRect.sizeDelta = new Vector2(labelText.preferredWidth + m_LabelPaddingLeftRight * 2,
|
||||||
|
labelText.preferredHeight + m_LabelPaddingTopBottom * 2);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetLabelPosition(Vector3 position)
|
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.
|
/// the left of symbol.
|
||||||
/// 图形标志的左边。
|
/// 图形标志的左边。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Left,
|
//Left,
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the right of symbol.
|
/// the right of symbol.
|
||||||
/// 图形标志的右边。
|
/// 图形标志的右边。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Right,
|
//Right,
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the bottom of symbol.
|
/// the bottom of symbol.
|
||||||
/// 图形标志的底部。
|
/// 图形标志的底部。
|
||||||
@@ -53,10 +53,14 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
[SerializeField] private bool m_Show = false;
|
[SerializeField] private bool m_Show = false;
|
||||||
[SerializeField] Position m_Position;
|
[SerializeField] Position m_Position;
|
||||||
[SerializeField] private float m_Distance;
|
[SerializeField] private float m_Distance = 0;
|
||||||
[SerializeField] private float m_Rotate;
|
[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_Color;
|
||||||
[SerializeField] private Color m_BackgroundColor;
|
[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 int m_FontSize = 18;
|
||||||
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
|
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
|
||||||
[SerializeField] private bool m_Line = true;
|
[SerializeField] private bool m_Line = true;
|
||||||
@@ -83,13 +87,39 @@ namespace XCharts
|
|||||||
/// 自定义文字颜色,默认和系列的颜色一致。
|
/// 自定义文字颜色,默认和系列的颜色一致。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Color color { get { return m_Color; } set { m_Color = value; } }
|
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; } }
|
public Color backgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; } }
|
||||||
/// <summary>
|
/// <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.
|
/// Rotate label.
|
||||||
/// 标签旋转。
|
/// 标签旋转。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float rotate { get { return m_Rotate; } set { m_Rotate = value; } }
|
public float rotate { get { return m_Rotate; } set { m_Rotate = value; } }
|
||||||
/// <summary>
|
/// <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.
|
/// font size.
|
||||||
/// 文字的字体大小。
|
/// 文字的字体大小。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -337,7 +337,7 @@ namespace XCharts
|
|||||||
private void DrawLabel(Serie serie, SerieData serieData, PieTempData tempData, Color serieColor,
|
private void DrawLabel(Serie serie, SerieData serieData, PieTempData tempData, Color serieColor,
|
||||||
float currAngle, float offsetRadius, float insideRadius, float outsideRadius)
|
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);
|
var isHighlight = (serieData.highlighted && serie.highlightLabel.show);
|
||||||
if (serie.label.show || isHighlight)
|
if (serie.label.show || isHighlight)
|
||||||
{
|
{
|
||||||
@@ -366,9 +366,9 @@ namespace XCharts
|
|||||||
var fontStyle = isHighlight ? serie.highlightLabel.fontStyle : serie.label.fontStyle;
|
var fontStyle = isHighlight ? serie.highlightLabel.fontStyle : serie.label.fontStyle;
|
||||||
float currRad = currAngle * Mathf.Deg2Rad;
|
float currRad = currAngle * Mathf.Deg2Rad;
|
||||||
|
|
||||||
serieData.label.color = color;
|
serieData.labelText.color = color;
|
||||||
serieData.label.fontSize = fontSize;
|
serieData.labelText.fontSize = fontSize;
|
||||||
serieData.label.fontStyle = fontStyle;
|
serieData.labelText.fontStyle = fontStyle;
|
||||||
|
|
||||||
serieData.labelImage.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
serieData.labelImage.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||||
|
|
||||||
@@ -388,7 +388,7 @@ namespace XCharts
|
|||||||
labelRadius = tempData.outsideRadius + serie.label.lineLength1;
|
labelRadius = tempData.outsideRadius + serie.label.lineLength1;
|
||||||
labelCenter = new Vector2(tempData.center.x + labelRadius * Mathf.Sin(currRad),
|
labelCenter = new Vector2(tempData.center.x + labelRadius * Mathf.Sin(currRad),
|
||||||
tempData.center.y + labelRadius * Mathf.Cos(currRad));
|
tempData.center.y + labelRadius * Mathf.Cos(currRad));
|
||||||
float labelWidth = serieData.label.preferredWidth;
|
float labelWidth = serieData.labelText.preferredWidth;
|
||||||
if (currAngle > 180)
|
if (currAngle > 180)
|
||||||
{
|
{
|
||||||
serieData.labelImage.transform.localPosition = new Vector2(labelCenter.x - serie.label.lineLength2 - 5 - labelWidth / 2, labelCenter.y);
|
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,
|
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 anchorMin = new Vector2(0.5f, 0.5f);
|
||||||
var anchorMax = new Vector2(0.5f, 0.5f);
|
var anchorMax = new Vector2(0.5f, 0.5f);
|
||||||
var pivot = 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);
|
GameObject labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||||
var img = GetOrAddComponent<Image>(labelObj);
|
var img = GetOrAddComponent<Image>(labelObj);
|
||||||
img.color = backgroundColor;
|
img.color = backgroundColor;
|
||||||
|
img.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||||
Text txt = AddTextObject("Text", labelObj.transform, font, textColor, TextAnchor.MiddleCenter,
|
Text txt = AddTextObject("Text", labelObj.transform, font, textColor, TextAnchor.MiddleCenter,
|
||||||
anchorMin, anchorMax, pivot, sizeDelta, fontSize, 0, fontStyle);
|
anchorMin, anchorMax, pivot, sizeDelta, fontSize, 0, fontStyle);
|
||||||
txt.text = "Text";
|
txt.text = "Text";
|
||||||
txt.transform.localPosition = new Vector2(0, 0);
|
txt.transform.localPosition = new Vector2(0, 0);
|
||||||
|
txt.transform.localEulerAngles = Vector3.zero;
|
||||||
labelObj.transform.localPosition = Vector3.zero;
|
labelObj.transform.localPosition = Vector3.zero;
|
||||||
return labelObj;
|
return labelObj;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user