增加LineChart的Label配置

This commit is contained in:
monitor1394
2019-08-16 00:13:01 +08:00
parent 3e506f9576
commit b464f5b25f
9 changed files with 21259 additions and 8069 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -18,6 +18,7 @@ namespace XCharts
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_Color = prop.FindPropertyRelative("m_Color"); SerializedProperty m_Color = prop.FindPropertyRelative("m_Color");
SerializedProperty m_BackgroundColor = prop.FindPropertyRelative("m_BackgroundColor");
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");
@@ -36,6 +37,8 @@ namespace XCharts
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_Color); EditorGUI.PropertyField(drawRect, m_Color);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_BackgroundColor);
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_FontSize); EditorGUI.PropertyField(drawRect, m_FontSize);
@@ -59,7 +62,7 @@ namespace XCharts
float height = 0; float height = 0;
if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop)) if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop))
{ {
height += 11 * EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing; height += 12 * EditorGUIUtility.singleLineHeight + 11 * EditorGUIUtility.standardVerticalSpacing;
} }
else else
{ {

View File

@@ -295,21 +295,32 @@ 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) 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++) for (int j = 0; j < serie.data.Count; j++)
{ {
var serieData = serie.data[j]; var serieData = serie.data[j];
var textName = s_SerieLabelObjectName + "_" + i + "_" + j + "_" + serieData.name; var textName = s_SerieLabelObjectName + "_" + i + "_" + j + "_" + serieData.name;
var color = (serie.label.position == SerieLabel.Position.Inside) ? Color.white : var color = Color.grey;
(Color)m_ThemeInfo.GetColor(count++); if (serie.type == SerieType.Pie)
var anchorMin = new Vector2(0.5f, 0.5f); {
var anchorMax = new Vector2(0.5f, 0.5f); color = (serie.label.position == SerieLabel.Position.Inside) ? Color.white :
var pivot = new Vector2(0.5f, 0.5f); (Color)m_ThemeInfo.GetColor(count);
serieData.label = ChartHelper.AddTextObject(textName, labelObject.transform, }
m_ThemeInfo.font, color, TextAnchor.MiddleCenter, anchorMin, anchorMax, pivot, else
new Vector2(50, serie.label.fontSize), serie.label.fontSize); {
serieData.label.text = serieData.name; color = serie.label.color != Color.clear ? serie.label.color :
ChartHelper.SetActive(serieData.label.gameObject, false); (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++;
} }
} }
} }

View File

@@ -342,6 +342,8 @@ namespace XCharts
private void InitYAxis(int yAxisIndex, YAxis yAxis) private void InitYAxis(int yAxisIndex, YAxis yAxis)
{ {
yAxis.minValue = 0;
yAxis.maxValue = 100;
yAxis.axisLabelTextList.Clear(); yAxis.axisLabelTextList.Clear();
float labelWidth = yAxis.GetScaleWidth(coordinateHig, m_DataZoom); float labelWidth = yAxis.GetScaleWidth(coordinateHig, m_DataZoom);
string objName = yAxisIndex > 0 ? s_DefaultAxisY + "2" : s_DefaultAxisY; string objName = yAxisIndex > 0 ? s_DefaultAxisY + "2" : s_DefaultAxisY;
@@ -641,10 +643,7 @@ namespace XCharts
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis) private void UpdateAxisMinMaxValue(int axisIndex, Axis axis)
{ {
axis.minValue = 0;
axis.maxValue = 0;
if (axis.IsCategory()) return; if (axis.IsCategory()) return;
int tempMinValue = 0; int tempMinValue = 0;
int tempMaxValue = 100; int tempMaxValue = 100;
if (m_XAxises[axisIndex].IsValue() && m_YAxises[axisIndex].IsValue()) 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); m_Series.GetYMinMaxValue(m_DataZoom, axisIndex, out tempMinValue, out tempMaxValue);
} }
axis.AdjustMinMaxValue(ref tempMinValue, ref tempMaxValue); axis.AdjustMinMaxValue(ref tempMinValue, ref tempMaxValue);
if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue) if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue)
{ {
axis.minValue = tempMinValue; axis.minValue = tempMinValue;

View File

@@ -48,6 +48,8 @@ namespace XCharts
/// 该数据项的文本标签。 /// 该数据项的文本标签。
/// </summary> /// </summary>
public Text label { get; set; } public Text label { get; set; }
public RectTransform labelRect { get; set; }
public Image labelImage { get; set; }
/// <summary> /// <summary>
/// the maxinum value. /// the maxinum value.
/// 最大值。 /// 最大值。
@@ -64,5 +66,31 @@ namespace XCharts
if (index >= 0 && index < m_Data.Count) return m_Data[index]; if (index >= 0 && index < m_Data.Count) return m_Data[index];
else return 0; 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;
}
}
} }
} }

View File

@@ -56,6 +56,7 @@ namespace XCharts
[SerializeField] private float m_Distance; [SerializeField] private float m_Distance;
[SerializeField] private float m_Rotate; [SerializeField] private float m_Rotate;
[SerializeField] private Color m_Color; [SerializeField] private Color m_Color;
[SerializeField] private Color m_BackgroundColor;
[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;
@@ -82,6 +83,7 @@ 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; } }
public Color backgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; } }
/// <summary> /// <summary>
/// Rotate label. /// Rotate label.
/// 标签旋转。 /// 标签旋转。

View File

@@ -738,5 +738,43 @@ namespace XCharts
break; 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);
}
}
}
}
}
} }
} }

View File

@@ -337,11 +337,11 @@ 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.label == 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)
{ {
ChartHelper.SetActive(serieData.label.gameObject,true); serieData.SetLabelActive(true);
float rotate = 0; float rotate = 0;
bool isInsidePosition = serie.label.position == SerieLabel.Position.Inside; bool isInsidePosition = serie.label.position == SerieLabel.Position.Inside;
if (serie.label.rotate > 0 && isInsidePosition) if (serie.label.rotate > 0 && isInsidePosition)
@@ -370,19 +370,19 @@ namespace XCharts
serieData.label.fontSize = fontSize; serieData.label.fontSize = fontSize;
serieData.label.fontStyle = fontStyle; 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) switch (serie.label.position)
{ {
case SerieLabel.Position.Center: case SerieLabel.Position.Center:
serieData.label.transform.localPosition = tempData.center; serieData.labelImage.transform.localPosition = tempData.center;
break; break;
case SerieLabel.Position.Inside: case SerieLabel.Position.Inside:
var labelRadius = offsetRadius + insideRadius + (outsideRadius - insideRadius) / 2; var labelRadius = offsetRadius + insideRadius + (outsideRadius - insideRadius) / 2;
var labelCenter = new Vector2(tempData.center.x + labelRadius * Mathf.Sin(currRad), var labelCenter = new Vector2(tempData.center.x + labelRadius * Mathf.Sin(currRad),
tempData.center.y + labelRadius * Mathf.Cos(currRad)); tempData.center.y + labelRadius * Mathf.Cos(currRad));
serieData.label.transform.localPosition = labelCenter; serieData.labelImage.transform.localPosition = labelCenter;
break; break;
case SerieLabel.Position.Outside: case SerieLabel.Position.Outside:
labelRadius = tempData.outsideRadius + serie.label.lineLength1; labelRadius = tempData.outsideRadius + serie.label.lineLength1;
@@ -391,19 +391,18 @@ namespace XCharts
float labelWidth = serieData.label.preferredWidth; float labelWidth = serieData.label.preferredWidth;
if (currAngle > 180) 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 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; break;
} }
serieData.label.gameObject.SetActive(true);
} }
else else
{ {
serieData.label.gameObject.SetActive(false); serieData.SetLabelActive(false);
} }
} }

View File

@@ -191,6 +191,24 @@ namespace XCharts
return tooltipObj; 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) public static GameObject AddTooltipLabel(string name, Transform parent, Font font, Vector2 pivot)
{ {
var anchorMax = new Vector2(0, 0); var anchorMax = new Vector2(0, 0);