graphic.js
14.9 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
define(function(require) {
'use strict';
var zrUtil = require('zrender/core/util');
var pathTool = require('zrender/tool/path');
var round = Math.round;
var Path = require('zrender/graphic/Path');
var colorTool = require('zrender/tool/color');
var matrix = require('zrender/core/matrix');
var vector = require('zrender/core/vector');
var Gradient = require('zrender/graphic/Gradient');
var graphic = {};
graphic.Group = require('zrender/container/Group');
graphic.Image = require('zrender/graphic/Image');
graphic.Text = require('zrender/graphic/Text');
graphic.Circle = require('zrender/graphic/shape/Circle');
graphic.Sector = require('zrender/graphic/shape/Sector');
graphic.Ring = require('zrender/graphic/shape/Ring');
graphic.Polygon = require('zrender/graphic/shape/Polygon');
graphic.Polyline = require('zrender/graphic/shape/Polyline');
graphic.Rect = require('zrender/graphic/shape/Rect');
graphic.Line = require('zrender/graphic/shape/Line');
graphic.BezierCurve = require('zrender/graphic/shape/BezierCurve');
graphic.Arc = require('zrender/graphic/shape/Arc');
graphic.CompoundPath = require('zrender/graphic/CompoundPath');
graphic.LinearGradient = require('zrender/graphic/LinearGradient');
graphic.RadialGradient = require('zrender/graphic/RadialGradient');
graphic.BoundingRect = require('zrender/core/BoundingRect');
/**
* Extend shape with parameters
*/
graphic.extendShape = function (opts) {
return Path.extend(opts);
};
/**
* Extend path
*/
graphic.extendPath = function (pathData, opts) {
return pathTool.extendFromString(pathData, opts);
};
/**
* Create a path element from path data string
* @param {string} pathData
* @param {Object} opts
* @param {module:zrender/core/BoundingRect} rect
* @param {string} [layout=cover] 'center' or 'cover'
*/
graphic.makePath = function (pathData, opts, rect, layout) {
var path = pathTool.createFromString(pathData, opts);
var boundingRect = path.getBoundingRect();
if (rect) {
var aspect = boundingRect.width / boundingRect.height;
if (layout === 'center') {
// Set rect to center, keep width / height ratio.
var width = rect.height * aspect;
var height;
if (width <= rect.width) {
height = rect.height;
}
else {
width = rect.width;
height = width / aspect;
}
var cx = rect.x + rect.width / 2;
var cy = rect.y + rect.height / 2;
rect.x = cx - width / 2;
rect.y = cy - height / 2;
rect.width = width;
rect.height = height;
}
this.resizePath(path, rect);
}
return path;
};
graphic.mergePath = pathTool.mergePath,
/**
* Resize a path to fit the rect
* @param {module:zrender/graphic/Path} path
* @param {Object} rect
*/
graphic.resizePath = function (path, rect) {
if (!path.applyTransform) {
return;
}
var pathRect = path.getBoundingRect();
var m = pathRect.calculateTransform(rect);
path.applyTransform(m);
};
/**
* Sub pixel optimize line for canvas
*
* @param {Object} param
* @param {Object} [param.shape]
* @param {number} [param.shape.x1]
* @param {number} [param.shape.y1]
* @param {number} [param.shape.x2]
* @param {number} [param.shape.y2]
* @param {Object} [param.style]
* @param {number} [param.style.lineWidth]
* @return {Object} Modified param
*/
graphic.subPixelOptimizeLine = function (param) {
var subPixelOptimize = graphic.subPixelOptimize;
var shape = param.shape;
var lineWidth = param.style.lineWidth;
if (round(shape.x1 * 2) === round(shape.x2 * 2)) {
shape.x1 = shape.x2 = subPixelOptimize(shape.x1, lineWidth, true);
}
if (round(shape.y1 * 2) === round(shape.y2 * 2)) {
shape.y1 = shape.y2 = subPixelOptimize(shape.y1, lineWidth, true);
}
return param;
};
/**
* Sub pixel optimize rect for canvas
*
* @param {Object} param
* @param {Object} [param.shape]
* @param {number} [param.shape.x]
* @param {number} [param.shape.y]
* @param {number} [param.shape.width]
* @param {number} [param.shape.height]
* @param {Object} [param.style]
* @param {number} [param.style.lineWidth]
* @return {Object} Modified param
*/
graphic.subPixelOptimizeRect = function (param) {
var subPixelOptimize = graphic.subPixelOptimize;
var shape = param.shape;
var lineWidth = param.style.lineWidth;
var originX = shape.x;
var originY = shape.y;
var originWidth = shape.width;
var originHeight = shape.height;
shape.x = subPixelOptimize(shape.x, lineWidth, true);
shape.y = subPixelOptimize(shape.y, lineWidth, true);
shape.width = Math.max(
subPixelOptimize(originX + originWidth, lineWidth, false) - shape.x,
originWidth === 0 ? 0 : 1
);
shape.height = Math.max(
subPixelOptimize(originY + originHeight, lineWidth, false) - shape.y,
originHeight === 0 ? 0 : 1
);
return param;
};
/**
* Sub pixel optimize for canvas
*
* @param {number} position Coordinate, such as x, y
* @param {number} lineWidth Should be nonnegative integer.
* @param {boolean=} positiveOrNegative Default false (negative).
* @return {number} Optimized position.
*/
graphic.subPixelOptimize = function (position, lineWidth, positiveOrNegative) {
// Assure that (position + lineWidth / 2) is near integer edge,
// otherwise line will be fuzzy in canvas.
var doubledPosition = round(position * 2);
return (doubledPosition + round(lineWidth)) % 2 === 0
? doubledPosition / 2
: (doubledPosition + (positiveOrNegative ? 1 : -1)) / 2;
};
function hasFillOrStroke(fillOrStroke) {
return fillOrStroke != null && fillOrStroke != 'none';
}
function liftColor(color) {
return color instanceof Gradient ? color : colorTool.lift(color, -0.1);
}
/**
* @private
*/
function cacheElementStl(el) {
if (el.__hoverStlDirty) {
var stroke = el.style.stroke;
var fill = el.style.fill;
// Create hoverStyle on mouseover
var hoverStyle = el.__hoverStl;
hoverStyle.fill = hoverStyle.fill
|| (hasFillOrStroke(fill) ? liftColor(fill) : null);
hoverStyle.stroke = hoverStyle.stroke
|| (hasFillOrStroke(stroke) ? liftColor(stroke) : null);
var normalStyle = {};
for (var name in hoverStyle) {
if (hoverStyle.hasOwnProperty(name)) {
normalStyle[name] = el.style[name];
}
}
el.__normalStl = normalStyle;
el.__hoverStlDirty = false;
}
}
/**
* @private
*/
function doSingleEnterHover(el) {
if (el.__isHover) {
return;
}
cacheElementStl(el);
el.setStyle(el.__hoverStl);
el.z2 += 1;
el.__isHover = true;
}
/**
* @inner
*/
function doSingleLeaveHover(el) {
if (!el.__isHover) {
return;
}
var normalStl = el.__normalStl;
normalStl && el.setStyle(normalStl);
el.z2 -= 1;
el.__isHover = false;
}
/**
* @inner
*/
function doEnterHover(el) {
el.type === 'group'
? el.traverse(function (child) {
if (child.type !== 'group') {
doSingleEnterHover(child);
}
})
: doSingleEnterHover(el);
}
function doLeaveHover(el) {
el.type === 'group'
? el.traverse(function (child) {
if (child.type !== 'group') {
doSingleLeaveHover(child);
}
})
: doSingleLeaveHover(el);
}
/**
* @inner
*/
function setElementHoverStl(el, hoverStl) {
// If element has sepcified hoverStyle, then use it instead of given hoverStyle
// Often used when item group has a label element and it's hoverStyle is different
el.__hoverStl = el.hoverStyle || hoverStl || {};
el.__hoverStlDirty = true;
if (el.__isHover) {
cacheElementStl(el);
}
}
/**
* @inner
*/
function onElementMouseOver() {
// Only if element is not in emphasis status
!this.__isEmphasis && doEnterHover(this);
}
/**
* @inner
*/
function onElementMouseOut() {
// Only if element is not in emphasis status
!this.__isEmphasis && doLeaveHover(this);
}
/**
* @inner
*/
function enterEmphasis() {
this.__isEmphasis = true;
doEnterHover(this);
}
/**
* @inner
*/
function leaveEmphasis() {
this.__isEmphasis = false;
doLeaveHover(this);
}
/**
* Set hover style of element
* @param {module:zrender/Element} el
* @param {Object} [hoverStyle]
*/
graphic.setHoverStyle = function (el, hoverStyle) {
el.type === 'group'
? el.traverse(function (child) {
if (child.type !== 'group') {
setElementHoverStl(child, hoverStyle);
}
})
: setElementHoverStl(el, hoverStyle);
// Remove previous bound handlers
el.on('mouseover', onElementMouseOver)
.on('mouseout', onElementMouseOut);
// Emphasis, normal can be triggered manually
el.on('emphasis', enterEmphasis)
.on('normal', leaveEmphasis);
};
/**
* Set text option in the style
* @param {Object} textStyle
* @param {module:echarts/model/Model} labelModel
* @param {string} color
*/
graphic.setText = function (textStyle, labelModel, color) {
var labelPosition = labelModel.getShallow('position') || 'inside';
var labelColor = labelPosition.indexOf('inside') >= 0 ? 'white' : color;
var textStyleModel = labelModel.getModel('textStyle');
zrUtil.extend(textStyle, {
textDistance: labelModel.getShallow('distance') || 5,
textFont: textStyleModel.getFont(),
textPosition: labelPosition,
textFill: textStyleModel.getTextColor() || labelColor
});
};
function animateOrSetProps(isUpdate, el, props, animatableModel, dataIndex, cb) {
if (typeof dataIndex === 'function') {
cb = dataIndex;
dataIndex = null;
}
var postfix = isUpdate ? 'Update' : '';
var duration = animatableModel
&& animatableModel.getShallow('animationDuration' + postfix);
var animationEasing = animatableModel
&& animatableModel.getShallow('animationEasing' + postfix);
var animationDelay = animatableModel
&& animatableModel.getShallow('animationDelay' + postfix);
if (typeof animationDelay === 'function') {
animationDelay = animationDelay(dataIndex);
}
animatableModel && animatableModel.getShallow('animation')
? el.animateTo(props, duration, animationDelay || 0, animationEasing, cb)
: (el.attr(props), cb && cb());
}
/**
* Update graphic element properties with or without animation according to the configuration in series
* @param {module:zrender/Element} el
* @param {Object} props
* @param {module:echarts/model/Model} [animatableModel]
* @param {number} [dataIndex]
* @param {Function} [cb]
* @example
* graphic.updateProps(el, {
* position: [100, 100]
* }, seriesModel, dataIndex, function () { console.log('Animation done!'); });
* // Or
* graphic.updateProps(el, {
* position: [100, 100]
* }, seriesModel, function () { console.log('Animation done!'); });
*/
graphic.updateProps = zrUtil.curry(animateOrSetProps, true);
/**
* Init graphic element properties with or without animation according to the configuration in series
* @param {module:zrender/Element} el
* @param {Object} props
* @param {module:echarts/model/Model} [animatableModel]
* @param {Function} cb
*/
graphic.initProps = zrUtil.curry(animateOrSetProps, false);
/**
* Get transform matrix of target (param target),
* in coordinate of its ancestor (param ancestor)
*
* @param {module:zrender/mixin/Transformable} target
* @param {module:zrender/mixin/Transformable} [ancestor]
*/
graphic.getTransform = function (target, ancestor) {
var mat = matrix.identity([]);
while (target && target !== ancestor) {
matrix.mul(mat, target.getLocalTransform(), mat);
target = target.parent;
}
return mat;
};
/**
* Apply transform to an vertex.
* @param {Array.<number>} vertex [x, y]
* @param {Array.<number>} transform Transform matrix: like [1, 0, 0, 1, 0, 0]
* @param {boolean=} invert Whether use invert matrix.
* @return {Array.<number>} [x, y]
*/
graphic.applyTransform = function (vertex, transform, invert) {
if (invert) {
transform = matrix.invert([], transform);
}
return vector.applyTransform([], vertex, transform);
};
/**
* @param {string} direction 'left' 'right' 'top' 'bottom'
* @param {Array.<number>} transform Transform matrix: like [1, 0, 0, 1, 0, 0]
* @param {boolean=} invert Whether use invert matrix.
* @return {string} Transformed direction. 'left' 'right' 'top' 'bottom'
*/
graphic.transformDirection = function (direction, transform, invert) {
// Pick a base, ensure that transform result will not be (0, 0).
var hBase = (transform[4] === 0 || transform[5] === 0 || transform[0] === 0)
? 1 : Math.abs(2 * transform[4] / transform[0]);
var vBase = (transform[4] === 0 || transform[5] === 0 || transform[2] === 0)
? 1 : Math.abs(2 * transform[4] / transform[2]);
var vertex = [
direction === 'left' ? -hBase : direction === 'right' ? hBase : 0,
direction === 'top' ? -vBase : direction === 'bottom' ? vBase : 0
];
vertex = graphic.applyTransform(vertex, transform, invert);
return Math.abs(vertex[0]) > Math.abs(vertex[1])
? (vertex[0] > 0 ? 'right' : 'left')
: (vertex[1] > 0 ? 'bottom' : 'top');
};
return graphic;
});