增加Formatter配置SerieLabel、Legend的格式化输出

This commit is contained in:
monitor1394
2019-09-20 12:38:45 +08:00
parent 1a3a1d2b03
commit 7757e396cb
8 changed files with 7319 additions and 7027 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -24,6 +24,7 @@ namespace XCharts
SerializedProperty itemHeight = prop.FindPropertyRelative("m_ItemHeight");
SerializedProperty itemGap = prop.FindPropertyRelative("m_ItemGap");
SerializedProperty itemFontSize = prop.FindPropertyRelative("m_ItemFontSize");
SerializedProperty m_Formatter = prop.FindPropertyRelative("m_Formatter");
SerializedProperty m_Data = prop.FindPropertyRelative("m_Data");
ChartEditorHelper.MakeFoldout(ref drawRect, ref m_LegendModuleToggle, "Legend", show);
@@ -45,6 +46,8 @@ namespace XCharts
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, location);
drawRect.y += EditorGUI.GetPropertyHeight(location);
EditorGUI.PropertyField(drawRect, m_Formatter);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
drawRect.width = EditorGUIUtility.labelWidth + 10;
m_DataFoldout = EditorGUI.Foldout(drawRect, m_DataFoldout, "Data");
ChartEditorHelper.MakeJsonData(ref drawRect, ref m_ShowJsonDataArea, ref m_JsonDataAreaText, prop,pos.width);
@@ -63,7 +66,7 @@ namespace XCharts
if (m_LegendModuleToggle)
{
SerializedProperty location = prop.FindPropertyRelative("m_Location");
height += 6 * EditorGUIUtility.singleLineHeight + 5 * EditorGUIUtility.standardVerticalSpacing;
height += 7 * EditorGUIUtility.singleLineHeight + 6 * EditorGUIUtility.standardVerticalSpacing;
height += EditorGUI.GetPropertyHeight(location);
height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
if (m_DataFoldout)

View File

@@ -15,6 +15,7 @@ namespace XCharts
drawRect.height = EditorGUIUtility.singleLineHeight;
SerializedProperty show = prop.FindPropertyRelative("m_Show");
SerializedProperty m_Position = prop.FindPropertyRelative("m_Position");
SerializedProperty m_Formatter = prop.FindPropertyRelative("m_Formatter");
SerializedProperty m_Distance = prop.FindPropertyRelative("m_Distance");
SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate");
SerializedProperty m_PaddingLeftRight = prop.FindPropertyRelative("m_PaddingLeftRight");
@@ -40,6 +41,8 @@ namespace XCharts
++EditorGUI.indentLevel;
EditorGUI.PropertyField(drawRect, m_Position);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_Formatter);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_Distance);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_Color);
@@ -83,7 +86,7 @@ namespace XCharts
float height = 0;
if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop))
{
height += 19 * EditorGUIUtility.singleLineHeight + 18 * EditorGUIUtility.standardVerticalSpacing;
height += 20 * EditorGUIUtility.singleLineHeight + 19 * EditorGUIUtility.standardVerticalSpacing;
}
else
{

View File

@@ -41,6 +41,7 @@ namespace XCharts
[SerializeField] private float m_ItemHeight = 20.0f;
[SerializeField] private float m_ItemGap = 5;
[SerializeField] private int m_ItemFontSize = 18;
[SerializeField] private string m_Formatter;
[SerializeField] private List<string> m_Data = new List<string>();
private Dictionary<string, Button> m_DataBtnList = new Dictionary<string, Button>();
@@ -87,6 +88,11 @@ namespace XCharts
/// </summary>
public int itemFontSize { get { return m_ItemFontSize; } set { m_ItemFontSize = value; } }
/// <summary>
/// 图例内容字符串模版格式器。支持用 \n 换行。
/// 模板变量为图例名称 {name}
/// </example>
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
/// <summary>
/// Data array of legend. An array item is usually a name representing string. (If it is a pie chart,
/// it could also be the name of a single data in the pie chart) of a series.
/// If data is not specified, it will be auto collected from series.
@@ -368,5 +374,17 @@ namespace XCharts
if (string.IsNullOrEmpty(jsonData) || !m_DataFromJson) return;
m_Data = ChartHelper.ParseStringFromString(jsonData);
}
public string GetFormatterContent(string category)
{
if (string.IsNullOrEmpty(m_Formatter))
return category;
else
{
var content = m_Formatter.Replace("{name}", category);
content = content.Replace("\\n","\n");
return content;
}
}
}
}

View File

@@ -204,11 +204,11 @@ namespace XCharts
m_Legend.RemoveButton();
for (int i = 0; i < datas.Count; i++)
{
string legendName = datas[i];
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i + "_" + legendName, legendObject.transform,
string legendName = m_Legend.GetFormatterContent(datas[i]);
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i + "_" + datas[i], legendObject.transform,
m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor,
anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight));
var bgColor = IsActiveByLegend(legendName) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor;
var bgColor = IsActiveByLegend(datas[i]) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor;
m_Legend.SetButton(legendName, btn, datas.Count);
m_Legend.UpdateButtonColor(legendName, bgColor);
btn.GetComponentInChildren<Text>().text = legendName;
@@ -256,6 +256,7 @@ namespace XCharts
{
for (int n = 0; n < datas.Count; n++)
{
var legendName = m_Legend.GetFormatterContent(datas[n]);
OnLegendButtonClick(n, datas[n], n == 0 ? true : false);
}
}

View File

@@ -1160,23 +1160,21 @@ namespace XCharts
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series.GetSerie(i);
if (serie.show)
for (int j = 0; j < serie.data.Count; j++)
{
for (int j = 0; j < serie.data.Count; j++)
var serieData = serie.data[j];
if (serie.show && serie.label.show && j < serie.dataPoints.Count)
{
var serieData = serie.data[j];
if (serie.label.show && j < serie.dataPoints.Count)
{
var pos = serie.dataPoints[j];
var value = serieData.data[1];
serieData.SetLabelActive(true);
serieData.SetLabelText(ChartCached.FloatToStr(value));
serieData.SetLabelPosition(serieData.labelPosition);
}
else
{
serieData.SetLabelActive(false);
}
var pos = serie.dataPoints[j];
var value = serieData.data[1];
var content = serie.label.GetFormatterContent(serie.name, serieData.name, value);
serieData.SetLabelActive(true);
serieData.SetLabelText(content);
serieData.SetLabelPosition(serieData.labelPosition);
}
else
{
serieData.SetLabelActive(false);
}
}
}

View File

@@ -54,6 +54,7 @@ namespace XCharts
[SerializeField] private bool m_Show = false;
[SerializeField] Position m_Position;
[SerializeField] private float m_Distance = 0;
[SerializeField] private string m_Formatter;
[SerializeField] private float m_Rotate = 0;
[SerializeField] private float m_PaddingLeftRight = 2f;
[SerializeField] private float m_PaddingTopBottom = 2f;
@@ -81,6 +82,19 @@ namespace XCharts
/// </summary>
public Position position { get { return m_Position; } set { m_Position = value; } }
/// <summary>
/// 标签内容字符串模版格式器。支持用 \n 换行。
/// 模板变量有:
/// <list type="bullet">
/// <item><description>{a}:系列名。</description></item>
/// <item><description>{b}:数据名。</description></item>
/// <item><description>{c}:数据值。</description></item>
/// </list>
/// </summary>
/// <example>
/// 示例:“{b}:{c}”
/// </example>
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
/// <summary>
/// Distance to the host graphic element. Works when position is Top,Left,Right,Bottom.
/// 距离图形元素的距离当position为TopLeftRightBottom时有效。
/// </summary>
@@ -167,5 +181,19 @@ namespace XCharts
/// 边框颜色。
/// </summary>
public Color borderColor { get { return m_BorderColor; } set { m_BorderColor = value; } }
public string GetFormatterContent(string serieName, string dataName, float dataValue)
{
if (string.IsNullOrEmpty(m_Formatter))
return ChartCached.FloatToStr(dataValue);
else
{
var content = m_Formatter.Replace("{a}", serieName);
content = content.Replace("{b}", dataName);
content = content.Replace("{c}", ChartCached.FloatToStr(dataValue));
content = content.Replace("\\n", "\n");
return content;
}
}
}
}

View File

@@ -23,6 +23,7 @@ QQ交流群XCharts交流群202030963
## 更新日志
* 2019.09.20)增加`Formatter`配置`SerieLabel``Legend`的格式化输出
* 2019.09.19)增加`LineArrow`配置带箭头曲线
* 2019.09.19)增加`Tooltip``FixedWidth``FixedHeight``MinWidth``MinHeight`设置支持
* 2019.09.18)增加单条堆叠柱状图