2019-07-28 00:44:53 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Collections.Generic;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
using UnityEngine;
|
2019-07-02 18:33:12 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
namespace XCharts
|
|
|
|
|
|
{
|
2019-07-15 00:24:04 +08:00
|
|
|
|
[AddComponentMenu("XCharts/PieChart", 15)]
|
|
|
|
|
|
[ExecuteInEditMode]
|
|
|
|
|
|
[RequireComponent(typeof(RectTransform))]
|
|
|
|
|
|
[DisallowMultipleComponent]
|
2019-05-11 04:33:54 +08:00
|
|
|
|
public class PieChart : BaseChart
|
|
|
|
|
|
{
|
|
|
|
|
|
[SerializeField] private Pie m_Pie = Pie.defaultPie;
|
|
|
|
|
|
|
2019-07-28 00:44:53 +08:00
|
|
|
|
private bool isDrawPie;
|
|
|
|
|
|
private bool m_IsEnterLegendButtom;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
private bool m_RefreshLabel;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
2019-05-15 09:44:18 +08:00
|
|
|
|
public Pie pie { get { return m_Pie; } }
|
|
|
|
|
|
|
2019-07-15 00:24:04 +08:00
|
|
|
|
protected override void Awake()
|
|
|
|
|
|
{
|
2019-07-28 00:44:53 +08:00
|
|
|
|
base.Awake();
|
|
|
|
|
|
raycastTarget = false;
|
2019-07-15 00:24:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
protected override void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Reset();
|
|
|
|
|
|
m_Pie = Pie.defaultPie;
|
2019-07-15 19:21:22 +08:00
|
|
|
|
m_Title.text = "PieChart";
|
2019-07-15 00:24:04 +08:00
|
|
|
|
RemoveData();
|
2019-10-05 18:23:06 +08:00
|
|
|
|
AddSerie(SerieType.Pie, "serie1");
|
2019-07-28 00:44:53 +08:00
|
|
|
|
AddData(0, 70, "pie1");
|
|
|
|
|
|
AddData(0, 20, "pie2");
|
|
|
|
|
|
AddData(0, 10, "pie3");
|
2019-07-15 00:24:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-07-28 00:44:53 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Update();
|
|
|
|
|
|
if (!isDrawPie) RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-11 04:33:54 +08:00
|
|
|
|
protected override void DrawChart(VertexHelper vh)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.DrawChart(vh);
|
2019-07-19 21:55:22 +08:00
|
|
|
|
int serieNameCount = -1;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
bool isClickOffset = false;
|
|
|
|
|
|
bool isDataHighlight = false;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
for (int i = 0; i < m_Series.Count; i++)
|
|
|
|
|
|
{
|
2019-10-01 13:52:02 +08:00
|
|
|
|
var serie = m_Series.list[i];
|
2019-07-19 21:55:22 +08:00
|
|
|
|
serie.index = i;
|
2019-09-05 09:21:37 +08:00
|
|
|
|
var data = serie.data;
|
|
|
|
|
|
serie.animation.InitProgress(data.Count, 0, 360);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (!serie.show)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-07-28 00:44:53 +08:00
|
|
|
|
continue;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-09-05 09:21:37 +08:00
|
|
|
|
if (!serie.animation.NeedAnimation(i)) break;
|
|
|
|
|
|
bool isFinish = true;
|
2019-08-15 21:44:30 +08:00
|
|
|
|
if (serie.pieClickOffset) isClickOffset = true;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serie.pieDataMax = serie.yMax;
|
|
|
|
|
|
serie.pieDataTotal = serie.yTotal;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
UpdatePieCenter(serie);
|
|
|
|
|
|
|
|
|
|
|
|
float totalDegree = 360;
|
|
|
|
|
|
float startDegree = 0;
|
2019-07-29 08:01:39 +08:00
|
|
|
|
int showdataCount = 0;
|
2019-09-05 09:21:37 +08:00
|
|
|
|
foreach (var sd in serie.data)
|
2019-07-29 08:01:39 +08:00
|
|
|
|
{
|
2019-09-05 09:21:37 +08:00
|
|
|
|
if (sd.show && serie.pieRoseType == RoseType.Area) showdataCount++;
|
|
|
|
|
|
sd.canShowLabel = false;
|
2019-07-29 08:01:39 +08:00
|
|
|
|
}
|
2019-07-28 00:44:53 +08:00
|
|
|
|
for (int n = 0; n < data.Count; n++)
|
2019-07-02 18:33:12 +08:00
|
|
|
|
{
|
2019-07-28 00:44:53 +08:00
|
|
|
|
var serieData = data[n];
|
|
|
|
|
|
float value = serieData.data[1];
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieNameCount = m_LegendRealShowName.IndexOf(serieData.name);
|
|
|
|
|
|
Color color = m_ThemeInfo.GetColor(serieNameCount);
|
|
|
|
|
|
serieData.pieStartAngle = startDegree;
|
|
|
|
|
|
serieData.pieToAngle = startDegree;
|
|
|
|
|
|
serieData.pieHalfAngle = startDegree;
|
|
|
|
|
|
serieData.pieCurrAngle = startDegree;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (!serieData.show)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2019-10-05 18:23:06 +08:00
|
|
|
|
float degree = serie.pieRoseType == RoseType.Area ?
|
|
|
|
|
|
(totalDegree / showdataCount) : (totalDegree * value / serie.pieDataTotal);
|
|
|
|
|
|
serieData.pieToAngle = startDegree + degree;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieOutsideRadius = serie.pieRoseType > 0 ?
|
|
|
|
|
|
serie.pieInsideRadius + (serie.pieOutsideRadius - serie.pieInsideRadius) * value / serie.pieDataMax :
|
|
|
|
|
|
serie.pieOutsideRadius;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (serieData.highlighted)
|
|
|
|
|
|
{
|
|
|
|
|
|
isDataHighlight = true;
|
|
|
|
|
|
color *= 1.2f;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieOutsideRadius += m_Pie.tooltipExtraRadius;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
2019-08-15 21:44:30 +08:00
|
|
|
|
var offset = serie.pieSpace;
|
|
|
|
|
|
if (serie.pieClickOffset && serieData.selected)
|
2019-07-28 00:44:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
offset += m_Pie.selectedOffset;
|
|
|
|
|
|
}
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var halfDegree = (serieData.pieToAngle - startDegree) / 2;
|
|
|
|
|
|
serieData.pieHalfAngle = startDegree + halfDegree;
|
|
|
|
|
|
float currRad = serieData.pieHalfAngle * Mathf.Deg2Rad;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
float currSin = Mathf.Sin(currRad);
|
|
|
|
|
|
float currCos = Mathf.Cos(currRad);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var center = serie.pieCenterPos;
|
2019-09-05 09:21:37 +08:00
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieCurrAngle = serieData.pieToAngle;
|
|
|
|
|
|
serieData.pieOffsetCenter = center;
|
|
|
|
|
|
serieData.pieInsideRadius = serie.pieInsideRadius;
|
|
|
|
|
|
if (serie.animation.CheckDetailBreak(n, serieData.pieToAngle))
|
2019-09-05 09:21:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
isFinish = false;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieCurrAngle = serie.animation.GetCurrDetail();
|
2019-09-05 09:21:37 +08:00
|
|
|
|
}
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (offset > 0)
|
|
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieOffsetRadius = serie.pieSpace / Mathf.Sin(halfDegree * Mathf.Deg2Rad);
|
|
|
|
|
|
serieData.pieInsideRadius -= serieData.pieOffsetRadius;
|
|
|
|
|
|
serieData.pieOutsideRadius -= serieData.pieOffsetRadius;
|
2019-08-15 21:44:30 +08:00
|
|
|
|
if (serie.pieClickOffset && serieData.selected)
|
2019-07-28 00:44:53 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieOffsetRadius += m_Pie.selectedOffset;
|
|
|
|
|
|
if (serieData.pieInsideRadius > 0) serieData.pieInsideRadius += m_Pie.selectedOffset;
|
|
|
|
|
|
serieData.pieOutsideRadius += m_Pie.selectedOffset;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
2019-07-29 00:25:10 +08:00
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.pieOffsetCenter = new Vector3(center.x + serieData.pieOffsetRadius * currSin,
|
|
|
|
|
|
center.y + serieData.pieOffsetRadius * currCos);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
ChartDrawer.DrawDoughnut(vh, serieData.pieOffsetCenter, serieData.pieInsideRadius, serieData.pieOutsideRadius,
|
2019-10-10 09:01:16 +08:00
|
|
|
|
color, m_ThemeInfo.backgroundColor, m_Settings.cicleSmoothness, startDegree, serieData.pieCurrAngle);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
ChartDrawer.DrawDoughnut(vh, center, serieData.pieInsideRadius, serieData.pieOutsideRadius,
|
2019-10-10 09:01:16 +08:00
|
|
|
|
color, m_ThemeInfo.backgroundColor, m_Settings.cicleSmoothness, startDegree, serieData.pieCurrAngle);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serieData.canShowLabel = serieData.pieCurrAngle >= serieData.pieHalfAngle;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
isDrawPie = true;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
startDegree = serieData.pieToAngle;
|
2019-09-05 09:21:37 +08:00
|
|
|
|
if (isFinish) serie.animation.SetDataFinish(n);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
else break;
|
2019-09-05 09:21:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (!serie.animation.IsFinish())
|
|
|
|
|
|
{
|
|
|
|
|
|
float duration = serie.animation.duration > 0 ? (float)serie.animation.duration / 1000 : 1;
|
|
|
|
|
|
float speed = 360 / duration;
|
|
|
|
|
|
float symbolSpeed = serie.symbol.size / duration;
|
|
|
|
|
|
serie.animation.CheckProgress(Time.deltaTime * speed);
|
|
|
|
|
|
serie.animation.CheckSymbol(Time.deltaTime * symbolSpeed, serie.symbol.size);
|
|
|
|
|
|
RefreshChart();
|
2019-07-02 18:33:12 +08:00
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-10-05 18:23:06 +08:00
|
|
|
|
DrawLabelLine(vh);
|
2019-09-06 09:11:46 +08:00
|
|
|
|
DrawLabelBackground(vh);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
raycastTarget = isClickOffset && isDataHighlight;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
private void DrawLabelLine(VertexHelper vh)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var serie in m_Series.list)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (serie.type == SerieType.Pie && serie.label.show)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var serieData in serie.data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (serieData.canShowLabel)
|
|
|
|
|
|
{
|
|
|
|
|
|
int colorIndex = m_LegendRealShowName.IndexOf(serieData.name);
|
|
|
|
|
|
Color color = m_ThemeInfo.GetColor(colorIndex);
|
|
|
|
|
|
DrawLabelLine(vh, serie, serieData, color);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-06 09:11:46 +08:00
|
|
|
|
private void DrawLabelBackground(VertexHelper vh)
|
|
|
|
|
|
{
|
2019-10-01 13:52:02 +08:00
|
|
|
|
foreach (var serie in m_Series.list)
|
2019-09-06 09:11:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (serie.type == SerieType.Pie && serie.label.show)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var serieData in serie.data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (serieData.canShowLabel)
|
|
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
UpdateLabelPostion(serie, serieData);
|
2019-09-06 09:11:46 +08:00
|
|
|
|
DrawLabelBackground(vh, serie, serieData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
private void DrawLabelLine(VertexHelper vh, Serie serie, SerieData serieData, Color color)
|
2019-07-29 00:25:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (serie.label.show
|
|
|
|
|
|
&& serie.label.position == SerieLabel.Position.Outside
|
|
|
|
|
|
&& serie.label.line)
|
|
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var insideRadius = serieData.pieInsideRadius;
|
|
|
|
|
|
var outSideRadius = serieData.pieOutsideRadius;
|
|
|
|
|
|
var center = serie.pieCenterPos;
|
|
|
|
|
|
var currAngle = serieData.pieHalfAngle;
|
|
|
|
|
|
if (serie.label.lineColor != Color.clear) color = serie.label.lineColor;
|
|
|
|
|
|
else if (serie.label.lineType == SerieLabel.LineType.HorizontalLine) color *= color;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
float currSin = Mathf.Sin(currAngle * Mathf.Deg2Rad);
|
|
|
|
|
|
float currCos = Mathf.Cos(currAngle * Mathf.Deg2Rad);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var radius1 = serie.label.lineType == SerieLabel.LineType.HorizontalLine ?
|
|
|
|
|
|
serie.pieOutsideRadius : outSideRadius;
|
|
|
|
|
|
var radius2 = serie.pieOutsideRadius + serie.label.lineLength1;
|
|
|
|
|
|
var radius3 = insideRadius + (outSideRadius - insideRadius) / 2;
|
|
|
|
|
|
var pos0 = new Vector3(center.x + radius3 * currSin, center.y + radius3 * currCos);
|
|
|
|
|
|
var pos1 = new Vector3(center.x + radius1 * currSin, center.y + radius1 * currCos);
|
|
|
|
|
|
var pos2 = new Vector3(center.x + radius2 * currSin, center.y + radius2 * currCos);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
float tx, ty;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
Vector3 pos3, pos4, pos6;
|
|
|
|
|
|
var horizontalLineCircleRadius = serie.label.lineWidth * 4f;
|
|
|
|
|
|
var lineCircleDiff = horizontalLineCircleRadius - 0.3f;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
if (currAngle < 90)
|
|
|
|
|
|
{
|
|
|
|
|
|
ty = serie.label.lineWidth * Mathf.Cos((90 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
tx = serie.label.lineWidth * Mathf.Sin((90 - currAngle) * Mathf.Deg2Rad);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
pos3 = new Vector3(pos2.x - tx, pos2.y + ty - serie.label.lineWidth);
|
|
|
|
|
|
var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3;
|
|
|
|
|
|
r4 += serie.label.lineLength1 - lineCircleDiff;
|
|
|
|
|
|
pos6 = pos0 + Vector3.right * lineCircleDiff;
|
|
|
|
|
|
pos4 = pos6 + Vector3.right * r4;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (currAngle < 180)
|
|
|
|
|
|
{
|
|
|
|
|
|
ty = serie.label.lineWidth * Mathf.Sin((180 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
tx = serie.label.lineWidth * Mathf.Cos((180 - currAngle) * Mathf.Deg2Rad);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
pos3 = new Vector3(pos2.x - tx, pos2.y - ty + serie.label.lineWidth);
|
|
|
|
|
|
var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3;
|
|
|
|
|
|
r4 += serie.label.lineLength1 - lineCircleDiff;
|
|
|
|
|
|
pos6 = pos0 + Vector3.right * lineCircleDiff;
|
|
|
|
|
|
pos4 = pos6 + Vector3.right * r4;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (currAngle < 270)
|
|
|
|
|
|
{
|
|
|
|
|
|
ty = serie.label.lineWidth * Mathf.Sin((180 + currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
tx = serie.label.lineWidth * Mathf.Cos((180 + currAngle) * Mathf.Deg2Rad);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var currSin1 = Mathf.Sin((360 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
var currCos1 = Mathf.Cos((360 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
pos3 = new Vector3(pos2.x + tx, pos2.y - ty + serie.label.lineWidth);
|
|
|
|
|
|
var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos1 * radius3, 2)) - currSin1 * radius3;
|
|
|
|
|
|
r4 += serie.label.lineLength1 - lineCircleDiff;
|
|
|
|
|
|
pos6 = pos0 + Vector3.left * lineCircleDiff;
|
|
|
|
|
|
pos4 = pos6 + Vector3.left * r4;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ty = serie.label.lineWidth * Mathf.Cos((90 + currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
tx = serie.label.lineWidth * Mathf.Sin((90 + currAngle) * Mathf.Deg2Rad);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
pos3 = new Vector3(pos2.x + tx, pos2.y + ty - serie.label.lineWidth);
|
|
|
|
|
|
var currSin1 = Mathf.Sin((360 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
var currCos1 = Mathf.Cos((360 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos1 * radius3, 2)) - currSin1 * radius3;
|
|
|
|
|
|
r4 += serie.label.lineLength1 - lineCircleDiff;
|
|
|
|
|
|
pos6 = pos0 + Vector3.left * lineCircleDiff;
|
|
|
|
|
|
pos4 = pos6 + Vector3.left * r4;
|
|
|
|
|
|
}
|
|
|
|
|
|
var pos5 = new Vector3(currAngle > 180 ? pos3.x - serie.label.lineLength2 : pos3.x + serie.label.lineLength2, pos3.y);
|
|
|
|
|
|
switch (serie.label.lineType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SerieLabel.LineType.BrokenLine:
|
|
|
|
|
|
ChartDrawer.DrawLine(vh, pos1, pos2, serie.label.lineWidth, color);
|
|
|
|
|
|
ChartDrawer.DrawLine(vh, pos3, pos5, serie.label.lineWidth, color);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SerieLabel.LineType.Curves:
|
2019-10-10 09:01:16 +08:00
|
|
|
|
ChartDrawer.DrawCurves(vh, pos1, pos5, pos1, pos2, serie.label.lineWidth, color, m_Settings.lineSmoothness);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case SerieLabel.LineType.HorizontalLine:
|
2019-10-11 12:32:42 +08:00
|
|
|
|
ChartDrawer.DrawCricle(vh, pos0, horizontalLineCircleRadius, color);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
ChartDrawer.DrawLine(vh, pos6, pos4, serie.label.lineWidth, color);
|
|
|
|
|
|
break;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnRefreshLabel()
|
|
|
|
|
|
{
|
|
|
|
|
|
int serieNameCount = -1;
|
|
|
|
|
|
for (int i = 0; i < m_Series.Count; i++)
|
|
|
|
|
|
{
|
2019-10-01 13:52:02 +08:00
|
|
|
|
var serie = m_Series.list[i];
|
2019-07-29 00:25:10 +08:00
|
|
|
|
serie.index = i;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
if (!serie.show) continue;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
var data = serie.data;
|
|
|
|
|
|
|
2019-07-29 08:01:39 +08:00
|
|
|
|
int showdataCount = 0;
|
2019-08-15 21:44:30 +08:00
|
|
|
|
if (serie.pieRoseType == RoseType.Area)
|
2019-07-29 08:01:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var sd in serie.data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sd.show) showdataCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-29 00:25:10 +08:00
|
|
|
|
for (int n = 0; n < data.Count; n++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var serieData = data[n];
|
2019-09-05 09:21:37 +08:00
|
|
|
|
if (!serieData.canShowLabel)
|
|
|
|
|
|
{
|
|
|
|
|
|
serieData.SetLabelActive(false);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2019-10-05 18:23:06 +08:00
|
|
|
|
if (!serieData.show) continue;
|
|
|
|
|
|
serieNameCount = m_LegendRealShowName.IndexOf(serieData.name);
|
|
|
|
|
|
Color color = m_ThemeInfo.GetColor(serieNameCount);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
DrawLabel(serie, n, serieData, color);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
private void DrawLabel(Serie serie, int dataIndex, SerieData serieData, Color serieColor)
|
2019-07-29 00:25:10 +08:00
|
|
|
|
{
|
2019-08-20 09:40:19 +08:00
|
|
|
|
if (serieData.labelText == null) return;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var currAngle = serieData.pieHalfAngle;
|
2019-10-14 07:45:56 +08:00
|
|
|
|
var isHighlight = (serieData.highlighted && serie.emphasis.label.show);
|
2019-09-25 09:44:53 +08:00
|
|
|
|
var showLabel = ((serie.label.show || isHighlight) && serieData.canShowLabel);
|
|
|
|
|
|
if (showLabel || serieData.showIcon)
|
2019-07-29 00:25:10 +08:00
|
|
|
|
{
|
2019-09-25 09:44:53 +08:00
|
|
|
|
serieData.SetLabelActive(showLabel);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
float rotate = 0;
|
|
|
|
|
|
bool isInsidePosition = serie.label.position == SerieLabel.Position.Inside;
|
|
|
|
|
|
if (serie.label.rotate > 0 && isInsidePosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currAngle > 180) rotate += 270 - currAngle;
|
|
|
|
|
|
else rotate += -(currAngle - 90);
|
|
|
|
|
|
}
|
|
|
|
|
|
Color color = serieColor;
|
|
|
|
|
|
if (isHighlight)
|
|
|
|
|
|
{
|
2019-10-14 07:45:56 +08:00
|
|
|
|
if (serie.emphasis.label.color != Color.clear) color = serie.emphasis.label.color;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (serie.label.color != Color.clear)
|
|
|
|
|
|
{
|
|
|
|
|
|
color = serie.label.color;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
color = isInsidePosition ? Color.white : serieColor;
|
|
|
|
|
|
}
|
2019-10-14 07:45:56 +08:00
|
|
|
|
var fontSize = isHighlight ? serie.emphasis.label.fontSize : serie.label.fontSize;
|
|
|
|
|
|
var fontStyle = isHighlight ? serie.emphasis.label.fontStyle : serie.label.fontStyle;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
|
2019-08-20 09:40:19 +08:00
|
|
|
|
serieData.labelText.color = color;
|
|
|
|
|
|
serieData.labelText.fontSize = fontSize;
|
|
|
|
|
|
serieData.labelText.fontStyle = fontStyle;
|
2019-07-29 00:25:10 +08:00
|
|
|
|
|
2019-09-06 09:11:46 +08:00
|
|
|
|
serieData.labelRect.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
UpdateLabelPostion(serie, serieData);
|
2019-09-23 09:23:51 +08:00
|
|
|
|
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);
|
2019-09-23 18:47:45 +08:00
|
|
|
|
if (serieData.SetLabelText(content)) RefreshChart();
|
2019-09-23 09:23:51 +08:00
|
|
|
|
}
|
2019-09-25 09:44:53 +08:00
|
|
|
|
serieData.SetGameObjectPosition(serieData.labelPosition);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
if (showLabel) serieData.SetLabelPosition(serie.label.offset);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-08-16 00:13:01 +08:00
|
|
|
|
serieData.SetLabelActive(false);
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
2019-09-25 09:44:53 +08:00
|
|
|
|
serieData.UpdateIcon();
|
2019-07-29 00:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
protected void UpdateLabelPostion(Serie serie, SerieData serieData)
|
|
|
|
|
|
{
|
|
|
|
|
|
var currAngle = serieData.pieHalfAngle;
|
|
|
|
|
|
var currRad = currAngle * Mathf.Deg2Rad;
|
|
|
|
|
|
var offsetRadius = serieData.pieOffsetRadius;
|
|
|
|
|
|
var insideRadius = serieData.pieInsideRadius;
|
|
|
|
|
|
var outsideRadius = serieData.pieOutsideRadius;
|
|
|
|
|
|
switch (serie.label.position)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SerieLabel.Position.Center:
|
|
|
|
|
|
serieData.labelPosition = serie.pieCenterPos;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SerieLabel.Position.Inside:
|
|
|
|
|
|
var labelRadius = offsetRadius + insideRadius + (outsideRadius - insideRadius) / 2;
|
|
|
|
|
|
var labelCenter = new Vector2(serie.pieCenterPos.x + labelRadius * Mathf.Sin(currRad),
|
|
|
|
|
|
serie.pieCenterPos.y + labelRadius * Mathf.Cos(currRad));
|
|
|
|
|
|
serieData.labelPosition = labelCenter;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SerieLabel.Position.Outside:
|
|
|
|
|
|
if (serie.label.lineType == SerieLabel.LineType.HorizontalLine)
|
|
|
|
|
|
{
|
|
|
|
|
|
var radius1 = serie.pieOutsideRadius;
|
|
|
|
|
|
var radius3 = insideRadius + (outsideRadius - insideRadius) / 2;
|
|
|
|
|
|
var currSin = Mathf.Sin(currRad);
|
|
|
|
|
|
var currCos = Mathf.Cos(currRad);
|
|
|
|
|
|
var pos0 = new Vector3(serie.pieCenterPos.x + radius3 * currSin, serie.pieCenterPos.y + radius3 * currCos);
|
|
|
|
|
|
if (currAngle > 180)
|
|
|
|
|
|
{
|
|
|
|
|
|
currSin = Mathf.Sin((360 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
currCos = Mathf.Cos((360 - currAngle) * Mathf.Deg2Rad);
|
|
|
|
|
|
}
|
|
|
|
|
|
var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3;
|
|
|
|
|
|
r4 += serie.label.lineLength1 + serie.label.lineWidth * 4;
|
|
|
|
|
|
r4 += serieData.labelText.preferredWidth / 2;
|
|
|
|
|
|
serieData.labelPosition = pos0 + (currAngle > 180 ? Vector3.left : Vector3.right) * r4;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
labelRadius = serie.pieOutsideRadius + serie.label.lineLength1;
|
|
|
|
|
|
labelCenter = new Vector2(serie.pieCenterPos.x + labelRadius * Mathf.Sin(currRad),
|
|
|
|
|
|
serie.pieCenterPos.y + labelRadius * Mathf.Cos(currRad));
|
|
|
|
|
|
float labelWidth = serieData.labelText.preferredWidth;
|
|
|
|
|
|
if (currAngle > 180)
|
|
|
|
|
|
{
|
|
|
|
|
|
serieData.labelPosition = new Vector2(labelCenter.x - serie.label.lineLength2 - 5 - labelWidth / 2, labelCenter.y);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
serieData.labelPosition = new Vector2(labelCenter.x + serie.label.lineLength2 + 5 + labelWidth / 2, labelCenter.y);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-04 15:24:31 +08:00
|
|
|
|
protected override void OnLegendButtonClick(int index, string legendName, bool show)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-08-04 15:24:31 +08:00
|
|
|
|
bool active = CheckDataShow(legendName, show);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
var bgColor1 = active ? m_ThemeInfo.GetColor(index) : m_ThemeInfo.legendUnableColor;
|
|
|
|
|
|
m_Legend.UpdateButtonColor(legendName, bgColor1);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
2019-07-28 00:44:53 +08:00
|
|
|
|
protected override void OnLegendButtonEnter(int index, string legendName)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_IsEnterLegendButtom = true;
|
2019-08-04 15:24:31 +08:00
|
|
|
|
CheckDataHighlighted(legendName, true);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
RefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-28 00:44:53 +08:00
|
|
|
|
protected override void OnLegendButtonExit(int index, string legendName)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-07-28 00:44:53 +08:00
|
|
|
|
m_IsEnterLegendButtom = false;
|
2019-08-04 15:24:31 +08:00
|
|
|
|
CheckDataHighlighted(legendName, false);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdatePieCenter(Serie serie)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-08-15 21:44:30 +08:00
|
|
|
|
if (serie.pieCenter.Length < 2) return;
|
|
|
|
|
|
var centerX = serie.pieCenter[0] <= 1 ? chartWidth * serie.pieCenter[0] : serie.pieCenter[0];
|
|
|
|
|
|
var centerY = serie.pieCenter[1] <= 1 ? chartHeight * serie.pieCenter[1] : serie.pieCenter[1];
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serie.pieCenterPos = new Vector2(centerX, centerY);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
var minWidth = Mathf.Min(chartWidth, chartHeight);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
serie.pieInsideRadius = serie.pieRadius[0] <= 1 ? minWidth * serie.pieRadius[0] : serie.pieRadius[0];
|
|
|
|
|
|
serie.pieOutsideRadius = serie.pieRadius[1] <= 1 ? minWidth * serie.pieRadius[1] : serie.pieRadius[1];
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void CheckTootipArea(Vector2 local)
|
|
|
|
|
|
{
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (m_IsEnterLegendButtom) return;
|
|
|
|
|
|
m_Tooltip.dataIndex.Clear();
|
|
|
|
|
|
bool selected = false;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
foreach (var serie in m_Series.list)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
int index = GetPosPieIndex(serie, local);
|
2019-07-28 00:44:53 +08:00
|
|
|
|
m_Tooltip.dataIndex.Add(index);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
if (serie.type != SerieType.Pie) continue;
|
2019-07-28 00:44:53 +08:00
|
|
|
|
bool refresh = false;
|
|
|
|
|
|
for (int j = 0; j < serie.data.Count; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var serieData = serie.data[j];
|
|
|
|
|
|
if (serieData.highlighted != (j == index)) refresh = true;
|
|
|
|
|
|
serieData.highlighted = j == index;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (index >= 0) selected = true;
|
|
|
|
|
|
if (refresh) RefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (selected)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-05-29 09:53:30 +08:00
|
|
|
|
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
|
2019-05-11 04:33:54 +08:00
|
|
|
|
RefreshTooltip();
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
2019-09-23 09:23:51 +08:00
|
|
|
|
else if (m_Tooltip.IsActive())
|
2019-07-28 00:44:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_Tooltip.SetActive(false);
|
2019-09-20 08:55:52 +08:00
|
|
|
|
RefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-05 18:23:06 +08:00
|
|
|
|
private int GetPosPieIndex(Serie serie, Vector2 local)
|
2019-07-02 18:33:12 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
if (serie.type != SerieType.Pie) return -1;
|
|
|
|
|
|
var dist = Vector2.Distance(local, serie.pieCenterPos);
|
|
|
|
|
|
if (dist < serie.pieInsideRadius || dist > serie.pieOutsideRadius) return -1;
|
|
|
|
|
|
Vector2 dir = local - new Vector2(serie.pieCenterPos.x, serie.pieCenterPos.y);
|
2019-07-02 18:33:12 +08:00
|
|
|
|
float angle = VectorAngle(Vector2.up, dir);
|
2019-10-05 18:23:06 +08:00
|
|
|
|
for (int i = 0; i < serie.data.Count; i++)
|
2019-07-02 18:33:12 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var serieData = serie.data[i];
|
|
|
|
|
|
if (angle >= serieData.pieStartAngle && angle <= serieData.pieToAngle)
|
2019-07-02 18:33:12 +08:00
|
|
|
|
{
|
2019-07-28 00:44:53 +08:00
|
|
|
|
return i;
|
2019-07-02 18:33:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-18 09:42:36 +08:00
|
|
|
|
return -1;
|
2019-07-02 18:33:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-11 04:33:54 +08:00
|
|
|
|
float VectorAngle(Vector2 from, Vector2 to)
|
|
|
|
|
|
{
|
|
|
|
|
|
float angle;
|
|
|
|
|
|
|
|
|
|
|
|
Vector3 cross = Vector3.Cross(from, to);
|
|
|
|
|
|
angle = Vector2.Angle(from, to);
|
|
|
|
|
|
angle = cross.z > 0 ? -angle : angle;
|
|
|
|
|
|
angle = (angle + 360) % 360;
|
|
|
|
|
|
return angle;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-28 00:44:53 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
protected override void RefreshTooltip()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.RefreshTooltip();
|
2019-07-28 00:44:53 +08:00
|
|
|
|
bool showTooltip = false;
|
2019-10-05 18:23:06 +08:00
|
|
|
|
foreach (var serie in m_Series.list)
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
int index = m_Tooltip.dataIndex[serie.index];
|
2019-07-28 00:44:53 +08:00
|
|
|
|
if (index < 0) continue;
|
|
|
|
|
|
showTooltip = true;
|
2019-09-23 09:23:51 +08:00
|
|
|
|
if (string.IsNullOrEmpty(tooltip.formatter))
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
2019-07-28 00:44:53 +08:00
|
|
|
|
{
|
2019-09-23 09:41:10 +08:00
|
|
|
|
m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series, null));
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
2019-07-28 00:44:53 +08:00
|
|
|
|
var pos = m_Tooltip.GetContentPos();
|
|
|
|
|
|
if (pos.x + m_Tooltip.width > chartWidth)
|
|
|
|
|
|
{
|
|
|
|
|
|
pos.x = chartWidth - m_Tooltip.width;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (pos.y - m_Tooltip.height < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
pos.y = m_Tooltip.height;
|
|
|
|
|
|
}
|
|
|
|
|
|
m_Tooltip.UpdateContentPos(pos);
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-07-28 00:44:53 +08:00
|
|
|
|
m_Tooltip.SetActive(showTooltip);
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-07-02 18:33:12 +08:00
|
|
|
|
|
|
|
|
|
|
public override void OnPointerDown(PointerEventData eventData)
|
|
|
|
|
|
{
|
2019-07-19 21:55:22 +08:00
|
|
|
|
Vector2 local;
|
|
|
|
|
|
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
|
|
|
|
|
eventData.position, canvas.worldCamera, out local))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2019-10-05 18:23:06 +08:00
|
|
|
|
for (int i = 0; i < m_Series.Count; i++)
|
2019-07-02 18:33:12 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var serie = m_Series.GetSerie(i);
|
|
|
|
|
|
if (serie.type == SerieType.Pie)
|
2019-07-28 00:44:53 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
var index = GetPosPieIndex(serie, local);
|
|
|
|
|
|
if (index >= 0)
|
2019-07-28 00:44:53 +08:00
|
|
|
|
{
|
2019-10-05 18:23:06 +08:00
|
|
|
|
for (int j = 0; j < serie.data.Count; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (j == index) serie.data[j].selected = !serie.data[j].selected;
|
|
|
|
|
|
else serie.data[j].selected = false;
|
|
|
|
|
|
}
|
2019-07-28 00:44:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-02 18:33:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|