mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-22 17:00:08 +00:00
优化代码
This commit is contained in:
@@ -554,6 +554,14 @@ namespace XCharts
|
||||
return type == AxisType.Log;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为时间轴。
|
||||
/// </summary>
|
||||
public bool IsTime()
|
||||
{
|
||||
return type == AxisType.Time;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一个类目到类目数据列表
|
||||
/// </summary>
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using XUGL;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
@@ -683,10 +684,18 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -991,6 +1000,12 @@ namespace XCharts
|
||||
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;
|
||||
@@ -1000,5 +1015,87 @@ namespace XCharts
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -279,6 +279,10 @@ namespace XCharts
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -674,7 +674,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(VertexHelper vh)
|
||||
public void DrawBase(VertexHelper vh)
|
||||
{
|
||||
foreach (var visualMap in chart.visualMaps)
|
||||
{
|
||||
@@ -691,6 +691,10 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTop(VertexHelper vh)
|
||||
{
|
||||
}
|
||||
|
||||
private void CheckVisualMap(VisualMap visualMap)
|
||||
{
|
||||
if (visualMap == null || !visualMap.enable || !visualMap.show) return;
|
||||
|
||||
@@ -176,6 +176,10 @@ namespace XCharts
|
||||
{
|
||||
m_Painter.Refresh();
|
||||
}
|
||||
internal void RefreshTopPainter()
|
||||
{
|
||||
m_PainterTop.Refresh();
|
||||
}
|
||||
|
||||
public void RefreshPainter(int index)
|
||||
{
|
||||
@@ -861,7 +865,7 @@ namespace XCharts
|
||||
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)
|
||||
{
|
||||
@@ -896,6 +900,7 @@ namespace XCharts
|
||||
{
|
||||
vh.Clear();
|
||||
DrawPainterTop(vh);
|
||||
foreach (var draw in m_ComponentHandlers) draw.DrawTop(vh);
|
||||
if (m_OnCustomDrawTopCallback != null)
|
||||
{
|
||||
m_OnCustomDrawTopCallback(vh);
|
||||
|
||||
@@ -78,8 +78,10 @@ namespace XCharts
|
||||
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)
|
||||
@@ -178,8 +180,12 @@ namespace XCharts
|
||||
|
||||
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))
|
||||
{
|
||||
@@ -1280,87 +1286,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -333,7 +333,7 @@ namespace XCharts
|
||||
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)
|
||||
@@ -348,7 +348,7 @@ namespace XCharts
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -14,7 +14,8 @@ namespace XCharts
|
||||
{
|
||||
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);
|
||||
|
||||
@@ -247,7 +247,7 @@ namespace XCharts
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user