Chart.js 円グラフ
円グラフ(パイチャートとも呼ばれる)は、いくつかの扇形に分割された円形の統計グラフで、量、頻度、またはパーセンテージの相対的な関係を表します。円グラフでは、各扇形の弧の長さ(および中心角と面積)は、その扇形が表す量の割合に比例します。これらの扇形を合わせると、ちょうど完全な円になります。名前のとおり、これらの扇形は切り分けられたパイ型の図を構成します。
円グラフtype属性はpie、type はグラフの種類を表します。
const config = {
type: 'pie',
data: data,
};
次に、簡単な円グラフを作成しましょう:
例
const ctx = document.getElementById('myChart');
const data = {
labels: [
'Red',
'Blue',
'Yellow'
],
datasets: [{
label: '円グラフの例',
data: [300, 50, 100],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 205, 86)'
],
hoverOffset: 4
}]
};
const config = {
type: 'pie',
data: data,
options: {
responsive: true, // グラフをレスポンシブに設定し、画面ウィンドウの変化に応じて変化させる
maintainAspectRatio: false,// グラフの元の比率を維持する
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
};
const myChart = new Chart(ctx, config);
const data = {
labels: [
'Red',
'Blue',
'Yellow'
],
datasets: [{
label: '円グラフの例',
data: [300, 50, 100],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 205, 86)'
],
hoverOffset: 4
}]
};
const config = {
type: 'pie',
data: data,
options: {
responsive: true, // グラフをレスポンシブに設定し、画面ウィンドウの変化に応じて変化させる
maintainAspectRatio: false,// グラフの元の比率を維持する
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
};
const myChart = new Chart(ctx, config);
試してみる »
上記の例の出力結果は次のとおりです:
その他の拡張