From cbe5ff2d581d46eab603c1734e0494537b193d39 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 25 Feb 2020 14:11:34 +0200 Subject: [PATCH 1/6] Give the boot to chartjs-adapter-moment --- karma.conf.js | 3 +- package-lock.json | 6 ++++ package.json | 1 + src/adapters/adapter.moment.js | 58 ---------------------------------- src/adapters/index.js | 8 ----- src/index.js | 3 -- 6 files changed, 9 insertions(+), 70 deletions(-) delete mode 100644 src/adapters/adapter.moment.js delete mode 100644 src/adapters/index.js diff --git a/karma.conf.js b/karma.conf.js index 2f115617e55..23777ccffdc 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -55,7 +55,8 @@ module.exports = function(karma) { {pattern: 'test/fixtures/**/*.png', included: false}, 'node_modules/moment/min/moment.min.js', 'test/index.js', - 'src/index.js' + 'src/index.js', + 'node_modules/chartjs-adapter-moment/dist/chartjs-adapter-moment.js' ].concat((args.inputs || 'test/specs/**/*.js').split(';')), preprocessors: { diff --git a/package-lock.json b/package-lock.json index 29cb3ce9c93..68c822db885 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3932,6 +3932,12 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "dev": true }, + "chartjs-adapter-moment": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/chartjs-adapter-moment/-/chartjs-adapter-moment-0.1.1.tgz", + "integrity": "sha512-lFGCiqpn+0e6CEbEKdG1L3LYR0jnKFqtXsExzmEWXGCxdymTFSqntiWAqWAYDblL/QB3sUk881xVGzhWZdn4kQ==", + "dev": true + }, "chokidar": { "version": "2.1.8", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.8.tgz", diff --git a/package.json b/package.json index 1953bcf09cd..0f4c95ed682 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@rollup/plugin-node-resolve": "^7.1.1", "babel-plugin-istanbul": "^6.0.0", "babel-preset-es2015-rollup": "^3.0.0", + "chartjs-adapter-moment": "^0.1.1", "coveralls": "^3.0.9", "eslint": "^6.8.0", "eslint-config-chartjs": "^0.2.0", diff --git a/src/adapters/adapter.moment.js b/src/adapters/adapter.moment.js deleted file mode 100644 index 5d37ea4d713..00000000000 --- a/src/adapters/adapter.moment.js +++ /dev/null @@ -1,58 +0,0 @@ -// TODO v3 - make this adapter external (chartjs-adapter-moment) - -import moment from 'moment'; -import adapters from '../core/core.adapters'; - -const FORMATS = { - datetime: 'MMM D, YYYY, h:mm:ss a', - millisecond: 'h:mm:ss.SSS a', - second: 'h:mm:ss a', - minute: 'h:mm a', - hour: 'hA', - day: 'MMM D', - week: 'll', - month: 'MMM YYYY', - quarter: '[Q]Q - YYYY', - year: 'YYYY' -}; - -adapters._date.override(typeof moment === 'function' ? { - _id: 'moment', // DEBUG ONLY - - formats() { - return FORMATS; - }, - - parse(value, format) { - if (typeof value === 'string' && typeof format === 'string') { - value = moment(value, format); - } else if (!(value instanceof moment)) { - value = moment(value); - } - return value.isValid() ? value.valueOf() : null; - }, - - format(time, format) { - return moment(time).format(format); - }, - - add(time, amount, unit) { - return moment(time).add(amount, unit).valueOf(); - }, - - diff(max, min, unit) { - return moment(max).diff(moment(min), unit); - }, - - startOf(time, unit, weekday) { - time = moment(time); - if (unit === 'isoWeek') { - return time.isoWeekday(weekday).valueOf(); - } - return time.startOf(unit).valueOf(); - }, - - endOf(time, unit) { - return moment(time).endOf(unit).valueOf(); - } -} : {}); diff --git a/src/adapters/index.js b/src/adapters/index.js deleted file mode 100644 index e6c67bc7492..00000000000 --- a/src/adapters/index.js +++ /dev/null @@ -1,8 +0,0 @@ -// ----------------------------------------------------------------------------- -// IMPORTANT: do NOT submit new adapters to this repository, instead -// create an external library named `chartjs-adapter-{lib-name}` -// ----------------------------------------------------------------------------- - -// Built-in moment adapter that we need to keep for backward compatibility -// https://github.com/chartjs/Chart.js/issues/5542 -import './adapter.moment'; diff --git a/src/index.js b/src/index.js index d1b19287a79..aed3c260d5e 100644 --- a/src/index.js +++ b/src/index.js @@ -46,9 +46,6 @@ Object.keys(scales).forEach((type) => { Chart.scaleService.registerScaleType(type, scale, scale._defaults); }); -// Load to register built-in adapters (as side effects) -import './adapters/index'; - // Loading built-in plugins import plugins from './plugins/index'; for (const k in plugins) { From 9594f96a275977b860191615e989071f71af0e5a Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 25 Feb 2020 16:19:24 +0200 Subject: [PATCH 2/6] Remove rollup configs for moment --- rollup.config.js | 31 ------------------------ rollup.plugins.js | 60 ----------------------------------------------- 2 files changed, 91 deletions(-) delete mode 100644 rollup.plugins.js diff --git a/rollup.config.js b/rollup.config.js index 877a3b7f1af..607221dc277 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,7 +4,6 @@ const babel = require('rollup-plugin-babel'); const cleanup = require('rollup-plugin-cleanup'); const json = require('@rollup/plugin-json'); -const optional = require('./rollup.plugins').optional; const resolve = require('@rollup/plugin-node-resolve'); const terser = require('rollup-plugin-terser').terser; const pkg = require('./package.json'); @@ -28,9 +27,6 @@ module.exports = [ json(), resolve(), babel(), - optional({ - include: ['moment'] - }), cleanup({ sourcemap: true }) @@ -41,13 +37,7 @@ module.exports = [ banner, format: 'umd', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, { input, @@ -55,9 +45,6 @@ module.exports = [ json(), resolve(), babel(), - optional({ - include: ['moment'] - }), terser({ output: { preamble: banner @@ -69,13 +56,7 @@ module.exports = [ file: 'dist/Chart.min.js', format: 'umd', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, // ES6 builds @@ -97,13 +78,7 @@ module.exports = [ banner, format: 'esm', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, { input, @@ -122,12 +97,6 @@ module.exports = [ file: 'dist/Chart.esm.min.js', format: 'esm', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, ]; diff --git a/rollup.plugins.js b/rollup.plugins.js deleted file mode 100644 index 0a8598d124d..00000000000 --- a/rollup.plugins.js +++ /dev/null @@ -1,60 +0,0 @@ -/* eslint-disable import/no-commonjs */ -const UMD_WRAPPER_RE = /(\(function \(global, factory\) \{)((?:\s.*?)*)(\}\(this,)/; -const CJS_FACTORY_RE = /(module.exports = )(factory\(.*?\))( :)/; -const AMD_FACTORY_RE = /(define\()(.*?, factory)(\) :)/; - -function optional(config = {}) { - return { - name: 'optional', - renderChunk(code, chunk, options) { - if (options.format !== 'umd') { - this.error('only UMD format is currently supported'); - } - - const wrapper = UMD_WRAPPER_RE.exec(code); - const include = config.include; - if (!wrapper) { - this.error('failed to parse the UMD wrapper'); - } - - let content = wrapper[2]; - let factory = (CJS_FACTORY_RE.exec(content) || [])[2]; - let updated = false; - - for (const lib of chunk.imports) { - if (!include || include.indexOf(lib) !== -1) { - const regex = new RegExp(`require\\('${lib}'\\)`); - if (!regex.test(factory)) { - this.error(`failed to parse the CJS require for ${lib}`); - } - - // We need to write inline try / catch with explicit require - // in order to enable statical extraction of dependencies: - // try { return require('moment'); } catch(e) {} - const loader = `function() { try { return require('${lib}'); } catch(e) { } }()`; - factory = factory.replace(regex, loader); - updated = true; - } - } - - if (!updated) { - return; - } - - // Replace the CJS factory by our updated one. - content = content.replace(CJS_FACTORY_RE, `$1${factory}$3`); - - // Replace the AMD factory by our updated one: we need to use the - // following AMD form in order to be able to try/catch require: - // define(['require'], function(require) { ... require(...); ... }) - // https://github.com/amdjs/amdjs-api/wiki/AMD#using-require-and-exports - content = content.replace(AMD_FACTORY_RE, `$1['require'], function(require) { return ${factory}; }$3`); - - return code.replace(UMD_WRAPPER_RE, `$1${content}$3`); - } - }; -} - -module.exports = { - optional -}; From 1b8ba6c4a797720ab6f36bfeb01605d3ce1648ed Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 25 Feb 2020 18:22:14 +0200 Subject: [PATCH 3/6] Docs --- docs/axes/cartesian/time.md | 2 +- docs/getting-started/integration.md | 28 +++++----------------------- docs/getting-started/v3-migration.md | 2 +- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/docs/axes/cartesian/time.md b/docs/axes/cartesian/time.md index c2d90abad68..939d6dd434e 100644 --- a/docs/axes/cartesian/time.md +++ b/docs/axes/cartesian/time.md @@ -4,7 +4,7 @@ The time scale is used to display times and dates. When building its ticks, it w ## Date Adapters -The time scale requires both a date library and corresponding adapter to be present. By default, Chart.js includes an adapter for Moment.js. You may wish to choose from [other available adapters](https://github.com/chartjs/awesome#adapters) instead. +The time scale **requires** both a date library and corresponding adapter to be present. Please choose from the [available adapters](https://github.com/chartjs/awesome#adapters). ## Data Sets diff --git a/docs/getting-started/integration.md b/docs/getting-started/integration.md index 049e23a56b2..257607fa986 100644 --- a/docs/getting-started/integration.md +++ b/docs/getting-started/integration.md @@ -35,32 +35,14 @@ require(['path/to/chartjs/dist/Chart.min.js'], function(Chart){ }); ``` -**Note:** in order to use the time scale, you need to make sure [one of the available date adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library are fully loaded **before** requiring Chart.js. The date adapter for Moment.js is included with Chart.js, but you still need to include Moment.js itself if this is the date adapter you choose to use. You can either use a shim: +**Note:** in order to use the time scale, you need to make sure [one of the available date adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library are fully loaded **after** requiring Chart.js. For this you can use nested requires: ```javascript -require.config({ - shim: { - 'chartjs': { - deps: ['moment'] // enforce moment to be loaded before chartjs - } - }, - paths: { - 'chartjs': 'path/to/chartjs/dist/Chart.min.js', - 'moment': 'path/to/moment' - } -}); - require(['chartjs'], function(Chart) { - new Chart(ctx, {...}); -}); -``` - -or simply use two nested `require()`: - -```javascript -require(['moment'], function() { - require(['chartjs'], function(Chart) { - new Chart(ctx, {...}); + require(['moment'], function() { + require(['chartjs-adapter-moment'], function() { + new Chart(ctx, {...}); + }); }); }); ``` diff --git a/docs/getting-started/v3-migration.md b/docs/getting-started/v3-migration.md index 9f3dc4f40b4..ed09de3923c 100644 --- a/docs/getting-started/v3-migration.md +++ b/docs/getting-started/v3-migration.md @@ -15,7 +15,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released ### Setup and installation * Chart.js is no longer providing the `Chart.bundle.js` and `Chart.bundle.min.js`. Please see the [installation](installation.md) and [integration](integration.md) docs for details on the recommended way to setup Chart.js if you were using these builds. -* `moment` is no longer specified as an npm dependency. If you are using the time scale, you must include one of [the available adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library. If you are using a date library other than moment, you no longer need to exclude moment from your build. +* `moment` is no longer specified as an npm dependency. If you are using the time scale, you must include one of [the available adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library. You no longer need to exclude moment from your build. ### Ticks From 098cebff8761a8e4f34381a9b4428449c04db9e0 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Tue, 25 Feb 2020 18:28:46 +0200 Subject: [PATCH 4/6] Comments --- src/scales/scale.time.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 42d7a1cb8bd..f801d4deec2 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -539,10 +539,10 @@ const defaultConfig = { adapters: {}, time: { - parser: false, // false == a pattern string from https://momentjs.com/docs/#/parsing/string-format/ or a custom callback that converts its argument to a moment + parser: false, // false == a pattern string from or a custom callback that converts its argument to a timestamp unit: false, // false == automatic or override with week, month, year, etc. round: false, // none, or override with week, month, year, etc. - isoWeekday: false, // override week start day - see https://momentjs.com/docs/#/get-set/iso-weekday/ + isoWeekday: false, // override week start day minUnit: 'millisecond', displayFormats: {} }, From f06ef13a64d466487376e383f73f2492b658c7ee Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Thu, 27 Feb 2020 22:05:43 +0200 Subject: [PATCH 5/6] Update samples --- samples/.eslintrc.yml | 1 + samples/scales/financial.html | 35 +++++++++++++----------- samples/scales/time/combo.html | 7 +++-- samples/scales/time/line-max-span.html | 3 +- samples/scales/time/line-point-data.html | 3 +- samples/scales/time/line.html | 3 +- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/samples/.eslintrc.yml b/samples/.eslintrc.yml index 5a16375624f..bdf8eab325c 100644 --- a/samples/.eslintrc.yml +++ b/samples/.eslintrc.yml @@ -3,6 +3,7 @@ globals: Chart: true Samples: true moment: true + luxon: true randomScalingFactor: true rules: diff --git a/samples/scales/financial.html b/samples/scales/financial.html index 32171add098..40671e68193 100644 --- a/samples/scales/financial.html +++ b/samples/scales/financial.html @@ -3,8 +3,9 @@ Line Chart - + +