mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-25 18:30:14 +00:00
优化代码,更好的支持自定义图表
This commit is contained in:
@@ -277,7 +277,7 @@ namespace XCharts
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
foreach (var serie in series.list)
|
foreach (var serie in series.list)
|
||||||
{
|
{
|
||||||
if (serie.show && (serie.type == type || serie.type == SerieType.Custom))
|
if (serie.show && (serie.type == type))
|
||||||
{
|
{
|
||||||
if (stackName.Equals(serie.stack)) count++;
|
if (stackName.Equals(serie.stack)) count++;
|
||||||
if (count >= 2) return true;
|
if (count >= 2) return true;
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ namespace XCharts
|
|||||||
protected Action<VertexHelper, Serie> m_OnCustomDrawSerieAfterCallback;
|
protected Action<VertexHelper, Serie> m_OnCustomDrawSerieAfterCallback;
|
||||||
protected Action<PointerEventData, int, int> m_OnPointerClickPie;
|
protected Action<PointerEventData, int, int> m_OnPointerClickPie;
|
||||||
|
|
||||||
internal bool m_RefreshLabel = false;
|
protected bool m_RefreshLabel = false;
|
||||||
internal bool m_ReinitLabel = false;
|
internal bool m_ReinitLabel = false;
|
||||||
internal bool m_ReinitTitle = false;
|
internal bool m_ReinitTitle = false;
|
||||||
internal bool m_CheckAnimation = false;
|
internal bool m_CheckAnimation = false;
|
||||||
@@ -870,7 +870,9 @@ namespace XCharts
|
|||||||
protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie)
|
protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie)
|
||||||
{
|
{
|
||||||
foreach (var drawSerie in m_DrawSeries)
|
foreach (var drawSerie in m_DrawSeries)
|
||||||
|
{
|
||||||
drawSerie.DrawSerie(vh, serie);
|
drawSerie.DrawSerie(vh, serie);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void DrawPainterTop(VertexHelper vh)
|
protected virtual void DrawPainterTop(VertexHelper vh)
|
||||||
@@ -975,7 +977,7 @@ namespace XCharts
|
|||||||
cornerRadius, backgroundColor, smoothness);
|
cornerRadius, backgroundColor, smoothness);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void DrawLabelBackground(VertexHelper vh, Serie serie, SerieData serieData)
|
public void DrawLabelBackground(VertexHelper vh, Serie serie, SerieData serieData)
|
||||||
{
|
{
|
||||||
if (serieData == null || serieData.labelObject == null) return;
|
if (serieData == null || serieData.labelObject == null) return;
|
||||||
var serieLabel = SerieHelper.GetSerieLabel(serie, serieData);
|
var serieLabel = SerieHelper.GetSerieLabel(serie, serieData);
|
||||||
|
|||||||
@@ -153,12 +153,12 @@ namespace XCharts
|
|||||||
protected override void DrawPainterSerie(VertexHelper vh, Serie serie)
|
protected override void DrawPainterSerie(VertexHelper vh, Serie serie)
|
||||||
{
|
{
|
||||||
base.DrawPainterSerie(vh, serie);
|
base.DrawPainterSerie(vh, serie);
|
||||||
serie.dataPoints.Clear();
|
|
||||||
var colorIndex = m_LegendRealShowName.IndexOf(serie.legendName);
|
var colorIndex = m_LegendRealShowName.IndexOf(serie.legendName);
|
||||||
bool yCategory = IsAnyYAxisIsCategory();
|
bool yCategory = IsAnyYAxisIsCategory();
|
||||||
switch (serie.type)
|
switch (serie.type)
|
||||||
{
|
{
|
||||||
case SerieType.Line:
|
case SerieType.Line:
|
||||||
|
serie.dataPoints.Clear();
|
||||||
if (yCategory) DrawYLineSerie(vh, serie, colorIndex);
|
if (yCategory) DrawYLineSerie(vh, serie, colorIndex);
|
||||||
else DrawXLineSerie(vh, serie, colorIndex);
|
else DrawXLineSerie(vh, serie, colorIndex);
|
||||||
if (!SeriesHelper.IsStack(m_Series))
|
if (!SeriesHelper.IsStack(m_Series))
|
||||||
@@ -168,20 +168,25 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SerieType.Bar:
|
case SerieType.Bar:
|
||||||
|
serie.dataPoints.Clear();
|
||||||
if (yCategory) DrawYBarSerie(vh, serie, colorIndex);
|
if (yCategory) DrawYBarSerie(vh, serie, colorIndex);
|
||||||
else DrawXBarSerie(vh, serie, colorIndex);
|
else DrawXBarSerie(vh, serie, colorIndex);
|
||||||
break;
|
break;
|
||||||
case SerieType.Scatter:
|
case SerieType.Scatter:
|
||||||
case SerieType.EffectScatter:
|
case SerieType.EffectScatter:
|
||||||
|
serie.dataPoints.Clear();
|
||||||
DrawScatterSerie(vh, colorIndex, serie);
|
DrawScatterSerie(vh, colorIndex, serie);
|
||||||
break;
|
break;
|
||||||
case SerieType.Heatmap:
|
case SerieType.Heatmap:
|
||||||
|
serie.dataPoints.Clear();
|
||||||
DrawHeatmapSerie(vh, colorIndex, serie);
|
DrawHeatmapSerie(vh, colorIndex, serie);
|
||||||
break;
|
break;
|
||||||
case SerieType.Candlestick:
|
case SerieType.Candlestick:
|
||||||
|
serie.dataPoints.Clear();
|
||||||
DrawCandlestickSerie(vh, colorIndex, serie);
|
DrawCandlestickSerie(vh, colorIndex, serie);
|
||||||
break;
|
break;
|
||||||
case SerieType.Gantt:
|
case SerieType.Gantt:
|
||||||
|
serie.dataPoints.Clear();
|
||||||
DrawGanttSerie(vh, colorIndex, serie);
|
DrawGanttSerie(vh, colorIndex, serie);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1567,7 +1572,7 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsAnyYAxisIsCategory()
|
public bool IsAnyYAxisIsCategory()
|
||||||
{
|
{
|
||||||
foreach (var yAxis in m_YAxes)
|
foreach (var yAxis in m_YAxes)
|
||||||
{
|
{
|
||||||
@@ -1579,7 +1584,7 @@ namespace XCharts
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void DrawLabelBackground(VertexHelper vh)
|
protected virtual void DrawLabelBackground(VertexHelper vh)
|
||||||
{
|
{
|
||||||
var isYAxis = IsAnyYAxisIsCategory();
|
var isYAxis = IsAnyYAxisIsCategory();
|
||||||
for (int n = 0; n < m_Series.Count; n++)
|
for (int n = 0; n < m_Series.Count; n++)
|
||||||
@@ -1663,7 +1668,6 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
base.OnRefreshLabel();
|
base.OnRefreshLabel();
|
||||||
var anyPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
var anyPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
|
||||||
|
|
||||||
for (int i = 0; i < m_Series.Count; i++)
|
for (int i = 0; i < m_Series.Count; i++)
|
||||||
{
|
{
|
||||||
var serie = m_Series.GetSerie(i);
|
var serie = m_Series.GetSerie(i);
|
||||||
@@ -1702,7 +1706,7 @@ namespace XCharts
|
|||||||
var content = "";
|
var content = "";
|
||||||
if (anyPercentStack && isPercentStack)
|
if (anyPercentStack && isPercentStack)
|
||||||
{
|
{
|
||||||
var tempTotal = Internal_GetBarSameStackTotalValue(serie.stack, j);
|
var tempTotal = Internal_GetBarSameStackTotalValue(serie.stack, j, SerieType.Custom);
|
||||||
content = SerieLabelHelper.GetFormatterContent(serie, serieData, value, tempTotal,
|
content = SerieLabelHelper.GetFormatterContent(serie, serieData, value, tempTotal,
|
||||||
serieLabel, theme.GetColor(i));
|
serieLabel, theme.GetColor(i));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,12 +27,12 @@ namespace XCharts
|
|||||||
var grid = GetSerieGridOrDefault(serie);
|
var grid = GetSerieGridOrDefault(serie);
|
||||||
var showData = serie.GetDataList(dataZoom);
|
var showData = serie.GetDataList(dataZoom);
|
||||||
float categoryWidth = AxisHelper.GetDataWidth(yAxis, grid.runtimeHeight, showData.Count, dataZoom);
|
float categoryWidth = AxisHelper.GetDataWidth(yAxis, grid.runtimeHeight, showData.Count, dataZoom);
|
||||||
float barGap = Internal_GetBarGap();
|
float barGap = Internal_GetBarGap(SerieType.Bar);
|
||||||
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap);
|
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap, SerieType.Bar);
|
||||||
float barWidth = serie.GetBarWidth(categoryWidth);
|
float barWidth = serie.GetBarWidth(categoryWidth);
|
||||||
float offset = (categoryWidth - totalBarWidth) / 2;
|
float offset = (categoryWidth - totalBarWidth) / 2;
|
||||||
float barGapWidth = barWidth + barWidth * barGap;
|
float barGapWidth = barWidth + barWidth * barGap;
|
||||||
float space = serie.barGap == -1 ? offset : offset + Internal_GetBarIndex(serie) * barGapWidth;
|
float space = serie.barGap == -1 ? offset : offset + Internal_GetBarIndex(serie, SerieType.Bar) * barGapWidth;
|
||||||
var isStack = SeriesHelper.IsStack(m_Series, serie.stack, SerieType.Bar);
|
var isStack = SeriesHelper.IsStack(m_Series, serie.stack, SerieType.Bar);
|
||||||
m_StackSerieData.Clear();
|
m_StackSerieData.Clear();
|
||||||
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
|
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
|
||||||
@@ -81,7 +81,7 @@ namespace XCharts
|
|||||||
var valueTotal = 0f;
|
var valueTotal = 0f;
|
||||||
if (isPercentStack)
|
if (isPercentStack)
|
||||||
{
|
{
|
||||||
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i);
|
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i, SerieType.Bar);
|
||||||
barHig = valueTotal != 0 ? (value / valueTotal * grid.runtimeWidth) : 0;
|
barHig = valueTotal != 0 ? (value / valueTotal * grid.runtimeWidth) : 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -175,12 +175,12 @@ namespace XCharts
|
|||||||
m_StackSerieData.Clear();
|
m_StackSerieData.Clear();
|
||||||
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
|
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
|
||||||
float categoryWidth = AxisHelper.GetDataWidth(xAxis, grid.runtimeWidth, showData.Count, dataZoom);
|
float categoryWidth = AxisHelper.GetDataWidth(xAxis, grid.runtimeWidth, showData.Count, dataZoom);
|
||||||
float barGap = Internal_GetBarGap();
|
float barGap = Internal_GetBarGap(SerieType.Bar);
|
||||||
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap);
|
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap, SerieType.Bar);
|
||||||
float barWidth = serie.GetBarWidth(categoryWidth);
|
float barWidth = serie.GetBarWidth(categoryWidth);
|
||||||
float offset = (categoryWidth - totalBarWidth) / 2;
|
float offset = (categoryWidth - totalBarWidth) / 2;
|
||||||
float barGapWidth = barWidth + barWidth * barGap;
|
float barGapWidth = barWidth + barWidth * barGap;
|
||||||
float space = serie.barGap == -1 ? offset : offset + Internal_GetBarIndex(serie) * barGapWidth;
|
float space = serie.barGap == -1 ? offset : offset + Internal_GetBarIndex(serie, SerieType.Bar) * barGapWidth;
|
||||||
int maxCount = serie.maxShow > 0
|
int maxCount = serie.maxShow > 0
|
||||||
? (serie.maxShow > showData.Count ? showData.Count : serie.maxShow)
|
? (serie.maxShow > showData.Count ? showData.Count : serie.maxShow)
|
||||||
: showData.Count;
|
: showData.Count;
|
||||||
@@ -224,7 +224,7 @@ namespace XCharts
|
|||||||
var valueTotal = 0f;
|
var valueTotal = 0f;
|
||||||
if (isPercentStack)
|
if (isPercentStack)
|
||||||
{
|
{
|
||||||
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i);
|
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i, SerieType.Bar);
|
||||||
barHig = valueTotal != 0 ? (value / valueTotal * grid.runtimeHeight) : 0;
|
barHig = valueTotal != 0 ? (value / valueTotal * grid.runtimeHeight) : 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -599,13 +599,13 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Internal_GetBarGap()
|
public float Internal_GetBarGap(SerieType type)
|
||||||
{
|
{
|
||||||
float gap = 0f;
|
float gap = 0f;
|
||||||
for (int i = 0; i < m_Series.Count; i++)
|
for (int i = 0; i < m_Series.Count; i++)
|
||||||
{
|
{
|
||||||
var serie = m_Series.list[i];
|
var serie = m_Series.list[i];
|
||||||
if (serie.type == SerieType.Bar || serie.type == SerieType.Candlestick || serie.type == SerieType.Custom)
|
if (serie.type == type)
|
||||||
{
|
{
|
||||||
if (serie.barGap != 0)
|
if (serie.barGap != 0)
|
||||||
{
|
{
|
||||||
@@ -616,13 +616,13 @@ namespace XCharts
|
|||||||
return gap;
|
return gap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Internal_GetBarSameStackTotalValue(string stack, int dataIndex)
|
public float Internal_GetBarSameStackTotalValue(string stack, int dataIndex, SerieType type)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(stack)) return 0;
|
if (string.IsNullOrEmpty(stack)) return 0;
|
||||||
float total = 0;
|
float total = 0;
|
||||||
foreach (var serie in m_Series.list)
|
foreach (var serie in m_Series.list)
|
||||||
{
|
{
|
||||||
if (serie.type == SerieType.Bar || serie.type == SerieType.Custom)
|
if (serie.type == type)
|
||||||
{
|
{
|
||||||
if (stack.Equals(serie.stack))
|
if (stack.Equals(serie.stack))
|
||||||
{
|
{
|
||||||
@@ -635,7 +635,7 @@ namespace XCharts
|
|||||||
|
|
||||||
|
|
||||||
private HashSet<string> barStackSet = new HashSet<string>();
|
private HashSet<string> barStackSet = new HashSet<string>();
|
||||||
public float Internal_GetBarTotalWidth(float categoryWidth, float gap)
|
public float Internal_GetBarTotalWidth(float categoryWidth, float gap, SerieType type)
|
||||||
{
|
{
|
||||||
float total = 0;
|
float total = 0;
|
||||||
float lastGap = 0;
|
float lastGap = 0;
|
||||||
@@ -644,14 +644,14 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
var serie = m_Series.list[i];
|
var serie = m_Series.list[i];
|
||||||
if (!serie.show) continue;
|
if (!serie.show) continue;
|
||||||
if (serie.type == SerieType.Bar || serie.type == SerieType.Candlestick || serie.type == SerieType.Custom)
|
if (serie.type == type)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(serie.stack))
|
if (!string.IsNullOrEmpty(serie.stack))
|
||||||
{
|
{
|
||||||
if (barStackSet.Contains(serie.stack)) continue;
|
if (barStackSet.Contains(serie.stack)) continue;
|
||||||
barStackSet.Add(serie.stack);
|
barStackSet.Add(serie.stack);
|
||||||
}
|
}
|
||||||
var width = GetStackBarWidth(categoryWidth, serie);
|
var width = GetStackBarWidth(categoryWidth, serie, type);
|
||||||
if (gap == -1)
|
if (gap == -1)
|
||||||
{
|
{
|
||||||
if (width > total) total = width;
|
if (width > total) total = width;
|
||||||
@@ -668,14 +668,14 @@ namespace XCharts
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
private float GetStackBarWidth(float categoryWidth, Serie now)
|
private float GetStackBarWidth(float categoryWidth, Serie now, SerieType type)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(now.stack)) return now.GetBarWidth(categoryWidth);
|
if (string.IsNullOrEmpty(now.stack)) return now.GetBarWidth(categoryWidth);
|
||||||
float barWidth = 0;
|
float barWidth = 0;
|
||||||
for (int i = 0; i < m_Series.Count; i++)
|
for (int i = 0; i < m_Series.Count; i++)
|
||||||
{
|
{
|
||||||
var serie = m_Series.list[i];
|
var serie = m_Series.list[i];
|
||||||
if ((serie.type == SerieType.Bar || serie.type == SerieType.Candlestick || serie.type == SerieType.Custom)
|
if ((serie.type == type)
|
||||||
&& serie.show && now.stack.Equals(serie.stack))
|
&& serie.show && now.stack.Equals(serie.stack))
|
||||||
{
|
{
|
||||||
if (serie.barWidth > barWidth) barWidth = serie.barWidth;
|
if (serie.barWidth > barWidth) barWidth = serie.barWidth;
|
||||||
@@ -686,14 +686,14 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<string> tempList = new List<string>();
|
private List<string> tempList = new List<string>();
|
||||||
public int Internal_GetBarIndex(Serie currSerie)
|
public int Internal_GetBarIndex(Serie currSerie, SerieType type)
|
||||||
{
|
{
|
||||||
tempList.Clear();
|
tempList.Clear();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int i = 0; i < m_Series.Count; i++)
|
for (int i = 0; i < m_Series.Count; i++)
|
||||||
{
|
{
|
||||||
var serie = m_Series.GetSerie(i);
|
var serie = m_Series.GetSerie(i);
|
||||||
if (serie.type != SerieType.Bar && serie.type != SerieType.Custom) continue;
|
if (serie.type != type) continue;
|
||||||
if (string.IsNullOrEmpty(serie.stack))
|
if (string.IsNullOrEmpty(serie.stack))
|
||||||
{
|
{
|
||||||
if (serie.index == currSerie.index) return index;
|
if (serie.index == currSerie.index) return index;
|
||||||
|
|||||||
Reference in New Issue
Block a user