餅圖,或稱餅狀圖,是一個劃分為幾個扇形的圓形統計圖表,用于描述量、頻率或百分比之間的相對關系。在餅圖中,每個扇區的弧長(以及圓心角和面積)大小為其所表示的數量的比例。這些扇區合在一起剛好是一個完全的圓形。顧名思義,這些扇區拼成了一個切開的餅形圖案。
餅圖 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);
以上執行個體輸出結果為: