Skip to content

Commit 95c14a3

Browse files
committed
Collapse small colonies
1 parent c1f3405 commit 95c14a3

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

mcc/src/client/Dashboard/Dashboard.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import PieChart from '../components/dashboard/PieChart';
88
import BarChart from '../components/dashboard/BarChart';
99

1010
export function Dashboard() {
11-
const [demographics, setDemographics] = useState(null);
12-
const [living, setLiving] = useState(null);
13-
const [u24Assigned, setu24Assigned] = useState(null);
11+
const [demographics, setDemographics] = useState<[]>(null);
12+
const [living, setLiving] = useState<[]>(null);
13+
const [u24Assigned, setu24Assigned] = useState<[]>(null);
1414

1515
const ctx = getServerContext().getModuleContext('mcc') || {};
1616
const containerPath = ctx.MCCContainer || null;
@@ -90,7 +90,7 @@ export function Dashboard() {
9090
<div className="panel panel-default">
9191
<div className="panel-heading">Center (Living Animals)</div>
9292
<div className="panel-body">
93-
<PieChart fieldName = "colony" demographics={living} cutout = "30%" />
93+
<PieChart fieldName = "colony" demographics={living} cutout = "30%" collapseBelow = {0.025} />
9494
</div>
9595
</div>
9696
</div>

mcc/src/client/U24Dashboard/Dashboard.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import BarChart, { ColorType } from '../components/dashboard/BarChart';
88
import { ActiveElement, Chart, ChartEvent } from 'chart.js/dist/types/index';
99

1010
export function Dashboard() {
11-
const [demographics, setDemographics] = useState(null);
12-
const [living, setLiving] = useState(null);
13-
const [u24Assigned, setu24Assigned] = useState(null);
14-
const [availableForTransfer, setAvailableForTransfer] = useState(null);
15-
const [requestRows, setRequestRows] = useState(null);
16-
const [censusRows, setCensusRows] = useState(null);
17-
const [birthData, setBirthData ] = useState(null);
18-
const [breedingPairData, setBreedingPairData ] = useState(null);
11+
const [demographics, setDemographics] = useState<[]>(null);
12+
const [living, setLiving] = useState<[]>(null);
13+
const [u24Assigned, setu24Assigned] = useState<[]>(null);
14+
const [availableForTransfer, setAvailableForTransfer] = useState<[]>(null);
15+
const [requestRows, setRequestRows] = useState<[]>(null);
16+
const [censusRows, setCensusRows] = useState<[]>(null);
17+
const [birthData, setBirthData ] = useState<[]>(null);
18+
const [breedingPairData, setBreedingPairData ] = useState<[]>(null);
1919

2020
const ctx = getServerContext().getModuleContext('mcc') || {};
2121
const containerPath = ctx.MCCContainer || null;

mcc/src/client/components/dashboard/PieChart.tsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import React, { useEffect, useRef } from 'react';
2-
import {
3-
Chart,
4-
ArcElement,
5-
Legend,
6-
PieController,
7-
Tooltip
8-
} from 'chart.js';
2+
import { ArcElement, Chart, Legend, PieController, Tooltip } from 'chart.js';
93

104
Chart.register(ArcElement, Legend, PieController, Tooltip);
115

@@ -21,14 +15,12 @@ const colors = [
2115
"#999999"
2216
];
2317

24-
export default function PieChart(props) {
18+
export default function PieChart(props: {demographics: [], fieldName: string, cutout?: string, collapseBelow?: number }) {
2519
const canvas = useRef(null);
2620

27-
const { demographics } = props;
28-
const { fieldName } = props;
29-
const { cutout } = props || 0;
21+
const { demographics, fieldName, cutout = '0', collapseBelow = 0 } = props;
3022

31-
const collectedData = demographics.reduce((acc, curr) => {
23+
const collectedData = demographics.reduce((acc, curr) => {
3224
const value = curr[fieldName] === null ? 'Unknown' : curr[fieldName];
3325
if (acc[value]) {
3426
acc[value] = acc[value] + 1;
@@ -37,7 +29,31 @@ export default function PieChart(props) {
3729
}
3830

3931
return acc;
40-
}, {});
32+
}, new Map<string, bigint>())
33+
34+
if (collapseBelow) {
35+
const total = Object.keys(collectedData).reduce((sum, keyName) => {
36+
sum += collectedData[keyName]
37+
38+
return sum
39+
}, 0)
40+
41+
const otherValue = Object.keys(collectedData).reduce((sum, keyName) => {
42+
const val = collectedData[keyName]
43+
const fraction = val / total
44+
if (fraction < collapseBelow) {
45+
delete collectedData[keyName]
46+
sum += val
47+
}
48+
49+
return sum
50+
}, 0)
51+
52+
if (otherValue) {
53+
collectedData['Other'] = otherValue
54+
}
55+
}
56+
4157
const labels = Object.keys(collectedData).sort(Intl.Collator().compare);
4258
const data = labels.map(label => collectedData[label]);
4359

0 commit comments

Comments
 (0)