-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
85 lines (70 loc) · 1.98 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React from 'react'
import { Polygon, Ellipse, Lines, Point } from './shapes'
const svgProps = {
version: '1.1',
baseProfile: 'full',
xmlns: 'http://www.w3.org/2000/svg',
preserveAspectRatio: 'xMidYMid slice',
width: '100%',
height: '100%'
}
const Paper = ({ color, opacity }) => (
<rect width='100%' height='100%' fill={color} opacity={opacity} />
)
const svgElements = (data, transforms) => {
const elements = data.map((element) => {
if (element.type === 'lines') {
return <Lines {...element} {...{ transforms }} key={element.id} />
}
if (element.type === 'polygon') {
return <Polygon {...element} {...{ transforms }} key={element.id} />
}
if (element.type === 'ellipse') {
return <Ellipse {...element} {...{ transforms }} key={element.id} />
}
const { size, color, opacity, id } = element
return element.x.map((x, i) => (
<Point
{...{ x, y: element.y[i], size, color, opacity, id, i, transforms }}
key={`${id}-${i}`}
/>
))
})
return elements.flat()
}
/**************************
* Minimal Plot
**************************/
class BareMinimum2d extends React.PureComponent {
xRange = null
yRange = null
/***
yRange/2
|
|
-xRange/2 -------(0,0)--------- xRange/2
|
|
-yRange/2
***/
transformX = (x) => x + this.xRange / 2
transformY = (y) => this.yRange / 2 - y
render() {
const { container, data } = this.props
this.xRange = container.xRange
this.yRange = container.yRange
const transforms = {
tx: this.transformX,
ty: this.transformY
}
const viewBox = `0 0 ${container.xRange} ${container.yRange}`
const { color, opacity } = container
return (
<svg {...svgProps} {...{ viewBox }}>
<Paper {...{ color, opacity }} />
{svgElements(data, transforms)}
</svg>
)
}
}
export default BareMinimum2d