fix: Correction erreur 'canvas is not defined' dans chart.min.js
- Ajout de 'const canvas = this.ctx' dans drawDoughnutChart - Ajout de 'const canvas = this.ctx' dans drawLineChart - Correction de la référence à la variable canvas dans les méthodes - Résolution de l'erreur ReferenceError lors du rendu des graphiques
This commit is contained in:
parent
1fd8ddf8b0
commit
0c8c0f1c39
48
services/web_interface/chart.min.js
vendored
48
services/web_interface/chart.min.js
vendored
@ -4,55 +4,56 @@ window.Chart = class Chart {
|
|||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
|
|
||||||
// Créer un canvas simple si Chart.js n'est pas disponible
|
// Créer un canvas simple si Chart.js n'est pas disponible
|
||||||
this.createSimpleChart();
|
this.createSimpleChart();
|
||||||
}
|
}
|
||||||
|
|
||||||
createSimpleChart() {
|
createSimpleChart() {
|
||||||
if (this.destroyed) return;
|
if (this.destroyed) return;
|
||||||
|
|
||||||
const canvas = this.ctx;
|
const canvas = this.ctx;
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
const config = this.config;
|
const config = this.config;
|
||||||
|
|
||||||
// Effacer le canvas
|
// Effacer le canvas
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
if (config.type === 'doughnut') {
|
if (config.type === 'doughnut') {
|
||||||
this.drawDoughnutChart(ctx, config);
|
this.drawDoughnutChart(ctx, config);
|
||||||
} else if (config.type === 'line') {
|
} else if (config.type === 'line') {
|
||||||
this.drawLineChart(ctx, config);
|
this.drawLineChart(ctx, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drawDoughnutChart(ctx, config) {
|
drawDoughnutChart(ctx, config) {
|
||||||
const data = config.data;
|
const data = config.data;
|
||||||
const labels = data.labels || [];
|
const labels = data.labels || [];
|
||||||
const values = data.datasets[0].data || [];
|
const values = data.datasets[0].data || [];
|
||||||
const colors = data.datasets[0].backgroundColor || ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF'];
|
const colors = data.datasets[0].backgroundColor || ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF'];
|
||||||
|
|
||||||
|
const canvas = this.ctx;
|
||||||
const centerX = canvas.width / 2;
|
const centerX = canvas.width / 2;
|
||||||
const centerY = canvas.height / 2;
|
const centerY = canvas.height / 2;
|
||||||
const radius = Math.min(centerX, centerY) - 20;
|
const radius = Math.min(centerX, centerY) - 20;
|
||||||
|
|
||||||
const total = values.reduce((sum, val) => sum + val, 0);
|
const total = values.reduce((sum, val) => sum + val, 0);
|
||||||
let currentAngle = 0;
|
let currentAngle = 0;
|
||||||
|
|
||||||
// Dessiner les segments
|
// Dessiner les segments
|
||||||
values.forEach((value, index) => {
|
values.forEach((value, index) => {
|
||||||
const sliceAngle = (value / total) * 2 * Math.PI;
|
const sliceAngle = (value / total) * 2 * Math.PI;
|
||||||
|
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.moveTo(centerX, centerY);
|
ctx.moveTo(centerX, centerY);
|
||||||
ctx.arc(centerX, centerY, radius, currentAngle, currentAngle + sliceAngle);
|
ctx.arc(centerX, centerY, radius, currentAngle, currentAngle + sliceAngle);
|
||||||
ctx.closePath();
|
ctx.closePath();
|
||||||
ctx.fillStyle = colors[index % colors.length];
|
ctx.fillStyle = colors[index % colors.length];
|
||||||
ctx.fill();
|
ctx.fill();
|
||||||
|
|
||||||
currentAngle += sliceAngle;
|
currentAngle += sliceAngle;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Dessiner la légende
|
// Dessiner la légende
|
||||||
let legendY = 20;
|
let legendY = 20;
|
||||||
labels.forEach((label, index) => {
|
labels.forEach((label, index) => {
|
||||||
@ -64,21 +65,22 @@ window.Chart = class Chart {
|
|||||||
legendY += 20;
|
legendY += 20;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
drawLineChart(ctx, config) {
|
drawLineChart(ctx, config) {
|
||||||
const data = config.data;
|
const data = config.data;
|
||||||
const labels = data.labels || [];
|
const labels = data.labels || [];
|
||||||
const values = data.datasets[0].data || [];
|
const values = data.datasets[0].data || [];
|
||||||
const color = data.datasets[0].borderColor || '#007bff';
|
const color = data.datasets[0].borderColor || '#007bff';
|
||||||
|
|
||||||
|
const canvas = this.ctx;
|
||||||
const padding = 40;
|
const padding = 40;
|
||||||
const chartWidth = canvas.width - 2 * padding;
|
const chartWidth = canvas.width - 2 * padding;
|
||||||
const chartHeight = canvas.height - 2 * padding;
|
const chartHeight = canvas.height - 2 * padding;
|
||||||
|
|
||||||
const maxValue = Math.max(...values);
|
const maxValue = Math.max(...values);
|
||||||
const minValue = Math.min(...values);
|
const minValue = Math.min(...values);
|
||||||
const valueRange = maxValue - minValue || 1;
|
const valueRange = maxValue - minValue || 1;
|
||||||
|
|
||||||
// Dessiner les axes
|
// Dessiner les axes
|
||||||
ctx.strokeStyle = '#ddd';
|
ctx.strokeStyle = '#ddd';
|
||||||
ctx.lineWidth = 1;
|
ctx.lineWidth = 1;
|
||||||
@ -87,36 +89,36 @@ window.Chart = class Chart {
|
|||||||
ctx.lineTo(padding, canvas.height - padding);
|
ctx.lineTo(padding, canvas.height - padding);
|
||||||
ctx.lineTo(canvas.width - padding, canvas.height - padding);
|
ctx.lineTo(canvas.width - padding, canvas.height - padding);
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
|
|
||||||
// Dessiner la ligne
|
// Dessiner la ligne
|
||||||
ctx.strokeStyle = color;
|
ctx.strokeStyle = color;
|
||||||
ctx.lineWidth = 2;
|
ctx.lineWidth = 2;
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
|
|
||||||
values.forEach((value, index) => {
|
values.forEach((value, index) => {
|
||||||
const x = padding + (index / (values.length - 1)) * chartWidth;
|
const x = padding + (index / (values.length - 1)) * chartWidth;
|
||||||
const y = canvas.height - padding - ((value - minValue) / valueRange) * chartHeight;
|
const y = canvas.height - padding - ((value - minValue) / valueRange) * chartHeight;
|
||||||
|
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
ctx.moveTo(x, y);
|
ctx.moveTo(x, y);
|
||||||
} else {
|
} else {
|
||||||
ctx.lineTo(x, y);
|
ctx.lineTo(x, y);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
|
|
||||||
// Dessiner les points
|
// Dessiner les points
|
||||||
ctx.fillStyle = color;
|
ctx.fillStyle = color;
|
||||||
values.forEach((value, index) => {
|
values.forEach((value, index) => {
|
||||||
const x = padding + (index / (values.length - 1)) * chartWidth;
|
const x = padding + (index / (values.length - 1)) * chartWidth;
|
||||||
const y = canvas.height - padding - ((value - minValue) / valueRange) * chartHeight;
|
const y = canvas.height - padding - ((value - minValue) / valueRange) * chartHeight;
|
||||||
|
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.arc(x, y, 4, 0, 2 * Math.PI);
|
ctx.arc(x, y, 4, 0, 2 * Math.PI);
|
||||||
ctx.fill();
|
ctx.fill();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Dessiner les labels
|
// Dessiner les labels
|
||||||
ctx.fillStyle = '#333';
|
ctx.fillStyle = '#333';
|
||||||
ctx.font = '10px Arial';
|
ctx.font = '10px Arial';
|
||||||
@ -126,14 +128,14 @@ window.Chart = class Chart {
|
|||||||
ctx.fillText(label, x, canvas.height - 10);
|
ctx.fillText(label, x, canvas.height - 10);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.destroyed = true;
|
this.destroyed = true;
|
||||||
if (this.ctx && this.ctx.clearRect) {
|
if (this.ctx && this.ctx.clearRect) {
|
||||||
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
if (!this.destroyed) {
|
if (!this.destroyed) {
|
||||||
this.createSimpleChart();
|
this.createSimpleChart();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user