-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (32 loc) · 932 Bytes
/
index.js
File metadata and controls
37 lines (32 loc) · 932 Bytes
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
import React, { useRef, useState, useEffect } from 'react'
import * as rv from 'rough-viz/dist/roughviz.min'
let lastId = 0;
const generateId = prefix => `${prefix}${'' + (++lastId)}`
const wrap = rvComp => ({ prefix, ...props }) => {
const ref = useRef()
const [ id ] = useState(generateId(prefix || 'roughviz'))
useEffect(() => {
const { current: node } = ref
if (node) {
new rvComp({
element: '#' + id,
...props
})
}
return () => {
if (node) {
while (node.firstChild) {
node.removeChild(node.firstChild)
}
}
}
}, [ id, props, ref ])
return <div id={id} ref={ref} />
}
export const Bar = wrap(rv.Bar)
export const BarH = wrap(rv.BarH)
export const Donut = wrap(rv.Donut)
export const Pie = wrap(rv.Pie)
export const Scatter = wrap(rv.Scatter)
export const Line = wrap(rv.Line)
export const StackedBar = wrap(rv.StackedBar)