mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-19 23:10:06 +00:00
增加LineChart的Label配置
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -18,6 +18,7 @@ namespace XCharts
|
||||
SerializedProperty m_Distance = prop.FindPropertyRelative("m_Distance");
|
||||
SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate");
|
||||
SerializedProperty m_Color = prop.FindPropertyRelative("m_Color");
|
||||
SerializedProperty m_BackgroundColor = prop.FindPropertyRelative("m_BackgroundColor");
|
||||
SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize");
|
||||
SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle");
|
||||
SerializedProperty m_Line = prop.FindPropertyRelative("m_Line");
|
||||
@@ -36,6 +37,8 @@ namespace XCharts
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_Color);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_BackgroundColor);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_Rotate);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_FontSize);
|
||||
@@ -59,7 +62,7 @@ namespace XCharts
|
||||
float height = 0;
|
||||
if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop))
|
||||
{
|
||||
height += 11 * EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing;
|
||||
height += 12 * EditorGUIUtility.singleLineHeight + 11 * EditorGUIUtility.standardVerticalSpacing;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -295,21 +295,32 @@ namespace XCharts
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
var serie = m_Series.series[i];
|
||||
if (serie.type != SerieType.Pie) continue;
|
||||
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++)
|
||||
{
|
||||
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;
|
||||
ChartHelper.SetActive(serieData.label.gameObject, false);
|
||||
var color = Color.grey;
|
||||
if (serie.type == SerieType.Pie)
|
||||
{
|
||||
color = (serie.label.position == SerieLabel.Position.Inside) ? Color.white :
|
||||
(Color)m_ThemeInfo.GetColor(count);
|
||||
}
|
||||
else
|
||||
{
|
||||
color = serie.label.color != Color.clear ? serie.label.color :
|
||||
(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>();
|
||||
serieData.SetLabelActive(false);
|
||||
serieData.SetLabelText(serieData.name);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,6 +342,8 @@ namespace XCharts
|
||||
|
||||
private void InitYAxis(int yAxisIndex, YAxis yAxis)
|
||||
{
|
||||
yAxis.minValue = 0;
|
||||
yAxis.maxValue = 100;
|
||||
yAxis.axisLabelTextList.Clear();
|
||||
float labelWidth = yAxis.GetScaleWidth(coordinateHig, m_DataZoom);
|
||||
string objName = yAxisIndex > 0 ? s_DefaultAxisY + "2" : s_DefaultAxisY;
|
||||
@@ -641,10 +643,7 @@ namespace XCharts
|
||||
|
||||
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis)
|
||||
{
|
||||
axis.minValue = 0;
|
||||
axis.maxValue = 0;
|
||||
if (axis.IsCategory()) return;
|
||||
|
||||
int tempMinValue = 0;
|
||||
int tempMaxValue = 100;
|
||||
if (m_XAxises[axisIndex].IsValue() && m_YAxises[axisIndex].IsValue())
|
||||
@@ -663,7 +662,7 @@ namespace XCharts
|
||||
m_Series.GetYMinMaxValue(m_DataZoom, axisIndex, out tempMinValue, out tempMaxValue);
|
||||
}
|
||||
axis.AdjustMinMaxValue(ref tempMinValue, ref tempMaxValue);
|
||||
|
||||
|
||||
if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue)
|
||||
{
|
||||
axis.minValue = tempMinValue;
|
||||
|
||||
@@ -48,6 +48,8 @@ namespace XCharts
|
||||
/// 该数据项的文本标签。
|
||||
/// </summary>
|
||||
public Text label { get; set; }
|
||||
public RectTransform labelRect { get; set; }
|
||||
public Image labelImage { get; set; }
|
||||
/// <summary>
|
||||
/// the maxinum value.
|
||||
/// 最大值。
|
||||
@@ -64,5 +66,31 @@ namespace XCharts
|
||||
if (index >= 0 && index < m_Data.Count) return m_Data[index];
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public void SetLabelActive(bool active)
|
||||
{
|
||||
if (labelImage)
|
||||
{
|
||||
ChartHelper.SetActive(labelImage.gameObject, active);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLabelText(string text)
|
||||
{
|
||||
if (label)
|
||||
{
|
||||
label.text = text;
|
||||
labelRect.sizeDelta = new Vector2(label.preferredWidth + 4,
|
||||
label.preferredHeight + 4);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLabelPosition(Vector3 position)
|
||||
{
|
||||
if (labelImage)
|
||||
{
|
||||
labelImage.transform.localPosition = position;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ namespace XCharts
|
||||
[SerializeField] private float m_Distance;
|
||||
[SerializeField] private float m_Rotate;
|
||||
[SerializeField] private Color m_Color;
|
||||
[SerializeField] private Color m_BackgroundColor;
|
||||
[SerializeField] private int m_FontSize = 18;
|
||||
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
|
||||
[SerializeField] private bool m_Line = true;
|
||||
@@ -82,6 +83,7 @@ namespace XCharts
|
||||
/// 自定义文字颜色,默认和系列的颜色一致。
|
||||
/// </summary>
|
||||
public Color color { get { return m_Color; } set { m_Color = value; } }
|
||||
public Color backgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; } }
|
||||
/// <summary>
|
||||
/// Rotate label.
|
||||
/// 标签旋转。
|
||||
|
||||
@@ -738,5 +738,43 @@ namespace XCharts
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnRefreshLabel()
|
||||
{
|
||||
var isYAxis = m_YAxises[0].type == Axis.AxisType.Category
|
||||
|| m_YAxises[1].type == Axis.AxisType.Category;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
var serie = m_Series.GetSerie(i);
|
||||
if (serie.type == SerieType.Line && serie.show)
|
||||
{
|
||||
for (int j = 0; j < serie.data.Count; j++)
|
||||
{
|
||||
var serieData = serie.data[j];
|
||||
if (serie.label.show)
|
||||
{
|
||||
var pos = serie.dataPoints[j];
|
||||
var value = serieData.data[1];
|
||||
serieData.SetLabelActive(true);
|
||||
serieData.SetLabelText(ChartCached.FloatToStr(value));
|
||||
if (isYAxis)
|
||||
{
|
||||
if (value >= 0) serieData.SetLabelPosition(new Vector3(pos.x + serie.label.distance, pos.y));
|
||||
else serieData.SetLabelPosition(new Vector3(pos.x - serie.label.distance, pos.y));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (value >= 0) serieData.SetLabelPosition(new Vector3(pos.x, pos.y + serie.label.distance));
|
||||
else serieData.SetLabelPosition(new Vector3(pos.x, pos.y - serie.label.distance));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
serieData.SetLabelActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,11 +337,11 @@ 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.label == null) return;
|
||||
var isHighlight = (serieData.highlighted && serie.highlightLabel.show);
|
||||
if (serie.label.show || isHighlight)
|
||||
{
|
||||
ChartHelper.SetActive(serieData.label.gameObject,true);
|
||||
serieData.SetLabelActive(true);
|
||||
float rotate = 0;
|
||||
bool isInsidePosition = serie.label.position == SerieLabel.Position.Inside;
|
||||
if (serie.label.rotate > 0 && isInsidePosition)
|
||||
@@ -370,19 +370,19 @@ namespace XCharts
|
||||
serieData.label.fontSize = fontSize;
|
||||
serieData.label.fontStyle = fontStyle;
|
||||
|
||||
serieData.label.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||
serieData.labelImage.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||
|
||||
|
||||
switch (serie.label.position)
|
||||
{
|
||||
case SerieLabel.Position.Center:
|
||||
serieData.label.transform.localPosition = tempData.center;
|
||||
serieData.labelImage.transform.localPosition = tempData.center;
|
||||
break;
|
||||
case SerieLabel.Position.Inside:
|
||||
var labelRadius = offsetRadius + insideRadius + (outsideRadius - insideRadius) / 2;
|
||||
var labelCenter = new Vector2(tempData.center.x + labelRadius * Mathf.Sin(currRad),
|
||||
tempData.center.y + labelRadius * Mathf.Cos(currRad));
|
||||
serieData.label.transform.localPosition = labelCenter;
|
||||
serieData.labelImage.transform.localPosition = labelCenter;
|
||||
break;
|
||||
case SerieLabel.Position.Outside:
|
||||
labelRadius = tempData.outsideRadius + serie.label.lineLength1;
|
||||
@@ -391,19 +391,18 @@ namespace XCharts
|
||||
float labelWidth = serieData.label.preferredWidth;
|
||||
if (currAngle > 180)
|
||||
{
|
||||
serieData.label.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);
|
||||
}
|
||||
else
|
||||
{
|
||||
serieData.label.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);
|
||||
}
|
||||
break;
|
||||
}
|
||||
serieData.label.gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
serieData.label.gameObject.SetActive(false);
|
||||
serieData.SetLabelActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -191,6 +191,24 @@ namespace XCharts
|
||||
return tooltipObj;
|
||||
}
|
||||
|
||||
public static GameObject AddSerieLabel(string name, Transform parent, Font font, Color textColor, Color backgroundColor,
|
||||
int fontSize, FontStyle fontStyle)
|
||||
{
|
||||
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);
|
||||
GameObject labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var img = GetOrAddComponent<Image>(labelObj);
|
||||
img.color = backgroundColor;
|
||||
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);
|
||||
labelObj.transform.localPosition = Vector3.zero;
|
||||
return labelObj;
|
||||
}
|
||||
|
||||
public static GameObject AddTooltipLabel(string name, Transform parent, Font font, Vector2 pivot)
|
||||
{
|
||||
var anchorMax = new Vector2(0, 0);
|
||||
|
||||
@@ -23,8 +23,10 @@ QQ交流群:XCharts交流群(202030963)
|
||||
|
||||
## 更新日志
|
||||
|
||||
* (2019.08.14)重构`BarChart`,移除`Bar`组件,相关参数统一放到`Serie`中配置。
|
||||
* (2019.08.15)重构`LineChart`,移除`Line`组件,相关参数统一放到`Serie`中配置。
|
||||
* (2019.08.15)增加`LineChart`的`Label`配置
|
||||
* (2019.08.15)重构`BarChart`,移除`Bar`组件,相关参数统一放到`Serie`中配置
|
||||
* (2019.08.15)重构`LineChart`,移除`Line`组件,相关参数统一放到`Serie`中配置
|
||||
* (2019.08.15)发布`v0.8.3`版本
|
||||
* (2019.08.14)修复`PieChart`的`Label`无法自动更新的问题
|
||||
* (2019.08.13)修复`UpdateData`接口无法更新数据的问题
|
||||
* (2019.08.07)增加`SerieSymbol`的`Color`、`Opacity`配置
|
||||
|
||||
Reference in New Issue
Block a user