Skip to content

Commit

Permalink
Merge pull request #120 from TNG/bar-chart
Browse files Browse the repository at this point in the history
Bar chart
  • Loading branch information
andreashille committed Sep 22, 2023
2 parents a57f034 + b5dccfc commit 64cb05a
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 200 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
- name: Install dependencies
run: npm ci
- name: Run tests
Expand All @@ -34,7 +34,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
Expand Down
1 change: 1 addition & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<link rel="icon" type="image/png" href="icon_192.png" />
<link rel="apple-touch-icon" sizes="192x192" href="icon_192.png" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
<link rel="stylesheet" href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css" />
<title>Next Generation Scrum Poker</title>
</head>
<body>
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/Components/BarChart/BarChart.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.ct-label {
font-size: 16px;
color: var(--text-primary);
}

.ct-grid {
stroke: var(--text-primary);
opacity: 50%;
}

.ct-horizontal {
display: none;
}

.ct-bar {
stroke: var(--primary) !important;
stroke-width: 48px;
}
5 changes: 5 additions & 0 deletions frontend/src/Components/BarChart/BarChart.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.chart {
height: 320px;
width: 640px;
max-width: 95vw;
}
26 changes: 26 additions & 0 deletions frontend/src/Components/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ChartistBarChart } from 'preact-chartist';
import { connectToWebSocket } from '../WebSocket/WebSocket';
import chartStyles from './BarChart.css?inline';
import classes from './BarChart.module.css';
import { useDeepCompareMemoize } from '../../helpers/helpers';
import { getChartVoteData } from './getChartVoteData';

export const BarChart = connectToWebSocket(({ socket }) => {
const { data, high } = useDeepCompareMemoize(getChartVoteData(socket.state));

return (
<>
<style>{chartStyles}</style>
<ChartistBarChart
data={data}
options={{
low: 0,
high,
axisY: { onlyInteger: true },
chartPadding: 16,
}}
className={classes.chart}
/>
</>
);
});
48 changes: 48 additions & 0 deletions frontend/src/Components/BarChart/getChartVoteData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CardValue, VOTE_COFFEE, VOTE_NOTE_VOTED, VOTE_OBSERVER } from '../../../../shared/cards';
import { getChartVoteData } from './getChartVoteData';

describe('The getChartVoteData function', () => {
it('accumulates votes correctly', () => {
const { data, high } = getChartVoteData({
votes: {
user1: '2',
user2: '2',
user3: '1',
user4: '2',
user5: '1',
user6: '0.5',
user7: '4',
},
scale: ['0', '0.5', '1', '2', '3', '4', '5'],
});
expect(data.series).toEqual([[1, 2, 3, 0, 1]]);
expect(data.labels).toEqual(['0.5', '1', '2', '3', '4']);
expect(high).toBe(3);
});

it.each<CardValue>([VOTE_COFFEE, VOTE_OBSERVER, VOTE_NOTE_VOTED])('ignores %j votes', (vote) => {
const { data, high } = getChartVoteData({
votes: {
user1: '1',
user2: vote,
user3: '?',
},
scale: ['0', '0.5', '1', '2', '3'],
});
expect(data.series).toEqual([[1, 1]]);
expect(data.labels).toEqual(['1', '?']);
expect(high).toBe(1);
});

it('returns full scale when no valid vote was given', () => {
const { data, high } = getChartVoteData({
votes: {
user1: VOTE_COFFEE,
},
scale: ['0', '0.5', '1', '2', '3', '4', '5'],
});
expect(data.series).toEqual([[]]);
expect(data.labels).toEqual(['0', '0.5', '1', '2', '3', '4', '5']);
expect(high).toBe(1);
});
});
47 changes: 47 additions & 0 deletions frontend/src/Components/BarChart/getChartVoteData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { CardValue, VOTE_COFFEE, VOTE_NOTE_VOTED, VOTE_OBSERVER } from '../../../../shared/cards';
import { WebSocketState } from '../../../../shared/serverMessages';

export const getChartVoteData = ({
votes,
scale,
}: Pick<WebSocketState, 'votes' | 'scale'>): {
data: { labels: CardValue[]; series: [number[]] };
high: number;
} => {
const labels = scale.filter(
(value) => ![VOTE_OBSERVER, VOTE_NOTE_VOTED, VOTE_COFFEE, '?'].includes(value),
);
const votesByValue = new Map(labels.map((value) => [value, 0]));
const otherVotesByValue = new Map<CardValue, number>();

for (const value of Object.values(votes)) {
if (labels.includes(value)) {
votesByValue.set(value, (votesByValue.get(value) ?? 0) + 1);
}

if (['?'].includes(value)) {
otherVotesByValue.set(value, (otherVotesByValue.get(value) ?? 0) + 1);
}
}

const accumulatedVotes = [...votesByValue.entries()];
const firstVoteIndex = accumulatedVotes.findIndex(([, votes]) => votes !== 0);
const lastVoteIndex = accumulatedVotes.findLastIndex(([, votes]) => votes !== 0);
const slicedAccumulatedVotes = [
...accumulatedVotes.slice(firstVoteIndex, lastVoteIndex + 1),
...otherVotesByValue.entries(),
];
const slicedLabels = slicedAccumulatedVotes.length
? [...labels.slice(firstVoteIndex, lastVoteIndex + 1), ...otherVotesByValue.keys()]
: [...labels, ...otherVotesByValue.keys()];

const data = slicedAccumulatedVotes.map(([, numberOfVotes]) => numberOfVotes);

return {
data: {
labels: slicedLabels,
series: [data],
},
high: Math.max(1, ...data),
};
};
65 changes: 0 additions & 65 deletions frontend/src/Components/PieChart/PieChart.tsx

This file was deleted.

26 changes: 0 additions & 26 deletions frontend/src/Components/PieChart/getPieChartVoteData.test.ts

This file was deleted.

47 changes: 0 additions & 47 deletions frontend/src/Components/PieChart/getPieChartVoteData.ts

This file was deleted.

4 changes: 4 additions & 0 deletions frontend/src/Components/ResultsPage/ResultsPage.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
color: var(--text-tertiary);
fill: var(--text-tertiary);
}

.loading {
line-height: 320px;
}
Loading

0 comments on commit 64cb05a

Please sign in to comment.