-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (87 loc) · 1.85 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const React = require('react');
import { VegaLite, Vega } from 'react-vega';
class IdyllVegaLite extends React.Component {
constructor(props) {
super(props)
this.state = {
handler: null
}
}
isVegaSpec() {
return this.props.mode === 'vega' || (this.props.spec.$schema && this.props.spec.$schema.startsWith("https://vega.github.io/schema/vega/"))
}
componentDidMount() {
const { Handler } = require('vega-tooltip')
this.setState({
handler: new Handler().call
})
}
render() {
const { spec, data, mode, ...props } = this.props;
let adjustedSpec = spec;
let Runtime = VegaLite;
if (this.isVegaSpec()) {
Runtime = Vega;
if (!adjustedSpec.data) {
console.warn('If passing a vega spec you must provide a data object in the spec.');
}
} else {
//vega-lite spec. Modify the spec if data was passed.
if (data) {
adjustedSpec = { data: { values: data }, ...spec };
}
}
const { handler } = this.state;
return (
<Runtime
{...props}
spec={adjustedSpec}
tooltip={handler} />
);
}
}
VegaLite._idyll = {
name: 'IdyllVegaLite',
tagType: 'closed',
props: [
{
name: 'data',
type: 'expression',
example: `\`[{x: 0, y: 0}, {x: 1, y: 1}]\``
},
{
name: 'spec',
type: 'expression',
example: `\`{
mark: "line",
encoding: {
x: {
field: "x",
type: "quantitative"
},
y: {
field: "y",
type: "quantitative"
}
}
}\``
},
{
name: 'mode',
type: 'string',
default: `"vega-lite"`,
example: `"vega-lite"`
},
{
name: 'width',
type: 'value',
example: `"container"`
},
{
name: 'height',
type: 'number',
example: `300`
}
]
};
module.exports = IdyllVegaLite;