增加Axis的图标、自动换行、自定义长宽的支持

This commit is contained in:
monitor1394
2021-05-24 07:17:54 +08:00
parent 355d3639b1
commit f071721d6d
17 changed files with 416 additions and 106 deletions

View File

@@ -538,8 +538,8 @@ namespace XCharts
var axisObj = ChartHelper.AddObject(objName, transform, graphAnchorMin,
graphAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
yAxis.gameObject = axisObj;
yAxis.axisLabelTextList.Clear();
axisObj.SetActive(yAxis.show && yAxis.axisLabel.show);
yAxis.runtimeAxisLabelList.Clear();
axisObj.SetActive(yAxis.show);
axisObj.hideFlags = chartHideFlags;
ChartHelper.HideAllObject(axisObj);
var grid = GetAxisGridOrDefault(yAxis);
@@ -549,34 +549,46 @@ namespace XCharts
float totalWidth = 0;
float eachWidth = AxisHelper.GetEachWidth(yAxis, grid.runtimeHeight, dataZoom);
float gapWidth = yAxis.boundaryGap ? eachWidth / 2 : 0;
if (yAxis.IsCategory()) splitNumber += 1;
float textWidth = yAxis.axisLabel.width > 0
? yAxis.axisLabel.width
: grid.left;
float textHeight = yAxis.axisLabel.height > 0
? yAxis.axisLabel.height
: 20f;
if (yAxis.IsCategory() && yAxis.boundaryGap)
{
splitNumber -= 1;
}
for (int i = 0; i < splitNumber; i++)
{
ChartText txt;
ChartLabel txt;
var inside = yAxis.axisLabel.inside;
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
var labelName = AxisHelper.GetLabelName(yAxis, grid.runtimeHeight, i, yAxis.runtimeMinValue,
yAxis.runtimeMaxValue, dataZoom, isPercentStack);
if ((inside && yAxis.IsLeft()) || (!inside && yAxis.IsRight()))
{
txt = ChartHelper.AddTextObject(objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(0, 0.5f), new Vector2(grid.left, 20), axisLabelTextStyle,
m_Theme.axis);
txt.SetAlignment(TextAnchor.MiddleLeft);
txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(0, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis);
if (yAxis.axisLabel.autoAlign)
{
txt.label.SetAlignment(TextAnchor.MiddleLeft);
}
}
else
{
txt = ChartHelper.AddTextObject(objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(1, 0.5f), new Vector2(grid.left, 20), axisLabelTextStyle,
m_Theme.axis);
txt.SetAlignment(TextAnchor.MiddleRight);
txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(1, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis);
if (yAxis.axisLabel.autoAlign)
{
txt.label.SetAlignment(TextAnchor.MiddleRight);
}
}
var labelWidth = AxisHelper.GetScaleWidth(yAxis, grid.runtimeHeight, i + 1, dataZoom);
if (i == 0) yAxis.axisLabel.SetRelatedText(txt, labelWidth);
txt.SetLocalPosition(GetLabelYPosition(totalWidth + gapWidth, i, yAxisIndex, yAxis));
if (i == 0) yAxis.axisLabel.SetRelatedText(txt.label, labelWidth);
txt.SetPosition(GetLabelYPosition(totalWidth + gapWidth, i, yAxisIndex, yAxis));
txt.SetText(labelName);
txt.SetActive(yAxis.show && (yAxis.axisLabel.interval == 0 || i % (yAxis.axisLabel.interval + 1) == 0));
yAxis.axisLabelTextList.Add(txt);
yAxis.runtimeAxisLabelList.Add(txt);
totalWidth += labelWidth;
}
if (yAxis.axisName.show)
@@ -654,8 +666,8 @@ namespace XCharts
var axisObj = ChartHelper.AddObject(objName, transform, graphAnchorMin,
graphAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
xAxis.gameObject = axisObj;
xAxis.axisLabelTextList.Clear();
axisObj.SetActive(xAxis.show && xAxis.axisLabel.show);
xAxis.runtimeAxisLabelList.Clear();
axisObj.SetActive(xAxis.show);
axisObj.hideFlags = chartHideFlags;
ChartHelper.HideAllObject(axisObj);
var grid = GetAxisGridOrDefault(xAxis);
@@ -666,22 +678,33 @@ namespace XCharts
float totalWidth = 0;
float eachWidth = AxisHelper.GetEachWidth(xAxis, grid.runtimeWidth, dataZoom);
float gapWidth = xAxis.boundaryGap ? eachWidth / 2 : 0;
float textWidth = AxisHelper.GetScaleWidth(xAxis, grid.runtimeWidth, 0, dataZoom);
//if (xAxis.IsCategory() && xAxis.boundaryGap) splitNumber += 1;
float textWidth = xAxis.axisLabel.width > 0
? xAxis.axisLabel.width
: AxisHelper.GetScaleWidth(xAxis, grid.runtimeWidth, 0, dataZoom);
float textHeight = xAxis.axisLabel.height > 0
? xAxis.axisLabel.height
: 20f;
if (xAxis.IsCategory() && xAxis.boundaryGap)
{
splitNumber -= 1;
}
for (int i = 0; i < splitNumber; i++)
{
var labelWidth = AxisHelper.GetScaleWidth(xAxis, grid.runtimeWidth, i + 1, dataZoom);
var inside = xAxis.axisLabel.inside;
var isPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
var txt = ChartHelper.AddTextObject(ChartCached.GetXAxisName(xAxisIndex, i), axisObj.transform, new Vector2(0, 1),
new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(textWidth, 20), axisLabelTextStyle, theme.axis);
if (i == 0) xAxis.axisLabel.SetRelatedText(txt, labelWidth);
txt.SetAlignment(TextAnchor.MiddleCenter);
txt.SetLocalPosition(GetLabelXPosition(totalWidth + textWidth / 2 + gapWidth, i, xAxisIndex, xAxis));
txt.SetText(AxisHelper.GetLabelName(xAxis, grid.runtimeWidth, i, xAxis.runtimeMinValue, xAxis.runtimeMaxValue, dataZoom,
var label = ChartHelper.AddAxisLabelObject(i, ChartCached.GetXAxisName(xAxisIndex, i), axisObj.transform,
new Vector2(0, 1), new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(textWidth, textHeight), xAxis, theme.axis);
if (i == 0) xAxis.axisLabel.SetRelatedText(label.label, labelWidth);
if (xAxis.axisLabel.autoAlign)
{
label.label.SetAlignment(TextAnchor.MiddleCenter);
}
label.SetPosition(GetLabelXPosition(totalWidth + textWidth / 2 + gapWidth, i, xAxisIndex, xAxis));
label.SetText(AxisHelper.GetLabelName(xAxis, grid.runtimeWidth, i, xAxis.runtimeMinValue, xAxis.runtimeMaxValue, dataZoom,
isPercentStack));
txt.SetActive(xAxis.show && (xAxis.axisLabel.interval == 0 || i % (xAxis.axisLabel.interval + 1) == 0));
xAxis.axisLabelTextList.Add(txt);
xAxis.runtimeAxisLabelList.Add(label);
totalWidth += labelWidth;
}
if (xAxis.axisName.show)
@@ -807,7 +830,7 @@ namespace XCharts
posX = startX - yAxis.axisLabel.margin;
}
posX += yAxis.offset;
return new Vector3(posX, grid.runtimeY + scaleWid, 0);
return new Vector3(posX, grid.runtimeY + scaleWid, 0) + yAxis.axisLabel.textStyle.offsetv3;
}
private Vector3 GetLabelXPosition(float scaleWid, int i, int xAxisIndex, XAxis xAxis)
@@ -827,7 +850,7 @@ namespace XCharts
posY = startY - xAxis.axisLabel.margin - fontSize / 2;
}
posY += xAxis.offset;
return new Vector3(grid.runtimeX + scaleWid, posY);
return new Vector3(grid.runtimeX + scaleWid, posY) + xAxis.axisLabel.textStyle.offsetv3;
}
protected virtual void CheckMinMaxValue()

View File

@@ -19,12 +19,33 @@ namespace XCharts
private RectTransform m_LabelRect;
private RectTransform m_IconRect;
private RectTransform m_ObjectRect;
private Vector3 m_IconOffest;
private Image m_IconImage;
public GameObject gameObject { get { return m_GameObject; } }
public Image icon { get { return m_IconImage; } }
public ChartText label { get { return m_LabelText; } }
public GameObject gameObject
{
get { return m_GameObject; }
set
{
m_GameObject = value;
m_ObjectRect = value.GetComponent<RectTransform>();
}
}
public Image icon
{
get { return m_IconImage; }
set { SetIcon(value); }
}
public ChartText label
{
get { return m_LabelText; }
set
{
m_LabelText = value;
if (value != null) m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
}
}
public ChartLabel()
{
@@ -41,6 +62,11 @@ namespace XCharts
m_ObjectRect = labelObj.GetComponent<RectTransform>();
}
public void SetAutoSize(bool flag)
{
m_LabelAutoSize = flag;
}
public void SetIcon(Image image)
{
m_IconImage = image;
@@ -60,16 +86,17 @@ namespace XCharts
if (m_IconRect != null) m_IconRect.sizeDelta = new Vector3(width, height);
}
public void UpdateIcon(IconStyle iconStyle)
public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null)
{
if (m_IconImage == null) return;
if (iconStyle.show)
{
ChartHelper.SetActive(m_IconImage.gameObject, true);
m_IconImage.sprite = iconStyle.sprite;
m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
m_IconImage.color = iconStyle.color;
m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
m_IconImage.transform.localPosition = iconStyle.offset;
m_IconOffest = iconStyle.offset;
AdjustIconPos();
if (iconStyle.layer == IconStyle.Layer.UnderLabel)
m_IconRect.SetSiblingIndex(0);
else
@@ -144,12 +171,40 @@ namespace XCharts
if (sizeChange)
{
m_LabelRect.sizeDelta = newSize;
m_ObjectRect.sizeDelta = newSize;
AdjustIconPos();
}
return sizeChange;
}
AdjustIconPos();
}
return false;
}
private void AdjustIconPos()
{
if (m_IconImage && m_IconImage.sprite != null && m_IconRect)
{
var iconX = 0f;
switch (m_LabelText.text.alignment)
{
case TextAnchor.LowerLeft:
case TextAnchor.UpperLeft:
case TextAnchor.MiddleLeft:
iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
break;
case TextAnchor.LowerRight:
case TextAnchor.UpperRight:
case TextAnchor.MiddleRight:
iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
break;
case TextAnchor.LowerCenter:
case TextAnchor.UpperCenter:
case TextAnchor.MiddleCenter:
iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
break;
}
m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);
}
}
}
}

View File

@@ -5,7 +5,6 @@
/* */
/************************************************/
using System;
using UnityEngine;
namespace XCharts

View File

@@ -247,13 +247,13 @@ namespace XCharts
chartText.tmpText.alignment = textStyle.tmpAlignment;
chartText.tmpText.richText = true;
chartText.tmpText.raycastTarget = false;
chartText.tmpText.enableWordWrapping = false;
chartText.tmpText.enableWordWrapping = textStyle.wrap;
#else
chartText.text = GetOrAddComponent<Text>(txtObj);
chartText.text.font = textStyle.font == null ? theme.font : textStyle.font;
chartText.text.fontStyle = textStyle.fontStyle;
chartText.text.alignment = textStyle.alignment;
chartText.text.horizontalOverflow = HorizontalWrapMode.Overflow;
chartText.text.horizontalOverflow = textStyle.wrap ? HorizontalWrapMode.Wrap : HorizontalWrapMode.Overflow;
chartText.text.verticalOverflow = VerticalWrapMode.Overflow;
chartText.text.supportRichText = true;
chartText.text.raycastTarget = false;
@@ -387,7 +387,7 @@ namespace XCharts
return ChartHelper.GetOrAddComponent<Painter>(painterObj);
}
public static GameObject AddIcon(string name, Transform parent, float width, float height)
public static Image AddIcon(string name, Transform parent, float width, float height)
{
var anchorMax = new Vector2(0.5f, 0.5f);
var anchorMin = new Vector2(0.5f, 0.5f);
@@ -396,7 +396,29 @@ namespace XCharts
GameObject iconObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
var img = GetOrAddComponent<Image>(iconObj);
img.raycastTarget = false;
return iconObj;
return img;
}
public static ChartLabel AddAxisLabelObject(int index, string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
Vector2 pivot, Vector2 sizeDelta, Axis axis, ComponentTheme theme)
{
var textStyle = axis.axisLabel.textStyle;
var iconStyle = axis.iconStyle;
var label = new ChartLabel();
label.gameObject = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
// TODO: 为了兼容旧版本,这里后面版本可以去掉
var oldText = label.gameObject.GetComponent<Text>();
if (oldText != null)
{
GameObject.DestroyImmediate(oldText);
}
label.label = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme);
label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, iconStyle.width, iconStyle.height);
label.SetAutoSize(false);
label.UpdateIcon(iconStyle, axis.GetIcon(index));
label.label.SetActive(axis.axisLabel.show && (axis.axisLabel.interval == 0 || index % (axis.axisLabel.interval + 1) == 0));
return label;
}
internal static GameObject AddSerieLabel(string name, Transform parent, float width, float height,