[英]how to label axis within radar chart with chart.js
由於您使用的是Chart.js版本2.1.3,因此實現所需的內容非常簡單。
在任何圖表中(包括您正在使用的雷達),值上的標簽都存儲在options.scale.ticks
。
然后,如果要編輯它們的顯示方式,則必須使用Chart.js回調,如下所示:
var options = {
scale: {
ticks: {
beginAtZero: true,
userCallback: function (value, index, values) {
// Default callback
return value;
}
}
}
}
根據需要編輯return
值。
var ctx = document.getElementById("myChart").getContext("2d"); var notations = { 0: "", 0.5: "", 1: "no", 1.5: "", 2: "basic", 2.5: "", 3: "proficient", 3.5: "", 4: "great", 4.5: "", 5: "outstanding", } var data = { labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"], datasets: [{ label: "My First dataset", backgroundColor: "rgba(179,181,198,0.2)", borderColor: "rgba(179,181,198,1)", pointBackgroundColor: "rgba(179,181,198,1)", pointBorderColor: "#fff", pointHoverBackgroundColor: "#fff", pointHoverBorderColor: "rgba(179,181,198,1)", data: [3.25, 2.95, 4.5, 4.05, 2.8, 2.75, 2.0] }, { label: "My Second dataset", backgroundColor: "rgba(255,99,132,0.2)", borderColor: "rgba(255,99,132,1)", pointBackgroundColor: "rgba(255,99,132,1)", pointBorderColor: "#fff", pointHoverBackgroundColor: "#fff", pointHoverBorderColor: "rgba(255,99,132,1)", data: [1.4, 2.4, 2.0, 0.95, 4.8, 1.35, 5.0] }] }; var myChart = new Chart(ctx, { type: "radar", data: data, options: { scale: { ticks: { beginAtZero: true, userCallback: function(value, index, values) { return notations[value]; } } } } }); console.log(myChart);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.js"></script> <canvas id="myChart" width="400" height="400"></canvas>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.