Skip to content

Commit

Permalink
Inject pfmps data
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Feb 20, 2025
1 parent c15f8c7 commit 70e2132
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 66 deletions.
36 changes: 36 additions & 0 deletions app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ def index
@total_paid = PaidPfmp.paid.sum(:amount)
@total_paid_students = PaidPfmp.paid.distinct.count(:student_id)
@total_paid_pfmps = PaidPfmp.paid.count
@validated_pfmps_per_academy = Pfmp
.in_state(:validated)
.joins(schooling: { classe: :establishment })
.group("establishments.academy_code")
.count
@validated_pfmps_per_academy = { "01" => 28_090,
"02" => 69_465,
"03" => 30_502,
"04" => 84_886,
"06" => 33_694,
"07" => 36_069,
"08" => 77_249,
"09" => 116_251,
"10" => 74_350,
"11" => 68_924,
"12" => 58_670,
"13" => 43_849,
"14" => 93_114,
"15" => 37_850,
"16" => 70_142,
"17" => 101_298,
"18" => 61_016,
"19" => 35_189,
"20" => 52_861,
"22" => 16_792,
"23" => 39_471,
"24" => 100_697,
"25" => 101_490,
"27" => 5952,
"28" => 40_150,
"31" => 11_239,
"32" => 14_327,
"33" => 16_709,
"43" => 9772,
"44" => 219,
"70" => 85_919 }
end

def paid_pfmps_per_month
Expand Down
85 changes: 50 additions & 35 deletions app/views/stats/_map.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.stat
%h2 Répartition géographique des PFMPs
#map-container{style: "width: 100%; height: 600px;", data: {controller: "map"}}
%h2 Répartition géographique des PFMPs validées
#map-container{style: "width: 100%; height: 600px;", data: {controller: "map", validated_pfmps: @validated_pfmps_per_academy.to_json}}

:javascript
const initMap = () => {
Expand All @@ -13,6 +13,13 @@
const width = container.offsetWidth;
const height = 600;

const validatedPfmps = JSON.parse(container.dataset.validatedPfmps);
const maxCount = Math.max(...Object.values(validatedPfmps));

const colorScale = d3.scaleLinear()
.domain([0, maxCount])
.range(["#bccdff", "#000091"]);

const svg = d3.select("#map-container")
.append("svg")
.attr("width", width)
Expand Down Expand Up @@ -41,47 +48,55 @@
.projection(projection);

d3.json("/data/academies.geojson").then(function(geojson) {
const nbScoExtent = d3.extent(geojson.features, d => d.properties.NB_SCO);
const colorScale = d3.scaleLinear()
.domain(nbScoExtent)
.range(["#bccdff", "#000091"]);

g.selectAll("path")
.data(geojson.features)
.enter()
.append("path")
.attr("d", path)
.attr("fill", d => colorScale(d.properties.NB_SCO))
.attr("fill", d => {
const count = validatedPfmps[d.properties.CODE_ACAD] || 0;
return colorScale(count);
})
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.attr("opacity", 0.7)
.on("mouseover", function(event, d) {
d3.select(this)
.transition()
.duration(200)
.attr("fill", "#88fdaa")
.attr("opacity", 1);

tooltip
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px")
.style("display", "block")
.html(`${d.properties.LIBL_ACAD} (${d.properties.CODE_ACAD})`);
})
.on("mouseout", function(event, d) {
d3.select(this)
.transition()
.duration(200)
.attr("fill", colorScale(d.properties.NB_SCO))
.attr("opacity", 0.7);

tooltip.style("display", "none");
})
.on("mousemove", function(event, d) {
tooltip
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
});
.on("mouseover", mouseOver)
.on("mouseout", mouseOut)
.on("mousemove", mouseMove);

function mouseOver(event, d) {
const count = validatedPfmps[d.properties.CODE_ACAD] || 0;

d3.select(this)
.transition()
.duration(200)
.attr("fill", "#88fdaa")
.attr("opacity", 1);

tooltip
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px")
.style("display", "block")
.html(`${d.properties.LIBL_ACAD} (${d.properties.CODE_ACAD})<br>${count} PFMPs validées`);
}

function mouseOut(event, d) {
const count = validatedPfmps[d.properties.CODE_ACAD] || 0;

d3.select(this)
.transition()
.duration(200)
.attr("fill", colorScale(count))
.attr("opacity", 0.7);

tooltip.style("display", "none");
}

function mouseMove(event, d) {
tooltip
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
}
}).catch(function(error) {
console.error("Error loading the geo file:", error);
});
Expand Down
Loading

0 comments on commit 70e2132

Please sign in to comment.