-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathGrapherFigureView.tsx
47 lines (44 loc) · 1.44 KB
/
GrapherFigureView.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
import { useRef } from "react"
import { Grapher, GrapherProgrammaticInterface } from "@ourworldindata/grapher"
import {
ADMIN_BASE_URL,
BAKED_GRAPHER_URL,
DATA_API_URL,
} from "../settings/clientSettings.js"
import { useElementBounds } from "./hooks.js"
// Wrapper for Grapher that uses css on figure element to determine the bounds
export const GrapherFigureView = ({
grapher,
extraProps,
}: {
grapher: Grapher
extraProps?: Partial<GrapherProgrammaticInterface>
}) => {
const base = useRef<HTMLDivElement>(null)
const bounds = useElementBounds(base)
const grapherProps: GrapherProgrammaticInterface = {
...grapher.toObject(),
isEmbeddedInADataPage: grapher.isEmbeddedInADataPage,
bindUrlToWindow: grapher.props.bindUrlToWindow,
queryStr: grapher.props.bindUrlToWindow
? window.location.search
: undefined,
bounds,
dataApiUrl: DATA_API_URL,
enableKeyboardShortcuts: true,
...extraProps,
}
return (
// They key= in here makes it so that the chart is re-loaded when the slug changes.
<figure data-grapher-src ref={base}>
{bounds && (
<Grapher
key={grapherProps.slug}
{...grapherProps}
adminBaseUrl={ADMIN_BASE_URL}
bakedGrapherURL={BAKED_GRAPHER_URL}
/>
)}
</figure>
)
}