目次
文書の過去の版を表示しています。
Chart.js - デザイナーと開発者向けのシンプルでありながら柔軟な JavaScript チャート
本家: Chart.js | Open source HTML5 Charts for your website
ソースコード: chartjs/Chart.js: Simple HTML5 Charts using the <canvas> tag
ドキュメント: Chart.js | Chart.js
使い方
コードのメンテナンス性とわかりやすさ
Getting Started | Chart.js
Chart.js の「はじめに」のように、HTML、JavaScript、CSS に分けて、JavaScript は const labels
(ラベル定義)、const data
(データ定義)、const config
(設定)、const myChart
(グラフ描画) に分けて記述する🤔
キャンバスサイズの変更
公式ドキュメント Responsive Charts | Chart.js より抜粋🤔
The following examples do not work:
<canvas style=“height:40vh; width:80vw”>
: invalid behavior, the canvas is resized but becomes blurry (example)
<canvas style=“margin: 0 auto;”>
: invalid behavior, the canvas continually shrinks. Chart.js needs a dedicated container for each canvas and this styling should be applied there.
Important Note
Detecting when the canvas size changes can not be done directly from the canvas
element. Chart.js uses its parent container to update the canvas render and display sizes. However, this method requires the container to be relatively positioned and dedicated to the chart canvas only. Responsiveness can then be achieved by setting relative values for the container size (example):
<div class="chart-container" style="position: relative; height:40vh; width:80vw"> <canvas id="chart"></canvas> </div>
The chart can also be programmatically resized by modifying the container size:
chart.canvas.parentNode.style.height = '128px'; chart.canvas.parentNode.style.width = '128px';
Note that in order for the above code to correctly resize the chart height, the maintainAspectRatio option must also be set to false
.
<canvas>
を直接リサイズするのではなく、コンテナ <div class="chart-container">
に入れてコンテナをリサイズする🤔
options
の maintainAspectRatio
はデフォルトで true
だが、これをキャンバスの縦横比を維持しないように false
に設定する🤔
maintainAspectRatio
: サイズ変更時に元のキャンバスの縦横比 (width/height) を維持します。
aspectRatio
: キャンバスの縦横比 (width/height)。チャートの種類によりデフォルト 1 または 2。
HTML
js
const config = { options: { maintainAspectRatio: false, } }
css
.chart-container { position: relative; width: 200px; height: 400px }
ハーフドーナツ グラフ
ビルド方法
node のインストール - anyenv - rbenv スタイルのオールインワン環境マネージャー
Chart.js のソースコードをクローンする…🤔
$ git clone --depth 1 https://github.com/chartjs/Chart.js.git
Cloning into 'Chart.js'... remote: Enumerating objects: 1890, done. remote: Counting objects: 100% (1890/1890), done. remote: Compressing objects: 100% (1501/1501), done. remote: Total 1890 (delta 506), reused 1350 (delta 373), pack-reused 0 (from 0) Receiving objects: 100% (1890/1890), 11.70 MiB | 10.65 MiB/s, done. Resolving deltas: 100% (506/506), done.
Performant NPM をグローバル インストールする…🤔
$ npm install -g pnpm
changed 1 package in 1s 1 package is looking for funding run `npm fund` for details
$ pnpm -v
9.8.0
Performant NPM でプロジェクトの依存関係をインストールする...🤔
$ pnpm install
Scope: all 7 workspace projects Lockfile is up to date, resolution step is skipped Packages: +1984 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Progress: resolved 1984, reused 1982, downloaded 0, added 1984, done dependencies: + @kurkle/color 0.3.2 devDependencies: + @rollup/plugin-commonjs 23.0.7 + @rollup/plugin-inject 5.0.3 + @rollup/plugin-json 5.0.2 + @rollup/plugin-node-resolve 15.0.1 + @swc/core 1.3.42 + @types/estree 1.0.0 + @types/offscreencanvas 2019.7.0 + @typescript-eslint/eslint-plugin 5.57.0 + @typescript-eslint/parser 5.57.0 + chartjs-adapter-luxon 1.3.1 + chartjs-adapter-moment 1.0.1 + chartjs-test-utils 0.4.0 + concurrently 7.6.0 + coveralls 3.1.1 + cross-env 7.0.3 + eslint 8.37.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 + esm 3.2.25 + glob 8.1.0 + jasmine 3.99.0 + jasmine-core 3.99.1 + karma 6.4.1 + karma-chrome-launcher 3.1.1 + karma-coverage 2.2.0 + karma-edge-launcher 0.4.2 + karma-firefox-launcher 2.1.2 + karma-jasmine 4.0.2 + karma-jasmine-html-reporter 1.7.0 + karma-rollup-preprocessor 7.0.7 + karma-safari-private-launcher 1.0.0 + karma-spec-reporter 0.0.32 + luxon 3.3.0 + moment 2.29.4 + moment-timezone 0.5.42 + pixelmatch 5.3.0 + rollup 3.20.2 + rollup-plugin-cleanup 3.2.1 + rollup-plugin-istanbul 4.0.0 + rollup-plugin-swc3 0.7.0 + rollup-plugin-terser 7.0.2 + typescript 4.9.5 + yargs 17.7.1 Done in 14.4s
Chart.js ドキュメントを実行する...🤔
Node.js の OpenSSL レガシープロバイダー オプションを有効にして pnpm で実行する😉
Node.js 17 では webpack v4 が使用する openssl 標準が非推奨になったので、これが修正されるまでは OpenSSL レガシープロバイダー オプションを有効にすることで回避する😊
nodejs 17: digital envelope routines::unsupported · Issue #14532 · webpack/webpack · GitHub
webpack v5 では対応されている😉 (add wasm md4 implementation by sokra · Pull Request #14584 · webpack/webpack · GitHub)
$ export NODE_OPTIONS=--openssl-legacy-provider
$ pnpm run docs:dev
> chart.js@4.4.4 docs:dev /home/tomoyan/my_projects/_js_src/Chart.js > pnpm run build && pnpm --filter "./docs/**" dev > chart.js@4.4.4 build /home/tomoyan/my_projects/_js_src/Chart.js > rollup -c && pnpm emitDeclarations src/index.umd.ts → dist/chart.umd.js... (!) Circular dependency src/plugins/index.js -> src/plugins/plugin.colors.ts -> src/index.ts -> src/plugins/index.js created dist/chart.umd.js in 6.9s src/index.ts, src/helpers/index.ts → ./... (!) Circular dependency src/index.ts -> src/plugins/index.js -> src/plugins/plugin.colors.ts -> src/index.ts created ./ in 1.8s src/index.ts, src/helpers/index.ts → ./... (!) Circular dependency src/index.ts -> src/plugins/index.js -> src/plugins/plugin.colors.ts -> src/index.ts created ./ in 1.9s > chart.js@4.4.4 emitDeclarations /home/tomoyan/my_projects/_js_src/Chart.js > tsc --emitDeclarationOnly && pnpm copyDeclarations > chart.js@4.4.4 copyDeclarations /home/tomoyan/my_projects/_js_src/Chart.js > node -e "fs.cpSync('./src/types/', './dist/types/', {recursive:true})" > docs@4.0.0-dev dev /home/tomoyan/my_projects/_js_src/Chart.js/docs > vuepress dev --no-cache wait Extracting site metadata... tip Apply theme vuepress-theme-chartjs (extends @vuepress/theme-default) ... warning pointStyle has multiple declarations with a comment. An arbitrary comment will be used. The comments for pointStyle are declared at: /home/tomoyan/my_projects/_js_src/Chart.js/src/types/index.d.ts:1994 /home/tomoyan/my_projects/_js_src/Chart.js/src/types/index.d.ts:2044 tip Apply plugin container (i.e. "vuepress-plugin-container") ... tip Apply plugin @vuepress/last-updated (i.e. "@vuepress/plugin-last-updated") ... tip Apply plugin @vuepress/register-components (i.e. "@vuepress/plugin-register-components") ... tip Apply plugin @vuepress/active-header-links (i.e. "@vuepress/plugin-active-header-links") ... tip Apply plugin @vuepress/search (i.e. "@vuepress/plugin-search") ... tip Apply plugin @vuepress/nprogress (i.e. "@vuepress/plugin-nprogress") ... tip Apply plugin tabs (i.e. "vuepress-plugin-tabs") ... tip Apply plugin flexsearch (i.e. "vuepress-plugin-flexsearch") ... tip Apply plugin @vuepress/html-redirect (i.e. "@vuepress/plugin-html-redirect") ... tip Apply plugin @vuepress/google-analytics (i.e. "@vuepress/plugin-google-analytics") ... tip Apply plugin redirect (i.e. "vuepress-plugin-redirect") ... tip Apply plugin dehydrate (i.e. "vuepress-plugin-dehydrate") ... tip Apply plugin code-copy (i.e. "vuepress-plugin-code-copy") ... tip Apply plugin typedoc (i.e. "vuepress-plugin-typedoc") ... tip Apply plugin @simonbrunel/versions (i.e. "@simonbrunel/vuepress-plugin-versions") ... Documentation generated at ./api tip Clean cache... ● Client █████████████████████████ building (66%) 1192/1270 modules 78 active ... cache-loader › vue-loader › markdown-loader › api/interfaces/RenderTextOpts.md ℹ 「wds」: Project is running at http://0.0.0.0:8080/ ℹ 「wds」: webpack output is served from /docs/master/ ℹ 「wds」: Content not from webpack is served from /home/tomoyan/my_projects/_js_src/Chart.js/docs/.vuepress/public ℹ 「wds」: 404s will fallback to /index.html Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme [BABEL] Note: The code generator has deoptimised the styling of /home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/@vuepress+core@1.9.9/node_modules/@vuepress/core/.temp/internal/siteData.js as it exceeds the max of 500KB.
success [09:27:10] Build e5d5ff finished in 98069 ms! > VuePress dev server listening at http://localhost:8080/docs/master/
トラブルシューティング
vuepress dev --no-cache
が opensslError を吐く場合…😢
$ pnpm run docs:dev
> chart.js@4.4.4 docs:dev /home/tomoyan/my_projects/_js_src/Chart.js > pnpm run build && pnpm --filter "./docs/**" dev > chart.js@4.4.4 build /home/tomoyan/my_projects/_js_src/Chart.js > rollup -c && pnpm emitDeclarations src/index.umd.ts → dist/chart.umd.js... (!) Circular dependency src/plugins/index.js -> src/plugins/plugin.colors.ts -> src/index.ts -> src/plugins/index.js created dist/chart.umd.js in 6.5s src/index.ts, src/helpers/index.ts → ./... (!) Circular dependency src/index.ts -> src/plugins/index.js -> src/plugins/plugin.colors.ts -> src/index.ts created ./ in 1.9s src/index.ts, src/helpers/index.ts → ./... (!) Circular dependency src/index.ts -> src/plugins/index.js -> src/plugins/plugin.colors.ts -> src/index.ts created ./ in 1.8s > chart.js@4.4.4 emitDeclarations /home/tomoyan/my_projects/_js_src/Chart.js > tsc --emitDeclarationOnly && pnpm copyDeclarations > chart.js@4.4.4 copyDeclarations /home/tomoyan/my_projects/_js_src/Chart.js > node -e "fs.cpSync('./src/types/', './dist/types/', {recursive:true})" > docs@4.0.0-dev dev /home/tomoyan/my_projects/_js_src/Chart.js/docs > vuepress dev --no-cache wait Extracting site metadata... tip Apply theme vuepress-theme-chartjs (extends @vuepress/theme-default) ... warning pointStyle has multiple declarations with a comment. An arbitrary comment will be used. The comments for pointStyle are declared at: /home/tomoyan/my_projects/_js_src/Chart.js/src/types/index.d.ts:1994 /home/tomoyan/my_projects/_js_src/Chart.js/src/types/index.d.ts:2044 tip Apply plugin container (i.e. "vuepress-plugin-container") ... tip Apply plugin @vuepress/last-updated (i.e. "@vuepress/plugin-last-updated") ... tip Apply plugin @vuepress/register-components (i.e. "@vuepress/plugin-register-components") ... tip Apply plugin @vuepress/active-header-links (i.e. "@vuepress/plugin-active-header-links") ... tip Apply plugin @vuepress/search (i.e. "@vuepress/plugin-search") ... tip Apply plugin @vuepress/nprogress (i.e. "@vuepress/plugin-nprogress") ... tip Apply plugin tabs (i.e. "vuepress-plugin-tabs") ... tip Apply plugin flexsearch (i.e. "vuepress-plugin-flexsearch") ... tip Apply plugin @vuepress/html-redirect (i.e. "@vuepress/plugin-html-redirect") ... tip Apply plugin @vuepress/google-analytics (i.e. "@vuepress/plugin-google-analytics") ... tip Apply plugin redirect (i.e. "vuepress-plugin-redirect") ... tip Apply plugin dehydrate (i.e. "vuepress-plugin-dehydrate") ... tip Apply plugin code-copy (i.e. "vuepress-plugin-code-copy") ... tip Apply plugin typedoc (i.e. "vuepress-plugin-typedoc") ... tip Apply plugin @simonbrunel/versions (i.e. "@simonbrunel/vuepress-plugin-versions") ... Documentation generated at ./api tip Clean cache... ● Client █████████████████████████ building (40%) 1/2 modules 1 active ..../node_modules/.pnpm/webpack-dev-server@3.11.3_webpack@4.46.0/node_modules/webpack-dev-server/client/ind ex.js ℹ 「wds」: Project is running at http://0.0.0.0:8080/ ℹ 「wds」: webpack output is served from /docs/master/ ℹ 「wds」: Content not from webpack is served from /home/tomoyan/my_projects/_js_src/Chart.js/docs/.vuepress/public ℹ 「wds」: 404s will fallback to /index.html node:internal/crypto/hash:79 this[kHandle] = new _Hash(algorithm, xofLen, algorithmId, getHashCache()); ^
Error: error:0308010C:digital envelope routines::unsupported at new Hash (node:internal/crypto/hash:79:19) at Object.createHash (node:crypto:139:10) at module.exports (/home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/webpack@4.46.0/node_modules/webpack/lib/util/createHash.js:135:53) at NormalModule._initBuildHash (/home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/webpack@4.46.0/node_modules/webpack/lib/NormalModule.js:417:16) at handleParseError (/home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/webpack@4.46.0/node_modules/webpack/lib/NormalModule.js:471:10) at /home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/webpack@4.46.0/node_modules/webpack/lib/NormalModule.js:503:5 at /home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/webpack@4.46.0/node_modules/webpack/lib/NormalModule.js:358:12 at /home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/loader-runner@2.4.0/node_modules/loader-runner/lib/LoaderRunner.js:373:3 at iterateNormalLoaders (/home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/loader-runner@2.4.0/node_modules/loader-runner/lib/LoaderRunner.js:214:10) at Array.<anonymous> (/home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/loader-runner@2.4.0/node_modules/loader-runner/lib/LoaderRunner.js:205:4) at Storage.finished (/home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/enhanced-resolve@4.5.0/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16) at /home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/enhanced-resolve@4.5.0/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9 at /home/tomoyan/my_projects/_js_src/Chart.js/node_modules/.pnpm/graceful-fs@4.2.11/node_modules/graceful-fs/graceful-fs.js:123:16 at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read/context:68:3) { opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error', 'error:0308010C:digital envelope routines::unsupported' ], library: 'digital envelope routines', reason: 'unsupported', code: 'ERR_OSSL_EVP_UNSUPPORTED' } Node.js v20.17.0 /home/tomoyan/my_projects/_js_src/Chart.js/docs: ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL docs@4.0.0-dev dev: `vuepress dev --no-cache` Exit status 1 ELIFECYCLE Command failed with exit code 1.
参考: Node 19 Error: error:0308010C:digital envelope routines::unsupported · Issue #3136 · vuejs/vuepress · GitHub
Node.js の OpenSSL レガシープロバイダー オプションを有効にして再実行する😉
$ export NODE_OPTIONS=--openssl-legacy-provider
$ pnpm run docs:dev