forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarClick.js
96 lines (89 loc) · 2.36 KB
/
BarClick.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import IconButton from '@mui/material/IconButton';
import UndoOutlinedIcon from '@mui/icons-material/UndoOutlined';
import { BarChart } from '@mui/x-charts/BarChart';
import { HighlightedCode } from '@mui/docs/HighlightedCode';
const barChartsParams = {
series: [
{
id: 'series-1',
data: [3, 4, 1, 6, 5],
label: 'A',
stack: 'total',
highlightScope: {
highlight: 'item',
},
},
{
id: 'series-2',
data: [4, 3, 1, 5, 8],
label: 'B',
stack: 'total',
highlightScope: {
highlight: 'item',
},
},
{
id: 'series-3',
data: [4, 2, 5, 4, 1],
label: 'C',
highlightScope: {
highlight: 'item',
},
},
],
xAxis: [{ data: ['0', '3', '6', '9', '12'], id: 'axis1' }],
height: 400,
};
export default function BarClick() {
const [itemData, setItemData] = React.useState();
const [axisData, setAxisData] = React.useState();
return (
<Stack
direction={{ xs: 'column', md: 'row' }}
spacing={{ xs: 0, md: 4 }}
sx={{ width: '100%' }}
>
<Box sx={{ flexGrow: 1 }}>
<BarChart
{...barChartsParams}
onItemClick={(event, d) => setItemData(d)}
onAxisClick={(event, d) => setAxisData(d)}
/>
</Box>
<Stack direction="column" sx={{ width: { xs: '100%', md: '40%' } }}>
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Typography>Click on the chart</Typography>
<IconButton
aria-label="reset"
size="small"
onClick={() => {
setItemData(undefined);
setAxisData(null);
}}
>
<UndoOutlinedIcon fontSize="small" />
</IconButton>
</Box>
<HighlightedCode
code={`// Data from item click
${itemData ? JSON.stringify(itemData, null, 2) : '// The data will appear here'}
// Data from axis click
${axisData ? JSON.stringify(axisData, null, 2) : '// The data will appear here'}
`}
language="json"
copyButtonHidden
/>
</Stack>
</Stack>
);
}