Created
August 9, 2022 09:56
-
-
Save HazemBZ/997544eceb9b0fcabed1e38ac17d46c1 to your computer and use it in GitHub Desktop.
Code snippet for Plotting 3D data (3D scatter plot) using react-typescript and plotly
This file contains hidden or 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
import { Data } from 'plotly.js' | |
import Plot from 'react-plotly.js' | |
interface WidgetProps { | |
x: number[] | |
y: number[] | |
z: number[] | |
} | |
function PlotWidget({ fileId = 434 }: { fileId: number }) { | |
const { id } = useOutletContext<ProjectType>() | |
const URL = `data/source` | |
const { data, isLoading } = useQuery<DevfileWidgetProps>('plot-data', () => { | |
return axios.get(URL).then(res => res.data) | |
}) | |
const plotData: Data[] = useMemo(() => { | |
return [ | |
{ | |
...data, | |
name: '3D data viewer', | |
mode: 'markers', | |
type: 'scatter3d', | |
marker: { | |
size: 12, | |
line: { | |
color: 'rgba(217, 217, 217, 0.14)', | |
width: 0.5, | |
}, | |
opacity: 0.8, | |
}, | |
}, | |
] | |
}, [data]) | |
const layout = { | |
margin: { | |
l: 0, | |
r: 0, | |
b: 0, | |
t: 0, | |
}, | |
} | |
if (isLoading) | |
return ( | |
<Card> | |
<Card.Header> | |
<Card.Title>3D Scatter</Card.Title> | |
</Card.Header> | |
<Card.Body> | |
<Card.LoadingDiv /> | |
</Card.Body> | |
</Card> | |
) | |
return ( | |
<Card> | |
<Card.Header> | |
<Card.Title>3D Scatter</Card.Title> | |
</Card.Header> | |
<Card.Body> | |
<Plot data={plotData} layout={layout} /> | |
</Card.Body> | |
</Card> | |
) | |
} | |
export default PlotWidget |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment