-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (52 loc) · 1.49 KB
/
script.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
const dataInput = document.getElementById('dataInput');
const chartTypeSelect = document.getElementById('chartType');
const ctx = document.getElementById('myChart').getContext('2d');
let chart = null; // Store the chart object
function updateChart() {
// Clear any existing chart
if (chart) {
chart.destroy();
}
const dataString = dataInput.value;
const chartType = chartTypeSelect.value;
const data = dataString.split(',').map(Number);
// Chart configuration based on the selected type
const chartConfig = {
type: chartType,
data: {
labels: Array.from({ length: data.length }, (_, i) => i + 1), // Labels for data points
datasets: [{
label: 'My Data',
data: data,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1,
}]
},
options: {
animations: {
tension: {
duration: 1000,
easing: 'linear',
from: 1,
to: 0,
loop: true
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
}
}]
}
}
};
// Create a new chart instance
chart = new Chart(ctx, chartConfig);
}
// Update the chart on data input change or chart type selection
dataInput.addEventListener('change', updateChart);
chartTypeSelect.addEventListener('change', updateChart);
// Call updateChart to create the initial chart (empty)
updateChart();