Skip to content

Commit

Permalink
Upgrade to date-fns 2 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermuc authored and kurkle committed May 10, 2019
1 parent b6d6ddc commit fe5a1f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

This adapter allows the use of date-fns with Chart.js.

Requires [Chart.js](https://github.com/chartjs/Chart.js/releases) **2.8.0** or later and [date-fns](https://date-fns.org/) **1.30.1** or later.
Requires [Chart.js](https://github.com/chartjs/Chart.js/releases) **2.8.0** or later and [date-fns](https://date-fns.org/) **2.0.0-alpha.27** or later.

**Note:** once loaded, this adapter overrides the default date-adapter provided in Chart.js (as a side-effect).

Expand All @@ -15,7 +15,7 @@ Requires [Chart.js](https://github.com/chartjs/Chart.js/releases) **2.8.0** or l
### npm

```bash
npm install date-fns chartjs-adapter-date-fns --save
npm install date-fns@next chartjs-adapter-date-fns@next --save
```

```javascript
Expand All @@ -25,12 +25,12 @@ import 'chartjs-adapter-date-fns';

### CDN

By default, `https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns` returns the latest (minified) version, however it's [highly recommended](https://www.jsdelivr.com/features) to always specify a version in order to avoid breaking changes. This can be achieved by appending `@{version}` to the url:
By default, `https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@next` returns the latest (minified) version, however it's [highly recommended](https://www.jsdelivr.com/features) to always specify a version in order to avoid breaking changes. This can be achieved by appending `@{version}` to the url:

```html
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@0.1.2"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/2.0.0-alpha0/date_fns.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@next"></script>
```

Read more about jsDeliver versioning on their [website](http://www.jsdelivr.com/).
Expand All @@ -42,10 +42,10 @@ Read more about jsDeliver versioning on their [website](http://www.jsdelivr.com/
date-fns requires a date-fns locale object to be tagged on to each `format()` call, which requires the locale to be explicitly set via the `adapters.date` option: [Chart.js documentation on adapters.date](https://www.chartjs.org/docs/latest/axes/cartesian/time.html?h=adapter)

For example:

```javascript
// import date-fns locale:
import de from 'date-fns/locale/de';
import {de} from 'date-fns/locale';


// scale options:
{
Expand All @@ -57,7 +57,7 @@ import de from 'date-fns/locale/de';
}
```

Further, read the [Chart.js documentation](https://www.chartjs.org/docs/latest) for other possible date/time related options. For example, the time scale [`time.*` options](https://www.chartjs.org/docs/latest/axes/cartesian/time.html#configuration-options) can be overridden using the [date-fns tokens](https://date-fns.org/v1.30.1/docs/format).
Further, read the [Chart.js documentation](https://www.chartjs.org/docs/latest) for other possible date/time related options. For example, the time scale [`time.*` options](https://www.chartjs.org/docs/latest/axes/cartesian/time.html#configuration-options) can be overridden using the [date-fns tokens](https://date-fns.org/v2.0.0-alpha.27/docs/format).

## Development

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
},
"peerDependencies": {
"chart.js": ">= 2.8.0 < 3",
"date-fns": "^1.30.1"
"date-fns": "^2.0.0-alpha"
}
}
33 changes: 21 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { _adapters, helpers } from 'chart.js';
import {
parse, format, isValid,
parse, parseISO, toDate, isValid, format,
startOfSecond, startOfMinute, startOfHour, startOfDay,
startOfWeek, startOfMonth, startOfQuarter, startOfYear,
addMilliseconds, addSeconds, addMinutes, addHours,
Expand All @@ -13,16 +13,16 @@ import {
} from 'date-fns';

var FORMATS = {
datetime: 'MMM D, YYYY, h:mm:ss a',
millisecond: 'h:mm:ss.SSS a',
second: 'h:mm:ss a',
minute: 'h:mm a',
datetime: 'MMM d, yyyy, h:mm:ss aaaa',
millisecond: 'h:mm:ss.SSS aaaa',
second: 'h:mm:ss aaaa',
minute: 'h:mm aaaa',
hour: 'ha',
day: 'MMM D',
week: 'DD',
month: 'MMM YYYY',
quarter: '[Q]Q - YYYY',
year: 'YYYY'
day: 'MMM d',
week: 'PP',
month: 'MMM yyyy',
quarter: 'qqq - yyyy',
year: 'yyyy'
};

_adapters._date.override({
Expand All @@ -32,11 +32,20 @@ _adapters._date.override({
return FORMATS;
},

parse: function(value) {
parse: function(value, fmt) {
if (helpers.isNullOrUndef(value)) {
return null;
}
value = parse(value, this.options);
var type = typeof value;
if (type === 'number' || value instanceof Date) {
value = toDate(value);
} else if (type === 'string') {
if (typeof fmt === 'string') {
value = parse(value, fmt, new Date(), this.options);
} else {
value = parseISO(value, this.options);
}
}
return isValid(value) ? value.getTime() : null;
},

Expand Down

0 comments on commit fe5a1f0

Please sign in to comment.