]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Give the boot to chartjs-adapter-moment (#7155)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 28 Feb 2020 00:40:31 +0000 (02:40 +0200)
committerGitHub <noreply@github.com>
Fri, 28 Feb 2020 00:40:31 +0000 (19:40 -0500)
Remove default of using chartjs-adapter-moment

18 files changed:
docs/axes/cartesian/time.md
docs/getting-started/integration.md
docs/getting-started/v3-migration.md
karma.conf.js
package-lock.json
package.json
rollup.config.js
rollup.plugins.js [deleted file]
samples/.eslintrc.yml
samples/scales/financial.html
samples/scales/time/combo.html
samples/scales/time/line-max-span.html
samples/scales/time/line-point-data.html
samples/scales/time/line.html
src/adapters/adapter.moment.js [deleted file]
src/adapters/index.js [deleted file]
src/index.js
src/scales/scale.time.js

index c2d90abad682e99c1dd98d3e7825586f5533d671..939d6dd434e68825b3dc98542731b319d094b2ee 100644 (file)
@@ -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
 
index 049e23a56b2ef2a7ce9f279f1a7c021acf904fc4..257607fa9860348dfe6d8ada246162019835c8e4 100644 (file)
@@ -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, {...});
+        });
     });
 });
 ```
index 982ae7323bff1bb22fd64f1fa410145f03d0f4d4..1df1a319271acf461c363fc5d77bb262e1dc3009 100644 (file)
@@ -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
 
index 2f115617e559dc36a9b564cc2f7554d0c981b2e5..23777ccffdc71379b754cfc4178c520a4167b79d 100644 (file)
@@ -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: {
index 29cb3ce9c93ec98f29323b326243835f86448913..68c822db885facf940cd5d9066460ec949b0270c 100644 (file)
       "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",
index 1953bcf09cd6c0a088034a6d8b51ea880c9a05d3..0f4c95ed682eedf7fd8712bee914b0a84e471f6c 100644 (file)
@@ -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",
index 877a3b7f1af4829a8b4193e4c5803882afe869b0..607221dc277575997f40ec9860ce0403af8aecab 100644 (file)
@@ -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 (file)
index 0a8598d..0000000
+++ /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
-};
index 5a16375624fd7e6c009c76afd83f1e56705845b3..bdf8eab325c1b54bcc848b09c1043368cff2a2ae 100644 (file)
@@ -3,6 +3,7 @@ globals:
   Chart: true
   Samples: true
   moment: true
+  luxon: true
   randomScalingFactor: true
 
 rules:
index 32171add098cb59639ac290dd9ea716138c2f06a..c92ac336880707e838ce5bae3abd17eff5050dd3 100644 (file)
@@ -3,8 +3,9 @@
 
 <head>
        <title>Line Chart</title>
-       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/moment.min.js"></script>
        <script src="../../dist/Chart.min.js"></script>
+       <script src="https://cdn.jsdelivr.net/npm/luxon@1.15.0"></script>
+       <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@0.2.0"></script>
        <script src="../utils.js"></script>
        <style>
                canvas {
        <button id="update">update</button>
        <script>
                function isFirstUnitOfPeriod(date, unit, period) {
-                       let first = date.clone().startOf(period);
-                       while (first.isoWeekday() > 5) {
-                               first.add(1, 'days');
+                       let first = date.startOf(period);
+                       while (first.weekday > 5) {
+                               first = first.plus({day: 1});
                        }
                        if (unit === 'second' || unit === 'minute' || unit === 'hour') {
-                               first = first.hours(9).minutes(30);
+                               first = first.set({hour: 9, minute: 30});
                        }
-                       return date.isSame(first);
+                       return date === first;
                }
 
                // Generate data between the stock market hours of 9:30am - 5pm.
                        }
 
                        function beforeNineThirty(date) {
-                               return date.hour() < 9 || (date.hour() === 9 && date.minute() < 30);
+                               return date.hour < 9 || (date.hour === 9 && date.minute < 30);
                        }
 
 
                        function after4pm(date) {
-                               return date.hour() > 16 || (date.hour() === 16 && date.minute() > 0);
+                               return date.hour > 16 || (date.hour === 16 && date.minute > 0);
                        }
 
                        // Returns true if outside 9:30am-4pm on a weekday
                        function outsideMarketHours(date) {
-                               if (date.isoWeekday() > 5) {
+                               if (date.weekday > 5) {
                                        return true;
                                }
                                if (unitLessThanDay() && (beforeNineThirty(date) || after4pm(date))) {
                                const open = randomNumber(lastClose * 0.95, lastClose * 1.05).toFixed(2);
                                const close = randomNumber(open * 0.95, open * 1.05).toFixed(2);
                                return {
-                                       t: date.valueOf(),
+                                       x: date.valueOf(),
                                        y: close
                                };
                        }
 
-                       let date = moment('Jan 01 1990', 'MMM DD YYYY');
-                       const now = moment();
+                       let date = luxon.DateTime.fromFormat('Jan 01 1990', 'LLL dd yyyy');
+                       const now = luxon.DateTime.local();
                        const data = [];
                        const lessThanDay = unitLessThanDay();
-                       for (; data.length < 600 && date.isBefore(now); date = date.clone().add(1, unit).startOf(unit)) {
+                       const plus = {};
+                       plus[unit] = 1;
+                       for (; data.length < 600 && date < now; date = date.plus(plus).startOf(unit)) {
                                if (outsideMarketHours(date)) {
                                        if (!lessThanDay || !beforeNineThirty(date)) {
-                                               date = date.clone().add(date.isoWeekday() >= 5 ? 8 - date.isoWeekday() : 1, 'day');
+                                               date = date.plus({day: date.weekday >= 5 ? 8 - date.weekday : 1});
                                        }
                                        if (lessThanDay) {
-                                               date = date.hour(9).minute(30).second(0);
+                                               date = date.set({hour: 9, minute: 30});
                                        }
                                }
                                data.push(randomBar(date, data.length > 0 ? data[data.length - 1].y : 30));
                                                        // major ticks
                                                        const ticks = scale.ticks;
                                                        for (let i = 0; i < ticks.length; i++) {
-                                                               ticks[i].major = !majorUnit || isFirstUnitOfPeriod(moment(ticks[i].value), unit, majorUnit);
+                                                               ticks[i].major = !majorUnit || isFirstUnitOfPeriod(luxon.DateTime.fromMillis(ticks[i].value), unit, majorUnit);
                                                        }
                                                        scale.ticks = ticks;
                                                }
index 2e385e24e4cb94065b7576a0839b6023ebac6615..c1e9d0a1a9e155d460c9acde7d13cf90b55cf7f2 100644 (file)
@@ -3,8 +3,8 @@
 
 <head>
        <title>Line Chart - Combo Time Scale</title>
-       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/moment.min.js"></script>
        <script src="../../../dist/Chart.min.js"></script>
+       <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@1.0.0/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
        <script src="../../utils.js"></script>
        <style>
                canvas {
        <button id="removeData">Remove Data</button>
        <script>
                function newDate(days) {
-                       return moment().startOf('day').add(days, 'd').valueOf();
+                       var date = new Date();
+                       date.setHours(0, 0, 0, 0);
+                       date.setDate(date.getDate() + days);
+                       return date.valueOf();
                }
 
                var labels = [];
index 3658839046aa82c1daaf50d727811eef1e42c6f6..d9c1168999ec4f933219c04add21efccc5b92efe 100644 (file)
@@ -3,8 +3,9 @@
 
 <head>
        <title>Time Scale Point Data</title>
-       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/moment.min.js"></script>
        <script src="../../../dist/Chart.min.js"></script>
+       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0"></script>
+       <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
        <script src="../../utils.js"></script>
        <style>
                canvas {
index 9bcf3af321b3f6e3c16ea18210a2553710b1bf92..1256616f031eba2a109202f4f28cd039e576a1b4 100644 (file)
@@ -3,8 +3,9 @@
 
 <head>
        <title>Time Scale Point Data</title>
-       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/moment.min.js"></script>
        <script src="../../../dist/Chart.min.js"></script>
+       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0"></script>
+       <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
        <script src="../../utils.js"></script>
        <style>
                canvas {
index 1d860f40b887d45a5cb65a75c6987c29ef755026..2605fad0af7bdd7da676b3a6020ea80732d78071 100644 (file)
@@ -3,8 +3,9 @@
 
 <head>
        <title>Line Chart</title>
-       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/moment.min.js"></script>
        <script src="../../../dist/Chart.min.js"></script>
+       <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0"></script>
+       <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
        <script src="../../utils.js"></script>
        <style>
                canvas {
diff --git a/src/adapters/adapter.moment.js b/src/adapters/adapter.moment.js
deleted file mode 100644 (file)
index 5d37ea4..0000000
+++ /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 (file)
index e6c67bc..0000000
+++ /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';
index 74dff96d2ab1dbe2a3874592338021bbcd962180..5be7ae14a11918ad8197da5128c0a25a7cd09ff1 100644 (file)
@@ -45,9 +45,6 @@ Chart.Ticks = Ticks;
 import * as scales from './scales/index';
 Object.keys(scales).forEach(key => Chart.scaleService.registerScale(scales[key]));
 
-// 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) {
index d3eef58b6b5c671cd6e678deaca36edd33e952c9..08297e0c3a8d6d7edb9b43c8e35c6ddd9c914141 100644 (file)
@@ -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: {}
        },