Skip to content

Commit e87521c

Browse files
authored
Merge branch 'dev' into time-filter-fix
2 parents 015f1fd + cdc9257 commit e87521c

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

tcf_website/static/course/javascript/course_data.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
let barConfig;
22
let pieConfig;
33
let myChart;
4-
let totalSum;
54
const ctx = document.getElementById("myChart");
65

76
function togglePieChart() {
@@ -57,9 +56,6 @@ const loadData = (data) => {
5756
dfw,
5857
];
5958

60-
// Calculate total number of students
61-
totalSum = grades_data.reduce((total, num) => total + num, 0);
62-
6359
// Create default pie chart
6460
/* eslint-enable camelcase */
6561
createChart(grades_data);
@@ -241,33 +237,46 @@ const createChart = (gradesData) => {
241237
},
242238
},
243239
};
240+
// eslint-disable-next-line no-new,no-undef
241+
Chart.register(ChartDataLabels);
244242

245-
// Generate configuration for Pie Chart
246243
pieConfig = {
247244
type: "doughnut",
248245
data: chartData,
249246
options: {
250247
maintainAspectRatio: false,
251-
tooltips: {
252-
callbacks: {
253-
label: function (tooltipItem, data) {
254-
const dataset = data.datasets[0];
255-
const percent = Math.round(
256-
(dataset.data[tooltipItem.index] / totalSum) * 100,
257-
);
258-
let label = data.labels[tooltipItem.index];
259-
if (tooltipItem.index === 9) {
260-
label = "D/F/Withdraw";
261-
}
262-
return label + ": " + percent + "%";
263-
},
264-
},
265-
displayColors: false,
266-
},
267248
plugins: {
268249
legend: {
269250
display: false,
270251
},
252+
datalabels: {
253+
color: "#fff",
254+
formatter: (value, context) => {
255+
const dataset = context.chart.data.datasets[0];
256+
const total = dataset.data.reduce((acc, num) => acc + num, 0);
257+
const percentage = (value / total) * 100;
258+
return percentage > 5
259+
? context.chart.data.labels[context.dataIndex]
260+
: "";
261+
},
262+
font: {
263+
size: 14,
264+
},
265+
anchor: "center",
266+
align: "center",
267+
},
268+
tooltip: {
269+
callbacks: {
270+
label: function (tooltipItem) {
271+
const dataset = tooltipItem.dataset;
272+
const total = dataset.data.reduce((acc, num) => acc + num, 0);
273+
const value = dataset.data[tooltipItem.dataIndex];
274+
const percent = ((value / total) * 100).toFixed(1);
275+
return `${value} (${percent}%)`;
276+
},
277+
},
278+
displayColors: false,
279+
},
271280
labels: {
272281
// render 'label', 'value', 'percentage', 'image' or custom function, default is 'percentage'
273282
render: "label",

tcf_website/templates/course/course_professor.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ <h3 class="reviews-heading mb-0 mt-1 mr-4">{{ num_reviews }} {% if num_reviews =
321321

322322
<!-- For grade data -->
323323
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
324-
<script type="module"
325-
src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js"></script>
324+
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
325+
326326
<script type="module" src="{% static 'course/javascript/course_data.js' %}"></script>
327327
<script type="module">
328328
// Run loaddata function from above script

0 commit comments

Comments
 (0)