增加Formatter配置Tooltip的格式化输出

This commit is contained in:
monitor1394
2019-09-23 09:23:51 +08:00
parent 1793488521
commit f708080ed4
10 changed files with 18217 additions and 18114 deletions

View File

@@ -201,41 +201,46 @@ namespace XCharts
return;
}
sb.Length = 0;
if (!isCartesian)
if (string.IsNullOrEmpty(tooltip.formatter))
{
sb.Append(tempAxis.GetData(index, m_DataZoom));
}
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series.GetSerie(i);
if (serie.show)
sb.Length = 0;
if (!isCartesian)
{
string key = serie.name;
float xValue, yValue;
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
if (isCartesian)
sb.Append(tempAxis.GetData(index, m_DataZoom));
}
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series.GetSerie(i);
if (serie.show)
{
var serieData = serie.GetSerieData(index, m_DataZoom);
if (serieData != null && serieData.highlighted)
string key = serie.name;
float xValue, yValue;
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
if (isCartesian)
{
sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "");
sb.Append("[").Append(ChartCached.FloatToStr(xValue)).Append(",")
.Append(ChartCached.FloatToStr(yValue)).Append("]\n");
var serieData = serie.GetSerieData(index, m_DataZoom);
if (serieData != null && serieData.highlighted)
{
sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "");
sb.Append("[").Append(ChartCached.FloatToStr(xValue)).Append(",")
.Append(ChartCached.FloatToStr(yValue)).Append("]\n");
}
}
else
{
sb.Append("\n")
.Append("<color=#").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
.Append(ChartCached.FloatToStr(yValue));
}
}
else
{
sb.Append("\n")
.Append("<color=#").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
.Append(ChartCached.FloatToStr(yValue));
}
}
m_Tooltip.UpdateContentText(sb.ToString().Trim());
}
else
{
m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series, m_DataZoom));
}
m_Tooltip.UpdateContentText(sb.ToString().Trim());
var pos = m_Tooltip.GetContentPos();
if (pos.x + m_Tooltip.width > chartWidth)
{
@@ -1160,6 +1165,7 @@ namespace XCharts
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series.GetSerie(i);
var total = serie.yTotal;
for (int j = 0; j < serie.data.Count; j++)
{
var serieData = serie.data[j];
@@ -1167,7 +1173,7 @@ namespace XCharts
{
var pos = serie.dataPoints[j];
var value = serieData.data[1];
var content = serie.label.GetFormatterContent(serie.name, serieData.name, value);
var content = serie.label.GetFormatterContent(serie.name, serieData.name, value, total);
serieData.SetLabelActive(true);
serieData.SetLabelText(content);
serieData.SetLabelPosition(serieData.labelPosition);

View File

@@ -53,8 +53,7 @@ namespace XCharts
/// </summary>
public Text labelText { get; private set; }
public RectTransform labelRect { get; private set; }
public Vector3 labelPosition{get;set;}
//public Image labelImage { get; private set; }
public Vector3 labelPosition { get; set; }
/// <summary>
/// 是否可以显示Label
/// </summary>

View File

@@ -88,6 +88,7 @@ namespace XCharts
/// <item><description>{a}:系列名。</description></item>
/// <item><description>{b}:数据名。</description></item>
/// <item><description>{c}:数据值。</description></item>
/// <item><description>{d}:百分比。</description></item>
/// </list>
/// </summary>
/// <example>
@@ -182,7 +183,7 @@ namespace XCharts
/// </summary>
public Color borderColor { get { return m_BorderColor; } set { m_BorderColor = value; } }
public string GetFormatterContent(string serieName, string dataName, float dataValue)
public string GetFormatterContent(string serieName, string dataName, float dataValue, float dataTotal = 0)
{
if (string.IsNullOrEmpty(m_Formatter))
return ChartCached.FloatToStr(dataValue);
@@ -191,7 +192,13 @@ namespace XCharts
var content = m_Formatter.Replace("{a}", serieName);
content = content.Replace("{b}", dataName);
content = content.Replace("{c}", ChartCached.FloatToStr(dataValue));
if (dataTotal > 0)
{
var percent = dataValue / dataTotal * 100;
content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1));
}
content = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
return content;
}
}