Skip to content

Commit 1cd934d

Browse files
authored
fix: shift bars independently from the specs order (#302)
Each bar series needs to be shifted depending on how many bars are clustered. We don't have to shift a bar if we previously have computed a set of lines or area series.
1 parent 0d10751 commit 1cd934d

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/chart_types/xy_chart/store/utils.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,64 @@ describe('Chart State utils', () => {
942942
expect(geometries.geometriesCounts.lines).toBe(0);
943943
expect(geometries.geometriesCounts.areas).toBe(0);
944944
});
945+
test('can compute the bar offset in mixed charts', () => {
946+
const line1: LineSeriesSpec = {
947+
id: getSpecId('line1'),
948+
groupId: getGroupId('group2'),
949+
seriesType: 'line',
950+
yScaleType: ScaleType.Log,
951+
xScaleType: ScaleType.Linear,
952+
xAccessor: 'x',
953+
yAccessors: ['y'],
954+
splitSeriesAccessors: ['g'],
955+
yScaleToDataExtent: false,
956+
data: BARCHART_1Y1G,
957+
};
958+
959+
const bar1: BarSeriesSpec = {
960+
id: getSpecId('line3'),
961+
groupId: getGroupId('group2'),
962+
seriesType: 'bar',
963+
yScaleType: ScaleType.Log,
964+
xScaleType: ScaleType.Linear,
965+
xAccessor: 'x',
966+
yAccessors: ['y'],
967+
splitSeriesAccessors: ['g'],
968+
yScaleToDataExtent: false,
969+
data: BARCHART_1Y1G,
970+
};
971+
const seriesSpecs = new Map<SpecId, BasicSeriesSpec>([[line1.id, line1], [bar1.id, bar1]]);
972+
const axesSpecs = new Map<AxisId, AxisSpec>();
973+
const chartRotation = 0;
974+
const chartDimensions = { width: 100, height: 100, top: 0, left: 0 };
975+
const chartColors = {
976+
vizColors: ['violet', 'green', 'blue'],
977+
defaultVizColor: 'red',
978+
};
979+
const chartTheme = {
980+
...LIGHT_THEME,
981+
scales: {
982+
barsPadding: 0,
983+
histogramPadding: 0,
984+
},
985+
};
986+
const domainsByGroupId = mergeYCustomDomainsByGroupId(axesSpecs, chartRotation);
987+
const seriesDomains = computeSeriesDomains(seriesSpecs, domainsByGroupId);
988+
const seriesColorMap = getSeriesColorMap(seriesDomains.seriesColors, chartColors, new Map());
989+
const geometries = computeSeriesGeometries(
990+
seriesSpecs,
991+
seriesDomains.xDomain,
992+
seriesDomains.yDomain,
993+
seriesDomains.formattedDataSeries,
994+
seriesColorMap,
995+
chartTheme,
996+
chartDimensions,
997+
chartRotation,
998+
axesSpecs,
999+
false,
1000+
);
1001+
expect(geometries.geometries.bars[0].x).toBe(0);
1002+
});
9451003
});
9461004
test('can merge geometry indexes', () => {
9471005
const map1 = new Map<string, IndexedGeometry[]>();

src/chart_types/xy_chart/store/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ export function renderGeometries(
416416
lines: 0,
417417
linePoints: 0,
418418
};
419+
let barIndexOffset = 0;
419420
for (i = 0; i < len; i++) {
420421
const ds = dataSeries[i];
421422
const spec = getSpecById(seriesSpecs, ds.specId);
@@ -425,8 +426,7 @@ export function renderGeometries(
425426
const color = seriesColorsMap.get(ds.seriesColorKey) || defaultColor;
426427

427428
if (isBarSeriesSpec(spec)) {
428-
const shift = isStacked ? indexOffset : indexOffset + i;
429-
429+
const shift = isStacked ? indexOffset : indexOffset + barIndexOffset;
430430
const barSeriesStyle = mergePartial(chartTheme.barSeriesStyle, spec.barSeriesStyle, {
431431
mergeOptionalPartialValues: true,
432432
});
@@ -456,6 +456,7 @@ export function renderGeometries(
456456
barGeometriesIndex = mergeGeometriesIndexes(barGeometriesIndex, renderedBars.indexedGeometries);
457457
bars.push(...renderedBars.barGeometries);
458458
geometriesCounts.bars += renderedBars.barGeometries.length;
459+
barIndexOffset += 1;
459460
} else if (isLineSeriesSpec(spec)) {
460461
const lineShift = clusteredCount > 0 ? clusteredCount : 1;
461462
const lineSeriesStyle = spec.lineSeriesStyle

0 commit comments

Comments
 (0)