Skip to content

Commit

Permalink
[optimize] Pie support startAngle
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed Apr 7, 2022
1 parent 621bc3d commit 80c77fc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions Editor/Series/PieEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public override void OnCustomInspectorGUI()
PropertyField("m_Gap");
PropertyTwoFiled("m_Center");
PropertyTwoFiled("m_Radius");
//PropertyField("m_StartAngle");

PropertyFiledMore(() =>
{
Expand Down
11 changes: 6 additions & 5 deletions Runtime/Helper/SerieLabelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static void SetGaugeLabelText(Serie serie)
public static void UpdatePieLabelPosition(Serie serie, SerieData serieData)
{
if (serieData.labelObject == null) return;
var startAngle = serie.context.startAngle;
var currAngle = serieData.context.halfAngle;
var currRad = currAngle * Mathf.Deg2Rad;
var offsetRadius = serieData.context.offsetRadius;
Expand All @@ -105,15 +106,15 @@ public static void UpdatePieLabelPosition(Serie serie, SerieData serieData)
var currSin = Mathf.Sin(currRad);
var currCos = Mathf.Cos(currRad);
var pos0 = new Vector3(serie.context.center.x + radius3 * currSin, serie.context.center.y + radius3 * currCos);
if (currAngle > 180)
if ((currAngle - startAngle) % 360 > 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 += labelLine.lineLength1 + labelLine.lineWidth * 4;
r4 += serieData.labelObject.label.GetPreferredWidth() / 2;
serieData.context.labelPosition = pos0 + (currAngle > 180 ? Vector3.left : Vector3.right) * r4;
serieData.context.labelPosition = pos0 + ((currAngle - startAngle) % 360 > 180 ? Vector3.left : Vector3.right) * r4;
}
else
{
Expand Down Expand Up @@ -184,11 +185,11 @@ private static void CheckSerieDataLabel(Serie serie, SerieData serieData, bool i
serieData.context.labelPosition = new Vector3(x1, y1);
}
lastCheckPos = serieData.context.labelPosition;
serieData.labelObject.SetPosition(SerieLabelHelper.GetRealLabelPosition(serieData, serieLabel, labelLine));
serieData.labelObject.SetPosition(SerieLabelHelper.GetRealLabelPosition(serie, serieData, serieLabel, labelLine));
}
}

public static Vector3 GetRealLabelPosition(SerieData serieData, LabelStyle label, LabelLine labelLine)
public static Vector3 GetRealLabelPosition(Serie serie, SerieData serieData, LabelStyle label, LabelLine labelLine)
{
if (label == null || labelLine == null)
return serieData.context.labelPosition;
Expand All @@ -197,7 +198,7 @@ public static Vector3 GetRealLabelPosition(SerieData serieData, LabelStyle label
{
var currAngle = serieData.context.halfAngle;
var offset = labelLine.lineLength2 + serieData.labelObject.GetLabelWidth() / 2;
if (currAngle > 180)
if ((currAngle - serie.context.startAngle) % 360 > 180)
return serieData.context.labelPosition + new Vector3(-offset, 0, 0);
else
return serieData.context.labelPosition + new Vector3(offset, 0, 0);
Expand Down
38 changes: 34 additions & 4 deletions Runtime/Serie/Pie/PieHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void UpdateTooltipSerieParams(int dataIndex, bool showCategory,
public override Vector3 GetSerieDataLabelPosition(SerieData serieData, LabelStyle label)
{
var labelLine = SerieHelper.GetSerieLabelLine(serie, serieData);
return SerieLabelHelper.GetRealLabelPosition(serieData, label, labelLine);
return SerieLabelHelper.GetRealLabelPosition(serie, serieData, label, labelLine);
}

public override void OnLegendButtonClick(int index, string legendName, bool show)
Expand Down Expand Up @@ -151,11 +151,12 @@ private void UpdateRuntimeData(Serie serie)
{
var data = serie.data;
serie.context.dataMax = serie.yMax;
serie.context.startAngle = GetStartAngle(serie);
var runtimePieDataTotal = serie.yTotal;

SerieHelper.UpdateCenter(serie, chart.chartPosition, chart.chartWidth, chart.chartHeight);
float startDegree = serie.context.startAngle;
float totalDegree = 0;
float startDegree = 0;
float zeroReplaceValue = 0;
int showdataCount = 0;
foreach (var sd in serie.data)
Expand Down Expand Up @@ -253,7 +254,7 @@ private void UpdateRuntimeData(Serie serie)

private double GetTotalAngle(Serie serie, double dataTotal, ref float totalAngle)
{
totalAngle = 360f;
totalAngle = serie.context.startAngle + 360f;
if (serie.minAngle > 0)
{
var rate = serie.minAngle / 360;
Expand Down Expand Up @@ -435,6 +436,7 @@ private void DrawPieLabelLine(VertexHelper vh, Serie serie, SerieData serieData,
Vector3 pos4, pos6;
var horizontalLineCircleRadius = labelLine.lineWidth * 4f;
var lineCircleDiff = horizontalLineCircleRadius - 0.3f;
var startAngle = serie.context.startAngle;
if (currAngle < 90)
{
var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3;
Expand Down Expand Up @@ -467,7 +469,8 @@ private void DrawPieLabelLine(VertexHelper vh, Serie serie, SerieData serieData,
pos6 = pos0 + Vector3.left * lineCircleDiff;
pos4 = pos6 + Vector3.left * r4;
}
var pos5X = currAngle > 180 ? pos2.x - labelLine.lineLength2 : pos2.x + labelLine.lineLength2;
var pos5X = (currAngle - startAngle) % 360 > 180
? pos2.x - labelLine.lineLength2 : pos2.x + labelLine.lineLength2;
var pos5 = new Vector3(pos5X, pos2.y);
switch (labelLine.lineType)
{
Expand Down Expand Up @@ -526,5 +529,32 @@ private bool PointerIsInPieSerie(Serie serie, Vector2 local)

return false;
}

private float GetStartAngle(Serie serie)
{
return serie.clockwise ? (serie.startAngle + 360) % 360 : 360 - serie.startAngle;
}

private float GetToAngle(Serie serie, float angle)
{
var toAngle = angle + serie.startAngle;
if (!serie.clockwise)
{
toAngle = 360 - angle - serie.startAngle;
}
if (!serie.animation.IsFinish())
{
var currAngle = serie.animation.GetCurrDetail();
if (serie.clockwise)
{
toAngle = toAngle > currAngle ? currAngle : toAngle;
}
else
{
toAngle = toAngle < 360 - currAngle ? 360 - currAngle : toAngle;
}
}
return toAngle;
}
}
}
1 change: 1 addition & 0 deletions Runtime/Serie/SerieContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SerieContext
/// 外半径
/// </summary>
public float outsideRadius;
public float startAngle;
/// <summary>
/// 最大值
/// </summary>
Expand Down

0 comments on commit 80c77fc

Please sign in to comment.