Skip to content

Commit 03cf4b8

Browse files
committed
2 parents a48b10b + e1a1c2f commit 03cf4b8

3 files changed

Lines changed: 51 additions & 7 deletions

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
// =========================================================================

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,9 +3666,9 @@ flat-cache@^4.0.0:
36663666
keyv "^4.5.4"
36673667

36683668
flatted@^3.2.9:
3669-
version "3.3.3"
3670-
resolved "https://registry.npmmirror.com/flatted/-/flatted-3.3.3.tgz"
3671-
integrity sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==
3669+
version "3.4.2"
3670+
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.4.2.tgz#f5c23c107f0f37de8dbdf24f13722b3b98d52726"
3671+
integrity sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA==
36723672

36733673
follow-redirects@^1.15.11:
36743674
version "1.15.11"
@@ -5057,9 +5057,9 @@ lodash.merge@^4.6.2:
50575057
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
50585058

50595059
lodash@^4.17.21:
5060-
version "4.17.23"
5061-
resolved "https://registry.npmmirror.com/lodash/-/lodash-4.17.23.tgz"
5062-
integrity sha512-LgVTMpQtIopCi79SJeDiP0TfWi5CNEc/L/aRdTh3yIvmZXTnheWpKjSZhnvMl8iXbC1tFg9gdHHDMLoV7CnG+w==
5060+
version "4.18.1"
5061+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.18.1.tgz#ff2b66c1f6326d59513de2407bf881439812771c"
5062+
integrity sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q==
50635063

50645064
loglevel@^1.9.2:
50655065
version "1.9.2"

0 commit comments

Comments
 (0)