Chart.js の使い方
Chart.js を使用してグラフを作成するには、Chart クラスを初期化し、グラフノードを渡す必要があります:
HTML Canvas コード、グラフ表示位置:
<canvas id="myChart" width="400" height="400"></canvas>
異なる方法でノードを取得します:
初期化方法
//以下の方法でも可能です
const ctx = document.getElementById('myChart'); // ノードを取得
const ctx = document.getElementById('myChart').getContext('2d'); // getContext() メソッドは canvas のコンテキストを返します
const ctx = $('#myChart'); // jQuery で取得
const ctx = 'myChart'; // ノード ID を渡す
const ctx = document.getElementById('myChart'); // ノードを取得
const ctx = document.getElementById('myChart').getContext('2d'); // getContext() メソッドは canvas のコンテキストを返します
const ctx = $('#myChart'); // jQuery で取得
const ctx = 'myChart'; // ノード ID を渡す
上記のコードで要素ノードを取得したら、独自のグラフタイプを作成できます。
次の例では、簡単な折れ線グラフを作成します:
サンプル
const ctx = document.getElementById('myChart');
const labels = ['1月', '2月', '3月','4月', '5月', '6月', '7月']; // X 軸に対応するラベルを設定
const data = {
labels: labels,
datasets: [{
label: '私の最初の折れ線グラフ',
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderColor: 'rgb(75, 192, 192)', // 線の色を設定
tension: 0.1
}]
};
const config = {
type: 'line', // グラフタイプを設定
data: data,
};
const myChart = new Chart(ctx, config);
const labels = ['1月', '2月', '3月','4月', '5月', '6月', '7月']; // X 軸に対応するラベルを設定
const data = {
labels: labels,
datasets: [{
label: '私の最初の折れ線グラフ',
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderColor: 'rgb(75, 192, 192)', // 線の色を設定
tension: 0.1
}]
};
const config = {
type: 'line', // グラフタイプを設定
data: data,
};
const myChart = new Chart(ctx, config);
試してみる »
上記の例の出力結果は次のとおりです:
次の例では、異なる色の票数を表示する棒グラフを作成します。
サンプル
const ctx = document.getElementById('myChart');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# 票数',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# 票数',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
試してみる »
上記の例の出力結果は次のとおりです:
設定オブジェクトの構造
設定オブジェクトの一般的な構造は次のとおりです:
const config = {
type: 'line'
data: {}
options: {}
plugins: []
}
type: 'line'
data: {}
options: {}
plugins: []
}
パラメータの説明:
- type-- グラフのタイプを指定
- data-- グラフに表示するデータセット
- options-- 一部のオプション設定。グラフの種類によって設定が異なります
- plugins-- プラグイン