mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 22:40:10 +00:00
增加Formatter配置Tooltip的格式化输出
This commit is contained in:
@@ -98,7 +98,7 @@ namespace XCharts
|
||||
public bool show { get { return m_Show; } set { m_Show = value; } }
|
||||
/// <summary>
|
||||
/// The type of dataZoom.
|
||||
/// 缩放区域的类型。
|
||||
/// 区域缩放类型。
|
||||
/// </summary>
|
||||
public DataZoomType type { get { return m_Type; } set { m_Type = value; } }
|
||||
/// <summary>
|
||||
@@ -113,7 +113,7 @@ namespace XCharts
|
||||
public Orient orient { get { return m_Orient; } set { m_Orient = value; } }
|
||||
/// <summary>
|
||||
/// Specify which xAxis is controlled by the dataZoom.
|
||||
/// 控制哪个一 x 轴。
|
||||
/// 控制哪一个 x 轴。
|
||||
/// </summary>
|
||||
public int xAxisIndex { get { return m_XAxisIndex; } set { m_XAxisIndex = value; } }
|
||||
/// <summary>
|
||||
@@ -142,7 +142,7 @@ namespace XCharts
|
||||
/// <summary>
|
||||
/// Whether to show data shadow in dataZoom-silder component, to indicate the data tendency in brief.
|
||||
/// default:true
|
||||
/// 如果设置为 true 则锁定选择区域的大小,也就是说,只能平移,不能缩放。
|
||||
/// 拖动时,是否实时更新系列的视图。如果设置为 false,则只在拖拽结束的时候更新。
|
||||
/// </summary>
|
||||
public bool realtime { get { return m_Realtime; } set { m_Realtime = value; } }
|
||||
/// <summary>
|
||||
|
||||
@@ -382,7 +382,8 @@ namespace XCharts
|
||||
else
|
||||
{
|
||||
var content = m_Formatter.Replace("{name}", category);
|
||||
content = content.Replace("\\n","\n");
|
||||
content = content.Replace("\\n", "\n");
|
||||
content = content.Replace("<br/>", "\n");
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicator type.
|
||||
/// 阴影指示器。
|
||||
/// 指示器类型。
|
||||
/// </summary>
|
||||
public enum Type
|
||||
{
|
||||
@@ -42,6 +42,7 @@ namespace XCharts
|
||||
|
||||
[SerializeField] private bool m_Show;
|
||||
[SerializeField] private Type m_Type;
|
||||
[SerializeField] private string m_Formatter;
|
||||
[SerializeField] private float m_FixedWidth = 0;
|
||||
[SerializeField] private float m_FixedHeight = 0;
|
||||
[SerializeField] private float m_MinWidth = 0;
|
||||
@@ -65,6 +66,21 @@ namespace XCharts
|
||||
/// </summary>
|
||||
public Type type { get { return m_Type; } set { m_Type = value; } }
|
||||
/// <summary>
|
||||
/// 提示框内容字符串模版格式器。支持用 \n 或 "<br/>" 换行。
|
||||
/// 模板变量有 {a}, {b},{c},{d},{e},分别表示系列名,数据名,数据值等。
|
||||
/// 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:
|
||||
/// <list type="bullet">
|
||||
/// <item><description>折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)。</description></item>
|
||||
/// <item><description>散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)。</description></item>
|
||||
/// <item><description>地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)。</description></item>
|
||||
/// <item><description>饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)。</description></item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// 示例:“{a}:{c}”
|
||||
/// </example>
|
||||
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
|
||||
/// <summary>
|
||||
/// 固定宽度。比 minWidth 优先。
|
||||
/// </summary>
|
||||
public float fixedWidth { get { return m_FixedWidth; } set { m_FixedWidth = value; } }
|
||||
@@ -300,5 +316,62 @@ namespace XCharts
|
||||
{
|
||||
return dataIndex[0] == index || dataIndex[1] == index;
|
||||
}
|
||||
|
||||
public string GetFormatterContent(int dataIndex, Series series, DataZoom dataZoom = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(m_Formatter))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
{
|
||||
string content = m_Formatter;
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var serie = series.GetSerie(i);
|
||||
if (serie.show)
|
||||
{
|
||||
var serieData = serie.GetSerieData(dataIndex, dataZoom);
|
||||
if (i == 0)
|
||||
{
|
||||
content = content.Replace("{a}", serie.name);
|
||||
content = content.Replace("{b}", serieData.name);
|
||||
content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1)));
|
||||
if (serie.type == SerieType.Pie)
|
||||
{
|
||||
var percent = serieData.GetData(1) / serie.yTotal * 100;
|
||||
content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1));
|
||||
}
|
||||
}
|
||||
content = content.Replace("{a" + i + "}", serie.name);
|
||||
content = content.Replace("{b" + i + "}", serieData.name);
|
||||
content = content.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1)));
|
||||
if (serie.type == SerieType.Pie)
|
||||
{
|
||||
var percent = serieData.GetData(1) / serie.yTotal * 100;
|
||||
content = content.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
content = content.Replace("\\n", "\n");
|
||||
content = content.Replace("<br/>", "\n");
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
content = content.Replace("<br/>", "\n");
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,6 +449,13 @@ namespace XCharts
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(serie.label.formatter))
|
||||
{
|
||||
var value = serieData.data[1];
|
||||
var total = serie.yTotal;
|
||||
var content = serie.label.GetFormatterContent(serie.name, serieData.name, value, total);
|
||||
serieData.SetLabelText(content);
|
||||
}
|
||||
serieData.SetLabelPosition(serieData.labelPosition);
|
||||
}
|
||||
else
|
||||
@@ -519,7 +526,7 @@ namespace XCharts
|
||||
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
|
||||
RefreshTooltip();
|
||||
}
|
||||
else if(m_Tooltip.IsActive())
|
||||
else if (m_Tooltip.IsActive())
|
||||
{
|
||||
m_Tooltip.SetActive(false);
|
||||
RefreshChart();
|
||||
@@ -568,18 +575,26 @@ namespace XCharts
|
||||
int index = m_Tooltip.dataIndex[i];
|
||||
if (index < 0) continue;
|
||||
showTooltip = true;
|
||||
var serie = m_Series.GetSerie(i);
|
||||
string key = serie.data[index].name;
|
||||
if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(index);
|
||||
float value = serie.data[index].data[1];
|
||||
sb.Length = 0;
|
||||
if (!string.IsNullOrEmpty(serie.name))
|
||||
if (string.IsNullOrEmpty(tooltip.formatter))
|
||||
{
|
||||
sb.Append(serie.name).Append("\n");
|
||||
var serie = m_Series.GetSerie(i);
|
||||
string key = serie.data[index].name;
|
||||
if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(index);
|
||||
|
||||
float value = serie.data[index].data[1];
|
||||
sb.Length = 0;
|
||||
if (!string.IsNullOrEmpty(serie.name))
|
||||
{
|
||||
sb.Append(serie.name).Append("\n");
|
||||
}
|
||||
sb.Append("<color=#").Append(m_ThemeInfo.GetColorStr(index)).Append(">● </color>")
|
||||
.Append(key).Append(": ").Append(ChartCached.FloatToStr(value));
|
||||
m_Tooltip.UpdateContentText(sb.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series));
|
||||
}
|
||||
sb.Append("<color=#").Append(m_ThemeInfo.GetColorStr(index)).Append(">● </color>")
|
||||
.Append(key).Append(": ").Append(ChartCached.FloatToStr(value));
|
||||
m_Tooltip.UpdateContentText(sb.ToString());
|
||||
|
||||
var pos = m_Tooltip.GetContentPos();
|
||||
if (pos.x + m_Tooltip.width > chartWidth)
|
||||
|
||||
@@ -492,7 +492,6 @@ namespace XCharts
|
||||
sb.AppendFormat("{0}: {1}", key, value);
|
||||
}
|
||||
m_Tooltip.UpdateContentText(sb.ToString());
|
||||
|
||||
var pos = m_Tooltip.GetContentPos();
|
||||
if (pos.x + m_Tooltip.width > chartWidth)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user