/
echarts.ts
174 lines (152 loc) · 5.17 KB
/
echarts.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
import {ModelEvent} from "@bokehjs/core/bokeh_events"
import {div} from "@bokehjs/core/dom"
import * as p from "@bokehjs/core/properties"
import type {Attrs} from "@bokehjs/core/types"
import {serializeEvent} from "./event-to-object"
import {HTMLBox, HTMLBoxView} from "./layout"
const mouse_events = [
'click', 'dblclick', 'mousedown', 'mousemove', 'mouseup', 'mouseover', 'mouseout',
'globalout', 'contextmenu'
];
const events = [
'highlight', 'downplay', 'selectchanged', 'legendselectchangedEvent', 'legendselected',
'legendunselected', 'legendselectall', 'legendinverseselect', 'legendscroll', 'datazoom',
'datarangeselected', 'timelineplaychanged', 'restore', 'dataviewchanged', 'magictypechanged',
'geoselectchanged', 'geoselected', 'geounselected', 'axisareaselected', 'brush', 'brushEnd',
'rushselected', 'globalcursortaken', 'rendered', 'finished'
];
const all_events = mouse_events.concat(events);
export class EChartsEvent extends ModelEvent {
constructor(readonly type: string, readonly data: any, readonly query: string) {
super()
}
protected get event_values(): Attrs {
return {model: this.origin, type: this.type, data: this.data, query: this.query}
}
static {
this.prototype.event_name = "echarts_event"
}
}
export class EChartsView extends HTMLBoxView {
model: ECharts
container: Element
_chart: any
_callbacks: Array<any>[] = []
connect_signals(): void {
super.connect_signals()
this.connect(this.model.properties.data.change, () => this._plot())
const {width, height, renderer, theme, event_config, js_events} = this.model.properties
this.on_change([width, height], () => this._resize())
this.on_change([theme, renderer], () => {
this.render()
this._chart.resize()
})
this.on_change([event_config, js_events], () => this._subscribe())
}
render(): void {
if (this._chart != null)
(window as any).echarts.dispose(this._chart);
super.render()
this.container = div({style: "height: 100%; width: 100%;"})
const config = {width: this.model.width, height: this.model.height, renderer: this.model.renderer}
this._chart = (window as any).echarts.init(
this.container,
this.model.theme,
config
)
this._plot()
this._subscribe()
this.shadow_el.append(this.container)
}
override remove(): void {
super.remove()
if (this._chart != null)
(window as any).echarts.dispose(this._chart);
}
after_layout(): void {
super.after_layout()
this._chart.resize()
}
_plot(): void {
if ((window as any).echarts == null)
return
this._chart.setOption(this.model.data, this.model.options );
}
_resize(): void {
this._chart.resize({width: this.model.width, height: this.model.height});
}
_subscribe(): void {
if ((window as any).echarts == null)
return
for (const [event_type, callback] of this._callbacks)
this._chart.off(event_type, callback)
this._callbacks = []
for (const event_type in this.model.event_config) {
if (!all_events.includes(event_type)) {
console.warn(`Could not subscribe to unknown Echarts event: ${event_type}.`);
continue
}
const queries = this.model.event_config[event_type];
for (const query of queries) {
const callback = (event: any) => {
const processed = {...event}
processed.event = serializeEvent(event.event?.event)
const serialized = JSON.parse(JSON.stringify(processed))
this.model.trigger_event(new EChartsEvent(event_type, serialized, query))
}
if (query == null)
this._chart.on(event_type, query, callback)
else
this._chart.on(event_type, callback)
this._callbacks.push([event_type, callback])
}
}
for (const event_type in this.model.js_events) {
if (!all_events.includes(event_type)) {
console.warn(`Could not subscribe to unknown Echarts event: ${event_type}.`);
continue
}
const handlers = this.model.js_events[event_type];
for (const handler of handlers) {
const callback = (event: any) => {
handler.callback.execute(this._chart, event)
}
if ('query' in handler)
this._chart.on(event_type, handler.query, callback)
else
this._chart.on(event_type, callback)
this._callbacks.push([event_type, callback])
}
}
}
}
export namespace ECharts {
export type Attrs = p.AttrsOf<Props>
export type Props = HTMLBox.Props & {
data: p.Property<any>
options: p.Property<any>
event_config: p.Property<any>
js_events: p.Property<any>
renderer: p.Property<string>
theme: p.Property<string>
}
}
export interface ECharts extends ECharts.Attrs {}
export class ECharts extends HTMLBox {
properties: ECharts.Props
constructor(attrs?: Partial<ECharts.Attrs>) {
super(attrs)
}
static __module__ = "panel.models.echarts"
static {
this.prototype.default_view = EChartsView
this.define<ECharts.Props>(({ Any, String }) => ({
data: [ Any, {} ],
options: [ Any, {} ],
event_config: [ Any, {} ],
js_events: [ Any, {} ],
theme: [ String, "default"],
renderer: [ String, "canvas"]
}))
}
}