优化代码,更好的支持自定义图表

This commit is contained in:
monitor1394
2021-04-30 13:10:19 +08:00
parent 852f93dec5
commit 4e4f501e68
4 changed files with 33 additions and 27 deletions

View File

@@ -277,7 +277,7 @@ namespace XCharts
int count = 0;
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 (count >= 2) return true;

View File

@@ -75,7 +75,7 @@ namespace XCharts
protected Action<VertexHelper, Serie> m_OnCustomDrawSerieAfterCallback;
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_ReinitTitle = false;
internal bool m_CheckAnimation = false;
@@ -870,7 +870,9 @@ namespace XCharts
protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie)
{
foreach (var drawSerie in m_DrawSeries)
{
drawSerie.DrawSerie(vh, serie);
}
}
protected virtual void DrawPainterTop(VertexHelper vh)
@@ -975,7 +977,7 @@ namespace XCharts
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;
var serieLabel = SerieHelper.GetSerieLabel(serie, serieData);

View File

@@ -153,12 +153,12 @@ namespace XCharts
protected override void DrawPainterSerie(VertexHelper vh, Serie serie)
{
base.DrawPainterSerie(vh, serie);
serie.dataPoints.Clear();
var colorIndex = m_LegendRealShowName.IndexOf(serie.legendName);
bool yCategory = IsAnyYAxisIsCategory();
switch (serie.type)
{
case SerieType.Line:
serie.dataPoints.Clear();
if (yCategory) DrawYLineSerie(vh, serie, colorIndex);
else DrawXLineSerie(vh, serie, colorIndex);
if (!SeriesHelper.IsStack(m_Series))
@@ -168,20 +168,25 @@ namespace XCharts
}
break;
case SerieType.Bar:
serie.dataPoints.Clear();
if (yCategory) DrawYBarSerie(vh, serie, colorIndex);
else DrawXBarSerie(vh, serie, colorIndex);
break;
case SerieType.Scatter:
case SerieType.EffectScatter:
serie.dataPoints.Clear();
DrawScatterSerie(vh, colorIndex, serie);
break;
case SerieType.Heatmap:
serie.dataPoints.Clear();
DrawHeatmapSerie(vh, colorIndex, serie);
break;
case SerieType.Candlestick:
serie.dataPoints.Clear();
DrawCandlestickSerie(vh, colorIndex, serie);
break;
case SerieType.Gantt:
serie.dataPoints.Clear();
DrawGanttSerie(vh, colorIndex, serie);
break;
}
@@ -1567,7 +1572,7 @@ namespace XCharts
}
}
private bool IsAnyYAxisIsCategory()
public bool IsAnyYAxisIsCategory()
{
foreach (var yAxis in m_YAxes)
{
@@ -1579,7 +1584,7 @@ namespace XCharts
return false;
}
protected void DrawLabelBackground(VertexHelper vh)
protected virtual void DrawLabelBackground(VertexHelper vh)
{
var isYAxis = IsAnyYAxisIsCategory();
for (int n = 0; n < m_Series.Count; n++)
@@ -1663,7 +1668,6 @@ namespace XCharts
{
base.OnRefreshLabel();
var anyPercentStack = SeriesHelper.IsPercentStack(m_Series, SerieType.Bar);
for (int i = 0; i < m_Series.Count; i++)
{
var serie = m_Series.GetSerie(i);
@@ -1702,7 +1706,7 @@ namespace XCharts
var content = "";
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,
serieLabel, theme.GetColor(i));
}

View File

@@ -27,12 +27,12 @@ namespace XCharts
var grid = GetSerieGridOrDefault(serie);
var showData = serie.GetDataList(dataZoom);
float categoryWidth = AxisHelper.GetDataWidth(yAxis, grid.runtimeHeight, showData.Count, dataZoom);
float barGap = Internal_GetBarGap();
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap);
float barGap = Internal_GetBarGap(SerieType.Bar);
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap, SerieType.Bar);
float barWidth = serie.GetBarWidth(categoryWidth);
float offset = (categoryWidth - totalBarWidth) / 2;
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);
m_StackSerieData.Clear();
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
@@ -81,7 +81,7 @@ namespace XCharts
var valueTotal = 0f;
if (isPercentStack)
{
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i);
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i, SerieType.Bar);
barHig = valueTotal != 0 ? (value / valueTotal * grid.runtimeWidth) : 0;
}
else
@@ -175,12 +175,12 @@ namespace XCharts
m_StackSerieData.Clear();
if (isStack) SeriesHelper.UpdateStackDataList(m_Series, serie, dataZoom, m_StackSerieData);
float categoryWidth = AxisHelper.GetDataWidth(xAxis, grid.runtimeWidth, showData.Count, dataZoom);
float barGap = Internal_GetBarGap();
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap);
float barGap = Internal_GetBarGap(SerieType.Bar);
float totalBarWidth = Internal_GetBarTotalWidth(categoryWidth, barGap, SerieType.Bar);
float barWidth = serie.GetBarWidth(categoryWidth);
float offset = (categoryWidth - totalBarWidth) / 2;
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
? (serie.maxShow > showData.Count ? showData.Count : serie.maxShow)
: showData.Count;
@@ -224,7 +224,7 @@ namespace XCharts
var valueTotal = 0f;
if (isPercentStack)
{
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i);
valueTotal = Internal_GetBarSameStackTotalValue(serie.stack, i, SerieType.Bar);
barHig = valueTotal != 0 ? (value / valueTotal * grid.runtimeHeight) : 0;
}
else
@@ -599,13 +599,13 @@ namespace XCharts
}
}
public float Internal_GetBarGap()
public float Internal_GetBarGap(SerieType type)
{
float gap = 0f;
for (int i = 0; i < m_Series.Count; 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)
{
@@ -616,13 +616,13 @@ namespace XCharts
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;
float total = 0;
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))
{
@@ -635,7 +635,7 @@ namespace XCharts
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 lastGap = 0;
@@ -644,14 +644,14 @@ namespace XCharts
{
var serie = m_Series.list[i];
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 (barStackSet.Contains(serie.stack)) continue;
barStackSet.Add(serie.stack);
}
var width = GetStackBarWidth(categoryWidth, serie);
var width = GetStackBarWidth(categoryWidth, serie, type);
if (gap == -1)
{
if (width > total) total = width;
@@ -668,14 +668,14 @@ namespace XCharts
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);
float barWidth = 0;
for (int i = 0; i < m_Series.Count; 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))
{
if (serie.barWidth > barWidth) barWidth = serie.barWidth;
@@ -686,14 +686,14 @@ namespace XCharts
}
private List<string> tempList = new List<string>();
public int Internal_GetBarIndex(Serie currSerie)
public int Internal_GetBarIndex(Serie currSerie, SerieType type)
{
tempList.Clear();
int index = 0;
for (int i = 0; i < m_Series.Count; 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 (serie.index == currSerie.index) return index;