Skip to content

Commit

Permalink
Add schooling count to map
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Feb 21, 2025
1 parent 1715cd9 commit dc1762a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 45 deletions.
105 changes: 74 additions & 31 deletions app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,80 @@ def index
.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 }
@amounts_per_academy = Pfmp
.in_state(:validated)
.joins({ schooling: { classe: :establishment } })
.group("establishments.academy_code")
.sum(:amount)
@schoolings_per_academy = Schooling
.joins(classe: :establishment)
.group("establishments.academy_code")
.count
@schoolings_per_academy = { "01" => 30_364,
"02" => 68_005,
"03" => 28_603,
"04" => 74_965,
"06" => 30_389,
"07" => 33_679,
"08" => 74_340,
"09" => 115_629,
"10" => 72_074,
"11" => 64_940,
"12" => 56_603,
"13" => 41_163,
"14" => 81_030,
"15" => 39_960,
"16" => 65_937,
"17" => 90_869,
"18" => 55_981,
"19" => 32_456,
"20" => 51_960,
"22" => 15_795,
"23" => 39_589,
"24" => 102_060,
"25" => 108_605,
"27" => 5224,
"28" => 35_332,
"31" => 11_329,
"32" => 14_353,
"33" => 18_422,
"40" => 508,
"41" => 310,
"42" => 266,
"43" => 14_597,
"44" => 195,
"70" => 77_195 }
@amounts_per_academy = { "01" => 8_182_195,
"02" => 17_760_710,
"03" => 7_453_000,
"04" => 21_159_275,
"06" => 8_270_190,
"07" => 9_172_010,
"08" => 19_829_765,
"09" => 28_590_700,
"10" => 19_192_040,
"11" => 17_818_590,
"12" => 15_073_770,
"13" => 10_506_095,
"14" => 21_728_710,
"15" => 9_716_290,
"16" => 17_673_220,
"17" => 24_343_820,
"18" => 15_491_725,
"19" => 8_587_015,
"20" => 12_952_985,
"22" => 3_948_640,
"23" => 10_327_355,
"24" => 28_287_885,
"25" => 28_182_105,
"27" => 1_544_630,
"28" => 10_092_775,
"31" => 2_965_100,
"32" => 3_800_170,
"33" => 4_312_560,
"43" => 2_716_880,
"44" => 51_785,
"70" => 20_503_580 }
end

def paid_pfmps_per_month
Expand Down
43 changes: 30 additions & 13 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 validées
#map-container{style: "width: 100%; height: 600px;", data: {controller: "map", validated_pfmps: @validated_pfmps_per_academy.to_json}}
%h2 Répartition géographique des montants des PFMPs
#map-container{style: "width: 100%; height: 600px;", data: {controller: "map", amounts: @amounts_per_academy.to_json, schoolings: @schoolings_per_academy.to_json}}

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

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

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

const strokeScale = d3.scaleLinear()
.domain([0, maxSchoolings])
.range([0.5, 2]);

const strokeColorScale = d3.scaleLinear()
.domain([0, maxSchoolings])
.range(["#ffbdbd", "#cd0000"]);

const svg = d3.select("#map-container")
.append("svg")
.attr("width", width)
Expand Down Expand Up @@ -54,18 +64,25 @@
.append("path")
.attr("d", path)
.attr("fill", d => {
const count = validatedPfmps[d.properties.CODE_ACAD] || 0;
return colorScale(count);
const amount = amounts[d.properties.CODE_ACAD] || 0;
return colorScale(amount);
})
.attr("stroke", d => {
const schoolingCount = schoolings[d.properties.CODE_ACAD] || 0;
return strokeColorScale(schoolingCount);
})
.attr("stroke-width", d => {
const schoolingCount = schoolings[d.properties.CODE_ACAD] || 0;
return strokeScale(schoolingCount);
})
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.attr("opacity", 0.7)
.on("mouseover", mouseOver)
.on("mouseout", mouseOut)
.on("mousemove", mouseMove);

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

d3.select(this)
.transition()
Expand All @@ -77,16 +94,16 @@
.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`);
.html(`${d.properties.LIBL_ACAD} (${d.properties.CODE_ACAD})<br>${amount.toLocaleString('fr-FR')} €<br>${schoolingCount} scolarités`);
}

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

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

tooltip.style("display", "none");
Expand Down
Loading

0 comments on commit dc1762a

Please sign in to comment.