增加Tooltip指示器类型,优化显示控制

This commit is contained in:
monitor1394
2019-07-18 09:42:36 +08:00
parent f9a853dee4
commit 9ed5c5b277
15 changed files with 52998 additions and 217127 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -9,6 +9,7 @@ namespace XCharts
SerializedProperty m_Tickness; SerializedProperty m_Tickness;
SerializedProperty m_Point; SerializedProperty m_Point;
SerializedProperty m_PointWidth; SerializedProperty m_PointWidth;
SerializedProperty m_PointSelectedWidth;
SerializedProperty m_Smooth; SerializedProperty m_Smooth;
SerializedProperty m_SmoothStyle; SerializedProperty m_SmoothStyle;
SerializedProperty m_Area; SerializedProperty m_Area;
@@ -22,6 +23,7 @@ namespace XCharts
m_Tickness = prop.FindPropertyRelative("m_Tickness"); m_Tickness = prop.FindPropertyRelative("m_Tickness");
m_Point = prop.FindPropertyRelative("m_Point"); m_Point = prop.FindPropertyRelative("m_Point");
m_PointWidth = prop.FindPropertyRelative("m_PointWidth"); m_PointWidth = prop.FindPropertyRelative("m_PointWidth");
m_PointSelectedWidth = prop.FindPropertyRelative("m_PointSelectedWidth");
m_Smooth = prop.FindPropertyRelative("m_Smooth"); m_Smooth = prop.FindPropertyRelative("m_Smooth");
m_SmoothStyle = prop.FindPropertyRelative("m_SmoothStyle"); m_SmoothStyle = prop.FindPropertyRelative("m_SmoothStyle");
m_Area = prop.FindPropertyRelative("m_Area"); m_Area = prop.FindPropertyRelative("m_Area");
@@ -43,22 +45,18 @@ namespace XCharts
EditorGUI.PropertyField(drawRect, m_Tickness); EditorGUI.PropertyField(drawRect, m_Tickness);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
drawRect.width = EditorGUIUtility.labelWidth + 10;
EditorGUI.PropertyField(drawRect, m_Point); EditorGUI.PropertyField(drawRect, m_Point);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
if (m_Point.boolValue) if (m_Point.boolValue)
{ {
drawRect.x = EditorGUIUtility.labelWidth + 15; ++EditorGUI.indentLevel;
EditorGUI.LabelField(drawRect, "Width"); EditorGUI.PropertyField(drawRect, m_PointWidth);
drawRect.x = EditorGUIUtility.labelWidth + 65; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
float tempWidth = EditorGUIUtility.currentViewWidth - EditorGUIUtility.labelWidth - 70;
if (tempWidth < 20) tempWidth = 20;
drawRect.width = tempWidth;
EditorGUI.PropertyField(drawRect, m_PointWidth, GUIContent.none);
drawRect.x = pos.x;
drawRect.width = pos.width;
}
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_PointSelectedWidth);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
--EditorGUI.indentLevel;
}
drawRect.width = EditorGUIUtility.labelWidth + 10; drawRect.width = EditorGUIUtility.labelWidth + 10;
EditorGUI.PropertyField(drawRect, m_Smooth); EditorGUI.PropertyField(drawRect, m_Smooth);
if (m_Smooth.boolValue) if (m_Smooth.boolValue)
@@ -99,9 +97,15 @@ namespace XCharts
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{ {
float height = 0;
if (m_LineModuleToggle) if (m_LineModuleToggle)
{ {
return 6 * EditorGUIUtility.singleLineHeight + 5 * EditorGUIUtility.standardVerticalSpacing; height = 6 * EditorGUIUtility.singleLineHeight + 5 * EditorGUIUtility.standardVerticalSpacing;
var m_Point = prop.FindPropertyRelative("m_Point");
if(m_Point.boolValue){
height += 2 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
}
return height;
} }
else else
{ {

View File

@@ -13,13 +13,14 @@ namespace XCharts
Rect drawRect = pos; Rect drawRect = pos;
drawRect.height = EditorGUIUtility.singleLineHeight; drawRect.height = EditorGUIUtility.singleLineHeight;
SerializedProperty show = prop.FindPropertyRelative("m_Show"); SerializedProperty show = prop.FindPropertyRelative("m_Show");
SerializedProperty crossLabel = prop.FindPropertyRelative("m_CrossLabel"); SerializedProperty type = prop.FindPropertyRelative("m_Type");
SerializedProperty trigger = prop.FindPropertyRelative("m_Trigger");
ChartEditorHelper.MakeFoldout(ref drawRect, ref m_TooltipModuleToggle, "Tooltip", show); ChartEditorHelper.MakeFoldout(ref drawRect, ref m_TooltipModuleToggle, "Tooltip", show);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
if (m_TooltipModuleToggle) if (m_TooltipModuleToggle)
{ {
EditorGUI.PropertyField(drawRect, crossLabel); EditorGUI.PropertyField(drawRect, type);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
} }
} }
@@ -27,9 +28,9 @@ 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 2 * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing); return 2 * EditorGUIUtility.singleLineHeight + 1 * EditorGUIUtility.standardVerticalSpacing;
else else
return 1 * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing); return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
} }
} }
} }

View File

@@ -21,9 +21,12 @@ namespace XCharts
public float barWidth { get { return m_BarWidth; } set { m_BarWidth = value; } } public float barWidth { get { return m_BarWidth; } set { m_BarWidth = value; } }
public float space { get { return m_Space; } set { m_Space = value; } } public float space { get { return m_Space; } set { m_Space = value; } }
public static Bar defaultBar{ public static Bar defaultBar
get{ {
return new Bar(){ get
{
return new Bar()
{
m_InSameBar = false, m_InSameBar = false,
m_BarWidth = 0.6f, m_BarWidth = 0.6f,
m_Space = 10 m_Space = 10
@@ -42,6 +45,7 @@ namespace XCharts
base.Reset(); base.Reset();
m_Bar = Bar.defaultBar; m_Bar = Bar.defaultBar;
m_Title.text = "BarChart"; m_Title.text = "BarChart";
m_Tooltip.type = Tooltip.Type.Shadow;
RemoveData(); RemoveData();
AddSerie("serie1", SerieType.Line); AddSerie("serie1", SerieType.Line);
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
@@ -159,44 +163,7 @@ namespace XCharts
} }
} }
} }
if (m_Tooltip.show && m_Tooltip.dataIndex > 0) DrawYTooltipIndicator(vh);
{
if (m_Tooltip.crossLabel)
{
for (int i = 0; i < m_YAxises.Count; i++)
{
var yAxis = m_YAxises[i];
if (!yAxis.show) continue;
Vector3 sp = new Vector2(m_Tooltip.pointerPos.x, coordinateY);
Vector3 ep = new Vector2(m_Tooltip.pointerPos.x, coordinateY + coordinateHig);
DrawSplitLine(vh, false, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
float splitWidth = yAxis.GetSplitWidth(coordinateHig, m_DataZoom);
float pY = coordinateY + (m_Tooltip.yValues[i] - 1) * splitWidth +
(yAxis.boundaryGap ? splitWidth / 2 : 0);
sp = new Vector2(coordinateX, pY);
ep = new Vector2(coordinateX + coordinateWid, pY);
DrawSplitLine(vh, true, Axis.SplitLineType.Solid, sp, ep, m_ThemeInfo.tooltipLineColor);
}
}
else
{
for (int i = 0; i < m_YAxises.Count; i++)
{
var yAxis = m_YAxises[i];
if (!yAxis.show) continue;
float splitWidth = yAxis.GetSplitWidth(coordinateHig, m_DataZoom);
float tooltipSplitWid = splitWidth < 1 ? 1 : splitWidth;
float pX = coordinateX + coordinateWid;
float pY = coordinateY + splitWidth * (m_Tooltip.yValues[i] - 1) -
(yAxis.boundaryGap ? 0 : splitWidth / 2);
Vector3 p1 = new Vector3(coordinateX, pY);
Vector3 p2 = new Vector3(coordinateX, pY + tooltipSplitWid);
Vector3 p3 = new Vector3(pX, pY + tooltipSplitWid);
Vector3 p4 = new Vector3(pX, pY);
ChartHelper.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.tooltipFlagAreaColor);
}
}
}
} }
else else
{ {
@@ -218,45 +185,7 @@ namespace XCharts
} }
} }
} }
if (m_Tooltip.show && m_Tooltip.dataIndex > 0) DrawXTooltipIndicator(vh);
{
if (m_Tooltip.crossLabel)
{
for (int i = 0; i < m_XAxises.Count; i++)
{
var xAxis = m_XAxises[i];
if (!xAxis.show) continue;
Vector3 sp = new Vector2(coordinateX, m_Tooltip.pointerPos.y);
Vector3 ep = new Vector2(coordinateX + coordinateWid, m_Tooltip.pointerPos.y);
DrawSplitLine(vh, true, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
float splitWidth = xAxis.GetSplitWidth(coordinateWid, m_DataZoom);
float px = coordinateX + (m_Tooltip.xValues[i] - 1) * splitWidth
+ (xAxis.boundaryGap ? splitWidth / 2 : 0);
sp = new Vector2(px, coordinateY);
ep = new Vector2(px, coordinateY + coordinateHig);
DrawSplitLine(vh, false, Axis.SplitLineType.Solid, sp, ep, m_ThemeInfo.tooltipLineColor);
}
}
else
{
for (int i = 0; i < m_XAxises.Count; i++)
{
var xAxis = m_XAxises[i];
if (!xAxis.show) continue;
float splitWidth = xAxis.GetSplitWidth(coordinateWid, m_DataZoom);
float tooltipSplitWid = splitWidth < 1 ? 1 : splitWidth;
float pX = coordinateX + splitWidth * (m_Tooltip.xValues[i] - 1) -
(xAxis.boundaryGap ? 0 : splitWidth / 2);
float pY = coordinateY + coordinateHig;
Vector3 p1 = new Vector3(pX, coordinateY);
Vector3 p2 = new Vector3(pX, pY);
Vector3 p3 = new Vector3(pX + tooltipSplitWid, pY);
Vector3 p4 = new Vector3(pX + tooltipSplitWid, coordinateY);
ChartHelper.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.tooltipFlagAreaColor);
}
}
}
} }
} }
} }

View File

@@ -12,7 +12,7 @@ namespace XCharts
Vertical Vertical
} }
public class BaseChart : MaskableGraphic, IPointerDownHandler, IPointerUpHandler, public class BaseChart : Graphic, IPointerDownHandler, IPointerUpHandler,
IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler,
IDragHandler, IEndDragHandler, IScrollHandler IDragHandler, IEndDragHandler, IScrollHandler
{ {
@@ -232,7 +232,7 @@ namespace XCharts
/// <returns>True when activated</returns> /// <returns>True when activated</returns>
public virtual bool IsActive(int serieIndex) public virtual bool IsActive(int serieIndex)
{ {
return m_Legend.IsActive(serieIndex) || m_Series.IsActive(serieIndex); return m_Legend.IsActive(serieIndex) && m_Series.IsActive(serieIndex);
} }
/// <summary> /// <summary>
@@ -284,6 +284,18 @@ namespace XCharts
CheckRefreshChart(); CheckRefreshChart();
} }
protected override void OnEnable()
{
base.OnEnable();
Awake();
}
protected override void OnDisable()
{
base.OnDisable();
ChartHelper.DestoryAllChilds(transform);
}
#if UNITY_EDITOR #if UNITY_EDITOR
protected override void Reset() protected override void Reset()
{ {
@@ -453,17 +465,18 @@ namespace XCharts
{ {
if (!m_Tooltip.show || !m_Tooltip.isInited) if (!m_Tooltip.show || !m_Tooltip.isInited)
{ {
if (m_Tooltip.dataIndex != 0)
if (m_Tooltip.dataIndex[0] != 0 || m_Tooltip.dataIndex[1] != 0)
{ {
m_Tooltip.dataIndex = 0; m_Tooltip.dataIndex[0] = m_Tooltip.dataIndex[1] = -1;
m_Tooltip.SetActive(false); m_Tooltip.SetActive(false);
RefreshChart(); RefreshChart();
} }
return; return;
} }
m_Tooltip.dataIndex = 0; m_Tooltip.dataIndex[0] = m_Tooltip.dataIndex[1] = -1;
Vector2 local; Vector2 local;
if (canvas == null) return;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
Input.mousePosition, canvas.worldCamera, out local)) Input.mousePosition, canvas.worldCamera, out local))

View File

@@ -1,4 +1,5 @@
using UnityEngine; using System;
using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -66,22 +67,44 @@ namespace XCharts
} }
/// <summary> /// <summary>
/// /// Add a category data to xAxis.
/// </summary> /// </summary>
/// <param name="category"></param> /// <param name="category">the category data</param>
/// <param name="xAxisIndex"></param> /// <param name="xAxisIndex">which xAxis should category add to</param>
public void AddXAxisData(string category, int xAxisIndex = 0) public void AddXAxisData(string category, int xAxisIndex = 0)
{ {
m_XAxises[xAxisIndex].AddData(category, m_MaxCacheDataNumber); m_XAxises[xAxisIndex].AddData(category, m_MaxCacheDataNumber);
OnXAxisChanged(); OnXAxisChanged();
} }
/// <summary>
/// Add a category data to yAxis.
/// </summary>
/// <param name="category">the category data</param>
/// <param name="yAxisIndex">which yAxis should category add to</param>
public void AddYAxisData(string category, int yAxisIndex = 0) public void AddYAxisData(string category, int yAxisIndex = 0)
{ {
m_YAxises[yAxisIndex].AddData(category, m_MaxCacheDataNumber); m_YAxises[yAxisIndex].AddData(category, m_MaxCacheDataNumber);
OnYAxisChanged(); OnYAxisChanged();
} }
/// <summary>
/// Whether is Cartesian coordinates
/// </summary>
/// <returns>reutrn true when all show axis is value type</returns>
public bool IsCartesian()
{
foreach (var axis in m_XAxises)
{
if (axis.show && !axis.IsValue()) return false;
}
foreach (var axis in m_YAxises)
{
if (axis.show && !axis.IsValue()) return false;
}
return true;
}
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
@@ -131,17 +154,44 @@ namespace XCharts
} }
else else
{ {
var isCartesian = IsCartesian();
for (int i = 0; i < m_XAxises.Count; i++) for (int i = 0; i < m_XAxises.Count; i++)
{ {
var xAxis = m_XAxises[i]; var xAxis = m_XAxises[i];
if (xAxis.IsValue()) var yAxis = m_YAxises[i];
if(!xAxis.show && !yAxis.show) continue;
if (isCartesian && xAxis.show && yAxis.show)
{ {
var value = (xAxis.maxValue - xAxis.minValue) * (local.x - coordinateX - xAxis.zeroXOffset) / coordinateWid; var yRate = (yAxis.maxValue - yAxis.minValue) / coordinateHig;
if (xAxis.minValue > 0) value += xAxis.minValue; var xRate = (xAxis.maxValue - xAxis.minValue) / coordinateWid;
m_Tooltip.xValues[i] = value; var yValue = yRate * (local.y - coordinateY - yAxis.zeroYOffset);
if (yAxis.minValue > 0) yValue += yAxis.minValue;
m_Tooltip.yValues[i] = yValue;
var xValue = xRate * (local.x - coordinateX - xAxis.zeroXOffset);
if (xAxis.minValue > 0) xValue += xAxis.minValue;
m_Tooltip.xValues[i] = xValue;
for (int j = 0; j < m_Series.Count; j++)
{
var serie = m_Series.GetSerie(j);
serie.selected = false;
for (int n = 0; n < serie.xData.Count; n++)
{
var xdata = serie.xData[n];
var ydata = serie.yData[n];
if (Mathf.Abs(xValue - xdata) / xRate < 5 && Mathf.Abs(yValue - ydata) / yRate < 5)
{
m_Tooltip.dataIndex[i] = n;
serie.selected = true;
}
}
}
} }
else else if (xAxis.IsCategory())
{ {
var value = (yAxis.maxValue - yAxis.minValue) * (local.y - coordinateY - yAxis.zeroYOffset) / coordinateHig;
if (yAxis.minValue > 0) value += yAxis.minValue;
m_Tooltip.yValues[i] = value;
for (int j = 0; j < xAxis.GetDataNumber(m_DataZoom); j++) for (int j = 0; j < xAxis.GetDataNumber(m_DataZoom); j++)
{ {
float splitWid = xAxis.GetDataWidth(coordinateWid, m_DataZoom); float splitWid = xAxis.GetDataWidth(coordinateWid, m_DataZoom);
@@ -149,24 +199,17 @@ namespace XCharts
if ((xAxis.boundaryGap && (local.x > pX && local.x <= pX + splitWid)) || if ((xAxis.boundaryGap && (local.x > pX && local.x <= pX + splitWid)) ||
(!xAxis.boundaryGap && (local.x > pX - splitWid / 2 && local.x <= pX + splitWid / 2))) (!xAxis.boundaryGap && (local.x > pX - splitWid / 2 && local.x <= pX + splitWid / 2)))
{ {
m_Tooltip.xValues[i] = j + 1; m_Tooltip.xValues[i] = j;
if (i == 0) m_Tooltip.dataIndex = j + 1; m_Tooltip.dataIndex[i] = j;
break; break;
} }
} }
} }
} else if (yAxis.IsCategory())
for (int i = 0; i < m_YAxises.Count; i++)
{
var yAxis = m_YAxises[i];
if (yAxis.IsValue())
{
var value = (yAxis.maxValue - yAxis.minValue) * (local.y - coordinateY - yAxis.zeroYOffset) / coordinateHig;
if (yAxis.minValue > 0) value += yAxis.minValue;
m_Tooltip.yValues[i] = value;
}
else
{ {
var value = (xAxis.maxValue - xAxis.minValue) * (local.x - coordinateX - xAxis.zeroXOffset) / coordinateWid;
if (xAxis.minValue > 0) value += xAxis.minValue;
m_Tooltip.xValues[i] = value;
for (int j = 0; j < yAxis.GetDataNumber(m_DataZoom); j++) for (int j = 0; j < yAxis.GetDataNumber(m_DataZoom); j++)
{ {
float splitWid = yAxis.GetDataWidth(coordinateHig, m_DataZoom); float splitWid = yAxis.GetDataWidth(coordinateHig, m_DataZoom);
@@ -174,23 +217,25 @@ namespace XCharts
if ((yAxis.boundaryGap && (local.y > pY && local.y <= pY + splitWid)) || if ((yAxis.boundaryGap && (local.y > pY && local.y <= pY + splitWid)) ||
(!yAxis.boundaryGap && (local.y > pY - splitWid / 2 && local.y <= pY + splitWid / 2))) (!yAxis.boundaryGap && (local.y > pY - splitWid / 2 && local.y <= pY + splitWid / 2)))
{ {
m_Tooltip.yValues[i] = j + 1; m_Tooltip.yValues[i] = j;
if (i == 0) m_Tooltip.dataIndex = j + 1; m_Tooltip.dataIndex[i] = j;
break; break;
} }
} }
} }
} }
} }
if (m_Tooltip.dataIndex > 0) if (m_Tooltip.IsSelected())
{ {
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25)); m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
RefreshTooltip(); RefreshTooltip();
if (m_Tooltip.lastDataIndex != m_Tooltip.dataIndex || m_Tooltip.crossLabel) if (m_Tooltip.IsDataIndexChanged() || m_Tooltip.type == Tooltip.Type.Corss)
{ {
m_Tooltip.UpdateLastDataIndex();
RefreshChart(); RefreshChart();
} }
m_Tooltip.lastDataIndex = m_Tooltip.dataIndex; }else{
m_Tooltip.SetActive(false);
} }
} }
@@ -199,14 +244,20 @@ namespace XCharts
base.RefreshTooltip(); base.RefreshTooltip();
int index; int index;
Axis tempAxis; Axis tempAxis;
if (m_XAxises[0].type == Axis.AxisType.Value) bool isCartesian = IsCartesian();
if (isCartesian)
{ {
index = (int)m_Tooltip.yValues[0] - 1; index = m_Tooltip.dataIndex[0];
tempAxis = m_XAxises[0];
}
else if (m_XAxises[0].type == Axis.AxisType.Value)
{
index = (int)m_Tooltip.yValues[0];
tempAxis = m_YAxises[0]; tempAxis = m_YAxises[0];
} }
else else
{ {
index = (int)m_Tooltip.xValues[0] - 1; index = (int)m_Tooltip.xValues[0];
tempAxis = m_XAxises[0]; tempAxis = m_XAxises[0];
} }
if (index < 0) if (index < 0)
@@ -214,30 +265,39 @@ namespace XCharts
m_Tooltip.SetActive(false); m_Tooltip.SetActive(false);
return; return;
} }
if (m_Series.Count == 1)
StringBuilder sb = new StringBuilder();
if (!isCartesian)
{ {
float value = m_Series.GetData(0, index); sb.Append(tempAxis.GetData(index, m_DataZoom));
string txt = tempAxis.GetData(index, m_DataZoom) + ": " + value;
m_Tooltip.UpdateContentText(txt);
} }
else for (int i = 0; i < m_Series.Count; i++)
{ {
StringBuilder sb = new StringBuilder(tempAxis.GetData(index, m_DataZoom)); var serie = m_Series.GetSerie(i);
for (int i = 0; i < m_Series.Count; i++) if (serie.show)
{ {
if (m_Series.series[i].show) string key = serie.name;
//if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(i);
if(!string.IsNullOrEmpty(key)) key += ":";
float xValue, yValue;
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
if (isCartesian)
{
if (serie.selected)
{
sb.AppendFormat("{0}[{1}, {2}]\n", key, xValue, yValue);
}
}
else
{ {
string strColor = ColorUtility.ToHtmlStringRGBA(m_ThemeInfo.GetColor(i)); string strColor = ColorUtility.ToHtmlStringRGBA(m_ThemeInfo.GetColor(i));
string key = m_Series.series[i].name;
if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(i);
float value = m_Series.series[i].GetYData(index, m_DataZoom);
sb.Append("\n"); sb.Append("\n");
sb.AppendFormat("<color=#{0}>● </color>", strColor); sb.AppendFormat("<color=#{0}>● </color>", strColor);
sb.AppendFormat("{0}: {1}", key, value); sb.AppendFormat("{0}{1}", key, yValue);
} }
} }
m_Tooltip.UpdateContentText(sb.ToString());
} }
m_Tooltip.UpdateContentText(sb.ToString().Trim());
var pos = m_Tooltip.GetContentPos(); var pos = m_Tooltip.GetContentPos();
if (pos.x + m_Tooltip.width > chartWidth) if (pos.x + m_Tooltip.width > chartWidth)
@@ -263,7 +323,8 @@ namespace XCharts
private void UpdateAxisTooltipLabel(int axisIndex, Axis axis) private void UpdateAxisTooltipLabel(int axisIndex, Axis axis)
{ {
axis.SetTooltipLabelActive(axis.show && m_Tooltip.crossLabel); var showTooltipLabel = axis.show && m_Tooltip.type == Tooltip.Type.Corss;
axis.SetTooltipLabelActive(showTooltipLabel);
string labelText = ""; string labelText = "";
Vector2 labelPos = Vector2.zero; Vector2 labelPos = Vector2.zero;
if (axis is XAxis) if (axis is XAxis)
@@ -277,9 +338,9 @@ namespace XCharts
} }
else else
{ {
labelText = axis.GetData((int)m_Tooltip.xValues[axisIndex] - 1, m_DataZoom); labelText = axis.GetData((int)m_Tooltip.xValues[axisIndex], m_DataZoom);
float splitWidth = axis.GetSplitWidth(coordinateWid, m_DataZoom); float splitWidth = axis.GetSplitWidth(coordinateWid, m_DataZoom);
int index = (int)m_Tooltip.xValues[axisIndex] - 1; int index = (int)m_Tooltip.xValues[axisIndex];
float px = coordinateX + index * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0) + 0.5f; float px = coordinateX + index * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0) + 0.5f;
labelPos = new Vector2(px, posY - diff); labelPos = new Vector2(px, posY - diff);
} }
@@ -295,9 +356,9 @@ namespace XCharts
} }
else else
{ {
labelText = axis.GetData((int)m_Tooltip.yValues[axisIndex] - 1, m_DataZoom); labelText = axis.GetData((int)m_Tooltip.yValues[axisIndex], m_DataZoom);
float splitWidth = axis.GetSplitWidth(coordinateHig, m_DataZoom); float splitWidth = axis.GetSplitWidth(coordinateHig, m_DataZoom);
int index = (int)m_Tooltip.yValues[axisIndex] - 1; int index = (int)m_Tooltip.yValues[axisIndex];
float py = coordinateY + index * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0); float py = coordinateY + index * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0);
labelPos = new Vector2(posX - diff, py); labelPos = new Vector2(posX - diff, py);
} }
@@ -433,7 +494,7 @@ namespace XCharts
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot); GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
yAxis.SetTooltipLabel(labelObj); yAxis.SetTooltipLabel(labelObj);
yAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor); yAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
yAxis.SetTooltipLabelActive(yAxis.show && m_Tooltip.show && m_Tooltip.crossLabel); yAxis.SetTooltipLabelActive(yAxis.show && m_Tooltip.show && m_Tooltip.type == Tooltip.Type.Corss);
} }
} }
@@ -520,7 +581,7 @@ namespace XCharts
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot); GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
xAxis.SetTooltipLabel(labelObj); xAxis.SetTooltipLabel(labelObj);
xAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor); xAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
xAxis.SetTooltipLabelActive(xAxis.show && m_Tooltip.show && m_Tooltip.crossLabel); xAxis.SetTooltipLabelActive(xAxis.show && m_Tooltip.show && m_Tooltip.type == Tooltip.Type.Corss);
} }
} }
@@ -640,7 +701,7 @@ namespace XCharts
private void UpdateAxisMinMaxValue(int axisIndex, Axis axis) private void UpdateAxisMinMaxValue(int axisIndex, Axis axis)
{ {
axis.minValue = 0; axis.minValue = 0;
axis.maxValue = 0; axis.maxValue = 100;
if (axis.IsCategory()) return; if (axis.IsCategory()) return;
int tempMinValue = 0; int tempMinValue = 0;
@@ -661,6 +722,7 @@ namespace XCharts
m_Series.GetYMinMaxValue(m_DataZoom, axisIndex, out tempMinValue, out tempMaxValue); m_Series.GetYMinMaxValue(m_DataZoom, axisIndex, out tempMinValue, out tempMaxValue);
} }
axis.AdjustMinMaxValue(ref tempMinValue, ref tempMaxValue); axis.AdjustMinMaxValue(ref tempMinValue, ref tempMaxValue);
if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue) if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue)
{ {
axis.minValue = tempMinValue; axis.minValue = tempMinValue;
@@ -827,15 +889,15 @@ namespace XCharts
{ {
var lineY = coordinateY + (xAxis.axisLine.onZero ? m_YAxises[xAxisIndex].zeroYOffset : 0); var lineY = coordinateY + (xAxis.axisLine.onZero ? m_YAxises[xAxisIndex].zeroYOffset : 0);
if (xAxis.IsValue() && xAxisIndex > 0) lineY += coordinateHig; if (xAxis.IsValue() && xAxisIndex > 0) lineY += coordinateHig;
var left = new Vector3(coordinateX - m_Coordinate.tickness, lineY);
var top = new Vector3(coordinateX + coordinateWid + m_Coordinate.tickness, lineY); var top = new Vector3(coordinateX + coordinateWid + m_Coordinate.tickness, lineY);
ChartHelper.DrawLine(vh, new Vector3(coordinateX - m_Coordinate.tickness, lineY), ChartHelper.DrawLine(vh, left,top, m_Coordinate.tickness, m_ThemeInfo.axisLineColor);
top, m_Coordinate.tickness, m_ThemeInfo.axisLineColor);
if (xAxis.axisLine.symbol) if (xAxis.axisLine.symbol)
{ {
var axisLine = xAxis.axisLine; var axisLine = xAxis.axisLine;
top.x += xAxis.axisLine.symbolOffset; top.x += xAxis.axisLine.symbolOffset;
var middle = new Vector3(top.x - axisLine.symbolHeight + axisLine.symbolDent, lineY); var middle = new Vector3(top.x - axisLine.symbolHeight + axisLine.symbolDent, lineY);
var left = new Vector3(top.x - axisLine.symbolHeight, lineY - axisLine.symbolWidth / 2); left = new Vector3(top.x - axisLine.symbolHeight, lineY - axisLine.symbolWidth / 2);
var right = new Vector3(top.x - axisLine.symbolHeight, lineY + axisLine.symbolWidth / 2); var right = new Vector3(top.x - axisLine.symbolHeight, lineY + axisLine.symbolWidth / 2);
ChartHelper.DrawTriangle(vh, middle, top, left, m_ThemeInfo.axisLineColor); ChartHelper.DrawTriangle(vh, middle, top, left, m_ThemeInfo.axisLineColor);
ChartHelper.DrawTriangle(vh, middle, top, right, m_ThemeInfo.axisLineColor); ChartHelper.DrawTriangle(vh, middle, top, right, m_ThemeInfo.axisLineColor);
@@ -961,6 +1023,91 @@ namespace XCharts
} }
} }
protected void DrawXTooltipIndicator(VertexHelper vh)
{
if (!m_Tooltip.show || !m_Tooltip.IsSelected()) return;
if (m_Tooltip.type == Tooltip.Type.None) return;
for (int i = 0; i < m_XAxises.Count; i++)
{
var xAxis = m_XAxises[i];
var yAxis = m_YAxises[i];
if (!xAxis.show) continue;
float splitWidth = xAxis.GetDataWidth(coordinateWid, m_DataZoom);
switch (m_Tooltip.type)
{
case Tooltip.Type.Corss:
case Tooltip.Type.Line:
float pX = coordinateX + m_Tooltip.xValues[i] * splitWidth
+ (xAxis.boundaryGap ? splitWidth / 2 : 0);
if (xAxis.IsValue()) pX = m_Tooltip.pointerPos.x;
Vector2 sp = new Vector2(pX, coordinateY);
Vector2 ep = new Vector2(pX, coordinateY + coordinateHig);
DrawSplitLine(vh, false, Axis.SplitLineType.Solid, sp, ep, m_ThemeInfo.tooltipLineColor);
if (m_Tooltip.type == Tooltip.Type.Corss)
{
sp = new Vector2(coordinateX, m_Tooltip.pointerPos.y);
ep = new Vector2(coordinateX + coordinateWid, m_Tooltip.pointerPos.y);
DrawSplitLine(vh, true, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
}
break;
case Tooltip.Type.Shadow:
float tooltipSplitWid = splitWidth < 1 ? 1 : splitWidth;
pX = coordinateX + splitWidth * m_Tooltip.xValues[i] -
(xAxis.boundaryGap ? 0 : splitWidth / 2);
if (xAxis.IsValue()) pX = m_Tooltip.xValues[i];
float pY = coordinateY + coordinateHig;
Vector3 p1 = new Vector3(pX, coordinateY);
Vector3 p2 = new Vector3(pX, pY);
Vector3 p3 = new Vector3(pX + tooltipSplitWid, pY);
Vector3 p4 = new Vector3(pX + tooltipSplitWid, coordinateY);
ChartHelper.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.tooltipFlagAreaColor);
break;
}
}
}
protected void DrawYTooltipIndicator(VertexHelper vh)
{
if (!m_Tooltip.show || !m_Tooltip.IsSelected()) return;
if (m_Tooltip.type == Tooltip.Type.None) return;
for (int i = 0; i < m_YAxises.Count; i++)
{
var yAxis = m_YAxises[i];
if (!yAxis.show) continue;
float splitWidth = yAxis.GetDataWidth(coordinateHig, m_DataZoom);
switch (m_Tooltip.type)
{
case Tooltip.Type.Corss:
case Tooltip.Type.Line:
float pY = coordinateY + m_Tooltip.yValues[i] * splitWidth + (yAxis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(coordinateX, pY);
Vector2 ep = new Vector2(coordinateX + coordinateWid, pY);
DrawSplitLine(vh, false, Axis.SplitLineType.Solid, sp, ep, m_ThemeInfo.tooltipLineColor);
if (m_Tooltip.type == Tooltip.Type.Corss)
{
sp = new Vector2(coordinateX, m_Tooltip.pointerPos.y);
ep = new Vector2(coordinateX + coordinateWid, m_Tooltip.pointerPos.y);
DrawSplitLine(vh, true, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
}
break;
case Tooltip.Type.Shadow:
float tooltipSplitWid = splitWidth < 1 ? 1 : splitWidth;
float pX = coordinateX + coordinateWid;
pY = coordinateY + splitWidth * m_Tooltip.yValues[i] -
(yAxis.boundaryGap ? 0 : splitWidth / 2);
Vector3 p1 = new Vector3(coordinateX, pY);
Vector3 p2 = new Vector3(coordinateX, pY + tooltipSplitWid);
Vector3 p3 = new Vector3(pX, pY + tooltipSplitWid);
Vector3 p4 = new Vector3(pX, pY);
ChartHelper.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.tooltipFlagAreaColor);
break;
}
}
}
private void CheckDataZoom() private void CheckDataZoom()
{ {
if (raycastTarget != m_DataZoom.show) if (raycastTarget != m_DataZoom.show)

View File

@@ -51,7 +51,7 @@ namespace XCharts
m_ItemFontSize = 16, m_ItemFontSize = 16,
m_Data = new List<string>() m_Data = new List<string>()
{ {
"Legend" "serie1"
} }
}; };
legend.location.top = 30; legend.location.top = 30;
@@ -138,9 +138,7 @@ namespace XCharts
public bool IsActive(int seriesIndex) public bool IsActive(int seriesIndex)
{ {
if (seriesIndex < 0 || seriesIndex > data.Count - 1) seriesIndex = 0; if (seriesIndex < 0 || seriesIndex >= m_DataActiveList.Count)
if (seriesIndex >= data.Count) return true;
if (seriesIndex < 0 || seriesIndex > m_DataActiveList.Count - 1)
return true; return true;
else else
return m_DataActiveList[seriesIndex]; return m_DataActiveList[seriesIndex];

View File

@@ -14,6 +14,7 @@ namespace XCharts
[SerializeField] private float m_Tickness; [SerializeField] private float m_Tickness;
[SerializeField] private bool m_Point; [SerializeField] private bool m_Point;
[SerializeField] private float m_PointWidth; [SerializeField] private float m_PointWidth;
[SerializeField] private float m_PointSelectedWidth;
[SerializeField] private bool m_Smooth; [SerializeField] private bool m_Smooth;
[SerializeField] [Range(1f, 10f)] private float m_SmoothStyle; [SerializeField] [Range(1f, 10f)] private float m_SmoothStyle;
[SerializeField] private bool m_Area; [SerializeField] private bool m_Area;
@@ -23,8 +24,9 @@ namespace XCharts
public float tickness { get { return m_Tickness; } set { m_Tickness = value; } } public float tickness { get { return m_Tickness; } set { m_Tickness = value; } }
public bool point { get { return m_Point; } set { m_Point = value; } } public bool point { get { return m_Point; } set { m_Point = value; } }
public float pointWidth { get { return m_PointWidth; } set { m_PointWidth = value; } } public float pointWidth { get { return m_PointWidth; } set { m_PointWidth = value; } }
public bool smooth { get { return m_Smooth; } set { m_Smooth = value; } } public float pointSelectedWidth { get { return m_PointSelectedWidth; } set { m_PointSelectedWidth = value; } }
public float smoothStyle { get { return m_SmoothStyle; } set { m_SmoothStyle = value; } } public float smoothStyle { get { return m_SmoothStyle; } set { m_SmoothStyle = value; } }
public bool smooth { get { return m_Smooth; } set { m_Smooth = value; } }
public bool area { get { return m_Area; } set { m_Area = value; } } public bool area { get { return m_Area; } set { m_Area = value; } }
public bool step { get { return m_Step; } set { m_Step = value; } } public bool step { get { return m_Step; } set { m_Step = value; } }
public StepType stepTpe { get { return m_StepType; } set { m_StepType = value; } } public StepType stepTpe { get { return m_StepType; } set { m_StepType = value; } }
@@ -38,6 +40,7 @@ namespace XCharts
m_Tickness = 0.8f, m_Tickness = 0.8f,
m_Point = true, m_Point = true,
m_PointWidth = 2.5f, m_PointWidth = 2.5f,
m_PointSelectedWidth = 5.5f,
m_Smooth = false, m_Smooth = false,
m_SmoothStyle = 2f, m_SmoothStyle = 2f,
m_Area = false, m_Area = false,

View File

@@ -18,6 +18,7 @@ namespace XCharts
{ {
[SerializeField] [DefaultValue("true")] private bool m_Show; [SerializeField] [DefaultValue("true")] private bool m_Show;
[SerializeField] private SerieType m_Type; [SerializeField] private SerieType m_Type;
[SerializeField] private bool m_Selected;
[SerializeField] private string m_Name; [SerializeField] private string m_Name;
[SerializeField] private string m_Stack; [SerializeField] private string m_Stack;
[SerializeField] private int m_AxisIndex; [SerializeField] private int m_AxisIndex;
@@ -26,6 +27,8 @@ namespace XCharts
[SerializeField] private List<float> m_YData = new List<float>(); [SerializeField] private List<float> m_YData = new List<float>();
[SerializeField] private List<float> m_XData = new List<float>(); [SerializeField] private List<float> m_XData = new List<float>();
public int index { get; set; }
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
public bool show { get { return m_Show; } set { m_Show = value; } } public bool show { get { return m_Show; } set { m_Show = value; } }
public SerieType type { get { return m_Type; } set { m_Type = value; } } public SerieType type { get { return m_Type; } set { m_Type = value; } }
public string name { get { return m_Name; } set { m_Name = value; } } public string name { get { return m_Name; } set { m_Name = value; } }

View File

@@ -20,6 +20,7 @@ namespace XCharts
{ {
m_Series = new List<Serie>(){new Serie(){ m_Series = new List<Serie>(){new Serie(){
show = true, show = true,
index = 0
}} }}
}; };
return series; return series;
@@ -106,6 +107,7 @@ namespace XCharts
if (serie == null) if (serie == null)
{ {
serie = new Serie(); serie = new Serie();
serie.index = m_Series.Count;
serie.type = type; serie.type = type;
serie.show = show; serie.show = show;
serie.name = serieName; serie.name = serieName;
@@ -130,6 +132,7 @@ namespace XCharts
if (serie == null) if (serie == null)
{ {
serie = new Serie(); serie = new Serie();
serie.index = m_Series.Count;
serie.name = name; serie.name = name;
serie.yData = new List<float>(); serie.yData = new List<float>();
m_Series.Add(serie); m_Series.Add(serie);
@@ -244,6 +247,13 @@ namespace XCharts
return false; return false;
} }
public bool IsTooltipSelected(int serieIndex)
{
var serie = GetSerie(serieIndex);
if (serie != null) return serie.selected;
else return false;
}
public void GetXMinMaxValue(DataZoom dataZoom, int axisIndex, out int minVaule, out int maxValue) public void GetXMinMaxValue(DataZoom dataZoom, int axisIndex, out int minVaule, out int maxValue)
{ {
GetMinMaxValue(dataZoom, axisIndex, false, out minVaule, out maxValue); GetMinMaxValue(dataZoom, axisIndex, false, out minVaule, out maxValue);
@@ -377,8 +387,10 @@ namespace XCharts
int count = 0; int count = 0;
Dictionary<string, int> sets = new Dictionary<string, int>(); Dictionary<string, int> sets = new Dictionary<string, int>();
Dictionary<int, List<Serie>> stackSeries = new Dictionary<int, List<Serie>>(); Dictionary<int, List<Serie>> stackSeries = new Dictionary<int, List<Serie>>();
foreach (var serie in m_Series) for (int i = 0; i < m_Series.Count; i++)
{ {
var serie = m_Series[i];
serie.index = i;
if (string.IsNullOrEmpty(serie.stack)) if (string.IsNullOrEmpty(serie.stack))
{ {
stackSeries[count] = new List<Serie>(); stackSeries[count] = new List<Serie>();

View File

@@ -8,8 +8,49 @@ namespace XCharts
[System.Serializable] [System.Serializable]
public class Tooltip public class Tooltip
{ {
/// <summary>
/// Type of triggering.
/// </summary>
public enum Trigger
{
/// <summary>
/// Triggered by axes, which is mainly used for charts that have category axes,
/// like bar charts or line charts.
/// </summary>
Axis,
/// <summary>
/// Triggered by data item, which is mainly used for charts that don't have a
/// category axis like scatter charts or pie charts.
/// </summary>
Item
}
/// <summary>
/// Indicator type.
/// </summary>
public enum Type
{
/// <summary>
/// line indicator.
/// </summary>
Line,
/// <summary>
/// shadow crosshair indicator.
/// </summary>
Shadow,
/// <summary>
/// no indicator displayed.
/// </summary>
None,
/// <summary>
/// crosshair indicator, which is actually the shortcut of enable two axisPointers of two orthometric axes.
/// </summary>
Corss
}
[SerializeField] private bool m_Show; [SerializeField] private bool m_Show;
[SerializeField] private bool m_CrossLabel; [SerializeField] private Type m_Type;
[SerializeField] private Trigger m_Trigger;
[NonSerialized] private GameObject m_GameObject; [NonSerialized] private GameObject m_GameObject;
[NonSerialized] private GameObject m_Content; [NonSerialized] private GameObject m_Content;
@@ -17,13 +58,17 @@ namespace XCharts
[NonSerialized] private RectTransform m_ContentRect; [NonSerialized] private RectTransform m_ContentRect;
public bool show { get { return m_Show; } set { m_Show = value; SetActive(value); } } public bool show { get { return m_Show; } set { m_Show = value; SetActive(value); } }
public bool crossLabel { get { return m_CrossLabel; } set { m_CrossLabel = value; } } public Type type { get { return m_Type; } set { m_Type = value; } }
public Trigger trigger { get { return m_Trigger; } set { m_Trigger = value; } }
public int dataIndex { get; set; }
/// <summary>
/// The data index currently indicated by Tooltip.
/// </summary>
public int[] dataIndex { get; set; }
public int[] lastDataIndex { get; set; }
public float[] xValues { get; set; } public float[] xValues { get; set; }
public float[] yValues { get; set; } public float[] yValues { get; set; }
public int lastDataIndex { get; set; }
public Vector2 pointerPos { get; set; } public Vector2 pointerPos { get; set; }
public float width { get { return m_ContentRect.sizeDelta.x; } } public float width { get { return m_ContentRect.sizeDelta.x; } }
public float height { get { return m_ContentRect.sizeDelta.y; } } public float height { get { return m_ContentRect.sizeDelta.y; } }
@@ -37,9 +82,10 @@ namespace XCharts
var tooltip = new Tooltip var tooltip = new Tooltip
{ {
m_Show = true, m_Show = true,
m_CrossLabel = false,
xValues = new float[2], xValues = new float[2],
yValues = new float[2], yValues = new float[2],
dataIndex = new int[2],
lastDataIndex = new int[2]
}; };
return tooltip; return tooltip;
} }
@@ -58,7 +104,8 @@ namespace XCharts
m_ContentText = m_Content.GetComponentInChildren<Text>(); m_ContentText = m_Content.GetComponentInChildren<Text>();
} }
public void UpdateToTop(){ public void UpdateToTop()
{
int count = m_GameObject.transform.parent.childCount; int count = m_GameObject.transform.parent.childCount;
m_GameObject.GetComponent<RectTransform>().SetSiblingIndex(count - 1); m_GameObject.GetComponent<RectTransform>().SetSiblingIndex(count - 1);
} }
@@ -88,14 +135,14 @@ namespace XCharts
public void ClearValue() public void ClearValue()
{ {
dataIndex = 0; dataIndex[0] = dataIndex[1] = -1;
xValues[0] = xValues[1] = 0; xValues[0] = xValues[1] = -1;
yValues[0] = yValues[1] = 0; yValues[0] = yValues[1] = -1;
} }
public void SetActive(bool flag) public void SetActive(bool flag)
{ {
lastDataIndex = 0; lastDataIndex[0] = lastDataIndex[1] = -1;
if (m_GameObject && m_GameObject.activeInHierarchy != flag) if (m_GameObject && m_GameObject.activeInHierarchy != flag)
m_GameObject.SetActive(flag); m_GameObject.SetActive(flag);
} }
@@ -113,5 +160,25 @@ namespace XCharts
else else
return Vector3.zero; return Vector3.zero;
} }
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 IsSelectedDataIndex(int index){
return dataIndex[0] == index || dataIndex[1] == index;
}
} }
} }

View File

@@ -20,6 +20,7 @@ namespace XCharts
base.Reset(); base.Reset();
m_Line = Line.defaultLine; m_Line = Line.defaultLine;
m_Title.text = "LineChart"; m_Title.text = "LineChart";
m_Tooltip.type = Tooltip.Type.Line;
RemoveData(); RemoveData();
AddSerie("serie1", SerieType.Line); AddSerie("serie1", SerieType.Line);
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
@@ -44,7 +45,6 @@ namespace XCharts
} }
} }
private void DrawXCategory(VertexHelper vh) private void DrawXCategory(VertexHelper vh)
{ {
var stackSeries = m_Series.GetStackSeries(); var stackSeries = m_Series.GetStackSeries();
@@ -52,70 +52,24 @@ namespace XCharts
int serieCount = 0; int serieCount = 0;
List<Vector3> points = new List<Vector3>(); List<Vector3> points = new List<Vector3>();
List<Color> colorList = new List<Color>(); List<Color> colors = new List<Color>();
int dataCount = 0; int dataCount = 0;
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
var seriesCurrHig = new Dictionary<int, float>(); var seriesCurrHig = new Dictionary<int, float>();
var serieList = stackSeries[j]; var serieList = stackSeries[j];
for (int n = 0; n < serieList.Count; n++) for (int n = 0; n < serieList.Count; n++)
{ {
Serie serie = serieList[n]; Serie serie = serieList[n];
DrawXLineSerie(vh, serieCount, serie, ref dataCount, ref points, ref colorList, ref seriesCurrHig); DrawXLineSerie(vh, serieCount, serie, ref dataCount, ref points, ref colors, ref seriesCurrHig);
if (serie.show) if (serie.show)
{ {
serieCount++; serieCount++;
} }
} }
// draw point DrawPoint(vh, dataCount, points, colors);
if (m_Line.point)
{
for (int i = 0; i < points.Count; i++)
{
Vector3 p = points[i];
float pointWid = m_Line.pointWidth;
if (m_Tooltip.show && i % dataCount == m_Tooltip.dataIndex - 1)
{
pointWid = pointWid * 1.8f;
}
if (m_Theme == Theme.Dark)
{
ChartHelper.DrawCricle(vh, p, pointWid, colorList[i],
(int)m_Line.pointWidth * 5);
}
else
{
ChartHelper.DrawCricle(vh, p, pointWid, Color.white);
ChartHelper.DrawDoughnut(vh, p, pointWid - m_Line.tickness,
pointWid, 0, 360, colorList[i]);
}
}
}
}
//draw tooltip line
if (m_Tooltip.show && m_Tooltip.dataIndex > 0)
{
for (int i = 0; i < m_XAxises.Count; i++)
{
var axis = m_XAxises[i];
if (!axis.show) continue;
float splitWidth = axis.GetSplitWidth(coordinateWid, m_DataZoom);
float px = coordinateX + (m_Tooltip.xValues[i] - 1) * splitWidth
+ (axis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(px, coordinateY);
Vector2 ep = new Vector2(px, coordinateY + coordinateHig);
ChartHelper.DrawLine(vh, sp, ep, m_Coordinate.tickness, m_ThemeInfo.tooltipLineColor);
if (m_Tooltip.crossLabel)
{
sp = new Vector2(coordinateX, m_Tooltip.pointerPos.y);
ep = new Vector2(coordinateX + coordinateWid, m_Tooltip.pointerPos.y);
DrawSplitLine(vh, true, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
}
}
} }
DrawXTooltipIndicator(vh);
} }
private void DrawYCategory(VertexHelper vh) private void DrawYCategory(VertexHelper vh)
@@ -130,7 +84,6 @@ namespace XCharts
{ {
var seriesHig = new Dictionary<int, float>(); var seriesHig = new Dictionary<int, float>();
var serieList = stackSeries[j]; var serieList = stackSeries[j];
for (int n = 0; n < serieList.Count; n++) for (int n = 0; n < serieList.Count; n++)
{ {
Serie serie = serieList[n]; Serie serie = serieList[n];
@@ -140,46 +93,41 @@ namespace XCharts
serieCount++; serieCount++;
} }
} }
// draw point DrawPoint(vh, dataCount, points, colors);
if (m_Line.point) }
DrawYTooltipIndicator(vh);
}
private void DrawPoint(VertexHelper vh, int dataCount, List<Vector3> points, List<Color> colors)
{
if (m_Line.point)
{
for (int i = 0; i < points.Count; i++)
{ {
for (int i = 0; i < points.Count; i++) Vector3 p = points[i];
float pointWid = m_Line.pointWidth;
if (m_Tooltip.show && m_Tooltip.IsSelectedDataIndex(i % dataCount))
{ {
Vector3 p = points[i]; if (IsCartesian())
float pointWid = m_Line.pointWidth;
if (m_Tooltip.show && i % dataCount == m_Tooltip.dataIndex - 1)
{ {
pointWid = pointWid * 1.8f; if (m_Series.IsTooltipSelected(i / dataCount))
} {
if (m_Theme == Theme.Dark) pointWid = m_Line.pointSelectedWidth;
{ }
ChartHelper.DrawCricle(vh, p, pointWid, colors[i], (int)m_Line.pointWidth * 5);
} }
else else
{ {
ChartHelper.DrawCricle(vh, p, pointWid, Color.white); pointWid = m_Line.pointSelectedWidth;
ChartHelper.DrawDoughnut(vh, p, pointWid - m_Line.tickness, pointWid, 0, 360, colors[i]);
} }
} }
} if (m_Theme == Theme.Dark)
}
//draw tooltip line
if (m_Tooltip.show && m_Tooltip.dataIndex > 0)
{
for (int i = 0; i < m_YAxises.Count; i++)
{
var axis = m_YAxises[i];
if (!axis.show) continue;
float splitWidth = axis.GetSplitWidth(coordinateHig, m_DataZoom);
float pY = coordinateY + (m_Tooltip.yValues[i] - 1) * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(coordinateX, pY);
Vector2 ep = new Vector2(coordinateX + coordinateWid, pY);
ChartHelper.DrawLine(vh, sp, ep, m_Coordinate.tickness, m_ThemeInfo.tooltipFlagAreaColor);
if (m_Tooltip.crossLabel)
{ {
sp = new Vector2(m_Tooltip.pointerPos.x, coordinateY); ChartHelper.DrawCricle(vh, p, pointWid, colors[i], (int)m_Line.pointWidth * 5);
ep = new Vector2(m_Tooltip.pointerPos.x, coordinateY + coordinateHig); }
DrawSplitLine(vh, false, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor); else
{
ChartHelper.DrawCricle(vh, p, pointWid, Color.white);
ChartHelper.DrawDoughnut(vh, p, pointWid - m_Line.tickness, pointWid, 0, 360, colors[i]);
} }
} }
} }
@@ -188,7 +136,7 @@ namespace XCharts
private void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref int dataCount, private void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref int dataCount,
ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig) ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig)
{ {
if (!IsActive(serie.name)) return; if (!IsActive(serie.index)) return;
List<Vector3> lastPoints = new List<Vector3>(); List<Vector3> lastPoints = new List<Vector3>();
List<Vector3> lastSmoothPoints = new List<Vector3>(); List<Vector3> lastSmoothPoints = new List<Vector3>();
List<Vector3> smoothPoints = new List<Vector3>(); List<Vector3> smoothPoints = new List<Vector3>();
@@ -385,7 +333,7 @@ namespace XCharts
private void DrawYLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref int dataCount, private void DrawYLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref int dataCount,
ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig) ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig)
{ {
if (!IsActive(serie.name)) return; if (!IsActive(serie.index)) return;
List<Vector3> lastPoints = new List<Vector3>(); List<Vector3> lastPoints = new List<Vector3>();
List<Vector3> lastSmoothPoints = new List<Vector3>(); List<Vector3> lastSmoothPoints = new List<Vector3>();
List<Vector3> smoothPoints = new List<Vector3>(); List<Vector3> smoothPoints = new List<Vector3>();
@@ -432,7 +380,7 @@ namespace XCharts
float pX = seriesHig[i] + coordinateX + m_Coordinate.tickness; float pX = seriesHig[i] + coordinateX + m_Coordinate.tickness;
float dataHig = (value - xAxis.minValue) / (xAxis.maxValue - xAxis.minValue) * coordinateWid; float dataHig = (value - xAxis.minValue) / (xAxis.maxValue - xAxis.minValue) * coordinateWid;
np = new Vector3(pX + dataHig, pY); np = new Vector3(pX + dataHig, pY);
//Debug.LogError(gameObject.name +","+(pX + dataHig)+","+startY+","+scaleWid+","+xAxis.maxValue+","+xAxis.minValue);
if (i > 0) if (i > 0)
{ {
if (m_Line.step) if (m_Line.step)

View File

@@ -110,12 +110,12 @@ namespace XCharts
float outSideRadius = m_Pie.rose ? float outSideRadius = m_Pie.rose ?
m_Pie.insideRadius + (m_PieRadius - m_Pie.insideRadius) * value / dataMax : m_Pie.insideRadius + (m_PieRadius - m_Pie.insideRadius) * value / dataMax :
m_PieRadius; m_PieRadius;
if (m_Tooltip.show && m_Tooltip.dataIndex == i + 1) if (m_Tooltip.show && m_Tooltip.dataIndex[0] == i)
{ {
outSideRadius += m_Pie.tooltipExtraRadius; outSideRadius += m_Pie.tooltipExtraRadius;
} }
var offset = m_Pie.space; var offset = m_Pie.space;
if (m_Pie.selected && m_Pie.selectedIndex == i + 1) if (m_Pie.selected && m_Pie.selectedIndex == i)
{ {
offset += m_Pie.selectedOffset; offset += m_Pie.selectedOffset;
} }
@@ -198,22 +198,22 @@ namespace XCharts
float dist = Vector2.Distance(local, m_PieCenter); float dist = Vector2.Distance(local, m_PieCenter);
if (dist > m_PieRadius) if (dist > m_PieRadius)
{ {
m_Tooltip.dataIndex = 0; m_Tooltip.dataIndex[0] = -1;
m_Tooltip.SetActive(false); m_Tooltip.SetActive(false);
} }
else else
{ {
m_Tooltip.dataIndex = GetPosPieIndex(local); m_Tooltip.dataIndex[0] = GetPosPieIndex(local);
} }
if (m_Tooltip.dataIndex > 0) if (m_Tooltip.dataIndex[0] >= 0)
{ {
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25)); m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
RefreshTooltip(); RefreshTooltip();
if (m_Tooltip.lastDataIndex != m_Tooltip.dataIndex) if (m_Tooltip.IsSelected())
{ {
m_Tooltip.UpdateLastDataIndex();
RefreshChart(); RefreshChart();
} }
m_Tooltip.lastDataIndex = m_Tooltip.dataIndex;
} }
} }
@@ -225,14 +225,14 @@ namespace XCharts
{ {
if (i == 0 && angle < m_AngleList[i]) if (i == 0 && angle < m_AngleList[i])
{ {
return m_Tooltip.dataIndex = 1; return m_Tooltip.dataIndex[0] = 0;
} }
else if (angle < m_AngleList[i] && angle > m_AngleList[i - 1]) else if (angle < m_AngleList[i] && angle > m_AngleList[i - 1])
{ {
return m_Tooltip.dataIndex = i + 1; return m_Tooltip.dataIndex[0] = i;
} }
} }
return 0; return -1;
} }
float VectorAngle(Vector2 from, Vector2 to) float VectorAngle(Vector2 from, Vector2 to)
@@ -249,7 +249,7 @@ namespace XCharts
protected override void RefreshTooltip() protected override void RefreshTooltip()
{ {
base.RefreshTooltip(); base.RefreshTooltip();
int index = m_Tooltip.dataIndex - 1; int index = m_Tooltip.dataIndex[0];
if (index < 0) if (index < 0)
{ {
m_Tooltip.SetActive(false); m_Tooltip.SetActive(false);
@@ -290,7 +290,7 @@ namespace XCharts
} }
else else
{ {
m_Pie.selectedIndex = 0; m_Pie.selectedIndex = -1;
} }
RefreshChart(); RefreshChart();
} }

View File

@@ -313,7 +313,7 @@ namespace XCharts
protected override void CheckTootipArea(Vector2 local) protected override void CheckTootipArea(Vector2 local)
{ {
if (dataPosList.Count <= 0) return; if (dataPosList.Count <= 0) return;
m_Tooltip.dataIndex = 0; m_Tooltip.dataIndex[0] = -1;
for (int i = 0; i < m_Series.Count; i++) for (int i = 0; i < m_Series.Count; i++)
{ {
if (!IsActive(i)) continue; if (!IsActive(i)) continue;
@@ -321,12 +321,12 @@ namespace XCharts
{ {
if (Vector3.Distance(local, dataPosList[i][j]) <= m_Radar.linePointSize * 1.2f) if (Vector3.Distance(local, dataPosList[i][j]) <= m_Radar.linePointSize * 1.2f)
{ {
m_Tooltip.dataIndex = i + 1; m_Tooltip.dataIndex[0] = i;
break; break;
} }
} }
} }
if (m_Tooltip.dataIndex > 0) if (m_Tooltip.dataIndex[0] >= 0)
{ {
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25)); m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
RefreshTooltip(); RefreshTooltip();
@@ -345,7 +345,7 @@ namespace XCharts
protected override void RefreshTooltip() protected override void RefreshTooltip()
{ {
base.RefreshTooltip(); base.RefreshTooltip();
int index = m_Tooltip.dataIndex - 1; int index = m_Tooltip.dataIndex[0];
if (index < 0) if (index < 0)
{ {
m_Tooltip.SetActive(false); m_Tooltip.SetActive(false);