Tôi có đoạn mã sau trong tệp html Bootstrap đơn giản hiển thị biểu đồ đường Chart.js.Thêm biểu đồ đường Chart.js vào trang Jinja2/Flask từ tệp js
<div class="card-block chartjs">
<canvas id="line-chart" height="500"></canvas>
</div>
Các tập tin js chứa thiết lập của bảng xếp hạng như sau:
$(window).on("load", function(){
var ctx = $("#line-chart");
var chartOptions = {
responsive: true,
maintainAspectRatio: false,
legend: {
position: 'bottom',
},
hover: {
mode: 'label'
},
scales: {
xAxes: [{
display: true,
gridLines: {
color: "#f3f3f3",
drawTicks: false,
},
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
gridLines: {
color: "#f3f3f3",
drawTicks: false,
},
scaleLabel: {
display: true,
labelString: 'Value'
}
}]
},
title: {
display: true,
text: 'Chart.js Line Chart - Legend'
}
};
var chartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderDash: [5, 5],
borderColor: "#9C27B0",
pointBorderColor: "#9C27B0",
pointBackgroundColor: "#FFF",
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
pointRadius: 4,
}, {
label: "My Second dataset",
data: [28, 48, 40, 19, 86, 27, 90],
fill: false,
borderDash: [5, 5],
borderColor: "#00A5A8",
pointBorderColor: "#00A5A8",
pointBackgroundColor: "#FFF",
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
pointRadius: 4,
}, {
label: "My Third dataset - No bezier",
data: [45, 25, 16, 36, 67, 18, 76],
lineTension: 0,
fill: false,
borderColor: "#FF7D4D",
pointBorderColor: "#FF7D4D",
pointBackgroundColor: "#FFF",
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
pointRadius: 4,
}]
};
var config = {
type: 'line',
options : chartOptions,
data : chartData
};
var lineChart = new Chart(ctx, config);
});
Tôi muốn tránh sử dụng một tập tin javascript riêng biệt và khá chỉ có tất cả mọi thứ trong trang html Jinja2/Flask tôi. Một ví dụ làm việc có thể được tìm thấy trong this tutorial, đây là cùng một cách mà tôi muốn làm theo. Tôi đã cố gắng để sao chép bất kỳ dán phần js vào trang html của tôi và đặt giữa <script>
thẻ, nhưng tiếc là nó không hoạt động.
Sau đây là cách tôi đã cố gắng:
# in my jinja2/flask html page
<div class="card-body collapse in">
<div class="card-block chartjs">
<canvas id="line-chart" height="500"></canvas>
</div>
</div>
<script>
var ctx = $("#line-chart");
var chartOptions = {
responsive: true,
maintainAspectRatio: false,
legend: {
position: 'bottom',
},
hover: {
mode: 'label'
},
scales: {
xAxes: [{
display: true,
gridLines: {
color: "#f3f3f3",
drawTicks: false,
},
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
gridLines: {
color: "#f3f3f3",
drawTicks: false,
},
scaleLabel: {
display: true,
labelString: 'Value'
}
}]
},
title: {
display: true,
text: 'Chart.js Line Chart - Legend'
}
};
// Chart Data
var chartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderDash: [5, 5],
borderColor: "#9C27B0",
pointBorderColor: "#9C27B0",
pointBackgroundColor: "#FFF",
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
pointRadius: 4,
}, {
label: "My Second dataset",
data: [28, 48, 40, 19, 86, 27, 90],
fill: false,
borderDash: [5, 5],
borderColor: "#00A5A8",
pointBorderColor: "#00A5A8",
pointBackgroundColor: "#FFF",
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
pointRadius: 4,
}, {
label: "My Third dataset - No bezier",
data: [45, 25, 16, 36, 67, 18, 76],
lineTension: 0,
fill: false,
borderColor: "#FF7D4D",
pointBorderColor: "#FF7D4D",
pointBackgroundColor: "#FFF",
pointBorderWidth: 2,
pointHoverBorderWidth: 2,
pointRadius: 4,
}]
};
var config = {
type: 'line',
// Chart Options
options : chartOptions,
data : chartData
};
// Create the chart
var lineChart = new Chart(ctx, config);
});
</script>
Đáng tiếc là tôi không quá quen thuộc với JS và không có nhiều ý tưởng về những gì tôi nên làm gì để hiển thị biểu đồ trong ứng dụng Flask tôi. Tôi thực sự sẽ đánh giá cao nếu ai đó có thể cho tôi thấy những sửa đổi cần thiết mà tôi cần thực hiện để làm cho nó hoạt động.
Vui lòng cung cấp nhật ký từ bảng điều khiển gỡ lỗi trình duyệt của bạn (hoặc nêu rõ là không có). Xem [bài viết này dành cho Firefox] (https://developer.mozilla.org/en/docs/Tools/Browser_Console) và [bài viết này dành cho Chrome] (https://developers.google.com/web/tools/chrome- devtools/console /). Chỉnh sửa: Bạn đã đính kèm tất cả các tập lệnh được yêu cầu (Chart.js, jQuery) chưa? – krassowski
'nhưng tiếc là nó không hoạt động' những gì không hoạt động? Bất kỳ lỗi nào bạn thấy trong bảng điều khiển phát triển của Chrome hay bất kỳ điều gì khác? –
Vui lòng cung cấp mẫu jinja2 đầy đủ, chúng tôi cần biết nếu bạn đã nhập tất cả các tập lệnh mà bạn cần. –