-
Notifications
You must be signed in to change notification settings - Fork 3
/
LinePlot.tsx
188 lines (174 loc) · 4.72 KB
/
LinePlot.tsx
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
Box,
Dialog,
DialogContent,
Typography,
useTheme
} from "@mui/material";
import DialogTitle from "../DialogTitle";
import { LinePlotProps } from "./LinePlot.types";
import Plotly from "react-plotly.js";
import React from "react";
const LinePlot = ({
fullscreenTitle = "",
title = "",
xdata = [],
ydata = [],
xlabel = "",
ylabel = "",
showMarkers = true,
showGrid = true
}: LinePlotProps) => {
// theme hook
const theme = useTheme();
// state for fullscreen
const [isFullscreen, setIsFullscreen] = React.useState(false);
// callback for fullscreen button
const handleClickFullscreen = () => {
setIsFullscreen(true);
};
// callback for closing fullscreen
const handleClose = () => {
setIsFullscreen(false);
};
// get config for plotly
const config = getConfig({ handleClickFullscreen, isFullscreen });
// determine whether to show plot title
const showTitle = title !== "";
return (
<ConditionalDialog
condition={isFullscreen}
onClose={handleClose}
dialogTitle={fullscreenTitle}
>
<Box
display="flex"
flexDirection="column"
overflow="hidden"
height="100%"
width="100%"
>
{!isFullscreen && showTitle ? (
<Typography
align="center"
style={{ padding: "0 16px", wordWrap: "break-word" }}
>
{title || ""}
</Typography>
) : null}
<Plotly
data={[
{
line: {
color: theme.palette.primary.main,
width: 2
},
marker: { color: theme.palette.primary.dark, size: 7 },
mode: showMarkers ? "lines+markers" : "lines",
type: "scatter",
x: xdata,
y: ydata
}
]}
layout={{
autosize: true,
font: {
family: "Montserrat, sans-serif"
},
margin: { b: 35, l: 80, r: 10, t: 30 },
paper_bgcolor: "rgba(0,0,0,0)",
plot_bgcolor: "rgba(0,0,0,0)",
xaxis: {
color: theme.palette.mode === "light" ? "" : "white",
exponentformat: "E",
gridcolor: theme.palette.mode === "light" ? "" : "grey",
showgrid: showGrid,
title: {
font: {
size: 12
},
text: xlabel || ""
}
},
yaxis: {
color: theme.palette.mode === "light" ? "black" : "white",
exponentformat: "E",
gridcolor: theme.palette.mode === "light" ? "" : "grey",
showgrid: showGrid,
ticksuffix: " ",
title: {
font: {
size: 12
},
text: ylabel || ""
}
}
}}
style={{ flexGrow: 1, height: "100%", width: "100%" }}
useResizeHandler={true}
config={config}
/>
</Box>
</ConditionalDialog>
);
};
export default LinePlot;
// svg path for fullscreen icon in plotly menu bar
const fullscreenIcon = {
height: 1792,
path: "M256 1408h1280v-768h-1280v768zm1536-1120v1216q0 66-47 113t-113 47h-1472q-66 0-113-47t-47-113v-1216q0-66 47-113t113-47h1472q66 0 113 47t47 113z",
width: 1792
};
type ConfigProps = {
isFullscreen: boolean;
handleClickFullscreen: () => void;
};
/**
* Returns a plotly config function with provided callback for clicking fullscreen
*/
const getConfig = ({ isFullscreen, handleClickFullscreen }: ConfigProps) => {
return {
displaylogo: false, // never display plotly logo
modeBarButtonsToAdd: !isFullscreen // if we are not in full screen, show a button to launch to fullscreen
? [
{
click: handleClickFullscreen,
direction: "up",
icon: fullscreenIcon,
name: "Fullscreen",
title: "Fullscreen"
}
]
: []
};
};
type ConditionalDialogProps = {
condition: boolean;
onClose: () => void;
children: React.ReactNode;
dialogTitle?: string;
};
/**
* Returns children in a fullscreen dialog if condition is true, otherwise just returns children
*/
function ConditionalDialog({
condition,
onClose,
children,
dialogTitle
}: ConditionalDialogProps) {
if (condition) {
return (
<Dialog maxWidth="xl" fullWidth open>
<DialogTitle onClose={onClose}>
<Box sx={dialogTitle ? {} : { p: 2 }}>{dialogTitle}</Box>
</DialogTitle>
<DialogContent dividers>
<Box style={{ height: "calc(100vh - 168px)" }}>{children}</Box>
</DialogContent>
</Dialog>
);
} else {
return children;
}
}