Skip to content

Commit 406cc57

Browse files
authored
Merge pull request #5192 from Krishna-Chidrawar/fix/datalabels-out-of-bound
ApexCharts : Fix for data labels on first/last bars hidden in mixed charts due to missing barPad offset.
2 parents 3a583f4 + 93bfc9e commit 406cc57

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/modules/DataLabels.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,15 @@ class DataLabels {
272272

273273
if (correctedLabels.textRects) {
274274
// fixes #2264
275+
const barPad = w.globals.barPadForNumericAxis || 0
275276
if (
276-
x < -20 - /** @type {any} */ (correctedLabels.textRects).width ||
277+
x <
278+
-(barPad + 20) -
279+
/** @type {any} */ (correctedLabels.textRects).width ||
277280
x >
278281
w.layout.gridWidth +
279282
/** @type {any} */ (correctedLabels.textRects).width +
283+
barPad +
280284
30
281285
) {
282286
// datalabels fall outside drawing area, so draw a blank label

tests/unit/bar-chart.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,46 @@ describe('Bar chart', () => {
606606
const datalabels = el.querySelectorAll('.apexcharts-datalabels')
607607
expect(datalabels.length).toBeGreaterThan(0)
608608
})
609+
610+
it('should not blank first/last bar data labels in mixed charts (bar + line)', () => {
611+
const chart = createChartWithOptions({
612+
chart: { type: 'line', height: 380 },
613+
series: [
614+
{ name: 'Incidents', type: 'bar', data: [1, 9, 2, 1, 0, 1] },
615+
{ name: 'Invites', type: 'bar', data: [2, 16, 2, 1, 3, 1] },
616+
{ name: 'Accepted', type: 'bar', data: [2, 8, 2, 1, 1, 1] },
617+
{ name: '% Accepted', type: 'line', data: [100, 50, 100, 100, 0, 100] },
618+
],
619+
dataLabels: { enabled: true },
620+
stroke: { width: [0, 0, 0, 3] },
621+
xaxis: {
622+
categories: ['Mar-2021', 'Aug-2021', 'Sep-2021', 'Nov-2021', 'Jan-2022', 'Jan-2023'],
623+
},
624+
yaxis: [
625+
{ seriesName: 'Incidents', min: 0, max: 20 },
626+
{ seriesName: 'Invites', show: false, min: 0, max: 20 },
627+
{ seriesName: 'Accepted', show: false, min: 0, max: 20 },
628+
{ seriesName: '% Accepted', opposite: true, min: 0, max: 120 },
629+
],
630+
plotOptions: { bar: { columnWidth: '70%' } },
631+
})
632+
633+
const el = chart.el
634+
const datalabelTexts = el.querySelectorAll(
635+
'.apexcharts-datalabels .apexcharts-datalabel'
636+
)
637+
const labels = Array.from(datalabelTexts).map((t) =>
638+
t.textContent.trim()
639+
)
640+
641+
const barLabels = labels.filter((l) => l !== '')
642+
const firstCategoryLabels = labels.slice(0, 4).filter((l) => l !== '')
643+
const lastCategoryLabels = labels.slice(-4).filter((l) => l !== '')
644+
645+
expect(barLabels.length).toBeGreaterThan(0)
646+
expect(firstCategoryLabels.length).toBeGreaterThan(0)
647+
expect(lastCategoryLabels.length).toBeGreaterThan(0)
648+
})
609649
})
610650

611651
// =========================================================================

0 commit comments

Comments
 (0)