Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed May 25, 2021
1 parent 6872d42 commit f656cba
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 91 deletions.
8 changes: 8 additions & 0 deletions Assets/XCharts/Runtime/Component/Main/Axis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@ public bool IsLog()
return type == AxisType.Log;
}

/// <summary>
/// 是否为时间轴。
/// </summary>
public bool IsTime()
{
return type == AxisType.Time;
}

/// <summary>
/// 添加一个类目到类目数据列表
/// </summary>
Expand Down
99 changes: 98 additions & 1 deletion Assets/XCharts/Runtime/Component/Main/DataZoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XUGL;

namespace XCharts
{
Expand Down Expand Up @@ -683,10 +684,18 @@ public void Update()
}
}

public void Draw(VertexHelper vh)
public void DrawBase(VertexHelper vh)
{
}

public void DrawTop(VertexHelper vh)
{
foreach (var dataZoom in chart.dataZooms)
{
DrawDataZoomSlider(vh, dataZoom);
}
}

public void OnBeginDrag(PointerEventData eventData)
{
if (chart == null) return;
Expand Down Expand Up @@ -991,6 +1000,12 @@ private void CheckDataZoomLabel(DataZoom dataZoom)
dataZoom.SetStartLabelText(xAxis.data[startIndex]);
dataZoom.SetEndLabelText(xAxis.data[endIndex]);
}
else if (xAxis.IsTime())
{
//TODO:
dataZoom.SetStartLabelText("");
dataZoom.SetEndLabelText("");
}
chart.InitAxisX();
}
var start = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.start / 100;
Expand All @@ -1000,5 +1015,87 @@ private void CheckDataZoomLabel(DataZoom dataZoom)
dataZoom.UpdateEndLabelPosition(new Vector3(end + 10, chart.chartY + dataZoom.bottom + hig / 2));
}
}

private void DrawDataZoomSlider(VertexHelper vh, DataZoom dataZoom)
{
if (!dataZoom.enable || !dataZoom.supportSlider) return;
var p1 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY);
var p2 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY + dataZoom.runtimeHeight);
var p3 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY + dataZoom.runtimeHeight);
var p4 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY);
var xAxis = chart.GetXAxis(0);
var lineColor = dataZoom.lineStyle.GetColor(chart.theme.dataZoom.dataLineColor);
var lineWidth = dataZoom.lineStyle.GetWidth(chart.theme.dataZoom.dataLineWidth);
var borderWidth = dataZoom.borderWidth == 0 ? chart.theme.dataZoom.borderWidth : dataZoom.borderWidth;
var borderColor = dataZoom.GetBorderColor(chart.theme.dataZoom.borderColor);
var backgroundColor = dataZoom.GetBackgroundColor(chart.theme.dataZoom.backgroundColor);
var areaColor = dataZoom.areaStyle.GetColor(chart.theme.dataZoom.dataAreaColor);
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, backgroundColor);
var centerPos = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth / 2,
dataZoom.runtimeY + dataZoom.runtimeHeight / 2);
UGL.DrawBorder(vh, centerPos, dataZoom.runtimeWidth, dataZoom.runtimeHeight, borderWidth, borderColor);
if (dataZoom.showDataShadow && chart.series.Count > 0)
{
Serie serie = chart.series.list[0];
Axis axis = chart.GetYAxis(0);
var showData = serie.GetDataList(null);
float scaleWid = dataZoom.runtimeWidth / (showData.Count - 1);
Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero;
float minValue = 0;
float maxValue = 0;
SeriesHelper.GetYMinMaxValue(chart.series, null, 0, chart.IsValue(), axis.inverse, out minValue, out maxValue);
AxisHelper.AdjustMinMaxValue(axis, ref minValue, ref maxValue, true);

int rate = 1;
var sampleDist = serie.sampleDist < 2 ? 2 : serie.sampleDist;
var maxCount = showData.Count;
if (sampleDist > 0) rate = (int)((maxCount - serie.minShow) / (dataZoom.runtimeWidth / sampleDist));
if (rate < 1) rate = 1;
var totalAverage = serie.sampleAverage > 0 ? serie.sampleAverage :
chart.DataAverage(ref showData, serie.sampleType, serie.minShow, maxCount, rate);
var dataChanging = false;
for (int i = 0; i < maxCount; i += rate)
{
float value = chart.SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage, i,
serie.animation.GetUpdateAnimationDuration(), ref dataChanging, axis);
float pX = dataZoom.runtimeX + i * scaleWid;
float dataHig = (maxValue - minValue) == 0 ? 0 :
(value - minValue) / (maxValue - minValue) * dataZoom.runtimeHeight;
np = new Vector3(pX, chart.chartY + dataZoom.bottom + dataHig);
if (i > 0)
{
UGL.DrawLine(vh, lp, np, lineWidth, lineColor);
Vector3 alp = new Vector3(lp.x, lp.y - lineWidth);
Vector3 anp = new Vector3(np.x, np.y - lineWidth);

Vector3 tnp = new Vector3(np.x, chart.chartY + dataZoom.bottom + lineWidth);
Vector3 tlp = new Vector3(lp.x, chart.chartY + dataZoom.bottom + lineWidth);
UGL.DrawQuadrilateral(vh, alp, anp, tnp, tlp, areaColor);
}
lp = np;
}
if (dataChanging)
{
chart.RefreshTopPainter();
}
}
switch (dataZoom.rangeMode)
{
case DataZoom.RangeMode.Percent:
var start = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.start / 100;
var end = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.end / 100;
var fillerColor = dataZoom.GetFillerColor(chart.theme.dataZoom.fillerColor);

p1 = new Vector2(start, dataZoom.runtimeY);
p2 = new Vector2(start, dataZoom.runtimeY + dataZoom.runtimeHeight);
p3 = new Vector2(end, dataZoom.runtimeY + dataZoom.runtimeHeight);
p4 = new Vector2(end, dataZoom.runtimeY);
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, fillerColor);
UGL.DrawLine(vh, p1, p2, lineWidth, fillerColor);
UGL.DrawLine(vh, p3, p4, lineWidth, fillerColor);
break;
}
}
}
}
4 changes: 4 additions & 0 deletions Assets/XCharts/Runtime/Component/Main/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ public Serie AddSerie(SerieType type, string serieName, bool show = true, bool a
serie.animation.Restart();
if (addToHead) m_Series.Insert(0, serie);
else m_Series.Add(serie);
for (int i = 0; i < m_Series.Count; i++)
{
m_Series[i].index = i;
}
SetVerticesDirty();
return serie;
}
Expand Down
6 changes: 5 additions & 1 deletion Assets/XCharts/Runtime/Component/Main/VisualMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ public void Update()
}
}

public void Draw(VertexHelper vh)
public void DrawBase(VertexHelper vh)
{
foreach (var visualMap in chart.visualMaps)
{
Expand All @@ -691,6 +691,10 @@ public void Draw(VertexHelper vh)
}
}

public void DrawTop(VertexHelper vh)
{
}

private void CheckVisualMap(VisualMap visualMap)
{
if (visualMap == null || !visualMap.enable || !visualMap.show) return;
Expand Down
7 changes: 6 additions & 1 deletion Assets/XCharts/Runtime/Internal/BaseChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ internal void RefreshBasePainter()
{
m_Painter.Refresh();
}
internal void RefreshTopPainter()
{
m_PainterTop.Refresh();
}

public void RefreshPainter(int index)
{
Expand Down Expand Up @@ -861,7 +865,7 @@ protected override void OnDrawPainterBase(VertexHelper vh, Painter painter)
DrawBackground(vh);
DrawPainterBase(vh);
DrawLegend(vh);
foreach (var draw in m_ComponentHandlers) draw.Draw(vh);
foreach (var draw in m_ComponentHandlers) draw.DrawBase(vh);
foreach (var draw in m_DrawSeries) draw.DrawBase(vh);
if (m_OnCustomDrawBaseCallback != null)
{
Expand Down Expand Up @@ -896,6 +900,7 @@ protected virtual void OnDrawPainterTop(VertexHelper vh, Painter painter)
{
vh.Clear();
DrawPainterTop(vh);
foreach (var draw in m_ComponentHandlers) draw.DrawTop(vh);
if (m_OnCustomDrawTopCallback != null)
{
m_OnCustomDrawTopCallback(vh);
Expand Down
94 changes: 10 additions & 84 deletions Assets/XCharts/Runtime/Internal/CoordinateChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ private void RefreshSeriePainterByAxisIndex(Axis axis)
protected override void DrawPainterBase(VertexHelper vh)
{
base.DrawPainterBase(vh);
DrawCoordinate(vh);
DrawDataZoomSlider(vh);
if (!SeriesHelper.IsAnyClipSerie(m_Series))
{
DrawCoordinate(vh);
}
}

protected override void DrawBackground(VertexHelper vh)
Expand Down Expand Up @@ -178,8 +180,12 @@ protected override void DrawPainterSerie(VertexHelper vh, Serie serie)

protected override void DrawPainterTop(VertexHelper vh)
{
DrawAxisTick(vh);
DrawClip(vh);
if (SeriesHelper.IsAnyClipSerie(m_Series))
{
DrawCoordinate(vh);
}
DrawAxisTick(vh);
DrawLabelBackground(vh);
if (SeriesHelper.IsStack(m_Series))
{
Expand Down Expand Up @@ -1280,87 +1286,7 @@ private void DrawYAxisLine(VertexHelper vh, int yAxisIndex, YAxis yAxis)
}
}

private void DrawDataZoomSlider(VertexHelper vh)
{
if (!dataZoom.enable || !dataZoom.supportSlider) return;
var p1 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY);
var p2 = new Vector3(dataZoom.runtimeX, dataZoom.runtimeY + dataZoom.runtimeHeight);
var p3 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY + dataZoom.runtimeHeight);
var p4 = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth, dataZoom.runtimeY);
var xAxis = xAxes[0];
var lineColor = dataZoom.lineStyle.GetColor(m_Theme.dataZoom.dataLineColor);
var lineWidth = dataZoom.lineStyle.GetWidth(m_Theme.dataZoom.dataLineWidth);
var borderWidth = dataZoom.borderWidth == 0 ? m_Theme.dataZoom.borderWidth : dataZoom.borderWidth;
var borderColor = dataZoom.GetBorderColor(m_Theme.dataZoom.borderColor);
var backgroundColor = dataZoom.GetBackgroundColor(m_Theme.dataZoom.backgroundColor);
var areaColor = dataZoom.areaStyle.GetColor(m_Theme.dataZoom.dataAreaColor);
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, backgroundColor);
var centerPos = new Vector3(dataZoom.runtimeX + dataZoom.runtimeWidth / 2,
dataZoom.runtimeY + dataZoom.runtimeHeight / 2);
UGL.DrawBorder(vh, centerPos, dataZoom.runtimeWidth, dataZoom.runtimeHeight, borderWidth, borderColor);
if (dataZoom.showDataShadow && m_Series.Count > 0)
{
Serie serie = m_Series.list[0];
Axis axis = yAxes[0];
var showData = serie.GetDataList(null);
float scaleWid = dataZoom.runtimeWidth / (showData.Count - 1);
Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero;
float minValue = 0;
float maxValue = 0;
SeriesHelper.GetYMinMaxValue(m_Series, null, 0, IsValue(), axis.inverse, out minValue, out maxValue);
AxisHelper.AdjustMinMaxValue(axis, ref minValue, ref maxValue, true);

int rate = 1;
var sampleDist = serie.sampleDist < 2 ? 2 : serie.sampleDist;
var maxCount = showData.Count;
if (sampleDist > 0) rate = (int)((maxCount - serie.minShow) / (dataZoom.runtimeWidth / sampleDist));
if (rate < 1) rate = 1;
var totalAverage = serie.sampleAverage > 0 ? serie.sampleAverage :
DataAverage(ref showData, serie.sampleType, serie.minShow, maxCount, rate);
var dataChanging = false;
for (int i = 0; i < maxCount; i += rate)
{
float value = SampleValue(ref showData, serie.sampleType, rate, serie.minShow, maxCount, totalAverage, i,
serie.animation.GetUpdateAnimationDuration(), ref dataChanging, axis);
float pX = dataZoom.runtimeX + i * scaleWid;
float dataHig = (maxValue - minValue) == 0 ? 0 :
(value - minValue) / (maxValue - minValue) * dataZoom.runtimeHeight;
np = new Vector3(pX, m_ChartY + dataZoom.bottom + dataHig);
if (i > 0)
{
UGL.DrawLine(vh, lp, np, lineWidth, lineColor);
Vector3 alp = new Vector3(lp.x, lp.y - lineWidth);
Vector3 anp = new Vector3(np.x, np.y - lineWidth);

Vector3 tnp = new Vector3(np.x, m_ChartY + dataZoom.bottom + lineWidth);
Vector3 tlp = new Vector3(lp.x, m_ChartY + dataZoom.bottom + lineWidth);
UGL.DrawQuadrilateral(vh, alp, anp, tnp, tlp, areaColor);
}
lp = np;
}
if (dataChanging)
{
RefreshChart();
}
}
switch (dataZoom.rangeMode)
{
case DataZoom.RangeMode.Percent:
var start = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.start / 100;
var end = dataZoom.runtimeX + dataZoom.runtimeWidth * dataZoom.end / 100;
var fillerColor = dataZoom.GetFillerColor(m_Theme.dataZoom.fillerColor);

p1 = new Vector2(start, dataZoom.runtimeY);
p2 = new Vector2(start, dataZoom.runtimeY + dataZoom.runtimeHeight);
p3 = new Vector2(end, dataZoom.runtimeY + dataZoom.runtimeHeight);
p4 = new Vector2(end, dataZoom.runtimeY);
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, fillerColor);
UGL.DrawLine(vh, p1, p2, lineWidth, fillerColor);
UGL.DrawLine(vh, p3, p4, lineWidth, fillerColor);
break;
}
}


protected void DrawXTooltipIndicator(VertexHelper vh)
{
Expand Down
4 changes: 2 additions & 2 deletions Assets/XCharts/Runtime/Internal/CoordinateChart_DrawLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private Vector3 GetLLPos(List<Vector3> dataPoints, int index, Vector3 lp)
return lp;
}

private float DataAverage(ref List<SerieData> showData, SampleType sampleType, int minCount, int maxCount, int rate)
internal float DataAverage(ref List<SerieData> showData, SampleType sampleType, int minCount, int maxCount, int rate)
{
var totalAverage = 0f;
if (rate > 1 && sampleType == SampleType.Peak)
Expand All @@ -348,7 +348,7 @@ private float DataAverage(ref List<SerieData> showData, SampleType sampleType, i
return totalAverage;
}

private float SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate,
internal float SampleValue(ref List<SerieData> showData, SampleType sampleType, int rate,
int minCount, int maxCount, float totalAverage, int index, float dataChangeDuration,
ref bool dataChanging, Axis axis)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public interface IComponentHandler
{
void Init();
void Update();
void Draw(VertexHelper vh);
void DrawBase(VertexHelper vh);
void DrawTop(VertexHelper vh);
void OnDrag(PointerEventData eventData);
void OnBeginDrag(PointerEventData eventData);
void OnEndDrag(PointerEventData eventData);
Expand Down
2 changes: 1 addition & 1 deletion Assets/XCharts/Runtime/Internal/Utility/ChartHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static ChartText AddTextObject(string name, Transform parent, Vector2 anc
chartText.tmpText.alignment = textStyle.tmpAlignment;
chartText.tmpText.richText = true;
chartText.tmpText.raycastTarget = false;
chartText.tmpText.enableWordWrapping = textStyle.wrap;
chartText.tmpText.enableWordWrapping = textStyle.autoWrap;
#else
chartText.text = GetOrAddComponent<Text>(txtObj);
chartText.text.font = textStyle.font == null ? theme.font : textStyle.font;
Expand Down

0 comments on commit f656cba

Please sign in to comment.