Files
XCharts/Runtime/ScatterChart.cs

122 lines
4.2 KiB
C#
Raw Normal View History

2021-01-11 08:54:28 +08:00
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
2019-07-21 23:10:38 +08:00
using UnityEngine;
namespace XCharts
{
[AddComponentMenu("XCharts/ScatterChart", 17)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class ScatterChart : CoordinateChart
{
2019-07-22 19:02:28 +08:00
private float m_EffectScatterSpeed = 15;
private float m_EffectScatterSize;
private float m_EffectScatterAplha;
2019-07-21 23:10:38 +08:00
#if UNITY_EDITOR
protected override void Reset()
{
base.Reset();
2021-01-11 08:54:28 +08:00
title.text = "ScatterChart";
tooltip.type = Tooltip.Type.None;
m_XAxes[0].type = Axis.AxisType.Value;
m_XAxes[0].boundaryGap = false;
m_YAxes[1].type = Axis.AxisType.Value;
m_XAxes[1].boundaryGap = false;
2019-07-21 23:10:38 +08:00
RemoveData();
2021-01-11 08:54:28 +08:00
SerieTemplate.AddDefaultScatterSerie(this, "serie1");
2019-07-21 23:10:38 +08:00
}
#endif
2019-07-22 19:02:28 +08:00
protected override void Update()
{
base.Update();
bool hasEffectScatter = false;
foreach (var serie in m_Series.list)
2019-07-22 19:02:28 +08:00
{
if (serie.type == SerieType.EffectScatter)
{
hasEffectScatter = true;
2021-01-11 08:54:28 +08:00
var symbolSize = serie.symbol.GetSize(null, m_Theme.serie.scatterSymbolSize);
2019-07-22 19:02:28 +08:00
for (int i = 0; i < serie.symbol.animationSize.Count; ++i)
{
serie.symbol.animationSize[i] += m_EffectScatterSpeed * Time.deltaTime;
2021-01-11 08:54:28 +08:00
if (serie.symbol.animationSize[i] > symbolSize)
2019-07-22 19:02:28 +08:00
{
2019-07-28 00:44:53 +08:00
serie.symbol.animationSize[i] = i * 5;
2019-07-22 19:02:28 +08:00
}
}
}
}
if (hasEffectScatter)
{
RefreshChart();
}
}
2021-01-11 08:54:28 +08:00
protected override void CheckTootipArea(Vector2 local, bool isActivedOther)
{
2021-01-11 08:54:28 +08:00
base.CheckTootipArea(local, isActivedOther);
if (isActivedOther) return;
tooltip.ClearSerieDataIndex();
bool selected = false;
foreach (var serie in m_Series.list)
{
if (!serie.show) continue;
if (serie.type != SerieType.Scatter && serie.type != SerieType.EffectScatter) continue;
bool refresh = false;
var dataCount = serie.data.Count;
for (int j = 0; j < serie.data.Count; j++)
{
var serieData = serie.data[j];
var symbol = SerieHelper.GetSerieSymbol(serie, serieData);
if (!symbol.ShowSymbol(j, dataCount)) continue;
var dist = Vector3.Distance(local, serieData.runtimePosition);
2021-01-11 08:54:28 +08:00
if (dist <= symbol.GetSize(serieData.data, m_Theme.serie.scatterSymbolSize))
{
serieData.selected = true;
2021-01-11 08:54:28 +08:00
tooltip.AddSerieDataIndex(serie.index, j);
selected = true;
}
else
{
serieData.selected = false;
}
}
if (refresh) RefreshChart();
}
if (selected)
{
2021-01-11 08:54:28 +08:00
tooltip.UpdateContentPos(local + tooltip.offset);
UpdateTooltip();
}
2021-01-11 08:54:28 +08:00
else if (tooltip.IsActive())
{
2021-01-11 08:54:28 +08:00
tooltip.SetActive(false);
RefreshChart();
}
}
protected override void UpdateTooltip()
{
base.UpdateTooltip();
2021-01-11 08:54:28 +08:00
if (tooltip.isAnySerieDataIndex())
{
2021-01-11 08:54:28 +08:00
var content = TooltipHelper.GetFormatterContent(tooltip, 0, m_Series, m_Theme);
TooltipHelper.SetContentAndPosition(tooltip, content, chartRect);
2021-01-11 08:54:28 +08:00
tooltip.SetActive(true);
}
else
{
2021-01-11 08:54:28 +08:00
tooltip.SetActive(false);
}
}
2019-07-21 23:10:38 +08:00
}
}