前提・実現したいこと
ここに質問の内容を詳しく書いてください。
Chartjsでグラフを表示したいのですが表示されません。
表示する方法を教えていただけないでしょうか。
以下がコードです。
htmlとjsは同一フォルダに存在します。
■HTML(index.html)
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>chartjs</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script> </head> </html> <body> <div class="container"> <h2>Chart.js — Bar Chart Demo</h2> <div> <canvas id="myChart"></canvas> </div> <p id="caption">The chart is displaying apples & oranges</p> </div> <script type="text/javascript" src="index.js"></script> </body>■js
var ctx = document.getElementById("myChart").getContext('2d');
var original = Chart.defaults.global.legend.onClick;
Chart.defaults.global.legend.onClick = function(e, legendItem) {
update_caption(legendItem);
original.call(this, e, legendItem);
};
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["M", "T", "W", "T", "F", "S", "S"],
datasets: [{
label: 'apples',
backgroundColor: "rgba(153,255,51,1)",
data: [12, 19, 3, 17, 28, 24, 7],
}, {
label: 'oranges',
backgroundColor: "rgba(255,153,0,1)",
data: [30, 29, 5, 5, 20, 3, 10],
}]
}
});
var labels = {
"apples": true,
"oranges": true
};
var caption = document.getElementById("caption");
var update_caption = function(legend) {
labels[legend.text] = legend.hidden;
var selected = Object.keys(labels).filter(function(key) {
return labels[key];
});
var text = selected.length ? selected.join(" & ") : "nothing";
caption.innerHTML = "The chart is displaying " + text;
};
回答1件
あなたの回答
tips
プレビュー