Skip to content

Commit f5ee02d

Browse files
committed
services/chartjs: Use date-fns instead of Moment.js
This is based on the implementation in https://github.com/chartjs/chartjs-adapter-date-fns, but stripped down to only support the formats that we need to save a few bytes.
1 parent 99d3791 commit f5ee02d

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

app/components/download-graph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class DownloadGraph extends Component {
3030
padding: 10,
3131
},
3232
scales: {
33-
xAxes: [{ type: 'time', time: { stepSize: 7, tooltipFormat: 'MMM D', unit: 'day' } }],
33+
xAxes: [{ type: 'time', time: { stepSize: 7, tooltipFormat: 'MMM d', unit: 'day' } }],
3434
yAxes: [{ stacked: true, ticks: { min: 0, precision: 0 } }],
3535
},
3636
tooltips: {

app/services/chartjs.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
1+
import { assert } from '@ember/debug';
12
import Service from '@ember/service';
23

4+
import addDays from 'date-fns/addDays';
5+
import differenceInDays from 'date-fns/differenceInDays';
6+
import endOfDay from 'date-fns/endOfDay';
7+
import format from 'date-fns/format';
8+
import startOfDay from 'date-fns/startOfDay';
39
import { task } from 'ember-concurrency';
410

511
export default class ChartJsLoader extends Service {
612
@(task(function* () {
713
let Chart = yield import('chart.js').then(module => module.default);
14+
15+
Chart._adapters._date.override({
16+
_id: 'date-fns', // DEBUG
17+
18+
formats() {
19+
return { day: 'MMM d' };
20+
},
21+
22+
parse(value) {
23+
if (value === null || value === undefined) {
24+
return null;
25+
}
26+
assert('`value` must be a `Date`', value instanceof Date);
27+
return !isNaN(value) ? value.getTime() : null;
28+
},
29+
30+
format(time, fmt) {
31+
return format(time, fmt, this.options);
32+
},
33+
34+
add(time, amount, unit) {
35+
assert('This basic Chart.js adapter only supports `unit: day`', unit === 'day');
36+
return addDays(time, amount);
37+
},
38+
39+
diff(max, min, unit) {
40+
assert('This basic Chart.js adapter only supports `unit: day`', unit === 'day');
41+
return differenceInDays(max, min);
42+
},
43+
44+
startOf(time, unit) {
45+
assert('This basic Chart.js adapter only supports `unit: day`', unit === 'day');
46+
return startOfDay(time);
47+
},
48+
49+
endOf(time, unit) {
50+
assert('This basic Chart.js adapter only supports `unit: day`', unit === 'day');
51+
return endOfDay(time);
52+
},
53+
});
54+
855
Chart.platform.disableCSSInjection = true;
956
return Chart;
1057
}).drop())

ember-cli-build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = function (defaults) {
2828
autoImport: {
2929
webpack: {
3030
externals: {
31+
// prevent Chart.js from bundling Moment.js
3132
moment: 'moment',
3233
},
3334
},

0 commit comments

Comments
 (0)