-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
87 lines (74 loc) · 2.39 KB
/
main.js
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
let chartInstance = null;
document.getElementById('csvForm').addEventListener('submit', function(event) {
event.preventDefault();
const fileInput = document.getElementById('csvFile');
const chartType = document.getElementById('chartType').value;
if (fileInput.files.length === 0) {
alert('Please upload a CSV file');
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const csv = e.target.result;
const data = parseCSV(csv);
generateChart(data, chartType);
};
reader.readAsText(file);
});
function parseCSV(csv) {
const lines = csv.split('\n').filter(line => line.trim() !== '');
const headers = lines[0].split(',');
const data = lines.slice(1).map(line => {
const values = line.split(',');
return values.reduce((obj, value, index) => {
obj[headers[index]] = value;
return obj;
}, {});
});
return { headers, data };
}
function generateChart(data, chartType) {
const ctx = document.getElementById('csvChart').getContext('2d');
const headers = data.headers;
const dataPoints = data.data;
const labels = dataPoints.map(point => point[headers[0]]);
const datasets = headers.slice(1).map((header, index) => {
return {
label: header,
data: dataPoints.map(point => point[header]),
backgroundColor: `rgba(${index * 30}, 99, 132, 0.2)`,
borderColor: `rgba(${index * 30}, 99, 132, 1)`,
borderWidth: 1,
fill: false
};
});
if (chartInstance) {
chartInstance.destroy();
}
chartInstance = new Chart(ctx, {
type: chartType,
data: {
labels: labels,
datasets: datasets
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
}
document.getElementById('downloadBtn').addEventListener('click', function() {
if (chartInstance) {
const link = document.createElement('a');
link.href = chartInstance.toBase64Image();
link.download = 'chart.png';
link.click();
} else {
alert('Please generate a chart first');
}
});