【发布时间】:2021-06-26 23:13:25
【问题描述】:
我有以下 javascript Chartjs 代码,当我在 django 模板中使用它时效果很好。但是,此模板适用于仪表板,我需要重新创建 7 个相同的图表。因此,我可以将其重新配置为某种函数并调用它,而不是多次重新创建此代码?
var ctx_category_driving = document.getElementById('myChart_Category_Drving').getContext('2d');
var data = {
// labels: ["Chocolate", "Vanilla", "Strawberry"],
labels: {{ lbl_category_driving|safe }},
datasets: [
{
label: "acceptable",
// backgroundColor: "blue",
data: {{ data_cat_driving_acceptable|safe }},
fill: true,
// backgroundColor: "rgba(179,181,198,0.2)",
backgroundColor: "#3cba9f",
borderColor: "rgba(179,181,198,1)",
pointBorderColor: "#fff",
pointBackgroundColor: "rgba(179,181,198,1)",
},
{
label: "unacceptable",
// backgroundColor: "red",
data: {{ data_cat_driving_unacceptable|safe }},
fill: true,
// backgroundColor: "rgba(255,99,132,0.2)",
// backgroundColor: "rgba(216, 27, 96, 0.6)",
backgroundColor: "rgba(216, 27, 96, 0.6)",
borderColor: "rgba(255,99,132,1)",
pointBorderColor: "#fff",
pointBackgroundColor: "rgba(255,99,132,1)",
pointBorderColor: "#fff",
},
]
};
var myBarChart = new Chart(ctx_category_driving, {
type: 'bar',
data: data,
options: {
responsive: true,
maintainAspectRatio: false,
barValueSpacing: 20,
scales: {
yAxes: [{
ticks: {
min: 0,
}
}]
}
}
});
一旦重新配置,我将如何调用它以使其可重复使用?任何帮助都将不胜感激,因为我现在正在学习 Django 和各种网络技术。
【问题讨论】:
标签: javascript django function chart.js