using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
///
/// Tooltip component.
/// 提示框组件
///
[System.Serializable]
public class Tooltip
{
///
/// Indicator type.
/// 指示器类型。
///
public enum Type
{
///
/// line indicator.
/// 直线指示器
///
Line,
///
/// shadow crosshair indicator.
/// 阴影指示器
///
Shadow,
///
/// no indicator displayed.
/// 无指示器
///
None,
///
/// crosshair indicator, which is actually the shortcut of enable two axisPointers of two orthometric axes.
/// 十字准星指示器。坐标轴显示Label和交叉线。
///
Corss
}
[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;
[SerializeField] private float m_MinHeight = 0;
private GameObject m_GameObject;
private GameObject m_Content;
private Text m_ContentText;
private RectTransform m_ContentRect;
private List lastDataIndex { get; set; }
///
/// Whether to show the tooltip component.
/// 是否显示提示框组件。
///
///
public bool show { get { return m_Show; } set { m_Show = value; SetActive(value); } }
///
/// Indicator type.
/// 提示框指示器类型。
///
public Type type { get { return m_Type; } set { m_Type = value; } }
///
/// 提示框内容字符串模版格式器。支持用 \n 或 "
" 换行。
/// 模板变量有 {a}, {b},{c},{d},{e},分别表示系列名,数据名,数据值等。
/// 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:
///
/// - 折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)。
/// - 散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)。
/// - 地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)。
/// - 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)。
///
///
///
/// 示例:“{a}:{c}”
///
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
///
/// 固定宽度。比 minWidth 优先。
///
public float fixedWidth { get { return m_FixedWidth; } set { m_FixedWidth = value; } }
///
/// 固定高度。比 minHeight 优先。
///
public float fixedHeight { get { return m_FixedHeight; } set { m_FixedHeight = value; } }
///
/// 最小宽度。如若 fixedWidth 设有值,优先取 fixedWidth。
///
public float minWidth { get { return m_MinWidth; } set { m_MinWidth = value; } }
///
/// 最小高度。如若 fixedHeight 设有值,优先取 fixedHeight。
///
public float minHeight { get { return m_MinHeight; } set { m_MinHeight = value; } }
///
/// The data index currently indicated by Tooltip.
/// 当前提示框所指示的数据项索引。
///
public List dataIndex { get; set; }
///
/// the value for x indicator label.
/// 指示器X轴上要显示的值。
///
public float[] xValues { get; set; }
///
/// the value for y indicator label.
/// 指示器Y轴上要显示的值。
///
public float[] yValues { get; set; }
///
/// the current pointer position.
/// 当前鼠标位置。
///
public Vector2 pointerPos { get; set; }
///
/// the width of tooltip.
/// 提示框宽。
///
public float width { get { return m_ContentRect.sizeDelta.x; } }
///
/// the height of tooltip.
/// 提示框高。
///
public float height { get { return m_ContentRect.sizeDelta.y; } }
///
/// Whether the tooltip has been initialized.
/// 提示框是否已初始化。
///
public bool inited { get { return m_GameObject != null; } }
///
/// the gameObject of tooltip.
/// 提示框的gameObject。
///
public GameObject gameObject { get { return m_GameObject; } }
public static Tooltip defaultTooltip
{
get
{
var tooltip = new Tooltip
{
m_Show = true,
xValues = new float[2],
yValues = new float[2],
dataIndex = new List() { -1, -1 },
lastDataIndex = new List() { -1, -1 }
};
return tooltip;
}
}
///
/// 绑定提示框gameObject
///
///
public void SetObj(GameObject obj)
{
m_GameObject = obj;
m_GameObject.SetActive(false);
}
///
/// 绑定提示框的文本框gameObject
///
///
public void SetContentObj(GameObject content)
{
m_Content = content;
m_ContentRect = m_Content.GetComponent();
m_ContentText = m_Content.GetComponentInChildren();
}
///
/// Keep Tooltiop displayed at the top.
/// 保持Tooltiop显示在最顶上
///
public void UpdateToTop()
{
int count = m_GameObject.transform.parent.childCount;
m_GameObject.GetComponent().SetSiblingIndex(count - 1);
}
///
/// 设置提示框文本背景色
///
///
public void SetContentBackgroundColor(Color color)
{
m_Content.GetComponent().color = color;
}
///
/// 设置提示框文本字体颜色
///
///
public void SetContentTextColor(Color color)
{
if (m_ContentText)
{
m_ContentText.color = color;
}
}
///
/// 设置提示框文本内容
///
///
public void UpdateContentText(string txt)
{
if (m_ContentText)
{
m_ContentText.text = txt;
float wid, hig;
if (m_FixedWidth > 0) wid = m_FixedWidth;
else if (m_MinWidth > 0 && m_ContentText.preferredWidth < m_MinWidth) wid = m_MinWidth;
else wid = m_ContentText.preferredWidth + 8;
if (m_FixedHeight > 0) hig = m_FixedHeight;
else if (m_MinHeight > 0 && m_ContentText.preferredHeight < m_MinHeight) hig = m_MinHeight;
else hig = m_ContentText.preferredHeight + 8;
m_ContentRect.sizeDelta = new Vector2(wid, hig);
}
}
///
/// 清除提示框指示数据
///
public void ClearValue()
{
dataIndex[0] = dataIndex[1] = -1;
xValues[0] = xValues[1] = -1;
yValues[0] = yValues[1] = -1;
}
///
/// 提示框是否显示
///
///
public bool IsActive()
{
return m_GameObject != null && m_GameObject.activeInHierarchy;
}
///
/// 设置提示框是否显示
///
///
public void SetActive(bool flag)
{
lastDataIndex[0] = lastDataIndex[1] = -1;
if (m_GameObject && m_GameObject.activeInHierarchy != flag)
m_GameObject.SetActive(flag);
}
///
/// 更新文本框位置
///
///
public void UpdateContentPos(Vector2 pos)
{
if (m_Content)
m_Content.transform.localPosition = pos;
}
///
/// 获得当前提示框的位置
///
///
public Vector3 GetContentPos()
{
if (m_Content)
return m_Content.transform.localPosition;
else
return Vector3.zero;
}
///
/// Whether the data item indicated by tooltip has changed.
/// 提示框所指示的数据项是否发生变化。
///
///
public bool IsDataIndexChanged()
{
return dataIndex[0] != lastDataIndex[0] ||
dataIndex[1] != lastDataIndex[1];
}
///
/// 当前索引缓存
///
public void UpdateLastDataIndex()
{
lastDataIndex[0] = dataIndex[0];
lastDataIndex[1] = dataIndex[1];
}
///
/// 当前提示框是否选中数据项
///
///
public bool IsSelected()
{
return dataIndex[0] >= 0 || dataIndex[1] >= 0;
}
///
/// 指定索引的数据项是否被提示框选中
///
///
///
public bool IsSelected(int index)
{
return dataIndex[0] == index || dataIndex[1] == index;
}
public string GetFormatterContent(int dataIndex, Series series, string category, 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 needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
var serieData = serie.GetSerieData(dataIndex, dataZoom);
if (i == 0)
{
content = content.Replace("{a}", serie.name);
content = content.Replace("{b}", needCategory ? category : 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 + "}", needCategory ? category : 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("
", "\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("
", "\n");
return content;
}
}
}
}