forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTooltipContent.js
More file actions
81 lines (75 loc) · 1.95 KB
/
CustomTooltipContent.js
File metadata and controls
81 lines (75 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import React from 'react'
import { Box, Grid, Text } from '@chakra-ui/react'
import { array, bool, func, string } from 'prop-types'
export const CustomTooltipContent = ({
label,
payload,
formatter,
calculatePercentages,
}) => {
const totalValue =
payload !== undefined
? payload.reduce((acc, currentValue) => {
return currentValue.payload[currentValue.dataKey] + acc
}, 0)
: 1
const listItems = []
if (payload !== undefined)
payload.forEach((entry) => {
if (entry !== undefined) {
listItems.push(
<Text
key={`${entry.dataKey}-name`}
color={entry.color}
>{`${entry.name} :`}</Text>,
)
listItems.push(
<Text
mx="1rem"
textAlign="right"
key={`${entry.dataKey}-value`}
color={entry.color}
>{`${formatter(entry.payload[entry.dataKey])}`}</Text>,
)
if (calculatePercentages) {
const percentage =
Math.round(
formatter((entry.payload[entry.dataKey] / totalValue) * 100),
) || 0
listItems.push(
<Text
textAlign="right"
key={`${entry.dataKey}-percent`}
color={entry.color}
>{`${percentage}%`}</Text>,
)
}
}
})
const columns = calculatePercentages ? 3 : 2
const templateColumns = `repeat(${columns}, auto)}`
return (
<Box
backgroundColor="white"
margin="0px"
padding="10px"
borderWidth="1px"
borderStyle="solid"
borderColor="#ccc"
>
<Text>{label}</Text>
<Grid templateColumns={templateColumns}>{listItems}</Grid>
</Box>
)
}
CustomTooltipContent.propTypes = {
formatter: func,
label: string,
payload: array,
calculatePercentages: bool,
}
CustomTooltipContent.defaultProps = {
separator: ' : ',
formatter: (value) => value,
calculatePercentages: false,
}