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

This commit is contained in:
monitor1394
2019-09-20 12:38:45 +08:00
parent 8fad3b0f71
commit 9c653d3059
7 changed files with 7318 additions and 7027 deletions

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;
}
}
}
}