feat: Add responsive options to the Chart.js configuration

This commit is contained in:
Paul Gauthier (aider) 2024-09-26 17:44:50 -07:00
parent d1dd640577
commit 1099cda9e4

View file

@ -40,6 +40,12 @@ top coding models, as compared to their previous "solo" scores (striped bars).
} }
</style> </style>
<style>
#passRateChart {
max-width: 100%;
height: auto !important;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation@1.0.2"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation@1.0.2"></script>
{% assign sorted_data = site.data.architect | sort: "pass_rate_2" | reverse %} {% assign sorted_data = site.data.architect | sort: "pass_rate_2" | reverse %}
@ -47,6 +53,11 @@ top coding models, as compared to their previous "solo" scores (striped bars).
<script> <script>
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
var ctx = document.getElementById('passRateChart').getContext('2d'); var ctx = document.getElementById('passRateChart').getContext('2d');
// Function to determine aspect ratio based on screen width
function getAspectRatio() {
return window.innerWidth < 600 ? 1.5 : 2;
}
var labels = []; var labels = [];
var data = []; var data = [];
var colorMapping = { var colorMapping = {
@ -84,21 +95,22 @@ top coding models, as compared to their previous "solo" scores (striped bars).
data.reverse(); data.reverse();
backgroundColors.reverse(); backgroundColors.reverse();
borderColors.reverse(); borderColors.reverse();
new Chart(ctx, { var chart = new Chart(ctx, {
type: 'bar', type: 'bar',
data: { data: {
labels: labels, labels: labels,
datasets: [{ datasets: [{
label: 'Pass Rate', label: 'Pass Rate',
data: data, data: data,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1,
backgroundColor: backgroundColors, backgroundColor: backgroundColors,
borderColor: borderColors borderColor: borderColors,
borderWidth: 1
}] }]
}, },
options: { options: {
responsive: true,
maintainAspectRatio: true,
aspectRatio: getAspectRatio(),
scales: { scales: {
y: { y: {
beginAtZero: true, beginAtZero: true,
@ -186,6 +198,12 @@ top coding models, as compared to their previous "solo" scores (striped bars).
} }
} }
}}); }});
// Update aspect ratio on window resize
window.addEventListener('resize', function() {
chart.options.aspectRatio = getAspectRatio();
chart.resize();
});
}); });
function createStripePattern(baseColor) { function createStripePattern(baseColor) {