@@ -19,6 +19,9 @@ export type BoundaryLayersProps = {
1919 code : string
2020}
2121
22+ const MIN_OPACITY = 0.1
23+ const MAX_OPACITY = 0.7
24+
2225export default function BoundaryLayers ( {
2326 election,
2427 code : parentCode ,
@@ -56,22 +59,41 @@ export default function BoundaryLayers({
5659 { childAreas . map ( ( _childArea ) => {
5760 const votes = getVotesByArea ( _childArea . code )
5861
59- // Calculate the winner
60- const winner = Object . keys ( candidates ) . reduce ( ( acc , id ) => {
61- if ( votes [ id ] > votes [ acc ] ) {
62- return id
63- }
64- return acc
65- } )
62+ // Prepare numeric votes for sorting
63+ const candidateIds = Object . keys ( candidates )
64+ const numericVotes = candidateIds . map ( ( id ) => ( {
65+ id,
66+ val : votes [ id ] ,
67+ } ) )
68+
69+ // Sort descending to get winner and runner-up
70+ numericVotes . sort ( ( a , b ) => b . val - a . val )
71+ const winnerId = numericVotes [ 0 ] ?. id
72+ const winnerVal = numericVotes [ 0 ] . val
73+ const runnerUpVal = numericVotes [ 1 ] ?. val ?? 0
74+
75+ // Total numeric votes
76+ const total = numericVotes . reduce ( ( s , x ) => s + x . val , 0 )
77+
78+ // Compute margin = winnerShare - runnerUpShare, in [0,1]
79+ const winnerShare = winnerVal / total
80+ const runnerUpShare = runnerUpVal / total
81+ const margin = Math . max ( 0 , winnerShare - runnerUpShare )
82+
83+ // Emphasize large margins using power > 1 (exponent = 2)
84+ const EXPONENT = 2
85+ const transformed = Math . max ( 0 , Math . min ( 1 , margin ) ) ** EXPONENT
6686
6787 return (
6888 < AreaBoundary
6989 area = { childArea }
7090 key = { _childArea . code }
7191 code = { _childArea . code }
7292 pathOptions = { {
73- color : `var(--chart-${ candidates [ winner ] . nomor_urut } )` ,
74- fillOpacity : 0.3 ,
93+ color : `var(--chart-${ candidates [ winnerId ] . nomor_urut } )` ,
94+ fillColor : `var(--chart-${ candidates [ winnerId ] . nomor_urut } )` ,
95+ fillOpacity :
96+ MIN_OPACITY + ( MAX_OPACITY - MIN_OPACITY ) * transformed ,
7597 } }
7698 eventHandlers = { {
7799 add : ( e ) => {
0 commit comments