修复Bar在同一个Chart不同Grid里时绘制有些异常的问题

This commit is contained in:
monitor1394
2025-03-09 23:14:26 +08:00
parent ffd2521360
commit e4923ea598
5 changed files with 51 additions and 24 deletions

View File

@@ -79,6 +79,7 @@ slug: /changelog
## master ## master
* (2025.03.09) 修复`Bar`在同一个`Chart`不同`Grid`里时绘制有些异常的问题
* (2025.03.07) 增加`Animation``speed`可指定动画速度 * (2025.03.07) 增加`Animation``speed`可指定动画速度
* (2025.03.06) 优化`Animation`的新增动画表现 * (2025.03.06) 优化`Animation`的新增动画表现
* (2025.03.04) 修复`Treemap``label`显示异常的问题 * (2025.03.04) 修复`Treemap``label`显示异常的问题

View File

@@ -851,7 +851,7 @@ namespace XCharts.Runtime
} }
} }
public float GetSerieBarGap<T>() where T : Serie public float GetSerieBarGap<T>(int gridIndex) where T : Serie
{ {
float gap = 0f; float gap = 0f;
for (int i = 0; i < m_Series.Count; i++) for (int i = 0; i < m_Series.Count; i++)
@@ -859,6 +859,7 @@ namespace XCharts.Runtime
var serie = m_Series[i]; var serie = m_Series[i];
if (serie.show && serie is T) if (serie.show && serie is T)
{ {
if (!CheckSerieGridIndex(serie, gridIndex)) continue;
if (serie.barGap != 0) if (serie.barGap != 0)
{ {
gap = serie.barGap; gap = serie.barGap;
@@ -868,7 +869,7 @@ namespace XCharts.Runtime
return gap; return gap;
} }
public double GetSerieSameStackTotalValue<T>(string stack, int dataIndex) where T : Serie public double GetSerieSameStackTotalValue<T>(string stack, int dataIndex, int gridIndex) where T : Serie
{ {
if (string.IsNullOrEmpty(stack)) return 0; if (string.IsNullOrEmpty(stack)) return 0;
double total = 0; double total = 0;
@@ -876,6 +877,7 @@ namespace XCharts.Runtime
{ {
if (serie is T) if (serie is T)
{ {
if (!CheckSerieGridIndex(serie, gridIndex)) continue;
if (stack.Equals(serie.stack)) if (stack.Equals(serie.stack))
{ {
total += serie.data[dataIndex].data[1]; total += serie.data[dataIndex].data[1];
@@ -885,7 +887,7 @@ namespace XCharts.Runtime
return total; return total;
} }
public int GetSerieBarRealCount<T>() where T : Serie public int GetSerieBarRealCount<T>(int gridIndex) where T : Serie
{ {
var count = 0; var count = 0;
barStackSet.Clear(); barStackSet.Clear();
@@ -895,6 +897,7 @@ namespace XCharts.Runtime
if (!serie.show) continue; if (!serie.show) continue;
if (serie is T) if (serie is T)
{ {
if (!CheckSerieGridIndex(serie, gridIndex)) continue;
if (!string.IsNullOrEmpty(serie.stack)) if (!string.IsNullOrEmpty(serie.stack))
{ {
if (barStackSet.Contains(serie.stack)) continue; if (barStackSet.Contains(serie.stack)) continue;
@@ -907,8 +910,26 @@ namespace XCharts.Runtime
return count; return count;
} }
private bool CheckSerieGridIndex(Serie serie, int gridIndex)
{
if (gridIndex >= 0)
{
if (serie.xAxisIndex >= 0 && serie.xAxisIndex < m_XAxes.Count)
{
var xAxis = m_XAxes[serie.xAxisIndex];
if (xAxis.gridIndex != gridIndex) return false;
}
if (serie.yAxisIndex >= 0 && serie.yAxisIndex < m_YAxes.Count)
{
var yAxis = m_YAxes[serie.yAxisIndex];
if (yAxis.gridIndex != gridIndex) return false;
}
}
return true;
}
private HashSet<string> barStackSet = new HashSet<string>(); private HashSet<string> barStackSet = new HashSet<string>();
public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount) where T : Serie public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount, int gridIndex) where T : Serie
{ {
float total = 0; float total = 0;
float lastGap = 0; float lastGap = 0;
@@ -919,6 +940,7 @@ namespace XCharts.Runtime
if (!serie.show) continue; if (!serie.show) continue;
if (serie is T) if (serie is T)
{ {
if (!CheckSerieGridIndex(serie, gridIndex)) continue;
if (!string.IsNullOrEmpty(serie.stack)) if (!string.IsNullOrEmpty(serie.stack))
{ {
if (barStackSet.Contains(serie.stack)) continue; if (barStackSet.Contains(serie.stack)) continue;
@@ -941,12 +963,12 @@ namespace XCharts.Runtime
return total; return total;
} }
public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index) where T : Serie public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index, int gridIndex) where T : Serie
{ {
if (index <= 0) return 0; if (index <= 0) return 0;
var total = 0f; var total = 0f;
var count = 0; var count = 0;
var totalRealBarCount = GetSerieBarRealCount<T>(); var totalRealBarCount = GetSerieBarRealCount<T>(gridIndex);
barStackSet.Clear(); barStackSet.Clear();
for (int i = 0; i < m_Series.Count; i++) for (int i = 0; i < m_Series.Count; i++)
{ {
@@ -954,6 +976,7 @@ namespace XCharts.Runtime
if (!serie.show) continue; if (!serie.show) continue;
if (serie is T) if (serie is T)
{ {
if (!CheckSerieGridIndex(serie, gridIndex)) continue;
if (!string.IsNullOrEmpty(serie.stack)) if (!string.IsNullOrEmpty(serie.stack))
{ {
if (barStackSet.Contains(serie.stack)) continue; if (barStackSet.Contains(serie.stack)) continue;
@@ -1003,7 +1026,7 @@ namespace XCharts.Runtime
} }
private List<string> tempList = new List<string>(); private List<string> tempList = new List<string>();
public int GetSerieIndexIfStack<T>(Serie currSerie) where T : Serie public int GetSerieIndexIfStack<T>(Serie currSerie, int gridIndex) where T : Serie
{ {
tempList.Clear(); tempList.Clear();
int index = 0; int index = 0;
@@ -1012,6 +1035,7 @@ namespace XCharts.Runtime
var serie = m_Series[i]; var serie = m_Series[i];
if (!serie.show) continue; if (!serie.show) continue;
if (!(serie is T)) continue; if (!(serie is T)) continue;
if (!CheckSerieGridIndex(serie, gridIndex)) 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;

View File

@@ -125,16 +125,17 @@ namespace XCharts.Runtime
if (isStack) if (isStack)
SeriesHelper.UpdateStackDataList(chart.series, serie, null, m_StackSerieData); SeriesHelper.UpdateStackDataList(chart.series, serie, null, m_StackSerieData);
var barCount = chart.GetSerieBarRealCount<Bar>(); var barCount = chart.GetSerieBarRealCount<Bar>(-1);
var categoryWidth = m_AngleAxis.IsCategory() ? var categoryWidth = m_AngleAxis.IsCategory() ?
AxisHelper.GetDataWidth(m_AngleAxis, 360, datas.Count, null) : AxisHelper.GetDataWidth(m_AngleAxis, 360, datas.Count, null) :
AxisHelper.GetDataWidth(m_RadiusAxis, m_SeriePolar.context.radius, datas.Count, null); AxisHelper.GetDataWidth(m_RadiusAxis, m_SeriePolar.context.radius, datas.Count, null);
var barGap = chart.GetSerieBarGap<Bar>(); var barGap = chart.GetSerieBarGap<Bar>(-1);
var totalBarWidth = chart.GetSerieTotalWidth<Bar>(categoryWidth, barGap, barCount); var totalBarWidth = chart.GetSerieTotalWidth<Bar>(categoryWidth, barGap, barCount, -1);
var barWidth = serie.GetBarWidth(categoryWidth, barCount); var barWidth = serie.GetBarWidth(categoryWidth, barCount);
var offset = (categoryWidth - totalBarWidth) * 0.5f; var offset = (categoryWidth - totalBarWidth) * 0.5f;
var serieReadIndex = chart.GetSerieIndexIfStack<Bar>(serie); var serieReadIndex = chart.GetSerieIndexIfStack<Bar>(serie, -1);
float gap = serie.barGap == -1 ? offset : offset + chart.GetSerieTotalGap<Bar>(categoryWidth, barGap, serieReadIndex); float gap = serie.barGap == -1 ? offset :
offset + chart.GetSerieTotalGap<Bar>(categoryWidth, barGap, serieReadIndex, -1);
var areaColor = ColorUtil.clearColor32; var areaColor = ColorUtil.clearColor32;
var areaToColor = ColorUtil.clearColor32; var areaToColor = ColorUtil.clearColor32;

View File

@@ -180,15 +180,16 @@ namespace XCharts.Runtime
if (isStack) if (isStack)
SeriesHelper.UpdateStackDataList(chart.series, serie, dataZoom, m_StackSerieData); SeriesHelper.UpdateStackDataList(chart.series, serie, dataZoom, m_StackSerieData);
var barCount = chart.GetSerieBarRealCount<Bar>(); var barCount = chart.GetSerieBarRealCount<Bar>(m_SerieGrid.index);
float categoryWidth = AxisHelper.GetDataWidth(axis, axisLength, showData.Count, dataZoom); float categoryWidth = AxisHelper.GetDataWidth(axis, axisLength, showData.Count, dataZoom);
float relativedCategoryWidth = AxisHelper.GetDataWidth(relativedAxis, relativedAxisLength, showData.Count, dataZoom); float relativedCategoryWidth = AxisHelper.GetDataWidth(relativedAxis, relativedAxisLength, showData.Count, dataZoom);
float barGap = chart.GetSerieBarGap<Bar>(); float barGap = chart.GetSerieBarGap<Bar>(m_SerieGrid.index);
float totalBarWidth = chart.GetSerieTotalWidth<Bar>(categoryWidth, barGap, barCount); float totalBarWidth = chart.GetSerieTotalWidth<Bar>(categoryWidth, barGap, barCount, m_SerieGrid.index);
float barWidth = serie.GetBarWidth(categoryWidth, barCount); float barWidth = serie.GetBarWidth(categoryWidth, barCount);
float offset = (categoryWidth - totalBarWidth) * 0.5f; float offset = (categoryWidth - totalBarWidth) * 0.5f;
var serieReadIndex = chart.GetSerieIndexIfStack<Bar>(serie); var serieReadIndex = chart.GetSerieIndexIfStack<Bar>(serie, m_SerieGrid.index);
float gap = serie.barGap == -1 ? offset : offset + chart.GetSerieTotalGap<Bar>(categoryWidth, barGap, serieReadIndex); float gap = serie.barGap == -1 ? offset :
offset + chart.GetSerieTotalGap<Bar>(categoryWidth, barGap, serieReadIndex, m_SerieGrid.index);
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;
@@ -240,7 +241,7 @@ namespace XCharts.Runtime
var barHig = 0f; var barHig = 0f;
if (isPercentStack) if (isPercentStack)
{ {
var valueTotal = chart.GetSerieSameStackTotalValue<Bar>(serie.stack, i); var valueTotal = chart.GetSerieSameStackTotalValue<Bar>(serie.stack, i, m_SerieGrid.index);
barHig = valueTotal != 0 ? (float)(relativedValue / valueTotal * relativedAxisLength) : 0; barHig = valueTotal != 0 ? (float)(relativedValue / valueTotal * relativedAxisLength) : 0;
} }
else else

View File

@@ -122,11 +122,11 @@ namespace XCharts.Runtime
var relativedAxisLength = isY ? m_SerieGrid.context.width : m_SerieGrid.context.height; var relativedAxisLength = isY ? m_SerieGrid.context.width : m_SerieGrid.context.height;
var axisXY = isY ? m_SerieGrid.context.y : m_SerieGrid.context.x; var axisXY = isY ? m_SerieGrid.context.y : m_SerieGrid.context.x;
var barCount = chart.GetSerieBarRealCount<SimplifiedBar>(); var barCount = chart.GetSerieBarRealCount<SimplifiedBar>(m_SerieGrid.index);
float categoryWidth = AxisHelper.GetDataWidth(axis, axisLength, showData.Count, dataZoom); float categoryWidth = AxisHelper.GetDataWidth(axis, axisLength, showData.Count, dataZoom);
float relativedCategoryWidth = AxisHelper.GetDataWidth(relativedAxis, relativedAxisLength, showData.Count, dataZoom); float relativedCategoryWidth = AxisHelper.GetDataWidth(relativedAxis, relativedAxisLength, showData.Count, dataZoom);
float barGap = chart.GetSerieBarGap<SimplifiedBar>(); float barGap = chart.GetSerieBarGap<SimplifiedBar>(m_SerieGrid.index);
float totalBarWidth = chart.GetSerieTotalWidth<SimplifiedBar>(categoryWidth, barGap, barCount); float totalBarWidth = chart.GetSerieTotalWidth<SimplifiedBar>(categoryWidth, barGap, barCount,m_SerieGrid.index);
float barWidth = serie.GetBarWidth(categoryWidth, barCount); float barWidth = serie.GetBarWidth(categoryWidth, barCount);
float offset = (categoryWidth - totalBarWidth) * 0.5f; float offset = (categoryWidth - totalBarWidth) * 0.5f;
float barGapWidth = barWidth + barWidth * barGap; float barGapWidth = barWidth + barWidth * barGap;