-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance_chart.js
104 lines (89 loc) · 4.13 KB
/
performance_chart.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
fetch('model_data.json')
.then(response => response.json())
.then(data => {
const modelSelect = document.getElementById('model');
const engineSelect = document.getElementById('engine');
let carChart = null;
// Populate the models dropdown
const models = [...new Set(data.map(item => item.Model))];
models.forEach(model => {
const option = document.createElement('option');
option.value = model;
option.textContent = model;
modelSelect.appendChild(option);
});
modelSelect.addEventListener('change', function() {
const selectedModel = this.value;
engineSelect.innerHTML = '<option value="">Select an engine</option>';
const engines = data.filter(item => item.Model === selectedModel).map(item => item.Engine);
engines.forEach(engine => {
const option = document.createElement('option');
option.value = engine;
option.textContent = engine;
engineSelect.appendChild(option);
});
// Clear chart when model changes
if (carChart) {
carChart.destroy();
}
});
engineSelect.addEventListener('change', function() {
const selectedModel = modelSelect.value;
const selectedEngine = this.value;
const carData = data.find(item => item.Model === selectedModel && item.Engine === selectedEngine);
if (carData) {
const hp = [carData.HP, carData["Stage 1 HP"], carData["Stage 2 HP"]];
const nm = [carData.NM, carData["Stage 1 NM"], carData["Stage 2 NM"]];
/* const hpGains = [
carData.HP, // Standard
carData["Stage 1 HP"] - carData.HP, // Gain from Standard to Stage 1
carData["Stage 2 HP"] - carData["Stage 1 HP"] // Gain from Stage 1 to Stage 2
];
const nmGains = [
carData.NM, // Standard
carData["Stage 1 NM"] - carData.NM, // Gain from Standard to Stage 1
carData["Stage 2 NM"] - carData["Stage 1 NM"] // Gain from Stage 1 to Stage 2
]; */
// Render the chart
const ctx = document.getElementById('carChart').getContext('2d');
if (carChart) {
carChart.destroy(); // Clear the previous chart if it exists
}
carChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [
`Standard: ${carData.HP} HP - ${carData.NM} NM`,
`Stage 1: +${carData["Stage 1 HP"]} HP - +${carData["Stage 1 NM"]} NM`,
`Stage 2: +${carData["Stage 2 HP"]} HP - +${carData["Stage 2 NM"]} NM`
],
datasets: [
{
label: 'HP',
data: hp,
backgroundColor: 'rgba(160, 1, 1, 0.6)',
borderColor: 'black',
borderWidth: 1
},
{
label: 'NM',
data: nm,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
borderColor: 'black',
borderWidth: 1
}
]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
}
});
})
.catch(error => console.error('Error fetching data:', error));