From 893ffc612bbdfc1701141acbcc04652407388abb Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 16 Apr 2022 17:13:00 -0500 Subject: [PATCH] Add legend to free response graph --- web/components/answers/answers-graph.tsx | 41 ++++++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/web/components/answers/answers-graph.tsx b/web/components/answers/answers-graph.tsx index 357662cd..1282b928 100644 --- a/web/components/answers/answers-graph.tsx +++ b/web/components/answers/answers-graph.tsx @@ -38,7 +38,8 @@ export const AnswersGraph = memo(function AnswersGraph(props: { const { width } = useWindowSize() - const labelLength = !width || width > 800 ? 50 : 20 + const isLargeWidth = !width || width > 800 + const labelLength = isLargeWidth ? 50 : 20 const endTime = resolutionTime || isClosed @@ -68,16 +69,15 @@ export const AnswersGraph = memo(function AnswersGraph(props: { answers?.find((answer) => answer.id === outcome)?.text ?? 'None' const answerText = answer.slice(0, labelLength) + (answer.length > labelLength ? '...' : '') - const id = `#${outcome}: ${answerText}` - return { id, data: points } + return { id: answerText, data: points } }) data.reverse() const yTickValues = [0, 25, 50, 75, 100] - const numXTickValues = !width || width < 800 ? 2 : 5 + const numXTickValues = isLargeWidth ? 5 : 2 const hoursAgo = latestTime.subtract(5, 'hours') const startDate = dayjs(contract.createdTime).isBefore(hoursAgo) ? new Date(contract.createdTime) @@ -87,8 +87,8 @@ export const AnswersGraph = memo(function AnswersGraph(props: { return (
= 800 ? 350 : 225) }} + className="w-full" + style={{ height: height ?? (isLargeWidth ? 350 : 250) }} > = 800} enableArea areaOpacity={1} - margin={{ top: 20, right: 28, bottom: 22, left: 40 }} + margin={{ top: 20, right: 0, bottom: 22, left: 40 }} + legends={[ + { + anchor: 'top-left', + direction: 'column', + justify: false, + translateX: isLargeWidth ? 5 : 2, + translateY: 0, + itemsSpacing: 0, + itemTextColor: 'black', + itemDirection: 'left-to-right', + itemWidth: isLargeWidth ? 288 : 138, + itemHeight: 20, + itemBackground: 'white', + symbolSize: 12, + symbolBorderColor: 'rgba(0, 0, 0, 0.5)', + symbolBorderWidth: 1, + effects: [ + { + on: 'hover', + style: { + itemBackground: 'rgba(255, 255, 255, 1)', + itemOpacity: 1, + }, + }, + ], + }, + ]} />
)