Skip to content

Commit

Permalink
完善TextStylealignment的支持 (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed May 25, 2021
1 parent 93e376b commit 6872d42
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 53 deletions.
5 changes: 3 additions & 2 deletions Assets/XCharts/CHANGELOG-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@

## master

* (2021.05.25) Improved `TextStyle` support for `alignment`
* (2021.05.24) Fixed the problem that `Label` could not display properly when `PieChart` data were all `0`
* (2021.05.24) Fixed an issue where `Serie Name` was not working on the `Add Serie` panel (#149)
* (2021.05.23) Added `TextStyle` `wrap` to set whether to wrap lines
* (2021.05.23) Added` axisLabel `to` autoAlign `whether to set alignment automatically
* (2021.05.23) Added `TextStyle` `autoWrap` to set whether to wrap lines
* (2021.05.23) Added `TextStyle` `autoAlign` whether to set alignment automatically
* (2021.05.23) Added `width` and `height` of `axisLabel` to support custom text length and width
* (2021.05.23) Added `Axis` `iconStyle` and `icons` to support setting coordinate Axis labels to display icons
* (2021.05.20) Added the `insertDataHead` parameter to `Serie` and `Axis` to control whether data is inserted into the head or tail
Expand Down
5 changes: 3 additions & 2 deletions Assets/XCharts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@

## master

* (2021.05.25) 完善`TextStyle``alignment`的支持 (#150)
* (2021.05.24) 修复`PieChart`数据全为`0``Label`无法正常显示的问题
* (2021.05.24) 修复`Add Serie`面板上`Serie Name`不生效的问题 (#149)
* (2021.05.23) 增加`TextStyle``wrap`设置是否自动换行
* (2021.05.23) 增加`AxisLabel``autoAlign`设置是否让系统自动设置对齐方式
* (2021.05.23) 增加`TextStyle``autoWrap`设置是否自动换行
* (2021.05.23) 增加`TextStyle``autoAlign`设置是否让系统自动设置对齐方式
* (2021.05.23) 增加`AxisLabel``width``height`支持自定义文本的长宽
* (2021.05.23) 增加`Axis``iconStyle``icons`支持设置坐标轴标签显示图标
* (2021.05.20) 增加`Serie``Axis``insertDataToHead`参数控制数据插入头部还是尾部
Expand Down
5 changes: 3 additions & 2 deletions Assets/XCharts/Documentation/XCharts配置项手册.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@
* `fontSize`:字体大小。
* `fontStyle`:字体风格。
* `lineSpacing`:行间距。
* `wrap`:是否自动换行。
* `alignment`:对齐方式。
* `autoWrap`:是否自动换行。
* `autoAlign`:是否让系统自动设置对齐方式。为true时系统自动选择对齐方式,为false时用alignment。

## `Tooltip`

Expand Down Expand Up @@ -834,7 +836,6 @@ K线图系列。
* `onZero`:刻度标签显示在`0`刻度上。
* `width`:刻度标签的宽。当为0时系统自动设置。
* `height`:刻度标签的高。当为0时系统自动设置。
* `autoAlign`:是否让系统自动设置对齐方式。为true时系统自动选择对齐方式,为false时用textStyle的对齐方式。
* `textLimit`:文本自适应 [TextLimit](#TextLimit)。只在类目轴中有效。
* `textStyle`:文本样式 [TextStyle](#TextStyle)

Expand Down
5 changes: 3 additions & 2 deletions Assets/XCharts/Documentation/xcharts-configuration-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ The component of settings related to text.
* `fontSize`: the size of text. [default: `18`].
* `fontStyle`: the font style of text. [default: `FontStyle.Normal`].
* `lineSpacing`: the space of text line. [default: `1f`].
* `wrap`: Whether to wrap lines.
* `autoWrap`: Whether to wrap lines.
* `autoAlign`: Whether to let the system automatically set alignment. If true, the system automatically selects alignment, and if false, use alignment.

## `Tooltip`

Expand Down Expand Up @@ -728,7 +729,7 @@ K线图系列。
* `onZero`: 刻度标签显示在`0`刻度上。
* `width`:刻度标签的宽。当为0时系统自动设置。
* `height`:刻度标签的高。当为0时系统自动设置。
* `autoAlign`:是否让系统自动设置对齐方式。为true时系统自动选择对齐方式,为false时用textStyle的对齐方式。

* `textLimit`: 文本自适应 [TextLimit](#TextLimit)。只在类目轴中有效。
* `textStyle`: The style of text [TextStyle](#TextStyle).

Expand Down
1 change: 0 additions & 1 deletion Assets/XCharts/Editor/PropertyDrawers/AxisDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
PropertyField(prop, "m_NumericFormatter");
PropertyField(prop, "m_ShowAsPositiveNumber");
PropertyField(prop, "m_OnZero");
PropertyField(prop, "m_AutoAlign");
PropertyField(prop, "m_TextLimit");
PropertyField(prop, "m_TextStyle");
--EditorGUI.indentLevel;
Expand Down
1 change: 0 additions & 1 deletion Assets/XCharts/Editor/PropertyDrawers/SerieLabelDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/* */
/************************************************/

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

Expand Down
3 changes: 2 additions & 1 deletion Assets/XCharts/Editor/PropertyDrawers/TextStyleDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
#else
PropertyField(prop, "m_Font");
#endif
PropertyField(prop, "m_Wrap");
PropertyField(prop, "m_Rotate");
PropertyField(prop, "m_Offset");
PropertyField(prop, "m_Color");
Expand All @@ -41,6 +40,8 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
#else
PropertyField(prop, "m_FontStyle");
PropertyField(prop, "m_Alignment");
PropertyField(prop, "m_AutoWrap");
PropertyField(prop, "m_AutoAlign");
#endif
--EditorGUI.indentLevel;
}
Expand Down
11 changes: 0 additions & 11 deletions Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class AxisLabel : SubComponent
[SerializeField] private bool m_OnZero = false;
[SerializeField] private float m_Width = 0f;
[SerializeField] private float m_Height = 0f;
[SerializeField] private bool m_AutoAlign = true;
[SerializeField] private TextLimit m_TextLimit = new TextLimit();
[SerializeField] private TextStyle m_TextStyle = new TextStyle();

Expand Down Expand Up @@ -125,14 +124,6 @@ public float height
get { return m_Height; }
set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); }
}
/// <summary>
/// 文本是否自动选对齐方式。为false时会用TextStyle下的alignment。
/// </summary>
public bool autoAlign
{
get { return m_AutoAlign; }
set { if (PropertyUtil.SetStruct(ref m_AutoAlign, value)) SetComponentDirty(); }
}

/// <summary>
/// 文本限制。
Expand Down Expand Up @@ -186,7 +177,6 @@ public AxisLabel Clone()
axisLabel.numericFormatter = numericFormatter;
axisLabel.width = width;
axisLabel.height = height;
axisLabel.autoAlign = autoAlign;
axisLabel.textLimit = textLimit.Clone();
axisLabel.textStyle.Copy(textStyle);
return axisLabel;
Expand All @@ -202,7 +192,6 @@ public void Copy(AxisLabel axisLabel)
numericFormatter = axisLabel.numericFormatter;
width = axisLabel.width;
height = axisLabel.height;
autoAlign = axisLabel.autoAlign;
textLimit.Copy(axisLabel.textLimit);
textStyle.Copy(axisLabel.textStyle);
}
Expand Down
26 changes: 20 additions & 6 deletions Assets/XCharts/Runtime/Component/Sub/TextStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace XCharts
public class TextStyle : SubComponent
{
[SerializeField] private Font m_Font;

[SerializeField] private bool m_Wrap = true;
[SerializeField] private bool m_AutoWrap = true;
[SerializeField] private bool m_AutoAlign = true;
[SerializeField] private float m_Rotate = 0;
[SerializeField] private Vector2 m_Offset = Vector2.zero;
[SerializeField] private Color m_Color = Color.clear;
Expand Down Expand Up @@ -122,10 +122,18 @@ public float lineSpacing
/// <summary>
/// 是否自动换行。
/// </summary>
public bool wrap
public bool autoWrap
{
get { return m_AutoWrap; }
set { if (PropertyUtil.SetStruct(ref m_AutoWrap, value)) SetComponentDirty(); }
}
/// <summary>
/// 文本是否让系统自动选对齐方式。为false时才会用alignment。
/// </summary>
public bool autoAlign
{
get { return m_Wrap; }
set { if (PropertyUtil.SetStruct(ref m_Wrap, value)) SetComponentDirty(); }
get { return m_AutoAlign; }
set { if (PropertyUtil.SetStruct(ref m_AutoAlign, value)) SetComponentDirty(); }
}
/// <summary>
/// 对齐方式。
Expand Down Expand Up @@ -195,7 +203,8 @@ public void Copy(TextStyle textStyle)
fontStyle = textStyle.fontStyle;
lineSpacing = textStyle.lineSpacing;
alignment = textStyle.alignment;
wrap = textStyle.wrap;
autoWrap = textStyle.autoWrap;
autoAlign = textStyle.autoAlign;
#if dUI_TextMeshPro
m_TMPFont = textStyle.tmpFont;
m_TMPAlignment = textStyle.tmpAlignment;
Expand Down Expand Up @@ -223,5 +232,10 @@ public int GetFontSize(ComponentTheme defaultTheme)
if (fontSize == 0) return defaultTheme.fontSize;
else return fontSize;
}

public TextAnchor GetAlignment(TextAnchor systemAlignment)
{
return m_AutoAlign ? systemAlignment : alignment;
}
}
}
2 changes: 1 addition & 1 deletion Assets/XCharts/Runtime/Helper/LegendHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static LegendItem AddLegendItem(Legend legend, int i, string legendName,
ChartHelper.GetOrAddComponent<Image>(contentObj);
var txt = ChartHelper.AddTextObject("Text", contentObj.transform, anchorMin, anchorMax, pivot, sizeDelta,
textStyle, theme.legend);
txt.SetAlignment(TextAnchor.MiddleLeft);
txt.SetAlignment(textStyle.GetAlignment(TextAnchor.MiddleLeft));
txt.SetColor(contentColor);
var item = new LegendItem();
item.index = i;
Expand Down
27 changes: 9 additions & 18 deletions Assets/XCharts/Runtime/Internal/CoordinateChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,13 @@ private void InitYAxis(int yAxisIndex, YAxis yAxis)
{
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);
}
txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleLeft));
}
else
{
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);
}
txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleRight));
}
var labelWidth = AxisHelper.GetScaleWidth(yAxis, grid.runtimeHeight, i + 1, dataZoom);
if (i == 0) yAxis.axisLabel.SetRelatedText(txt.label, labelWidth);
Expand All @@ -604,7 +598,7 @@ private void InitYAxis(int yAxisIndex, YAxis yAxis)
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform, new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(100, 20), axisNameTextStyle,
m_Theme.axis);
axisName.SetAlignment(TextAnchor.MiddleCenter);
axisName.SetAlignment(axisNameTextStyle.GetAlignment(TextAnchor.MiddleCenter));
axisName.SetLocalPosition(yAxis.position == Axis.AxisPosition.Right ?
new Vector2(grid.runtimeX + grid.runtimeWidth + offset.x + yAxis.offset, grid.runtimeY - offset.y) :
new Vector2(zeroPos.x + offset.x + yAxis.offset, grid.runtimeY - offset.y));
Expand All @@ -613,7 +607,7 @@ private void InitYAxis(int yAxisIndex, YAxis yAxis)
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform, new Vector2(1, 0.5f),
new Vector2(1, 0.5f), new Vector2(1, 0.5f), new Vector2(100, 20), axisNameTextStyle,
m_Theme.axis);
axisName.SetAlignment(TextAnchor.MiddleRight);
axisName.SetAlignment(axisNameTextStyle.GetAlignment(TextAnchor.MiddleRight));
axisName.SetLocalPosition(yAxis.position == Axis.AxisPosition.Right ?
new Vector2(grid.runtimeX + grid.runtimeWidth - offset.x + yAxis.offset, grid.runtimeY + grid.runtimeHeight / 2 + offset.y) :
new Vector2(grid.runtimeX - offset.x + yAxis.offset, grid.runtimeY + grid.runtimeHeight / 2 + offset.y));
Expand All @@ -622,7 +616,7 @@ private void InitYAxis(int yAxisIndex, YAxis yAxis)
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform, new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(100, 20), axisNameTextStyle,
m_Theme.axis);
axisName.SetAlignment(TextAnchor.MiddleCenter);
axisName.SetAlignment(axisNameTextStyle.GetAlignment(TextAnchor.MiddleCenter));
axisName.SetLocalPosition(yAxis.position == Axis.AxisPosition.Right ?
new Vector2(grid.runtimeX + grid.runtimeWidth + offset.x + yAxis.offset, grid.runtimeY + grid.runtimeHeight + offset.y) :
new Vector2(zeroPos.x + offset.x + yAxis.offset, grid.runtimeY + grid.runtimeHeight + offset.y));
Expand Down Expand Up @@ -697,10 +691,7 @@ private void InitXAxis(int xAxisIndex, XAxis xAxis)
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.label.SetAlignment(axisLabelTextStyle.GetAlignment(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));
Expand All @@ -719,23 +710,23 @@ private void InitXAxis(int xAxisIndex, XAxis xAxis)
case AxisName.Location.Start:
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform, new Vector2(1, 0.5f),
new Vector2(1, 0.5f), new Vector2(1, 0.5f), new Vector2(100, 20), axisNameTextStyle, theme.axis);
axisName.SetAlignment(TextAnchor.MiddleRight);
axisName.SetAlignment(axisNameTextStyle.GetAlignment(TextAnchor.MiddleRight));
axisName.SetLocalPosition(xAxis.position == Axis.AxisPosition.Top ?
new Vector2(zeroPos.x - offset.x, grid.runtimeY + grid.runtimeHeight + offset.y + xAxis.offset) :
new Vector2(zeroPos.x - offset.x, zeroPos.y + offset.y + xAxis.offset));
break;
case AxisName.Location.Middle:
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform, new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(100, 20), axisNameTextStyle, theme.axis);
axisName.SetAlignment(TextAnchor.MiddleCenter);
axisName.SetAlignment(axisNameTextStyle.GetAlignment(TextAnchor.MiddleCenter));
axisName.SetLocalPosition(xAxis.position == Axis.AxisPosition.Top ?
new Vector2(grid.runtimeX + grid.runtimeWidth / 2 + offset.x, grid.runtimeY + grid.runtimeHeight - offset.y + xAxis.offset) :
new Vector2(grid.runtimeX + grid.runtimeWidth / 2 + offset.x, grid.runtimeY - offset.y + xAxis.offset));
break;
case AxisName.Location.End:
axisName = ChartHelper.AddTextObject(s_DefaultAxisName, axisObj.transform, new Vector2(0, 0.5f),
new Vector2(0, 0.5f), new Vector2(0, 0.5f), new Vector2(100, 20), axisNameTextStyle, theme.axis);
axisName.SetAlignment(TextAnchor.MiddleLeft);
axisName.SetAlignment(axisNameTextStyle.GetAlignment(TextAnchor.MiddleLeft));
axisName.SetLocalPosition(xAxis.position == Axis.AxisPosition.Top ?
new Vector2(grid.runtimeX + grid.runtimeWidth + offset.x, grid.runtimeY + grid.runtimeHeight + offset.y + xAxis.offset) :
new Vector2(grid.runtimeX + grid.runtimeWidth + offset.x, zeroPos.y + offset.y + xAxis.offset));
Expand Down
2 changes: 1 addition & 1 deletion Assets/XCharts/Runtime/Internal/DrawSerieRadar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void InitRadar(Radar radar)
new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(txtWid, txtHig),
textStyle, chart.theme.radar);
txt.gameObject.hideFlags = chart.chartHideFlags;
txt.SetAlignment(TextAnchor.MiddleCenter);
txt.SetAlignment(textStyle.GetAlignment(TextAnchor.MiddleCenter));
txt.SetText(radar.indicatorList[i].name);
txt.SetActive(radar.indicator);
var offset = new Vector3(textStyle.offset.x, textStyle.offset.y);
Expand Down
6 changes: 3 additions & 3 deletions Assets/XCharts/Runtime/Internal/Utility/ChartHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public static ChartText AddTextObject(string name, Transform parent, Vector2 anc
chartText.text.font = textStyle.font == null ? theme.font : textStyle.font;
chartText.text.fontStyle = textStyle.fontStyle;
chartText.text.alignment = textStyle.alignment;
chartText.text.horizontalOverflow = textStyle.wrap ? HorizontalWrapMode.Wrap : HorizontalWrapMode.Overflow;
chartText.text.horizontalOverflow = textStyle.autoWrap ? HorizontalWrapMode.Wrap : HorizontalWrapMode.Overflow;
chartText.text.verticalOverflow = VerticalWrapMode.Overflow;
chartText.text.supportRichText = true;
chartText.text.raycastTarget = false;
Expand Down Expand Up @@ -371,7 +371,7 @@ internal static GameObject AddTooltipContent(string name, Transform parent, Text
img.color = Color.black;
var txt = AddTextObject("Text", tooltipObj.transform, anchorMin, anchorMax, pivot, sizeDelta,
textStyle, theme.tooltip);
txt.SetAlignment(TextAnchor.UpperLeft);
txt.SetAlignment(textStyle.GetAlignment(TextAnchor.UpperLeft));
txt.SetText("Text");
txt.SetLocalPosition(new Vector2(3, -3));
tooltipObj.transform.localPosition = Vector3.zero;
Expand Down Expand Up @@ -434,7 +434,7 @@ internal static GameObject AddSerieLabel(string name, Transform parent, float wi
var txt = AddTextObject("Text", labelObj.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle,
theme.common);
txt.SetColor(color);
txt.SetAlignment(TextAnchor.MiddleCenter);
txt.SetAlignment(textStyle.alignment);
txt.SetText("Text");
txt.SetLocalPosition(new Vector2(0, 0));
txt.SetLocalEulerAngles(Vector3.zero);
Expand Down
Loading

0 comments on commit 6872d42

Please sign in to comment.