fix: results

pull/100/head
Pierre-Louis Guhur 1 year ago
parent c72315ebec
commit 058523aa1d

@ -58,10 +58,12 @@ const GradeBar = ({index, grade, size, params}: GradeBarInterface) => {
const DashedMedian = () => {
return (
<div
className="position-relative d-flex justify-content-center"
style={{top: '60px', height: '50px'}}
className="d-flex justify-content-center"
style={{marginTop: '-40px', height: '50px'}}
>
<div className="border h-100 border-1 border-dark border-opacity-75 border-dashed"></div>
<div className="border h-100 border-1 border-dark border-opacity-75 border-dashed position-relative"
></div>
</div>
);
};
@ -134,11 +136,13 @@ const MeritProfileBar = ({profile, grades}: MeritProfileBarInterface) => {
// find the majority grade
const majorityValue = getMajorityGrade(normalized);
const majorityGrade = gradesByValue[majorityValue];
console.log(majorityGrade, majorityValue, grades, normalized)
const proponentSizes = values
.filter((v) => v > majorityGrade.value)
.map((v) => normalized[v]);
const proponentWidth = proponentSizes.reduce((a, b) => a + b, 0);
console.log(proponentWidth, proponentSizes)
const opponentSizes = values
.filter((v) => v < majorityGrade.value)
@ -161,7 +165,6 @@ const MeritProfileBar = ({profile, grades}: MeritProfileBarInterface) => {
return (
<>
<DashedMedian />
<MajorityGrade
grade={majorityGrade}
left={proponentWidth + normalized[majorityValue] / 2}
@ -175,6 +178,7 @@ const MeritProfileBar = ({profile, grades}: MeritProfileBarInterface) => {
>
{values
.filter((v) => v > majorityGrade.value)
.reverse()
.map((v) => {
const index = values.indexOf(v);
const size =
@ -242,6 +246,7 @@ const MeritProfileBar = ({profile, grades}: MeritProfileBarInterface) => {
)}
</div>
{/* <div className='median dash'> </div> */}
<DashedMedian />
</>
);
};

@ -71,6 +71,7 @@ export async function getServerSideProps({query, locale}) {
const values = grades.map((g) => g.value);
values.forEach((v) => (profile[v] = profile[v] || 0));
const majValue = getMajorityGrade(profile);
console.log(profile, majValue)
return {
...c,
meritProfile: payload.merit_profile[c.id],

@ -2,28 +2,29 @@
* A few useful function for dealing with majority judgment
*/
import { MeritProfileInterface } from './type';
import {MeritProfileInterface} from './type';
/**
* Return the index corresponding to the majority grade
*/
export const getMajorityGrade = (profile: MeritProfileInterface): number => {
const indices = Object.keys(profile);
const grades = Object.keys(profile).map(k => parseInt(k)).sort()
if (grades.length === 0) {
throw new Error('Merit profile is empty');
}
console.log("INDICES", grades);
const numVotes = Object.values(profile).reduce((a, b) => a + b, 0);
let majorityGrade = indices[0];
let majorityGrade = grades[0];
let accBefore = 0;
let isBefore = true;
for (const value of indices) {
if (isBefore) {
accBefore += profile[value];
}
if (isBefore && accBefore > numVotes / 2) {
majorityGrade = value;
accBefore -= profile[value];
isBefore = false;
for (const grade of grades) {
if (accBefore + profile[grade] > numVotes / 2 - 1e-5) {
return grade;
}
accBefore -= profile[grade];
}
const value = indices.indexOf(majorityGrade);
return value;
return grades[grades.length - 1];
};

Loading…
Cancel
Save