Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give the boot to chartjs-adapter-moment #7155

Merged
merged 6 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/axes/cartesian/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 5 additions & 23 deletions docs/getting-started/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, {...});
});
});
});
```
2 changes: 1 addition & 1 deletion docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
kurkle marked this conversation as resolved.
Show resolved Hide resolved
].concat((args.inputs || 'test/specs/**/*.js').split(';')),

preprocessors: {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 0 additions & 31 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -28,9 +27,6 @@ module.exports = [
json(),
resolve(),
babel(),
optional({
include: ['moment']
}),
cleanup({
sourcemap: true
})
Expand All @@ -41,23 +37,14 @@ module.exports = [
banner,
format: 'umd',
indent: false,
globals: {
moment: 'moment'
}
},
external: [
'moment'
]
},
{
input,
plugins: [
json(),
resolve(),
babel(),
optional({
include: ['moment']
}),
terser({
output: {
preamble: banner
Expand All @@ -69,13 +56,7 @@ module.exports = [
file: 'dist/Chart.min.js',
format: 'umd',
indent: false,
globals: {
moment: 'moment'
}
},
external: [
'moment'
]
},

// ES6 builds
Expand All @@ -97,13 +78,7 @@ module.exports = [
banner,
format: 'esm',
indent: false,
globals: {
moment: 'moment'
}
},
external: [
'moment'
]
},
{
input,
Expand All @@ -122,12 +97,6 @@ module.exports = [
file: 'dist/Chart.esm.min.js',
format: 'esm',
indent: false,
globals: {
moment: 'moment'
}
},
external: [
'moment'
]
},
];
60 changes: 0 additions & 60 deletions rollup.plugins.js

This file was deleted.

1 change: 1 addition & 0 deletions samples/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ globals:
Chart: true
Samples: true
moment: true
luxon: true
randomScalingFactor: true

rules:
Expand Down
35 changes: 19 additions & 16 deletions samples/scales/financial.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -39,14 +40,14 @@
<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.
Expand All @@ -59,17 +60,17 @@
}

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))) {
Expand All @@ -86,22 +87,24 @@
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));
Expand Down Expand Up @@ -164,7 +167,7 @@
// 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;
}
Expand Down
7 changes: 5 additions & 2 deletions samples/scales/time/combo.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,7 +28,10 @@
<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 = [];
Expand Down
3 changes: 2 additions & 1 deletion samples/scales/time/line-max-span.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion samples/scales/time/line-point-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion samples/scales/time/line.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading