Skip to content

SAK-51241 Gradebook canvas for chartjs needs a container to get its correct width #13553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<body>
<wicket:panel>

<canvas wicket:id="gradingSchemaChart"></canvas>
<div class="container-fluid">
<canvas wicket:id="gradingSchemaChart"></canvas>
</div>

<div wicket:id="stats"></div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<body>
<wicket:panel>

<canvas wicket:id="gradingSchemaChart"></canvas>
<div class="container-fluid">
<canvas wicket:id="gradingSchemaChart"></canvas>
</div>

<div wicket:id="stats"></div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ <h2 class="accordion-header">
<!-- chart -->
<div>
<div wicket:id="noStudentsWithGradesMessage" class="sak-banner-info">There are no students with grades</div>
<canvas wicket:id="gradingSchemaChart" id="gradingSchemaChart" class="gb-schema-chart"></canvas>
<div class="container-fluid">
<canvas wicket:id="gradingSchemaChart" id="gradingSchemaChart" class="gb-schema-chart"></canvas>
</div>
</div>

<!-- stats -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<body>
<wicket:panel>

<canvas wicket:id="chart"></canvas>
<div class="container-fluid">
<canvas wicket:id="chart"></canvas>
</div>

<div>
<input type="button" class="button_color" wicket:id="done" wicket:message="value:button.done,aria-label:button.done" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<body>
<wicket:panel>

<canvas wicket:id="chart"></canvas>
<div class="container-fluid">
<canvas wicket:id="chart"></canvas>
</div>

<div>
<input type="button" class="button_color" wicket:id="done" wicket:message="value:button.done,aria-label:button.done" />
Expand Down
217 changes: 122 additions & 95 deletions gradebookng/tool/src/webapp/scripts/gradebook-chart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* GradebookNG Chart JS.
*
Expand All @@ -22,6 +21,17 @@ $(document).ready(function() {
chartYourGradeMessage: TrimPath.parseTemplate(
$("#chartYourGradeMessage").html().trim().toString()),
};

// Handle window resizes
let resizeTimer;
$(window).on('resize', function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
if (myChart) {
myChart.resize();
}
}, 250);
});
});

var myChart;
Expand All @@ -35,112 +45,129 @@ function renderChart(gbChartData) {
var xAxisLabel = chartData.xAxisLabel;
var yAxisLabel = chartData.yAxisLabel;

// Ensure the container is ready before initializing the chart
var ctx = $('#'+chartId);
myChart = new Chart(ctx, {
type: chartType,

options: {

title: {
display: true,
text: chartTitle,
fontSize: 18,
fontStyle: 'bold',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
},
legend: {
display: false
},
scales: {
xAxes: [{
ticks: {
beginAtZero:true,
fontStyle: 'bold',
autoSkip: true,
maxRotation: 0,
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
callback: function(value, index, values) {
if (isNaN(value)) {
return value;
}
// Display student values only as integers
else if (Math.floor(value) === value) {
return value;
}
}
},
scaleLabel: {
display: true,
labelString: xAxisLabel,
fontSize: 14,
fontFamily: 'Monospace',
fontStyle: 'bold',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
}
}],
yAxes: [{
ticks: {
beginAtZero:true,
fontStyle: 'bold',
fontFamily: 'Monospace',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
autoskip: true,
maxRotation: 0,
callback: function(value, index, values) {
if (isNaN(value)) {
// Include a space to even out the plusses and minuses
return value + (value.length < 2 ? ' ' : '');
}
// Display student values only as integers
else if (Math.floor(value) === value) {
return value;
}
}
},
scaleLabel: {
display: true,
labelString: yAxisLabel,
fontSize: 14,
fontStyle: 'bold',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
}
}]
},
tooltips: {
displayColors: false,
callbacks: {
title: function(tooltipItem, data) {
var chartMessage = GbChart.templates['chartStudentsGradeMessage'].process();
switch(chartType) {
case 'bar':
return chartMessage.replace('{0}', tooltipItem[0].yLabel).replace('{1}', tooltipItem[0].xLabel);
break;
case 'horizontalBar':
return chartMessage.replace('{0}', tooltipItem[0].xLabel).replace('{1}', tooltipItem[0].yLabel);
}

// Destroy existing chart if it exists
if (myChart) {
myChart.destroy();
}

},
label: function() {},
afterTitle: function(tooltipItem) {
if (typeof(window.studentGradeRange) !== "undefined") {
// Wait for Bootstrap to finish layout calculations
setTimeout(() => {
myChart = new Chart(ctx, {
type: chartType,

options: {
responsive: true,
maintainAspectRatio: true,
aspectRatio: 2,
animation: {
delay: 10,
duration: 10 // Add a small animation duration for smoother rendering
},

title: {
display: true,
text: chartTitle,
fontSize: 18,
fontStyle: 'bold',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
},
legend: {
display: false
},
scales: {
xAxes: [{
ticks: {
beginAtZero:true,
fontStyle: 'bold',
autoSkip: true,
maxRotation: 0,
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
callback: function(value, index, values) {
if (isNaN(value)) {
return value;
}
// Display student values only as integers
else if (Math.floor(value) === value) {
return value;
}
}
},
scaleLabel: {
display: true,
labelString: xAxisLabel,
fontSize: 14,
fontFamily: 'Monospace',
fontStyle: 'bold',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
}
}],
yAxes: [{
ticks: {
beginAtZero:true,
fontStyle: 'bold',
fontFamily: 'Monospace',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
autoskip: true,
maxRotation: 0,
callback: function(value, index, values) {
if (isNaN(value)) {
// Include a space to even out the plusses and minuses
return value + (value.length < 2 ? ' ' : '');
}
// Display student values only as integers
else if (Math.floor(value) === value) {
return value;
}
}
},
scaleLabel: {
display: true,
labelString: yAxisLabel,
fontSize: 14,
fontStyle: 'bold',
fontColor: getComputedStyle(document.documentElement).getPropertyValue('--sakai-text-color-1'),
}
}]
},
tooltips: {
displayColors: false,
callbacks: {
title: function(tooltipItem, data) {
var chartMessage = GbChart.templates['chartStudentsGradeMessage'].process();
switch(chartType) {
case 'bar':
if (window.studentGradeRange != null && window.studentGradeRange == tooltipItem[0].xLabel) {
return GbChart.templates['chartYourGradeMessage'].process();
}
return chartMessage.replace('{0}', tooltipItem[0].yLabel).replace('{1}', tooltipItem[0].xLabel);
break;
case 'horizontalBar':
if (window.studentGradeRange != null && window.studentGradeRange == tooltipItem[0].yLabel) {
return GbChart.templates['chartYourGradeMessage'].process();
return chartMessage.replace('{0}', tooltipItem[0].xLabel).replace('{1}', tooltipItem[0].yLabel);
}

},
label: function() {},
afterTitle: function(tooltipItem) {
if (typeof(window.studentGradeRange) !== "undefined") {
switch(chartType) {
case 'bar':
if (window.studentGradeRange != null && window.studentGradeRange == tooltipItem[0].xLabel) {
return GbChart.templates['chartYourGradeMessage'].process();
}
break;
case 'horizontalBar':
if (window.studentGradeRange != null && window.studentGradeRange == tooltipItem[0].yLabel) {
return GbChart.templates['chartYourGradeMessage'].process();
}
}
}
}
}
}
}
}
});
renderChartData(gbChartData);
});
renderChartData(gbChartData);
}, 0);
}

function renderChartData(gbChartData) {
Expand Down
Loading