Skip to content

Commit e88f75c

Browse files
committed
prevent drawing cropped label in datetime x-axis - fixes #305
1 parent d1a242b commit e88f75c

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

src/modules/Dimensions.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,26 @@ export default class Dimensions {
306306

307307
if (
308308
(w.config.xaxis.type === 'category' && w.globals.isBarHorizontal) ||
309-
w.config.xaxis.type === 'numeric'
309+
w.config.xaxis.type === 'numeric' ||
310+
w.config.xaxis.type === 'datetime'
310311
) {
311312
const rightPad = (labels) => {
312-
if (w.config.grid.padding.right < labels.width) {
313-
this.xPadRight = labels.width / 2 + 1
313+
if (this.timescaleLabels) {
314+
// for timeline labels, we take the last label and check if it exceeds gridWidth
315+
const lastTimescaleLabel = this.timescaleLabels[
316+
this.timescaleLabels.length - 1
317+
]
318+
const labelPosition = lastTimescaleLabel.position + labels.width
319+
if (labelPosition > w.globals.gridWidth) {
320+
w.globals.skipLastTimelinelabel = true
321+
} else {
322+
// we have to make it false again in case of zooming/panning
323+
w.globals.skipLastTimelinelabel = false
324+
}
325+
} else if (w.config.xaxis.type !== 'datetime') {
326+
if (w.config.grid.padding.right < labels.width) {
327+
this.xPadRight = labels.width / 2 + 1
328+
}
314329
}
315330
}
316331

@@ -320,20 +335,19 @@ export default class Dimensions {
320335
}
321336
}
322337

323-
const lineArea =
324-
w.config.chart.type === 'line' || w.config.chart.type === 'area'
338+
const isXNumeric = w.globals.isXNumeric
325339

326340
w.config.yaxis.forEach((yaxe, i) => {
327341
let shouldPad =
328342
!yaxe.show ||
329343
yaxe.floating ||
330344
w.globals.collapsedSeriesIndices.indexOf(i) !== -1 ||
331-
lineArea ||
345+
isXNumeric ||
332346
(yaxe.opposite && w.globals.isBarHorizontal)
333347

334348
if (shouldPad) {
335349
if (
336-
(lineArea &&
350+
(isXNumeric &&
337351
w.globals.isMultipleYAxis &&
338352
w.globals.collapsedSeriesIndices.indexOf(i) !== -1) ||
339353
(w.globals.isBarHorizontal && yaxe.opposite)
@@ -345,7 +359,7 @@ export default class Dimensions {
345359
(!w.globals.isBarHorizontal &&
346360
yaxe.opposite &&
347361
w.globals.collapsedSeriesIndices.indexOf(i) !== -1) ||
348-
(lineArea && !w.globals.isMultipleYAxis)
362+
(isXNumeric && !w.globals.isMultipleYAxis)
349363
) {
350364
rightPad(xaxisLabelCoords)
351365
}
@@ -438,12 +452,12 @@ export default class Dimensions {
438452
let w = this.w
439453
let rect
440454

441-
let timescaleLabels = w.globals.timelineLabels.slice()
455+
this.timescaleLabels = w.globals.timelineLabels.slice()
442456
if (w.globals.isBarHorizontal && w.config.xaxis.type === 'datetime') {
443-
timescaleLabels = w.globals.invertedTimelineLabels.slice()
457+
this.timescaleLabels = w.globals.invertedTimelineLabels.slice()
444458
}
445459

446-
let labels = timescaleLabels.map((label) => {
460+
let labels = this.timescaleLabels.map((label) => {
447461
return label.value
448462
})
449463

@@ -519,6 +533,7 @@ export default class Dimensions {
519533
val = xFormat.xLabelFormat(xlbFormatter, val)
520534

521535
let graphics = new Graphics(this.ctx)
536+
522537
let xLabelrect = graphics.getTextRects(
523538
val,
524539
w.config.xaxis.labels.style.fontSize

src/modules/ZoomPanSelection.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,10 @@ export default class ZoomPanSelection extends Toolbar {
425425
if (type === 'resizing') {
426426
timerInterval = 30
427427
}
428-
if (typeof w.config.chart.events.selection === 'function') {
428+
if (
429+
typeof w.config.chart.events.selection === 'function' &&
430+
w.globals.selectionEnabled
431+
) {
429432
// a small debouncer is required when resizing to avoid freezing the chart
430433
clearTimeout(this.w.globals.selectionResizeTimer)
431434
this.w.globals.selectionResizeTimer = window.setTimeout(() => {

src/modules/axes/Grid.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,15 @@ class Grid {
220220
}
221221

222222
let xAxis = new XAxis(this.ctx)
223-
xAxis.drawXaxisTicks(x1, elg)
223+
224+
if (i === xCount - 1) {
225+
if (!w.globals.skipLastTimelinelabel) {
226+
// skip drawing last label here
227+
xAxis.drawXaxisTicks(x1, elg)
228+
}
229+
} else {
230+
xAxis.drawXaxisTicks(x1, elg)
231+
}
224232
}
225233
} else {
226234
let xCountForCategoryCharts = xCount

src/modules/axes/XAxis.js

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export default class XAxis {
119119
'apexcharts-xaxis-label ' + w.config.xaxis.labels.style.cssClass
120120
})
121121

122+
if (i === labelsLen - 1) {
123+
if (w.globals.skipLastTimelinelabel) {
124+
label.text = ''
125+
}
126+
}
127+
122128
elXaxisTexts.add(elTick)
123129

124130
graphics.addTspan(elTick, label.text, this.xaxisFontFamily)

src/modules/settings/Globals.js

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export default class Globals {
120120
methodsToExec: []
121121
},
122122
shouldAnimate: true,
123+
skipLastTimelinelabel: false, // when last label is cropped, skip drawing it
123124
delayedElements: [], // element which appear after animation has finished
124125
axisCharts: true, // chart type = line or area or bar
125126
// (refer them also as plot charts in the code)

0 commit comments

Comments
 (0)