-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (109 loc) · 3.6 KB
/
script.js
File metadata and controls
118 lines (109 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
document.getElementById('uploadButton').addEventListener('click', () => {
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('Veuillez sélectionner un fichier !');
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
// Afficher le texte de chargement
document.getElementById('loading').classList.add('active');
reader.onload = (event) => {
const text = event.target.result;
const data = parseCSV(text);
renderChart(data);
// Masquer le texte de chargement
document.getElementById('loading').classList.remove('active');
};
reader.readAsText(file);
});
function parseCSV(text) {
const rows = text.split('\n').filter(row => row.trim() !== '');
const headers = rows[0].split(',').map(header => header.trim());
const data = rows.slice(1).map(row => {
const values = row.split(',').map(value => value.trim());
let rowData = {};
headers.forEach((header, index) => {
rowData[header] = values[index];
});
return rowData;
});
return { headers, data };
}
function renderChart(data) {
const labels = data.data.map(row => row.Date);
const datasets = data.headers.slice(1).map((label, index) => {
return {
label: label,
data: data.data.map(row => parseFloat(row[label] || 0)),
borderColor: `hsl(${Math.random() * 360}, 70%, 50%)`,
backgroundColor: `hsla(${Math.random() * 360}, 70%, 70%, 0.3)`,
borderWidth: 2,
fill: true,
tension: 0.1
};
});
const ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: datasets
},
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
tooltip: {
callbacks: {
label: function(context) {
const label = context.dataset.label || '';
if (label) {
return label + ': ' + context.raw;
}
return context.raw;
}
}
},
datalabels: {
display: true,
align: 'top',
anchor: 'end',
color: 'black',
font: {
weight: 'bold'
}
}
},
scales: {
x: {
ticks: {
autoSkip: true,
maxTicksLimit: 10
},
title: {
display: true,
text: 'Date'
}
},
y: {
beginAtZero: true,
ticks: {
callback: function(value) {
return value;
}
},
title: {
display: true,
text: 'Valeur'
}
}
}
}
});
}
function showError(message) {
alert(message);
}