CTOpsSpeedingReport / script.js
JOWilkins's picture
Let's add a chart using this data: Start Time End Time Driver Name Vehicle Name Location Max Speed (mph) Posted Speed Limit (mph) Severity Level
1a1d7bd verified
Raw History Blame Contribute Delete
4.05 kB
// Data for the dashboard (could be fetched from an API in a real application)
const fleetData = [
{
vehicle: "2017 F-350 King Ranch",
driver: "Luis Henriquez",
safetyScore: 95,
driveTime: "15:02:20",
distance: 422.4,
lightSpeeding: "0:25:06",
moderateSpeeding: "0:32:50",
heavySpeeding: "0:18:58",
severeSpeeding: "0:00:27",
maxSpeed: 85.74,
maxSpeedAt: "Nov 14 10:56PM CST",
lightEvents: 15,
moderateEvents: 35,
heavyEvents: 22,
severeEvents: 1
},
{
vehicle: "2022 F-250 CT Ops",
driver: "Jeffrey Perillo",
safetyScore: 89,
driveTime: "16:50:08",
distance: 656.1,
lightSpeeding: "4:03:16",
moderateSpeeding: "0:18:30",
heavySpeeding: "0:03:58",
severeSpeeding: "0:01:06",
maxSpeed: 80.15,
maxSpeedAt: "Nov 11 7:24AM CST",
lightEvents: 100,
moderateEvents: 32,
heavyEvents: 7,
severeEvents: 2
},
{
vehicle: "Toyota Highlander",
driver: "Luis Martinez",
safetyScore: 84,
driveTime: "6:50:38",
distance: 123.8,
lightSpeeding: "0:01:00",
moderateSpeeding: "0:04:50",
heavySpeeding: "0:05:21",
severeSpeeding: "0:00:40",
maxSpeed: 88.22,
maxSpeedAt: "Nov 15 4:56AM CST",
lightEvents: 1,
moderateEvents: 9,
heavyEvents: 9,
severeEvents: 1
}
];
// Calculate fleet statistics
function calculateFleetStats() {
const totalDistance = fleetData.reduce((sum, vehicle) => sum + vehicle.distance, 0);
const avgSafetyScore = fleetData.reduce((sum, vehicle) => sum + vehicle.safetyScore, 0) / fleetData.length;
return {
totalDistance: totalDistance.toFixed(1),
avgSafetyScore: avgSafetyScore.toFixed(1)
};
}
// Initialize the dashboard
// Speeding data for chart
const speedingData = {
labels: ['Nov 12', 'Nov 13', 'Nov 14', 'Nov 15', 'Nov 16'],
datasets: [
{
label: 'Light Speeding',
data: [12, 8, 5, 6, 2],
backgroundColor: 'rgba(234, 179, 8, 0.5)',
borderColor: 'rgba(234, 179, 8, 1)',
borderWidth: 1
},
{
label: 'Moderate Speeding',
data: [18, 15, 4, 7, 2],
backgroundColor: 'rgba(249, 115, 22, 0.5)',
borderColor: 'rgba(249, 115, 22, 1)',
borderWidth: 1
},
{
label: 'Heavy Speeding',
data: [14, 3, 2, 6, 1],
backgroundColor: 'rgba(239, 68, 68, 0.5)',
borderColor: 'rgba(239, 68, 68, 1)',
borderWidth: 1
},
{
label: 'Severe Speeding',
data: [1, 0, 0, 0, 0],
backgroundColor: 'rgba(220, 38, 38, 0.5)',
borderColor: 'rgba(220, 38, 38, 1)',
borderWidth: 1
}
]
};
document.addEventListener('DOMContentLoaded', () => {
const stats = calculateFleetStats();
console.log('Fleet statistics:', stats);
// Initialize chart
const ctx = document.getElementById('speedChart').getContext('2d');
new Chart(ctx, {
type: 'bar',
data: speedingData,
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
stacked: true,
grid: {
display: false
}
},
y: {
stacked: true,
beginAtZero: true,
ticks: {
stepSize: 5
}
}
},
plugins: {
legend: {
position: 'top',
},
tooltip: {
mode: 'index',
intersect: false
}
}
}
});
});