mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 06:20:15 +00:00
增加Formatter配置Tooltip的格式化输出
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user