Skip to content

Commit a3dd846

Browse files
committed
Improve MCC plots
1 parent 42a34ee commit a3dd846

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

mcc/src/client/U24Dashboard/Dashboard.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React, { useEffect, useState } from 'react';
44
import { ActionURL, Filter, getServerContext, Query } from '@labkey/api';
55

66
import PieChart from '../components/dashboard/PieChart';
7-
import BarChart from '../components/dashboard/BarChart';
7+
import BarChart, { ColorType } from '../components/dashboard/BarChart';
88
import { ActiveElement, Chart, ChartEvent } from 'chart.js/dist/types/index';
99

1010
export function Dashboard() {
@@ -164,7 +164,7 @@ export function Dashboard() {
164164
</div>
165165
<div className="col-md-4">
166166
<div className="panel panel-default">
167-
<div className="panel-heading">Center (All Animals)</div>
167+
<div className="panel-heading">U24 Animals By Center</div>
168168
<div className="panel-body">
169169
<PieChart fieldName = "colony" demographics={demographics} cutout = "30%" />
170170
</div>
@@ -184,7 +184,7 @@ export function Dashboard() {
184184
<div className="panel panel-default">
185185
<div className="panel-heading">Age (Living Animals)</div>
186186
<div className="panel-body">
187-
<BarChart demographics={living} fieldName="Id/ageClass/label" groupField="gender/meaning" onClick={clickHandler} />
187+
<BarChart demographics={living} fieldName="Id/ageClass/label" groupField="gender/meaning" missingDataTerm="unknown" colorBy={ColorType.GROUP} showLegend={true} onClick={clickHandler} />
188188
</div>
189189
</div>
190190
</div>
@@ -194,15 +194,15 @@ export function Dashboard() {
194194
<div className="panel panel-default">
195195
<div className="panel-heading">U24 Births By Year</div>
196196
<div className="panel-body">
197-
<BarChart demographics={birthData} fieldName="centerName" groupField="yearNo" indexAxis="x"/>
197+
<BarChart demographics={birthData} fieldName="yearNo" groupField="centerName" colorBy={ColorType.GROUP} showLegend={true} indexAxis="x"/>
198198
</div>
199199
</div>
200200
</div>
201201
<div className="col-md-4">
202202
<div className="panel panel-default">
203203
<div className="panel-heading">U24 Breeding Pairs</div>
204204
<div className="panel-body">
205-
<BarChart demographics={breedingPairData} fieldName="centerName" groupField="yearNo" indexAxis="x" />
205+
<BarChart demographics={breedingPairData} fieldName="yearNo" groupField="centerName" colorBy={ColorType.GROUP} showLegend={true} indexAxis="x" />
206206
</div>
207207
</div>
208208
</div>

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ const colors = [
1616
"#999999"
1717
];
1818

19-
export default function BarChart(props: {demographics: [], fieldName: string, groupField?: string, indexAxis?: 'x' | 'y', onClick?: (event: ChartEvent, elements: ActiveElement[], chart: Chart) => void }) {
19+
export enum ColorType {
20+
PRIMARY = "primary",
21+
GROUP = "group"
22+
}
23+
24+
export default function BarChart(props: {demographics: [], fieldName: string, groupField?: string, colorBy?: ColorType, showLegend?: boolean, indexAxis?: 'x' | 'y', missingDataTerm?: string, onClick?: (event: ChartEvent, elements: ActiveElement[], chart: Chart) => void }) {
2025
const canvas = useRef(null);
2126

2227
const { demographics, fieldName, groupField, onClick} = props
28+
const { colorBy = ColorType.PRIMARY } = props
29+
const { showLegend = false } = props
30+
const { missingDataTerm = 'None' } = props
31+
2332
const indexAxis: 'x' | 'y' = props.indexAxis || 'y'
2433

2534
const collectedData = demographics.reduce((acc, curr: {}, idx) => {
2635
const value = curr[fieldName] === null ? 'Unknown' : curr[fieldName];
27-
const group = groupField == null ? 'counts' : curr[groupField] || 'None'
36+
const group = groupField == null ? 'counts' : curr[groupField] || missingDataTerm
2837

2938
if (!acc[group]) {
3039
acc[group] = {}
@@ -39,13 +48,15 @@ export default function BarChart(props: {demographics: [], fieldName: string, gr
3948
return acc;
4049
}, {});
4150

42-
const labels = [...new Set(Object.keys(collectedData).flatMap(groupName => Object.keys(collectedData[groupName])))];
43-
const dataArr: any[] = Object.keys(collectedData).map(groupName => {
51+
const labels = [...new Set(Object.keys(collectedData).flatMap(groupName => Object.keys(collectedData[groupName])))].sort(Intl.Collator().compare)
52+
const groupNames = Object.keys(collectedData).sort(Intl.Collator().compare)
53+
54+
const dataArr: any[] = groupNames.map(groupName => {
4455
const dat = labels.map(label => collectedData[groupName] ? collectedData[groupName][label] || 0 : 0)
4556
return {
4657
label: groupName,
4758
data: dat,
48-
backgroundColor: colors.slice(0, labels.length)
59+
backgroundColor: colorBy == ColorType.PRIMARY ? colors.slice(0, labels.length) : colors[groupNames.indexOf(groupName)]
4960
}
5061
});
5162

@@ -68,7 +79,8 @@ export default function BarChart(props: {demographics: [], fieldName: string, gr
6879
},
6980
plugins: {
7081
legend: {
71-
display: false
82+
display: showLegend,
83+
position: 'right'
7284
}
7385
}
7486
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function PieChart(props) {
3838

3939
return acc;
4040
}, {});
41-
const labels = Object.keys(collectedData).sort();
41+
const labels = Object.keys(collectedData).sort(Intl.Collator().compare);
4242
const data = labels.map(label => collectedData[label]);
4343

4444
useEffect(() => {

0 commit comments

Comments
 (0)