====== Chart.js - デザイナーと開発者向けのシンプルでありながら柔軟な JavaScript チャート ====== {{:javascript:chartjs-logo.svg|Chart.js Logo}} 本家: [[https://www.chartjs.org|Chart.js | Open source HTML5 Charts for your website]]\\ ソースコード: [[git>chartjs/Chart.js|chartjs/Chart.js: Simple HTML5 Charts using the tag]]\\ ドキュメント: [[https://www.chartjs.org/docs/latest/|Chart.js | Chart.js]]\\ ===== 使い方 ===== ===== コードのメンテナンス性とわかりやすさ ===== [[https://www.chartjs.org/docs/latest/getting-started/|Getting Started | Chart.js]]\\ Chart.js の「はじめに」のように、HTML、JavaScript、CSS に分けて、JavaScript は ''const labels'' (ラベル定義)、''const data'' (データ定義)、''const config'' (設定)、''const myChart'' (グラフ描画) に分けて記述する🤔\\ ==== キャンバスサイズの変更 ==== 公式ドキュメント [[https://www.chartjs.org/docs/latest/configuration/responsive.html|Responsive Charts | Chart.js]] より抜粋🤔\\ The following examples **do not work:**\\ * '''': **invalid** values, the canvas doesn't resize ([[https://codepen.io/chartjs/pen/oWLZaR|example]])\\ * '''': **invalid** behavior, the canvas is resized but becomes blurry ([[https://codepen.io/chartjs/pen/WjxpmO|example]])\\ * '''': **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 ([[https://codepen.io/chartjs/pen/YVWZbz|example]]):
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 [[https://www.chartjs.org/docs/latest/configuration/responsive.html#configuration-options|maintainAspectRatio]] option must also be set to ''false''.
''%%%%'' を直接リサイズするのではなく、コンテナ ''%%
%%'' に入れてコンテナをリサイズする🤔\\ ''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 } ===== ハーフドーナツ グラフ ===== ===== 参考文献 ===== [[https://laracasts.com/discuss/channels/general-discussion/chartjs-and-half-donuts|ChartJS and "half donuts"]]\\ ==== 付録 ==== [[tw>tomoyan596sp/status/1579264862232137729|簡単なべろ👅べろ👅ば〜😋グラフ🤤]]\\ [[tw>tomoyan596sp/status/1590550329388830720|これはバームクーヘン?🤔ハーフドーナツ🤤サンプル追加しておきました😊]]\\