MonScoDocEssai/app/static/js/radar_bulletin.js

282 lines
9.0 KiB
JavaScript
Raw Normal View History

2020-09-26 16:19:37 +02:00
//
// Diagramme "radar" montrant les notes d'un étudiant
//
// ScoDoc, (c) E. Viennet 2012
//
// Ce code utilise d3.js
2021-06-21 18:46:35 +02:00
$().ready(function () {
2020-09-26 16:19:37 +02:00
var etudid = $("#etudid")[0].value;
var formsemestre_id = $("#formsemestre_id")[0].value;
get_notes_and_draw(formsemestre_id, etudid);
});
var WIDTH = 460; // taille du canvas SVG
var HEIGHT = WIDTH;
2021-06-21 18:46:35 +02:00
var CX = WIDTH / 2; // coordonnees centre du cercle
var CY = HEIGHT / 2;
var RR = 0.4 * WIDTH; // Rayon du cercle exterieur
2020-09-26 16:19:37 +02:00
/* Emplacements des marques (polygones et axe gradué) */
2021-06-21 18:46:35 +02:00
var R_TICS = [8, 10, 20]; /* [6, 8, 10, 12, 14, 16, 18, 20]; */
2020-09-26 16:19:37 +02:00
var R_AXIS_TICS = [4, 6, 8, 10, 12, 14, 16, 18, 20];
var NB_TICS = R_TICS.length;
function get_notes_and_draw(formsemestre_id, etudid) {
console.log("get_notes(" + formsemestre_id + ", " + etudid + " )");
/* Recupère le bulletin de note et extrait tableau de notes */
/*
var notes = [
{ 'module' : 'E1',
'note' : 13,
'moy' : 16 },
];
*/
2021-06-21 18:46:35 +02:00
var query = SCO_URL + "/Notes/formsemestre_bulletinetud?formsemestre_id=" + formsemestre_id + "&etudid=" + etudid + "&format=json&version=selectedevals&force_publishing=1"
$.get(query, '', function (bul) {
2020-09-26 16:19:37 +02:00
var notes = [];
2021-06-21 18:46:35 +02:00
bul.ue.forEach(
function (ue, i, ues) {
ue['module'].forEach(function (m, i) {
notes.push({
'code': m['code'],
'titre': m['titre'],
'note': m['note']['value'],
'moy': m['note']['moy']
});
});
});
draw_radar(notes);
2020-09-26 16:19:37 +02:00
});
}
function draw_radar(notes) {
/* Calcul coordonnées des éléments */
var nmod = notes.length;
2021-06-21 18:46:35 +02:00
var angle = 2 * Math.PI / nmod;
2020-09-26 16:19:37 +02:00
2021-06-21 18:46:35 +02:00
for (var i = 0; i < notes.length; i++) {
2020-09-26 16:19:37 +02:00
var d = notes[i];
2021-06-21 18:46:35 +02:00
var cx = Math.sin(i * angle);
var cy = - Math.cos(i * angle);
d["x_v"] = CX + RR * d.note / 20 * cx;
d["y_v"] = CY + RR * d.note / 20 * cy;
d["x_moy"] = CX + RR * d.moy / 20 * cx;
d["y_moy"] = CY + RR * d.moy / 20 * cy;
2020-09-26 16:19:37 +02:00
d["x_20"] = CX + RR * cx;
d["y_20"] = CY + RR * cy;
d["x_label"] = CX + (RR + 25) * cx - 10
d["y_label"] = CY + (RR + 25) * cy + 10;
d["tics"] = [];
// Coords des tics sur chaque axe
2021-06-21 18:46:35 +02:00
for (var j = 0; j < NB_TICS; j++) {
var r = R_TICS[j] / 20 * RR;
d["tics"][j] = { "x": CX + r * cx, "y": CY + r * cy };
2020-09-26 16:19:37 +02:00
}
}
var notes_circ = notes.slice(0);
notes_circ.push(notes[0])
2021-06-21 18:46:35 +02:00
var notes_circ_valid = notes_circ.filter(function (e, i, a) { return e.note != 'NA' && e.note != '-'; });
var notes_valid = notes.filter(function (e, i, a) { return e.note != 'NA' && e.note != '-'; })
2020-09-26 16:19:37 +02:00
/* Crée l'élément SVG */
g = d3.select("#radar_bulletin").append("svg")
.attr("class", "radar")
2021-06-21 18:46:35 +02:00
.attr("width", WIDTH + 100)
2020-09-26 16:19:37 +02:00
.attr("height", HEIGHT);
/* Centre */
2021-06-21 18:46:35 +02:00
g.append("circle").attr("cy", CY)
2020-09-26 16:19:37 +02:00
.attr("cx", CX)
.attr("r", 2)
.attr("class", "radar_center_mark");
/* Lignes "tics" */
2021-06-21 18:46:35 +02:00
for (var j = 0; j < NB_TICS; j++) {
var ligne_tics = d3.svg.line()
.x(function (d) { return d["tics"][j]["x"]; })
.y(function (d) { return d["tics"][j]["y"]; });
g.append("svg:path")
2020-09-26 16:19:37 +02:00
.attr("class", "radar_disk_tic")
2021-06-21 18:46:35 +02:00
.attr("id", "radar_disk_tic_" + R_TICS[j])
2020-09-26 16:19:37 +02:00
.attr("d", ligne_tics(notes_circ));
}
/* Lignes radiales pour chaque module */
g.selectAll("radar_rad")
.data(notes)
.enter().append("line")
.attr("x1", CX)
.attr("y1", CY)
2021-06-21 18:46:35 +02:00
.attr("x2", function (d) { return d["x_20"]; })
.attr("y2", function (d) { return d["y_20"]; })
2020-09-26 16:19:37 +02:00
.attr("class", "radarrad");
/* Lignes entre notes */
2021-06-21 18:46:35 +02:00
var ligne = d3.svg.line()
.x(function (d) { return d["x_v"]; })
.y(function (d) { return d["y_v"]; });
2020-09-26 16:19:37 +02:00
2021-06-21 18:46:35 +02:00
g.append("svg:path")
2020-09-26 16:19:37 +02:00
.attr("class", "radarnoteslines")
.attr("d", ligne(notes_circ_valid));
2021-06-21 18:46:35 +02:00
var ligne_moy = d3.svg.line()
.x(function (d) { return d["x_moy"]; })
.y(function (d) { return d["y_moy"]; })
2020-09-26 16:19:37 +02:00
2021-06-21 18:46:35 +02:00
g.append("svg:path")
2020-09-26 16:19:37 +02:00
.attr("class", "radarmoylines")
.attr("d", ligne_moy(notes_circ_valid));
/* Points (notes) */
g.selectAll("circle1")
.data(notes_valid)
.enter().append("circle")
2021-06-21 18:46:35 +02:00
.attr("cx", function (d) { return d["x_v"]; })
.attr("cy", function (d) { return d["y_v"]; })
.attr("r", function (x, i) { return 3; })
2020-09-26 16:19:37 +02:00
.style("stroke-width", 1)
.style("stroke", "black")
.style("fill", "blue")
2021-06-21 18:46:35 +02:00
.on("mouseover", function (d) {
var rwidth = 310;
var x = d["x_v"];
2020-09-26 16:19:37 +02:00
if ((x - CX) < 0) {
x = x + 5;
if (x + rwidth + 12 > WIDTH) {
x = WIDTH - rwidth - 12;
}
}
else {
if ((x - CX) > 0) {
x = x - rwidth - 5;
if (x < 12) {
x = 12;
}
}
else {
2021-06-21 18:46:35 +02:00
x = CX - rwidth / 2;
2020-09-26 16:19:37 +02:00
}
}
var yrect = d["y_v"];
var ytext = d["y_v"];
if ((yrect - CY) > 0) {
yrect = yrect - 5 - 20;
ytext = ytext - 5 - 20 + 16;
}
else {
yrect = yrect + 5;
ytext = ytext + 5 + 16;
2021-06-21 18:46:35 +02:00
}
var r = g.append("rect")
.attr('class', 'radartip')
.attr("x", x)
.attr("y", yrect);
var txt = g.append("text").text("Note: " + d.note + "/20, moyenne promo: " + d.moy + "/20")
.attr('class', 'radartip')
.attr("x", x + 5)
2020-09-26 16:19:37 +02:00
.attr("y", ytext);
2021-06-21 18:46:35 +02:00
r.attr("width", rwidth).attr("height", 20);
2020-09-26 16:19:37 +02:00
})
2021-06-21 18:46:35 +02:00
.on("mouseout", function (d) {
2020-09-26 16:19:37 +02:00
d3.selectAll(".radartip").remove()
});
/* Valeurs des notes */
g.selectAll("notes_labels")
.data(notes_valid)
.enter().append("text")
2021-06-21 18:46:35 +02:00
.text(function (d) { return d["note"]; })
.attr("x", function (d) {
return d["x_v"];
2020-09-26 16:19:37 +02:00
})
2021-06-21 18:46:35 +02:00
.attr("y", function (d) {
2020-09-26 16:19:37 +02:00
if (d["y_v"] > CY)
return d["y_v"] + 16;
else
return d["y_v"] - 8;
})
.attr("class", "note_label");
/* Petits points sur les moyennes */
g.selectAll("circle2")
.data(notes_valid)
.enter().append("circle")
2021-06-21 18:46:35 +02:00
.attr("cx", function (d) { return d["x_moy"]; })
.attr("cy", function (d) { return d["y_moy"]; })
.attr("r", function (x, i) { return 2; })
2020-09-26 16:19:37 +02:00
.style("stroke-width", 0)
.style("stroke", "black")
.style("fill", "rgb(20,90,50)");
/* Valeurs sur axe */
g.selectAll("textaxis")
2021-06-21 18:46:35 +02:00
.data(R_AXIS_TICS)
2020-09-26 16:19:37 +02:00
.enter().append("text")
.text(String)
.attr("x", CX - 10)
2021-06-21 18:46:35 +02:00
.attr("y", function (x, i) { return CY - x * RR / 20 + 6; })
2020-09-26 16:19:37 +02:00
.attr("class", "textaxis");
/* Noms des modules */
g.selectAll("text_modules")
.data(notes)
.enter().append("text")
2021-06-21 18:46:35 +02:00
.text(function (d) { return d['code']; })
.attr("x", function (d) { return d['x_label']; })
.attr("y", function (d) { return d['y_label']; })
2020-09-26 16:19:37 +02:00
.attr("dx", 0)
.attr("dy", 0)
2021-06-21 18:46:35 +02:00
.on("mouseover", function (d) {
2020-09-26 16:19:37 +02:00
var x = d["x_label"];
var yrect = d["y_label"];
var ytext = d["y_label"];
2021-06-21 18:46:35 +02:00
var titre = d['titre'].replace("&apos;", "'").substring(0, 64);
2020-09-26 16:19:37 +02:00
var rwidth = titre.length * 9; // rough estimate of string width in pixels
if ((x - CX) < 0) {
x = x + 5;
if (x + rwidth + 12 > WIDTH) {
x = WIDTH - rwidth - 12;
}
}
else {
if ((x - CX) > 0) {
x = x - rwidth - 5;
if (x < 12) {
x = 12;
}
}
else {
2021-06-21 18:46:35 +02:00
x = CX - rwidth / 2;
2020-09-26 16:19:37 +02:00
}
}
if ((yrect - CY) > 0) {
yrect = yrect - 5 - 20;
ytext = ytext - 5 - 20 + 16;
}
else {
yrect = yrect + 5;
ytext = ytext + 5 + 16;
2021-06-21 18:46:35 +02:00
}
2020-09-26 16:19:37 +02:00
var r = g.append("rect")
2021-06-21 18:46:35 +02:00
.attr('class', 'radartip')
.attr("x", x)
2020-09-26 16:19:37 +02:00
.attr("y", yrect)
.attr("height", 20)
.attr("width", rwidth);
2021-06-21 18:46:35 +02:00
var txt = g.append("text").text(titre)
.attr('class', 'radartip')
.attr("x", x + 5)
2020-09-26 16:19:37 +02:00
.attr("y", ytext);
})
2021-06-21 18:46:35 +02:00
.on("mouseout", function (d) {
2020-09-26 16:19:37 +02:00
d3.selectAll(".radartip").remove()
});
}