Last active
March 12, 2023 15:53
-
-
Save tony/f0938e379aef3c49648a2b1b63e00807 to your computer and use it in GitHub Desktop.
plotly.js react wrapper component (TypeScript, Functional Component, react-plotly.js alternative)
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 React from "react"; | |
import Plotly from "plotly.js/dist/plotly"; | |
export interface ChartProps { | |
data?: Plotly.Data[]; | |
layout: Partial<Plotly.Layout>; | |
frames?: Plotly.Frame[]; | |
config?: Partial<Plotly.Config>; | |
// react-specific | |
style?: React.CSSProperties; | |
} | |
export const Chart: React.FC<ChartProps> = ({ | |
style = {}, | |
data, | |
...props | |
}) => { | |
const ref = React.useRef<HTMLDivElement>(null); | |
React.useEffect(() => { | |
Plotly.react(ref.current, { data, ...props }); | |
}, [props, data]); | |
return <div ref={ref} style={style} />; | |
}; |
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
declare module "plotly.js/dist/plotly" { | |
export { Plotly as default } from "plotly.js"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made an issue for this at plotly/react-plotly.js#226
No need for
resizeHandler
as that's already in plotly.js as of plotly/plotly.js#2974 / Sep 7, 2018 / v1.41.0 / plotly/plotly.js@cfc720b byconfig = {{ responsive: true }}