add Chart.js packages

This commit is contained in:
Ben Goldsworthy 2025-02-20 23:42:38 +01:00
parent dae35f0e05
commit c77c48133b
Signed by: Rumperuu
SSH key fingerprint: SHA256:v3uompaUiPqV2w1/AIRWBSQOxr2dntH9Xs/y8fDnUPU
23 changed files with 13374 additions and 0 deletions

View file

@ -60,6 +60,8 @@
id="Charts-script"
src="/js/chart/chart.js"
></script>
<script src="/js/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<script
id="Charts-config-script"
src="{{ $chartsConfigJS.Permalink }}"

View file

@ -61,6 +61,8 @@
id="Charts-script"
src="/js/chart/chart.js"
></script>
<script src="/js/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
<script
id="Charts-config-script"
src="{{ $chartsConfigJS.Permalink }}"

32
package-lock.json generated
View file

@ -9,6 +9,8 @@
"version": "1.0.0",
"dependencies": {
"chart.js": "^4.4.7",
"chartjs-adapter-date-fns": "^3.0.0",
"chartjs-plugin-annotation": "^3.1.0",
"google-charts": "^2.0.0",
"leaflet": "^1.9.4",
"leaflet.fullscreen": "^3.0.2",
@ -780,6 +782,25 @@
"pnpm": ">=8"
}
},
"node_modules/chartjs-adapter-date-fns": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/chartjs-adapter-date-fns/-/chartjs-adapter-date-fns-3.0.0.tgz",
"integrity": "sha512-Rs3iEB3Q5pJ973J93OBTpnP7qoGwvq3nUnoMdtxO+9aoJof7UFcRbWcIDteXuYd1fgAvct/32T9qaLyLuZVwCg==",
"license": "MIT",
"peerDependencies": {
"chart.js": ">=2.8.0",
"date-fns": ">=2.0.0"
}
},
"node_modules/chartjs-plugin-annotation": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/chartjs-plugin-annotation/-/chartjs-plugin-annotation-3.1.0.tgz",
"integrity": "sha512-EkAed6/ycXD/7n0ShrlT1T2Hm3acnbFhgkIEJLa0X+M6S16x0zwj1Fv4suv/2bwayCT3jGPdAtI9uLcAMToaQQ==",
"license": "MIT",
"peerDependencies": {
"chart.js": ">=4.0.0"
}
},
"node_modules/cli-cursor": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-5.0.0.tgz",
@ -943,6 +964,17 @@
"node": ">=4"
}
},
"node_modules/date-fns": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-4.1.0.tgz",
"integrity": "sha512-Ukq0owbQXxa/U3EGtsdVBkR1w7KOQ5gIBqdH2hkvknzZPYvBxb/aa6E8L7tmjFtkwZBu3UXBbjIgPo/Ez4xaNg==",
"license": "MIT",
"peer": true,
"funding": {
"type": "github",
"url": "https://github.com/sponsors/kossnocorp"
}
},
"node_modules/debug": {
"version": "4.4.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.0.tgz",

View file

@ -49,6 +49,8 @@
},
"dependencies": {
"chart.js": "^4.4.7",
"chartjs-adapter-date-fns": "^3.0.0",
"chartjs-plugin-annotation": "^3.1.0",
"google-charts": "^2.0.0",
"leaflet": "^1.9.4",
"leaflet.fullscreen": "^3.0.2",

View file

@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright (c) 2019 Chart.js Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,79 @@
# chartjs-adapter-date-fns
[![release](https://img.shields.io/github/release/chartjs/chartjs-adapter-date-fns.svg?style=flat-square)](https://github.com/chartjs/chartjs-adapter-date-fns/releases/latest) [![travis](https://img.shields.io/travis/chartjs/chartjs-adapter-date-fns.svg?style=flat-square&maxAge=60)](https://travis-ci.org/chartjs/chartjs-adapter-date-fns) [![awesome](https://awesome.re/badge-flat2.svg)](https://github.com/chartjs/awesome)
## Overview
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/) **2.0.0** or later.
**Note:** once loaded, this adapter overrides the default date-adapter provided in Chart.js (as a side-effect).
## Installation
### npm
```bash
npm install date-fns chartjs-adapter-date-fns --save
```
```javascript
import { Chart } from 'chart.js';
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:
```html
<script src="https://cdn.jsdelivr.net/npm/chart.js/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
```
Read more about jsDeliver versioning on their [website](http://www.jsdelivr.com/).
## Configuration
### Locale support via scale options
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/next/axes/cartesian/time#date-adapters)
For example:
```javascript
// import date-fns locale:
import {de} from 'date-fns/locale';
// scale options:
{
adapters: {
date: {
locale: de
}
}
}
```
Further, read the [Chart.js documentation](https://www.chartjs.org/docs/next) for other possible date/time related options. For example, the time scale [`time.*` options](https://www.chartjs.org/docs/next/axes/cartesian/time#configuration-options) can be overridden using the [date-fns tokens](https://date-fns.org/docs/format).
## Development
You first need to install node dependencies (requires [Node.js](https://nodejs.org/)):
```bash
> npm install
```
The following commands will then be available from the repository root:
```bash
> npm run build // build dist files
> npm run lint // perform code linting
```
## License
`chartjs-adapter-date-fns` is available under the [MIT license](LICENSE.md).

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,109 @@
/*!
* chartjs-adapter-date-fns v3.0.0
* https://www.chartjs.org
* (c) 2022 chartjs-adapter-date-fns Contributors
* Released under the MIT license
*/
import { _adapters } from 'chart.js';
import { toDate, parse, parseISO, isValid, format, addYears, addQuarters, addMonths, addWeeks, addDays, addHours, addMinutes, addSeconds, addMilliseconds, differenceInYears, differenceInQuarters, differenceInMonths, differenceInWeeks, differenceInDays, differenceInHours, differenceInMinutes, differenceInSeconds, differenceInMilliseconds, startOfYear, startOfQuarter, startOfMonth, startOfWeek, startOfDay, startOfHour, startOfMinute, startOfSecond, endOfYear, endOfQuarter, endOfMonth, endOfWeek, endOfDay, endOfHour, endOfMinute, endOfSecond } from 'date-fns';
const FORMATS = {
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: 'PP',
month: 'MMM yyyy',
quarter: 'qqq - yyyy',
year: 'yyyy'
};
_adapters._date.override({
_id: 'date-fns', // DEBUG
formats: function() {
return FORMATS;
},
parse: function(value, fmt) {
if (value === null || typeof value === 'undefined') {
return null;
}
const 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;
},
format: function(time, fmt) {
return format(time, fmt, this.options);
},
add: function(time, amount, unit) {
switch (unit) {
case 'millisecond': return addMilliseconds(time, amount);
case 'second': return addSeconds(time, amount);
case 'minute': return addMinutes(time, amount);
case 'hour': return addHours(time, amount);
case 'day': return addDays(time, amount);
case 'week': return addWeeks(time, amount);
case 'month': return addMonths(time, amount);
case 'quarter': return addQuarters(time, amount);
case 'year': return addYears(time, amount);
default: return time;
}
},
diff: function(max, min, unit) {
switch (unit) {
case 'millisecond': return differenceInMilliseconds(max, min);
case 'second': return differenceInSeconds(max, min);
case 'minute': return differenceInMinutes(max, min);
case 'hour': return differenceInHours(max, min);
case 'day': return differenceInDays(max, min);
case 'week': return differenceInWeeks(max, min);
case 'month': return differenceInMonths(max, min);
case 'quarter': return differenceInQuarters(max, min);
case 'year': return differenceInYears(max, min);
default: return 0;
}
},
startOf: function(time, unit, weekday) {
switch (unit) {
case 'second': return startOfSecond(time);
case 'minute': return startOfMinute(time);
case 'hour': return startOfHour(time);
case 'day': return startOfDay(time);
case 'week': return startOfWeek(time);
case 'isoWeek': return startOfWeek(time, {weekStartsOn: +weekday});
case 'month': return startOfMonth(time);
case 'quarter': return startOfQuarter(time);
case 'year': return startOfYear(time);
default: return time;
}
},
endOf: function(time, unit) {
switch (unit) {
case 'second': return endOfSecond(time);
case 'minute': return endOfMinute(time);
case 'hour': return endOfHour(time);
case 'day': return endOfDay(time);
case 'week': return endOfWeek(time);
case 'month': return endOfMonth(time);
case 'quarter': return endOfQuarter(time);
case 'year': return endOfYear(time);
default: return time;
}
}
});

View file

@ -0,0 +1,114 @@
/*!
* chartjs-adapter-date-fns v3.0.0
* https://www.chartjs.org
* (c) 2022 chartjs-adapter-date-fns Contributors
* Released under the MIT license
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('chart.js'), require('date-fns')) :
typeof define === 'function' && define.amd ? define(['chart.js', 'date-fns'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Chart, global.dateFns));
})(this, (function (chart_js, dateFns) { 'use strict';
const FORMATS = {
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: 'PP',
month: 'MMM yyyy',
quarter: 'qqq - yyyy',
year: 'yyyy'
};
chart_js._adapters._date.override({
_id: 'date-fns', // DEBUG
formats: function() {
return FORMATS;
},
parse: function(value, fmt) {
if (value === null || typeof value === 'undefined') {
return null;
}
const type = typeof value;
if (type === 'number' || value instanceof Date) {
value = dateFns.toDate(value);
} else if (type === 'string') {
if (typeof fmt === 'string') {
value = dateFns.parse(value, fmt, new Date(), this.options);
} else {
value = dateFns.parseISO(value, this.options);
}
}
return dateFns.isValid(value) ? value.getTime() : null;
},
format: function(time, fmt) {
return dateFns.format(time, fmt, this.options);
},
add: function(time, amount, unit) {
switch (unit) {
case 'millisecond': return dateFns.addMilliseconds(time, amount);
case 'second': return dateFns.addSeconds(time, amount);
case 'minute': return dateFns.addMinutes(time, amount);
case 'hour': return dateFns.addHours(time, amount);
case 'day': return dateFns.addDays(time, amount);
case 'week': return dateFns.addWeeks(time, amount);
case 'month': return dateFns.addMonths(time, amount);
case 'quarter': return dateFns.addQuarters(time, amount);
case 'year': return dateFns.addYears(time, amount);
default: return time;
}
},
diff: function(max, min, unit) {
switch (unit) {
case 'millisecond': return dateFns.differenceInMilliseconds(max, min);
case 'second': return dateFns.differenceInSeconds(max, min);
case 'minute': return dateFns.differenceInMinutes(max, min);
case 'hour': return dateFns.differenceInHours(max, min);
case 'day': return dateFns.differenceInDays(max, min);
case 'week': return dateFns.differenceInWeeks(max, min);
case 'month': return dateFns.differenceInMonths(max, min);
case 'quarter': return dateFns.differenceInQuarters(max, min);
case 'year': return dateFns.differenceInYears(max, min);
default: return 0;
}
},
startOf: function(time, unit, weekday) {
switch (unit) {
case 'second': return dateFns.startOfSecond(time);
case 'minute': return dateFns.startOfMinute(time);
case 'hour': return dateFns.startOfHour(time);
case 'day': return dateFns.startOfDay(time);
case 'week': return dateFns.startOfWeek(time);
case 'isoWeek': return dateFns.startOfWeek(time, {weekStartsOn: +weekday});
case 'month': return dateFns.startOfMonth(time);
case 'quarter': return dateFns.startOfQuarter(time);
case 'year': return dateFns.startOfYear(time);
default: return time;
}
},
endOf: function(time, unit) {
switch (unit) {
case 'second': return dateFns.endOfSecond(time);
case 'minute': return dateFns.endOfMinute(time);
case 'hour': return dateFns.endOfHour(time);
case 'day': return dateFns.endOfDay(time);
case 'week': return dateFns.endOfWeek(time);
case 'month': return dateFns.endOfMonth(time);
case 'quarter': return dateFns.endOfQuarter(time);
case 'year': return dateFns.endOfYear(time);
default: return time;
}
}
});
}));

View file

@ -0,0 +1,7 @@
/*!
* chartjs-adapter-date-fns v3.0.0
* https://www.chartjs.org
* (c) 2022 chartjs-adapter-date-fns Contributors
* Released under the MIT license
*/
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(require("chart.js"),require("date-fns")):"function"==typeof define&&define.amd?define(["chart.js","date-fns"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).Chart,e.dateFns)}(this,(function(e,r){"use strict";const t={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:"PP",month:"MMM yyyy",quarter:"qqq - yyyy",year:"yyyy"};e._adapters._date.override({_id:"date-fns",formats:function(){return t},parse:function(e,t){if(null==e)return null;const n=typeof e;return"number"===n||e instanceof Date?e=r.toDate(e):"string"===n&&(e="string"==typeof t?r.parse(e,t,new Date,this.options):r.parseISO(e,this.options)),r.isValid(e)?e.getTime():null},format:function(e,t){return r.format(e,t,this.options)},add:function(e,t,n){switch(n){case"millisecond":return r.addMilliseconds(e,t);case"second":return r.addSeconds(e,t);case"minute":return r.addMinutes(e,t);case"hour":return r.addHours(e,t);case"day":return r.addDays(e,t);case"week":return r.addWeeks(e,t);case"month":return r.addMonths(e,t);case"quarter":return r.addQuarters(e,t);case"year":return r.addYears(e,t);default:return e}},diff:function(e,t,n){switch(n){case"millisecond":return r.differenceInMilliseconds(e,t);case"second":return r.differenceInSeconds(e,t);case"minute":return r.differenceInMinutes(e,t);case"hour":return r.differenceInHours(e,t);case"day":return r.differenceInDays(e,t);case"week":return r.differenceInWeeks(e,t);case"month":return r.differenceInMonths(e,t);case"quarter":return r.differenceInQuarters(e,t);case"year":return r.differenceInYears(e,t);default:return 0}},startOf:function(e,t,n){switch(t){case"second":return r.startOfSecond(e);case"minute":return r.startOfMinute(e);case"hour":return r.startOfHour(e);case"day":return r.startOfDay(e);case"week":return r.startOfWeek(e);case"isoWeek":return r.startOfWeek(e,{weekStartsOn:+n});case"month":return r.startOfMonth(e);case"quarter":return r.startOfQuarter(e);case"year":return r.startOfYear(e);default:return e}},endOf:function(e,t){switch(t){case"second":return r.endOfSecond(e);case"minute":return r.endOfMinute(e);case"hour":return r.endOfHour(e);case"day":return r.endOfDay(e);case"week":return r.endOfWeek(e);case"month":return r.endOfMonth(e);case"quarter":return r.endOfQuarter(e);case"year":return r.endOfYear(e);default:return e}}})}));

View file

@ -0,0 +1,61 @@
{
"name": "chartjs-adapter-date-fns",
"homepage": "https://www.chartjs.org",
"description": "Chart.js adapter to use date-fns for time functionalities",
"version": "3.0.0",
"license": "MIT",
"type": "module",
"main": "dist/chartjs-adapter-date-fns.esm.js",
"jsdelivr": "dist/chartjs-adapter-date-fns.bundle.min.js",
"unpkg": "dist/chartjs-adapter-date-fns.bundle.min.js",
"exports": {
"import": "./dist/chartjs-adapter-date-fns.esm.js",
"require": "./dist/chartjs-adapter-date-fns.min.js"
},
"repository": {
"type": "git",
"url": "https://github.com/chartjs/chartjs-adapter-date-fns.git"
},
"scripts": {
"build": "rollup -c",
"dev": "karma start ./karma.conf.cjs --auto-watch --no-single-run --browsers chrome",
"lint": "eslint test/**/*.js src/**/*.js",
"test": "cross-env NODE_ENV=test concurrently \"npm:test-*\"",
"test-lint": "npm run lint",
"test-karma": "karma start ./karma.conf.cjs --auto-watch --single-run"
},
"keywords": [
"chart.js",
"date",
"date-fns",
"time"
],
"files": [
"dist/*.js"
],
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.1.0",
"chart.js": "^4.0.1",
"chartjs-test-utils": "^0.5.0",
"concurrently": "^7.6.0",
"cross-env": "^7.0.3",
"date-fns": "2.19",
"eslint": "^8.29.0",
"eslint-config-chartjs": "^0.3.0",
"eslint-plugin-es": "^4.1.0",
"karma": "^6.1.1",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.3",
"karma-firefox-launcher": "^2.1.0",
"karma-jasmine": "^5.1.0",
"karma-jasmine-html-reporter": "^2.0.0",
"karma-rollup-preprocessor": "7.0.7",
"rollup": "^3.7.2",
"rollup-plugin-istanbul": "^4.0.0"
},
"peerDependencies": {
"chart.js": ">=2.8.0",
"date-fns": ">=2.0.0"
}
}

View file

@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright (c) 2016-2021 chartjs-plugin-annotation Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,41 @@
# chartjs-plugin-annotation.js
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/chartjs/chartjs-plugin-annotation/ci.yml?branch=master)](https://github.com/chartjs/chartjs-plugin-annotation/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/chartjs/chartjs-plugin-annotation/badge.svg?branch=master)](https://coveralls.io/github/chartjs/chartjs-plugin-annotation?branch=master)
[![release](https://img.shields.io/github/v/release/chartjs/chartjs-plugin-annotation?include_prereleases)](https://github.com/chartjs/chartjs-plugin-annotation/releases)
[![npm (latest)](https://img.shields.io/npm/v/chartjs-plugin-annotation/latest)](https://www.npmjs.com/package/chartjs-plugin-annotation/v/latest)
[![documentation](https://img.shields.io/static/v1?message=Documentation&color=informational)](https://www.chartjs.org/chartjs-plugin-annotation/index)
<a href="https://github.com/chartjs/awesome"><img src="https://awesome.re/badge-flat2.svg" alt="Awesome"></a>
An annotation plugin for Chart.js >= 4.0.0
---
> This plugin needs to be registered. It does not function as inline plugin.
---
For Chart.js 3.7.0 to 3.9.1 support, use [version 2.2.1 of this plugin](https://github.com/chartjs/chartjs-plugin-annotation/releases/tag/v2.2.1)
For Chart.js 3.0.0 to 3.6.2 support, use [version 1.4.0 of this plugin](https://github.com/chartjs/chartjs-plugin-annotation/releases/tag/v1.4.0)
For Chart.js 2.4.0 to 2.9.x support, use [version 0.5.7 of this plugin](https://github.com/chartjs/chartjs-plugin-annotation/releases/tag/v0.5.7)
This plugin draws lines, boxes, points, labels, polygons and ellipses on the chart area.
Annotations work with line, bar, scatter and bubble charts that use linear, logarithmic, time, or category scales.
Furthermore you can use a doughnut label annotation which can be used to add contents (text, image, canvas) in the middle area of the doughnut charts.
![Example Screenshot](docs/guide/banner.png)
[View this example](https://www.chartjs.org/chartjs-plugin-annotation/latest/samples/intro.html)
## Documentation
You can find documentation for chartjs-plugin-annotation at [www.chartjs.org/chartjs-plugin-annotation](https://www.chartjs.org/chartjs-plugin-annotation/index).
## Contributing
Before submitting an issue or a pull request to the project, please take a moment to look over the [contributing guidelines](CONTRIBUTING.md) first.
## License
Chart.Annotation.js is available under the [MIT license](LICENSE.md).

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,90 @@
{
"name": "chartjs-plugin-annotation",
"homepage": "https://www.chartjs.org/chartjs-plugin-annotation/index",
"description": "Annotations for Chart.js",
"version": "3.1.0",
"author": "Evert Timberg <evert.timberg@gmail.com>",
"license": "MIT",
"type": "module",
"main": "dist/chartjs-plugin-annotation.cjs",
"module": "dist/chartjs-plugin-annotation.esm.js",
"types": "types/index.d.ts",
"jsdelivr": "dist/chartjs-plugin-annotation.min.js",
"unpkg": "dist/chartjs-plugin-annotation.min.js",
"exports": {
"types": "./types/index.d.ts",
"import": "./dist/chartjs-plugin-annotation.esm.js",
"require": "./dist/chartjs-plugin-annotation.cjs",
"script": "./dist/chartjs-plugin-annotation.min.js"
},
"sideEffects": [
"dist/chartjs-plugin-annotation.cjs",
"dist/chartjs-plugin-annotation.min.js"
],
"repository": {
"type": "git",
"url": "https://github.com/chartjs/chartjs-plugin-annotation.git"
},
"files": [
"dist/*",
"!dist/docs/**",
"types/*.d.ts"
],
"scripts": {
"build": "rollup -c",
"dev": "karma start ./karma.conf.cjs --auto-watch --no-single-run --browsers chrome",
"dev:ff": "karma start ./karma.conf.cjs --auto-watch --no-single-run --browsers firefox",
"docs": "npm run build && vuepress build docs --no-cache",
"docs:dev": "npm run build && vuepress dev docs --no-cache",
"lint": "concurrently --group \"npm:lint-*\"",
"lint-js": "eslint \"test/**/*.js\" \"src/**/*.js\"",
"lint-md": "eslint \"**/*.md\"",
"lint-types": "eslint \"types/**/*.ts\" && tsc -p types/tests/",
"test": "cross-env NODE_ENV=test concurrently --group \"npm:test-*\"",
"test-karma": "karma start ./karma.conf.cjs --auto-watch --single-run",
"test-lint": "npm run lint",
"test-types": "tsc -p types/tests/",
"test-integration": "mocha --full-trace test/integration/*-test.js"
},
"devDependencies": {
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.0",
"@simonbrunel/vuepress-plugin-versions": "^0.2.0",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"chart.js": "^4.3.0",
"chartjs-test-utils": "^0.5.0",
"concurrently": "^7.6.0",
"cross-env": "^7.0.3",
"eslint": "^8.33.0",
"eslint-config-chartjs": "^0.3.0",
"eslint-plugin-es": "^4.1.0",
"eslint-plugin-html": "^7.1.0",
"eslint-plugin-markdown": "^3.0.0",
"fs-extra": "^11.1.0",
"karma": "^6.4.1",
"karma-chrome-launcher": "^3.1.1",
"karma-coverage": "^2.2.0",
"karma-firefox-launcher": "^2.1.2",
"karma-jasmine": "^5.1.0",
"karma-jasmine-html-reporter": "^2.0.0",
"karma-rollup-preprocessor": "7.0.7",
"markdown-it-include": "^2.0.0",
"mocha": "^10.2.0",
"pixelmatch": "^5.3.0",
"rollup": "^3.14.0",
"rollup-plugin-istanbul": "^4.0.0",
"typedoc": "^0.23.24",
"typedoc-plugin-markdown": "^3.14.0",
"typescript": "^4.2.4",
"vuepress": "^1.9.7",
"vuepress-plugin-flexsearch": "^0.3.0",
"vuepress-plugin-redirect": "^1.2.5",
"vuepress-plugin-typedoc": "^0.10.0",
"vuepress-theme-chartjs": "^0.2.0"
},
"peerDependencies": {
"chart.js": ">=4.0.0"
}
}

View file

@ -0,0 +1,18 @@
import { AnnotationOptions } from './options';
export interface AnnotationBoxModel {
x: number,
y: number,
x2: number,
y2: number,
centerX: number,
centerY: number,
height: number,
width: number,
radius?: number
}
export interface AnnotationElement extends AnnotationBoxModel {
label?: AnnotationElement,
options: AnnotationOptions
}

View file

@ -0,0 +1,28 @@
import { Chart, ChartEvent } from 'chart.js';
import { AnnotationElement } from './element';
export interface EventContext {
chart: Chart,
element: AnnotationElement,
elements: AnnotationElement[],
id: string,
type: string
}
/**
* Some scriptable options may be called with during the chart's initial
* display, when the element isn't fully initialized.
*/
export interface PartialEventContext {
chart: Chart,
element?: Partial<AnnotationElement>,
elements?: AnnotationElement[],
id?: string,
type?: string
}
export interface AnnotationEvents {
enter?(context: EventContext, event: ChartEvent): boolean | void,
leave?(context: EventContext, event: ChartEvent): boolean | void,
click?(context: EventContext, event: ChartEvent): boolean | void,
}

View file

@ -0,0 +1,31 @@
import { Chart, ChartType, Plugin } from 'chart.js';
import { AnnotationPluginOptions, BoxAnnotationOptions, EllipseAnnotationOptions, LabelAnnotationOptions, LineAnnotationOptions, PointAnnotationOptions, PolygonAnnotationOptions, DoughnutLabelAnnotationOptions } from './options';
import { AnnotationElement } from './element';
declare module 'chart.js' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface PluginOptionsByType<TType extends ChartType> {
annotation: AnnotationPluginOptions;
}
interface ElementOptionsByType<TType extends ChartType> {
boxAnnotation: BoxAnnotationOptions;
doughnutLabelAnnotation: DoughnutLabelAnnotationOptions;
ellipseAnnotation: EllipseAnnotationOptions;
labelAnnotation: LabelAnnotationOptions;
lineAnnotation: LineAnnotationOptions;
pointAnnotation: PointAnnotationOptions;
polygonAnnotation: PolygonAnnotationOptions;
}
}
declare const Annotation: Plugin & {
getAnnotations(chart: Chart): AnnotationElement[];
};
export default Annotation;
export * from './element';
export * from './events';
export * from './label';
export * from './options';

View file

@ -0,0 +1,165 @@
import { Color, FontSpec, BorderRadius } from 'chart.js';
import { PartialEventContext } from './events';
import { DrawTime, Scriptable, ShadowOptions } from './options';
type percentString = `${number}%`;
export type LabelPosition = 'start' | 'center' | 'end' | percentString;
export type LabelTextAlign = 'left' | 'start' | 'center' | 'right' | 'end';
export type CalloutPosition = 'left' | 'top' | 'bottom' | 'right' | 'auto';
export interface LabelPositionObject {
x?: LabelPosition,
y?: LabelPosition
}
export interface LabelPadding {
top?: number,
left?: number,
right?: number,
bottom?: number,
x?: number,
y?: number
}
export interface CalloutOptions {
borderCapStyle?: Scriptable<CanvasLineCap, PartialEventContext>,
borderColor?: Scriptable<Color, PartialEventContext>,
borderDash?: Scriptable<number[], PartialEventContext>,
borderDashOffset?: Scriptable<number, PartialEventContext>,
borderJoinStyle?: Scriptable<CanvasLineJoin, PartialEventContext>,
borderWidth?: Scriptable<number, PartialEventContext>,
display?: Scriptable<boolean, PartialEventContext>,
margin?: Scriptable<number, PartialEventContext>,
position?: Scriptable<CalloutPosition, PartialEventContext>,
side?: Scriptable<number, PartialEventContext>,
start?: Scriptable<number | string, PartialEventContext>,
}
export interface CoreLabelOptions {
drawTime?: Scriptable<DrawTime, PartialEventContext>,
font?: Scriptable<Partial<FontSpec> | Partial<FontSpec>[], PartialEventContext>,
color?: Scriptable<Color | Color[], PartialEventContext>,
/**
* Padding of label
* @default 6
*/
padding?: Scriptable<number | LabelPadding, PartialEventContext>,
/**
* Text alignment when the content of the label is multi-line.
* @default 'center'
*/
textAlign?: Scriptable<LabelTextAlign, PartialEventContext>,
textStrokeColor?: Scriptable<Color, PartialEventContext>,
textStrokeWidth?: Scriptable<number, PartialEventContext>,
/**
* Adjustment along x-axis (left-right) of label relative to above number (can be negative)
* For horizontal lines positioned left or right, negative values move
* the label toward the edge, and positive values toward the center.
* @default 0
*/
xAdjust?: Scriptable<number, PartialEventContext>,
/**
* Adjustment along y-axis (top-bottom) of label relative to above number (can be negative)
* For vertical lines positioned top or bottom, negative values move
* the label toward the edge, and positive values toward the center.
* @default 0
*/
yAdjust?: Scriptable<number, PartialEventContext>,
/**
* Text to display in label. Provide an array to display multiple lines
*/
content: Scriptable<string | string[] | HTMLImageElement | HTMLCanvasElement | null, PartialEventContext>,
/**
* Overrides the width of the image. Could be set in pixel by a number,
* or in percentage of current width of image by a string
*/
width?: Scriptable<number | percentString, PartialEventContext>,
/**
* Overrides the height of the image. Could be set in pixel by a number,
* or in percentage of current height of image by a string
*/
height?: Scriptable<number | percentString, PartialEventContext>,
/**
* Overrides the opacity of the image.
*/
opacity?: Scriptable<number, PartialEventContext>,
z?: Scriptable<number, PartialEventContext>
}
export interface ContainedLabelOptions extends CoreLabelOptions {
backgroundColor?: Scriptable<Color, PartialEventContext>,
borderWidth?: Scriptable<number, PartialEventContext>,
borderColor?: Scriptable<Color, PartialEventContext>,
/**
* Border line cap style. See MDN.
* @default 'butt'
*/
borderCapStyle?: Scriptable<CanvasLineCap, PartialEventContext>,
/**
* Border line dash. See MDN.
* @default []
*/
borderDash?: Scriptable<number[], PartialEventContext>,
/**
* Border line dash offset. See MDN.
* @default 0.0
*/
borderDashOffset?: Scriptable<number, PartialEventContext>,
/**
* Border line join style. See MDN.
* @default 'miter'
*/
borderJoinStyle?: Scriptable<CanvasLineJoin, PartialEventContext>,
/**
* Border radius of the label rectangle
* @default 6
*/
borderRadius?: Scriptable<number | BorderRadius, PartialEventContext>
}
export interface LabelOptions extends ContainedLabelOptions, ShadowOptions {
position?: Scriptable<LabelPosition, PartialEventContext>,
/**
* Whether the label should be displayed
* @default true
*/
display?: Scriptable<boolean, PartialEventContext>,
hitTolerance?: Scriptable<number, PartialEventContext>,
/**
* Rotation of label, in degrees, or 'auto' to use the degrees of the line, default is 0
* @default 90
*/
rotation?: Scriptable<number | 'auto', PartialEventContext>,
z?: Scriptable<number, PartialEventContext>,
callout?: CalloutOptions,
}
export interface BoxLabelOptions extends CoreLabelOptions {
position?: Scriptable<LabelPosition | LabelPositionObject, PartialEventContext>,
/**
* Whether the label should be displayed
* @default true
*/
display?: Scriptable<boolean, PartialEventContext>,
hitTolerance?: Scriptable<number, PartialEventContext>,
rotation?: Scriptable<number, PartialEventContext>,
z?: Scriptable<number, PartialEventContext>
}
export interface LabelTypeOptions extends ContainedLabelOptions {
position?: Scriptable<LabelPosition | LabelPositionObject, PartialEventContext>,
z?: Scriptable<number, PartialEventContext>,
callout?: CalloutOptions,
}
export interface DoughnutLabelOptions extends Omit<ContainedLabelOptions, 'borderCapStyle' | 'borderRadius' | 'padding'> {
position?: Scriptable<LabelPosition | LabelPositionObject, PartialEventContext>,
/**
* Whether the label should be displayed
* @default true
*/
display?: Scriptable<boolean, PartialEventContext>,
rotation?: Scriptable<number, PartialEventContext>
}

View file

@ -0,0 +1,166 @@
import { Chart, Color, PointStyle, BorderRadius, CoreInteractionOptions } from 'chart.js';
import { AnnotationEvents, PartialEventContext, EventContext } from './events';
import { LabelOptions, BoxLabelOptions, LabelTypeOptions, DoughnutLabelOptions } from './label';
import { AnnotationBoxModel, AnnotationElement } from './element';
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw' | number;
export interface AnnotationTypeRegistry {
box: BoxAnnotationOptions
ellipse: EllipseAnnotationOptions
label: LabelAnnotationOptions
line: LineAnnotationOptions
point: PointAnnotationOptions
polygon: PolygonAnnotationOptions
}
export type AnnotationType = keyof AnnotationTypeRegistry;
export type AnnotationOptions<TYPE extends AnnotationType = AnnotationType> =
{ [key in TYPE]: { type: key } & AnnotationTypeRegistry[key] }[TYPE]
interface AnnotationHooks {
beforeDraw?(context: EventContext): void,
afterDraw?(context: EventContext): void
}
export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnnotationOptions) => T);
export type ScaleValue = number | string;
interface ShadowOptions {
backgroundShadowColor?: Scriptable<Color, PartialEventContext>,
borderShadowColor?: Scriptable<Color, PartialEventContext>,
shadowBlur?: Scriptable<number, PartialEventContext>,
shadowOffsetX?: Scriptable<number, PartialEventContext>,
shadowOffsetY?: Scriptable<number, PartialEventContext>
}
export interface CoreAnnotationOptions extends AnnotationEvents, ShadowOptions, AnnotationHooks {
adjustScaleRange?: Scriptable<boolean, PartialEventContext>,
borderColor?: Scriptable<Color, PartialEventContext>,
borderDash?: Scriptable<number[], PartialEventContext>,
borderDashOffset?: Scriptable<number, PartialEventContext>,
borderWidth?: Scriptable<number, PartialEventContext>,
display?: Scriptable<boolean, PartialEventContext>,
drawTime?: Scriptable<DrawTime, PartialEventContext>,
hitTolerance?: Scriptable<number, PartialEventContext>,
init?: boolean | (({ chart: Chart, properties: AnnotationBoxModel, options: AnnotationOptions }) => void | boolean | Partial<AnnotationElement>),
id?: string,
xMax?: Scriptable<ScaleValue, PartialEventContext>,
xMin?: Scriptable<ScaleValue, PartialEventContext>,
xScaleID?: Scriptable<string, PartialEventContext>,
yMax?: Scriptable<ScaleValue, PartialEventContext>,
yMin?: Scriptable<ScaleValue, PartialEventContext>,
yScaleID?: Scriptable<string, PartialEventContext>,
z?: Scriptable<number, PartialEventContext>
}
interface AnnotationPointCoordinates {
xValue?: Scriptable<ScaleValue, PartialEventContext>,
yValue?: Scriptable<ScaleValue, PartialEventContext>,
}
export interface ArrowHeadOptions extends ShadowOptions {
backgroundColor?: Scriptable<Color, PartialEventContext>,
borderColor?: Scriptable<Color, PartialEventContext>,
borderDash?: Scriptable<number[], PartialEventContext>,
borderDashOffset?: Scriptable<number, PartialEventContext>,
borderWidth?: Scriptable<number, PartialEventContext>,
display?: Scriptable<boolean, PartialEventContext>,
fill?: Scriptable<boolean, PartialEventContext>,
length?: Scriptable<number, PartialEventContext>,
width?: Scriptable<number, PartialEventContext>,
}
export interface ArrowHeadsOptions extends ArrowHeadOptions{
end?: ArrowHeadOptions,
start?: ArrowHeadOptions,
}
export interface ControlPointOptions {
x?: Scriptable<number | string, PartialEventContext>,
y?: Scriptable<number | string, PartialEventContext>,
}
export interface LineAnnotationOptions extends CoreAnnotationOptions {
arrowHeads?: ArrowHeadsOptions,
curve?: Scriptable<boolean, PartialEventContext>,
controlPoint?: Scriptable<number | string | ControlPointOptions, PartialEventContext>,
endValue?: Scriptable<number|string, PartialEventContext>,
label?: LabelOptions,
scaleID?: Scriptable<string, PartialEventContext>,
value?: Scriptable<number|string, PartialEventContext>
}
export interface BoxAnnotationOptions extends CoreAnnotationOptions {
backgroundColor?: Scriptable<Color, PartialEventContext>,
/**
* Border line cap style. See MDN.
* @default 'butt'
*/
borderCapStyle?: Scriptable<CanvasLineCap, PartialEventContext>,
/**
* Border line dash. See MDN.
* @default []
*/
borderDash?: Scriptable<number[], PartialEventContext>,
/**
* Border line dash offset. See MDN.
* @default 0.0
*/
borderDashOffset?: Scriptable<number, PartialEventContext>,
/**
* Border line join style. See MDN.
* @default 'miter'
*/
borderJoinStyle?: Scriptable<CanvasLineJoin, PartialEventContext>,
borderRadius?: Scriptable<number | BorderRadius, PartialEventContext>,
label?: BoxLabelOptions,
rotation?: Scriptable<number, PartialEventContext>
}
export interface EllipseAnnotationOptions extends CoreAnnotationOptions {
backgroundColor?: Scriptable<Color, PartialEventContext>,
label?: BoxLabelOptions,
rotation?: Scriptable<number, PartialEventContext>
}
export interface PointAnnotationOptions extends CoreAnnotationOptions, AnnotationPointCoordinates {
backgroundColor: Scriptable<Color, PartialEventContext>,
pointStyle?: Scriptable<PointStyle, PartialEventContext>,
radius?: Scriptable<number, PartialEventContext>,
rotation?: Scriptable<number, PartialEventContext>,
xAdjust?: Scriptable<number, PartialEventContext>,
yAdjust?: Scriptable<number, PartialEventContext>,
}
export interface LabelAnnotationOptions extends CoreAnnotationOptions, LabelTypeOptions, AnnotationPointCoordinates {
rotation?: Scriptable<number, PartialEventContext>
}
export interface DoughnutLabelAnnotationOptions extends AnnotationEvents, DoughnutLabelOptions, ShadowOptions {
autoFit?: Scriptable<boolean, PartialEventContext>,
autoHide?: Scriptable<boolean, PartialEventContext>,
id?: string,
init: boolean | ((chart: Chart, properties: AnnotationBoxModel, options: AnnotationOptions) => void | boolean | AnnotationBoxModel),
spacing?: Scriptable<number, PartialEventContext>
}
interface PolygonAnnotationOptions extends CoreAnnotationOptions, AnnotationPointCoordinates {
backgroundColor: Scriptable<Color, PartialEventContext>,
borderCapStyle?: Scriptable<CanvasLineCap, PartialEventContext>,
borderJoinStyle?: Scriptable<CanvasLineJoin, PartialEventContext>,
point?: PointAnnotationOptions,
radius?: Scriptable<number, PartialEventContext>,
rotation?: Scriptable<number, PartialEventContext>,
sides?: Scriptable<number, PartialEventContext>,
xAdjust?: Scriptable<number, PartialEventContext>,
yAdjust?: Scriptable<number, PartialEventContext>,
}
export interface AnnotationPluginOptions extends AnnotationEvents, AnnotationHooks {
animations?: Record<string, unknown>,
annotations: AnnotationOptions[] | Record<string, AnnotationOptions>,
clip?: boolean,
common?: BoxAnnotationOptions | EllipseAnnotationOptions | LabelAnnotationOptions | LineAnnotationOptions | PointAnnotationOptions | PolygonAnnotationOptions | DoughnutLabelAnnotationOptions,
interaction?: CoreInteractionOptions
}