|
1 | | -import { chartColors10, chartColors20, deepnoteBlues } from './colors' |
2 | | -import React, { memo, useLayoutEffect } from 'react' |
3 | | -import { Vega } from 'react-vega' |
4 | | -import { vega } from 'vega-embed' |
5 | | - |
6 | | -import { numberFormats } from './number-formats' |
| 1 | +import { chartColors10, chartColors20, deepnoteBlues } from './colors'; |
| 2 | +import React, { memo, useLayoutEffect } from 'react'; |
| 3 | +import { Vega } from 'react-vega'; |
| 4 | +import { vega } from 'vega-embed'; |
7 | 5 |
|
| 6 | +import { numberFormats } from './number-formats'; |
8 | 7 |
|
9 | 8 | export interface VegaRendererProps { |
10 | | - spec: Record<string, unknown> |
11 | | - renderer?: 'svg' | 'canvas' |
| 9 | + spec: Record<string, unknown>; |
| 10 | + renderer?: 'svg' | 'canvas'; |
12 | 11 | } |
13 | 12 |
|
14 | 13 | export const VegaRenderer = memo(function VegaRenderer(props: VegaRendererProps) { |
15 | | - const { renderer, spec } = props |
16 | | - |
17 | | - |
18 | | - useLayoutEffect(function registerCustomVegaConfigsOnce() { |
19 | | - if (vega.expressionFunction('numberFormatFromNumberType')) { |
20 | | - return |
21 | | - } |
22 | | - |
23 | | - vega.expressionFunction( |
24 | | - 'numberFormatFromNumberType', |
25 | | - (value: number, params: { type: string; decimals: number | null }) => { |
26 | | - // NOTE: default params.type to "default" value to ensure that eg "Sum of" labels are displayed in custom tooltip |
27 | | - return numberFormats[params?.type ?? 'default']?.formatter(value, params?.decimals) |
28 | | - } |
29 | | - ) |
30 | | - |
31 | | - vega.scheme('deepnote10', chartColors10) |
32 | | - vega.scheme('deepnote20', chartColors20) |
33 | | - vega.scheme('deepnote_blues', deepnoteBlues) |
34 | | - }, []) |
35 | | - |
36 | | -// const unfreezedSpec = useMemo(() => clone(addRuntimeDataToVegaSpec(spec, theme)), [spec, theme]) |
37 | | - const unfreezedSpec = spec |
38 | | - |
39 | | - return ( |
40 | | - <Vega |
41 | | - spec={unfreezedSpec} |
42 | | - renderer={renderer} |
43 | | - actions={false} |
44 | | - style={{ |
45 | | - height: '100%', |
46 | | - width: '100%', |
47 | | - }} |
48 | | - /> |
49 | | - ) |
50 | | -}) |
| 14 | + const { renderer, spec } = props; |
| 15 | + |
| 16 | + useLayoutEffect(function registerCustomVegaConfigsOnce() { |
| 17 | + if (vega.expressionFunction('numberFormatFromNumberType')) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + vega.expressionFunction( |
| 22 | + 'numberFormatFromNumberType', |
| 23 | + (value: number, params: { type: string; decimals: number | null }) => { |
| 24 | + // NOTE: default params.type to "default" value to ensure that eg "Sum of" labels are displayed in custom tooltip |
| 25 | + return numberFormats[params?.type ?? 'default']?.formatter(value, params?.decimals); |
| 26 | + } |
| 27 | + ); |
| 28 | + |
| 29 | + vega.scheme('deepnote10', chartColors10); |
| 30 | + vega.scheme('deepnote20', chartColors20); |
| 31 | + vega.scheme('deepnote_blues', deepnoteBlues); |
| 32 | + }, []); |
| 33 | + |
| 34 | + // const unfreezedSpec = useMemo(() => clone(addRuntimeDataToVegaSpec(spec, theme)), [spec, theme]) |
| 35 | + const unfreezedSpec = spec; |
| 36 | + |
| 37 | + return ( |
| 38 | + <Vega |
| 39 | + spec={unfreezedSpec} |
| 40 | + renderer={renderer} |
| 41 | + actions={false} |
| 42 | + style={{ |
| 43 | + height: '100%', |
| 44 | + width: '100%' |
| 45 | + }} |
| 46 | + /> |
| 47 | + ); |
| 48 | +}); |
0 commit comments