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

This commit is contained in:
monitor1394
2019-09-23 09:23:51 +08:00
parent 0b9abefda9
commit 4adda8da9d
11 changed files with 18220 additions and 18117 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,7 @@ namespace XCharts
drawRect.height = EditorGUIUtility.singleLineHeight; drawRect.height = EditorGUIUtility.singleLineHeight;
SerializedProperty show = prop.FindPropertyRelative("m_Show"); SerializedProperty show = prop.FindPropertyRelative("m_Show");
SerializedProperty type = prop.FindPropertyRelative("m_Type"); SerializedProperty type = prop.FindPropertyRelative("m_Type");
SerializedProperty m_Formatter = prop.FindPropertyRelative("m_Formatter");
SerializedProperty m_FixedWidth = prop.FindPropertyRelative("m_FixedWidth"); SerializedProperty m_FixedWidth = prop.FindPropertyRelative("m_FixedWidth");
SerializedProperty m_FixedHeight = prop.FindPropertyRelative("m_FixedHeight"); SerializedProperty m_FixedHeight = prop.FindPropertyRelative("m_FixedHeight");
SerializedProperty m_MinWidth = prop.FindPropertyRelative("m_MinWidth"); SerializedProperty m_MinWidth = prop.FindPropertyRelative("m_MinWidth");
@@ -25,6 +26,8 @@ namespace XCharts
{ {
EditorGUI.PropertyField(drawRect, type); EditorGUI.PropertyField(drawRect, type);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_Formatter);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_FixedWidth); EditorGUI.PropertyField(drawRect, m_FixedWidth);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_FixedHeight); EditorGUI.PropertyField(drawRect, m_FixedHeight);
@@ -39,7 +42,7 @@ namespace XCharts
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{ {
if (m_TooltipModuleToggle) if (m_TooltipModuleToggle)
return 6 * EditorGUIUtility.singleLineHeight + 5 * EditorGUIUtility.standardVerticalSpacing; return 7 * EditorGUIUtility.singleLineHeight + 6 * EditorGUIUtility.standardVerticalSpacing;
else else
return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
} }

View File

@@ -98,7 +98,7 @@ namespace XCharts
public bool show { get { return m_Show; } set { m_Show = value; } } public bool show { get { return m_Show; } set { m_Show = value; } }
/// <summary> /// <summary>
/// The type of dataZoom. /// The type of dataZoom.
/// 缩放区域的类型。 /// 区域缩放类型。
/// </summary> /// </summary>
public DataZoomType type { get { return m_Type; } set { m_Type = value; } } public DataZoomType type { get { return m_Type; } set { m_Type = value; } }
/// <summary> /// <summary>
@@ -113,7 +113,7 @@ namespace XCharts
public Orient orient { get { return m_Orient; } set { m_Orient = value; } } public Orient orient { get { return m_Orient; } set { m_Orient = value; } }
/// <summary> /// <summary>
/// Specify which xAxis is controlled by the dataZoom. /// Specify which xAxis is controlled by the dataZoom.
/// 控制哪一 x 轴。 /// 控制哪一 x 轴。
/// </summary> /// </summary>
public int xAxisIndex { get { return m_XAxisIndex; } set { m_XAxisIndex = value; } } public int xAxisIndex { get { return m_XAxisIndex; } set { m_XAxisIndex = value; } }
/// <summary> /// <summary>
@@ -142,7 +142,7 @@ namespace XCharts
/// <summary> /// <summary>
/// Whether to show data shadow in dataZoom-silder component, to indicate the data tendency in brief. /// Whether to show data shadow in dataZoom-silder component, to indicate the data tendency in brief.
/// default:true /// default:true
/// 如果设置为 true 则锁定选择区域的大小,也就是说,只能平移,不能缩放 /// 拖动时,是否实时更新系列的视图。如果设置为 false则只在拖拽结束的时候更新
/// </summary> /// </summary>
public bool realtime { get { return m_Realtime; } set { m_Realtime = value; } } public bool realtime { get { return m_Realtime; } set { m_Realtime = value; } }
/// <summary> /// <summary>

View File

@@ -383,6 +383,7 @@ namespace XCharts
{ {
var content = m_Formatter.Replace("{name}", category); var content = m_Formatter.Replace("{name}", category);
content = content.Replace("\\n", "\n"); content = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
return content; return content;
} }
} }

View File

@@ -14,7 +14,7 @@ namespace XCharts
{ {
/// <summary> /// <summary>
/// Indicator type. /// Indicator type.
/// 阴影指示器。 /// 指示器类型
/// </summary> /// </summary>
public enum Type public enum Type
{ {
@@ -42,6 +42,7 @@ namespace XCharts
[SerializeField] private bool m_Show; [SerializeField] private bool m_Show;
[SerializeField] private Type m_Type; [SerializeField] private Type m_Type;
[SerializeField] private string m_Formatter;
[SerializeField] private float m_FixedWidth = 0; [SerializeField] private float m_FixedWidth = 0;
[SerializeField] private float m_FixedHeight = 0; [SerializeField] private float m_FixedHeight = 0;
[SerializeField] private float m_MinWidth = 0; [SerializeField] private float m_MinWidth = 0;
@@ -65,6 +66,21 @@ namespace XCharts
/// </summary> /// </summary>
public Type type { get { return m_Type; } set { m_Type = value; } } public Type type { get { return m_Type; } set { m_Type = value; } }
/// <summary> /// <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 优先。 /// 固定宽度。比 minWidth 优先。
/// </summary> /// </summary>
public float fixedWidth { get { return m_FixedWidth; } set { m_FixedWidth = value; } } public float fixedWidth { get { return m_FixedWidth; } set { m_FixedWidth = value; } }
@@ -300,5 +316,62 @@ namespace XCharts
{ {
return dataIndex[0] == index || dataIndex[1] == index; 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;
}
}
} }
} }

View File

@@ -201,8 +201,9 @@ namespace XCharts
return; return;
} }
if (string.IsNullOrEmpty(tooltip.formatter))
{
sb.Length = 0; sb.Length = 0;
if (!isCartesian) if (!isCartesian)
{ {
sb.Append(tempAxis.GetData(index, m_DataZoom)); sb.Append(tempAxis.GetData(index, m_DataZoom));
@@ -235,7 +236,11 @@ namespace XCharts
} }
} }
m_Tooltip.UpdateContentText(sb.ToString().Trim()); m_Tooltip.UpdateContentText(sb.ToString().Trim());
}
else
{
m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series, m_DataZoom));
}
var pos = m_Tooltip.GetContentPos(); var pos = m_Tooltip.GetContentPos();
if (pos.x + m_Tooltip.width > chartWidth) if (pos.x + m_Tooltip.width > chartWidth)
{ {
@@ -1160,6 +1165,7 @@ namespace XCharts
for (int i = 0; i < m_Series.Count; i++) for (int i = 0; i < m_Series.Count; i++)
{ {
var serie = m_Series.GetSerie(i); var serie = m_Series.GetSerie(i);
var total = serie.yTotal;
for (int j = 0; j < serie.data.Count; j++) for (int j = 0; j < serie.data.Count; j++)
{ {
var serieData = serie.data[j]; var serieData = serie.data[j];
@@ -1167,7 +1173,7 @@ namespace XCharts
{ {
var pos = serie.dataPoints[j]; var pos = serie.dataPoints[j];
var value = serieData.data[1]; 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.SetLabelActive(true);
serieData.SetLabelText(content); serieData.SetLabelText(content);
serieData.SetLabelPosition(serieData.labelPosition); serieData.SetLabelPosition(serieData.labelPosition);

View File

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

View File

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

View File

@@ -449,6 +449,13 @@ namespace XCharts
} }
break; 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); serieData.SetLabelPosition(serieData.labelPosition);
} }
else else
@@ -568,9 +575,12 @@ namespace XCharts
int index = m_Tooltip.dataIndex[i]; int index = m_Tooltip.dataIndex[i];
if (index < 0) continue; if (index < 0) continue;
showTooltip = true; showTooltip = true;
if (string.IsNullOrEmpty(tooltip.formatter))
{
var serie = m_Series.GetSerie(i); var serie = m_Series.GetSerie(i);
string key = serie.data[index].name; string key = serie.data[index].name;
if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(index); if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(index);
float value = serie.data[index].data[1]; float value = serie.data[index].data[1];
sb.Length = 0; sb.Length = 0;
if (!string.IsNullOrEmpty(serie.name)) if (!string.IsNullOrEmpty(serie.name))
@@ -580,6 +590,11 @@ namespace XCharts
sb.Append("<color=#").Append(m_ThemeInfo.GetColorStr(index)).Append(">● </color>") sb.Append("<color=#").Append(m_ThemeInfo.GetColorStr(index)).Append(">● </color>")
.Append(key).Append(": ").Append(ChartCached.FloatToStr(value)); .Append(key).Append(": ").Append(ChartCached.FloatToStr(value));
m_Tooltip.UpdateContentText(sb.ToString()); m_Tooltip.UpdateContentText(sb.ToString());
}
else
{
m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series));
}
var pos = m_Tooltip.GetContentPos(); var pos = m_Tooltip.GetContentPos();
if (pos.x + m_Tooltip.width > chartWidth) if (pos.x + m_Tooltip.width > chartWidth)

View File

@@ -492,7 +492,6 @@ namespace XCharts
sb.AppendFormat("{0}: {1}", key, value); sb.AppendFormat("{0}: {1}", key, value);
} }
m_Tooltip.UpdateContentText(sb.ToString()); m_Tooltip.UpdateContentText(sb.ToString());
var pos = m_Tooltip.GetContentPos(); var pos = m_Tooltip.GetContentPos();
if (pos.x + m_Tooltip.width > chartWidth) if (pos.x + m_Tooltip.width > chartWidth)
{ {

View File

@@ -23,7 +23,7 @@ QQ交流群XCharts交流群202030963
## 更新日志 ## 更新日志
* 2019.09.20)增加`Formatter`配置`SerieLabel``Legend`的格式化输出 * 2019.09.23)增加`Formatter`配置`SerieLabel``Legend``Tooltip`的格式化输出
* 2019.09.19)增加`LineArrow`配置带箭头曲线 * 2019.09.19)增加`LineArrow`配置带箭头曲线
* 2019.09.19)增加`Tooltip``FixedWidth``FixedHeight``MinWidth``MinHeight`设置支持 * 2019.09.19)增加`Tooltip``FixedWidth``FixedHeight``MinWidth``MinHeight`设置支持
* 2019.09.18)增加单条堆叠柱状图 * 2019.09.18)增加单条堆叠柱状图
@@ -49,8 +49,8 @@ QQ交流群XCharts交流群202030963
* 2019.08.07)增加`SerieSymbol``Color``Opacity`配置 * 2019.08.07)增加`SerieSymbol``Color``Opacity`配置
* 2019.08.07)发布`v0.8.2`版本 * 2019.08.07)发布`v0.8.2`版本
* 2019.08.07)修复区域平滑折线图显示异常的问题 * 2019.08.07)修复区域平滑折线图显示异常的问题
* 2019.08.06修复serie系列数超过调色盘颜色数时获取的颜色异常的问题 * 2019.08.06)修复`serie`系列数超过调色盘颜色数时获取的颜色异常的问题
* 2019.08.06修复当AxisminMaxTypeCustommax设置为100不生效的问题 * 2019.08.06)修复当`Axis``minMaxType``Custom``max`设置为`100`不生效的问题
* 2019.08.04)发布`v0.8.1`版本 * 2019.08.04)发布`v0.8.1`版本
* 2019.08.04修复从Inspector中修改数据不生效的问题 * 2019.08.04修复从Inspector中修改数据不生效的问题
* 2019.08.04)发布`v0.8.0`版本 * 2019.08.04)发布`v0.8.0`版本