-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (196 loc) · 5.71 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
import { Group } from 'spritejs'
import { isArray, getGlobal, invariant } from '../../util'
import BaseNode from '../BaseNode'
import { BaseVisual } from '../BaseVisual'
import { render } from '../vnode'
import Dataset from '../dataset'
import { Plot } from './Plot'
import { Global } from '../Global'
const global = getGlobal()
const defaultTheme = 'default'
const _plot = Symbol('plot')
const _initPlot = Symbol('initPlot')
const _mountToPlot = Symbol('mountToPlot')
const _addChild = Symbol('addChild')
const _renderChild = Symbol('renderChild')
export class Chart extends BaseNode {
constructor(attrs = {}) {
super(attrs)
this.emit(this.lifecycle.beforeCreate)
this[_plot] = null
this[_initPlot](this.attr())
this[_mountToPlot]()
this.setTheme(defaultTheme) // defaultTheme
this.dataset = null
this.visuals = []
this.plugins = []
this.children = []
this.Global = Global
delete this.state
delete this.__vnode__
this.emit(this.lifecycle.created)
}
getDefaultAttrs() {
return {
pos: [0, 0],
size: ['100%', '100%'],
forceFit: true,
zoomable: false
}
}
[_initPlot](attrs) {
const $ = node =>
typeof node === 'string'
? global.document && global.document.querySelector
? global.document.querySelector(node)
: { id: node } // use reference for WeakMap
: node
this.domElement = $(attrs.container)
invariant(this.domElement, `Need a domNode to render chart!`)
let plot = Global.PLOT_INSTANCES.get(this.domElement)
if (!plot) {
plot = new Plot({ ...attrs, container: this.domElement })
Global.PLOT_INSTANCES.set(this.domElement, plot)
}
this[_plot] = plot
this[_plot].addChart(this)
this.scene = plot.scene
this.canvas = plot.canvas
}
[_mountToPlot]() {
const pos = this.attr('pos')
const size = this.attr('size')
const group = this[_plot].subPlot(pos, size)
this.$group = group
this.attr('size', this.$group.attr('size'))
this.attr('pos', this.$group.attr('pos'))
}
setTheme(name) {
const target = Global.THEMES.get(name)
invariant(target, `Unknown theme type! Register it first!`)
// Object.keys(target).forEach(key => this.style(key, target[key]))
Global.useTheme(name)
this.__isRendered__ && this.emit('theme:change', target)
if (this.$group) {
this.$group.attr(this.style('Chart')() || {})
}
}
resolveTheme(name) {
return Global.CURRENT_THEME[name] || this.style(name)()
}
setCanvasCursor(type = 'default') {
if (this.canvas) this.canvas.style.cursor = type
}
getSize() {
return this.$group.attr('size')
}
getPos() {
return this.$group.attr('pos')
}
source(data, options = {}) {
if (this.dataset) {
this.dataset.source(data, options)
this.update()
} else {
let dataset = null
if (!(data instanceof Dataset)) {
dataset = new Dataset()
dataset.source(data, options)
} else {
dataset = data
}
this.dataset = dataset
}
return this
}
[_addChild](instance) {
if (!instance) {
return
}
instance.connectedCallback && instance.connectedCallback(this)
if (instance instanceof BaseVisual) {
this.visuals.push(instance)
} else {
this.plugins.push(instance)
}
this.children.push(instance)
return this
}
add(instance) {
if (isArray(instance)) {
instance.map(this[_addChild].bind(this))
} else {
this[_addChild](instance)
}
return this
}
getChildren() {
return [].concat(this.plugins, this.visuals)
}
[_renderChild](child) {
invariant(child.render, `Chart need a instance which has render method!`)
invariant(
this.dataset,
`The chart can't render because havn't source data!`
)
if (!child.dataset) {
child.dataset = this.dataset
}
this.dataset.addDep(child)
const $group = new Group()
this.$group.appendChild($group)
child.$group = $group
const data = child.beforeRender && child.beforeRender()
const vnode = child.render && child.render(data)
$group.attr({ ...vnode.attrs })
child.__vnode__ = vnode
render(vnode.children, $group)
child.__isRendered__ = true
child.rendered()
}
render() {
invariant(
!this.__isRendered__,
`The chart is rendered! Do not invoke chart.render many a time!`
)
this.emit(this.lifecycle.beforeRender, this)
void this.children.map(this[_renderChild].bind(this))
this.emit(this.lifecycle.rendered, this)
this.__isRendered__ = true
}
update() {
this.attr('size', this.$group.attr('size'))
this.attr('pos', this.$group.attr('pos'))
this.emit(this.lifecycle.beforeUpdate, this)
void this.children.map(child => child.update())
this.emit(this.lifecycle.updated, this)
}
destroy() {
this.emit(this.lifecycle.beforeDestroy, this)
this[_plot].plots.pop()
this.$group.parentNode.removeChild(this.$group)
this.emit(this.lifecycle.destroyed, this)
}
save(fileName = 'qcharts', type = 'image/png') {
invariant(
!!global.document,
`In current platform, the chart can't save to a image!`
)
const dataURL = this.canvas
.toDataURL(type)
.replace(type, 'image/octet-stream')
const aLink = document.createElement('a')
aLink.style.position = 'fixed'
aLink.style.zIndex = -9999
document.body.appendChild(aLink)
const evt = document.createEvent('HTMLEvents')
evt.initEvent('click', false, false)
aLink.download = /\.\w+/.test(fileName)
? fileName
: fileName + '.' + type.split('/')[1]
aLink.href = dataURL
aLink.dispatchEvent(evt)
aLink.click()
document.body.removeChild(aLink)
}
}