-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from TNG/bar-chart
Bar chart
- Loading branch information
Showing
14 changed files
with
260 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.chart { | ||
height: 320px; | ||
width: 640px; | ||
max-width: 95vw; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
</> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
frontend/src/Components/PieChart/getPieChartVoteData.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,7 @@ | |
color: var(--text-tertiary); | ||
fill: var(--text-tertiary); | ||
} | ||
|
||
.loading { | ||
line-height: 320px; | ||
} |
Oops, something went wrong.