Skip to content

Commit 1155c07

Browse files
committed
prevent cropped last xaxis label - fixes #305; fix wrong formatter function being applied for tooltips
1 parent 336e8ac commit 1155c07

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

src/modules/Dimensions.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ export default class Dimensions {
322322
// we have to make it false again in case of zooming/panning
323323
w.globals.skipLastTimelinelabel = false
324324
}
325+
} else if (w.config.xaxis.type === 'datetime') {
326+
if (w.config.grid.padding.right < labels.width) {
327+
w.globals.skipLastTimelinelabel = true
328+
}
325329
} else if (w.config.xaxis.type !== 'datetime') {
326330
if (w.config.grid.padding.right < labels.width) {
327331
this.xPadRight = labels.width / 2 + 1
@@ -532,7 +536,8 @@ export default class Dimensions {
532536
}
533537

534538
let xFormat = new Formatters(this.ctx)
535-
val = xFormat.xLabelFormat(xlbFormatter, val)
539+
let timestamp = val
540+
val = xFormat.xLabelFormat(xlbFormatter, val, timestamp)
536541

537542
let graphics = new Graphics(this.ctx)
538543

src/modules/Formatters.js

+31-27
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ class Formatters {
1414
this.tooltipKeyFormat = 'dd MMM'
1515
}
1616

17-
xLabelFormat(fn, val) {
17+
xLabelFormat(fn, val, timestamp) {
1818
let w = this.w
1919

2020
if (w.config.xaxis.type === 'datetime') {
21-
// if user has not specified a custom formatter, use the default tooltip.x.format
22-
if (w.config.tooltip.x.formatter === undefined) {
23-
let datetimeObj = new DateTime(this.ctx)
24-
return datetimeObj.formatDate(
25-
new Date(val),
26-
w.config.tooltip.x.format,
27-
true,
28-
true
29-
)
21+
if (w.config.xaxis.labels.formatter === undefined) {
22+
// if user has not specified a custom formatter, use the default tooltip.x.format
23+
if (w.config.tooltip.x.formatter === undefined) {
24+
let datetimeObj = new DateTime(this.ctx)
25+
return datetimeObj.formatDate(
26+
new Date(val),
27+
w.config.tooltip.x.format,
28+
true,
29+
true
30+
)
31+
}
3032
}
3133
}
3234

33-
return fn(val)
35+
return fn(val, timestamp)
3436
}
3537

3638
setLabelFormatters() {
@@ -56,8 +58,26 @@ class Formatters {
5658
return val
5759
}
5860

61+
// formatter function will always overwrite format property
62+
if (w.config.xaxis.labels.formatter !== undefined) {
63+
w.globals.xLabelFormatter = w.config.xaxis.labels.formatter
64+
} else {
65+
w.globals.xLabelFormatter = function(val) {
66+
if (Utils.isNumber(val)) {
67+
// numeric xaxis may have smaller range, so defaulting to 1 decimal
68+
if (w.config.xaxis.type === 'numeric' && w.globals.dataPoints < 50) {
69+
return val.toFixed(1)
70+
}
71+
return val.toFixed(0)
72+
}
73+
return val
74+
}
75+
}
76+
5977
if (typeof w.config.tooltip.x.formatter === 'function') {
6078
w.globals.ttKeyFormatter = w.config.tooltip.x.formatter
79+
} else {
80+
w.globals.ttKeyFormatter = w.globals.xLabelFormatter
6181
}
6282

6383
if (typeof w.config.xaxis.tooltip.formatter === 'function') {
@@ -81,22 +101,6 @@ class Formatters {
81101
w.globals.legendFormatter = w.config.legend.formatter
82102
}
83103

84-
// formatter function will always overwrite format property
85-
if (w.config.xaxis.labels.formatter !== undefined) {
86-
w.globals.xLabelFormatter = w.config.xaxis.labels.formatter
87-
} else {
88-
w.globals.xLabelFormatter = function(val) {
89-
if (Utils.isNumber(val)) {
90-
// numeric xaxis may have smaller range, so defaulting to 1 decimal
91-
if (w.config.xaxis.type === 'numeric' && w.globals.dataPoints < 50) {
92-
return val.toFixed(1)
93-
}
94-
return val.toFixed(0)
95-
}
96-
return val
97-
}
98-
}
99-
100104
// formatter function will always overwrite format property
101105
w.config.yaxis.forEach((yaxe, i) => {
102106
if (yaxe.labels.formatter !== undefined) {

src/modules/axes/AxesUtils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default class AxesUtils {
1717
let customFormatter = w.config.xaxis.labels.formatter
1818

1919
let xFormat = new Formatters(this.ctx)
20-
label = xFormat.xLabelFormat(xlbFormatter, rawLabel)
20+
let timestamp = rawLabel
21+
label = xFormat.xLabelFormat(xlbFormatter, rawLabel, timestamp)
2122

2223
if (customFormatter !== undefined) {
2324
label = customFormatter(rawLabel, labels[i], i)

src/modules/tooltip/Labels.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ export default class Labels {
320320

321321
if (w.globals.isXNumeric && w.config.xaxis.type === 'datetime') {
322322
let xFormat = new Formatters(this.ctx)
323-
xVal = xFormat.xLabelFormat(w.globals.ttKeyFormatter, bufferXVal)
323+
xVal = xFormat.xLabelFormat(
324+
w.globals.ttKeyFormatter,
325+
bufferXVal,
326+
bufferXVal
327+
)
324328
} else {
325329
xVal = w.globals.xLabelFormatter(bufferXVal, customFormatterOpts)
326330
}

0 commit comments

Comments
 (0)