/
vega.ts
217 lines (192 loc) · 6.28 KB
/
vega.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {div} from "@bokehjs/core/dom"
import * as p from "@bokehjs/core/properties"
import {ModelEvent} from "@bokehjs/core/bokeh_events"
import {isArray} from "@bokehjs/core/util/types"
import {LayoutDOM, LayoutDOMView} from "@bokehjs/models/layouts/layout_dom"
import {Attrs} from "@bokehjs/core/types"
import {set_size} from "./layout"
import {debounce} from "debounce"
export class VegaEvent extends ModelEvent {
constructor(readonly data: any) {
super()
}
protected get event_values(): Attrs {
return {model: this.origin, data: this.data}
}
static {
this.prototype.event_name = "vega_event"
}
}
export class VegaPlotView extends LayoutDOMView {
model: VegaPlot
vega_view: any
container: HTMLDivElement
_callbacks: string[]
_connected: string[]
_replot: any
_resize: any
_rendered: boolean = false
connect_signals(): void {
super.connect_signals()
const {data, show_actions, theme} = this.model.properties
this._replot = debounce(() => this._plot(), 20)
this.on_change([data, show_actions, theme], () => {
this._replot()
})
this.connect(this.model.properties.data_sources.change, () => this._connect_sources())
this.connect(this.model.properties.events.change, () => {
for (const event of this.model.events) {
if (this._callbacks.indexOf(event) > -1)
continue
this._callbacks.push(event)
const callback = (name: string, value: any) => this._dispatch_event(name, value)
const timeout = this.model.throttle[event] || 20
this.vega_view.addSignalListener(event, debounce(callback, timeout, false))
}
})
this._connected = []
this._connect_sources()
}
_connect_sources(): void {
for (const ds in this.model.data_sources) {
const cds = this.model.data_sources[ds]
if (this._connected.indexOf(ds) < 0) {
this.connect(cds.properties.data.change, () => this._replot())
this._connected.push(ds)
}
}
}
override remove(): void {
if (this.vega_view) {
this.vega_view.finalize()
}
super.remove()
}
_dispatch_event(name: string, value: any): void {
if ('vlPoint' in value && value.vlPoint.or != null) {
const indexes = []
for (const index of value.vlPoint.or) {
if (index._vgsid_ !== undefined) { // If "_vgsid_" property exists
indexes.push(index._vgsid_);
} else { // If "_vgsid_" property doesn't exist
// Iterate through all properties in the "index" object
for (const key in index) {
if (index.hasOwnProperty(key)) { // To ensure key comes from "index" object itself, not its prototype
indexes.push({[key]: index[key]}); // Push a new object with this key-value pair into the array
}
}
}
}
value = indexes
}
this.model.trigger_event(new VegaEvent({type: name, value: value}))
}
_fetch_datasets() {
const datasets: any = {}
for (const ds in this.model.data_sources) {
const cds = this.model.data_sources[ds];
const data: any = []
const columns = cds.columns()
for (let i = 0; i < cds.get_length(); i++) {
const item: any = {}
for (const column of columns) {
item[column] = cds.data[column][i]
}
data.push(item)
}
datasets[ds] = data;
}
return datasets
}
get child_models(): LayoutDOM[] {
return []
}
render(): void {
super.render()
this._rendered = false
this.container = div()
set_size(this.container, this.model)
this._callbacks = []
this._plot()
this.shadow_el.append(this.container)
}
_plot(): void {
const data = this.model.data
if ((data == null) || !(window as any).vegaEmbed)
return
if (this.model.data_sources && (Object.keys(this.model.data_sources).length > 0)) {
const datasets = this._fetch_datasets()
if ('data' in datasets) {
data.data['values'] = datasets['data']
delete datasets['data']
}
if (data.data != null) {
const data_objs = isArray(data.data) ? data.data : [data.data]
for (const d of data_objs) {
if (d.name in datasets) {
d['values'] = datasets[d.name]
delete datasets[d.name]
}
}
}
this.model.data['datasets'] = datasets
}
const config: any = {actions: this.model.show_actions, theme: this.model.theme};
(window as any).vegaEmbed(this.container, this.model.data, config).then((result: any) => {
this.vega_view = result.view
this._resize = debounce(() => this.resize_view(result.view), 50)
const callback = (name: string, value: any) => this._dispatch_event(name, value)
for (const event of this.model.events) {
this._callbacks.push(event)
const timeout = this.model.throttle[event] || 20
this.vega_view.addSignalListener(event, debounce(callback, timeout, false))
}
})
}
after_layout(): void {
super.after_layout()
if (this.vega_view != null) {
this._resize()
}
}
resize_view(view: any): void {
const canvas = view._renderer.canvas()
if (!this._rendered && canvas !== null) {
for (const listener of view._eventListeners) {
if (listener.type === 'resize')
listener.handler(new Event('resize'))
}
this._rendered = true
}
}
}
export namespace VegaPlot {
export type Attrs = p.AttrsOf<Props>
export type Props = LayoutDOM.Props & {
data: p.Property<any>
data_sources: p.Property<any>
events: p.Property<string[]>
show_actions: p.Property<boolean>
theme: p.Property<string | null>
throttle: p.Property<any>
}
}
export interface VegaPlot extends VegaPlot.Attrs {}
export class VegaPlot extends LayoutDOM {
properties: VegaPlot.Props
constructor(attrs?: Partial<VegaPlot.Attrs>) {
super(attrs)
}
static __module__ = "panel.models.vega"
static {
this.prototype.default_view = VegaPlotView
this.define<VegaPlot.Props>(({Any, Array, Boolean, Nullable, String}) => ({
data: [ Any, {} ],
data_sources: [ Any, {} ],
events: [ Array(String), [] ],
show_actions: [ Boolean, false ],
theme: [ Nullable(String), null ],
throttle: [ Any, {} ]
}))
}
}