jmchart 1.2.13 → 1.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/jmchart.amd.js +1389 -1218
- package/dist/jmchart.cjs.js +1389 -1218
- package/dist/jmchart.esm.js +1389 -1218
- package/dist/jmchart.iife.js +1389 -1218
- package/dist/jmchart.system.js +1389 -1218
- package/package.json +2 -2
- package/src/component/vchart.js +24 -3
- package/src/core/axis/axis.js +6 -3
- package/src/core/markLine/manager.js +2 -6
- package/src/jmChart.js +40 -49
- package/src/series/areaSeries.js +7 -230
- package/src/series/barSeries.js +3 -6
- package/src/series/bubbleSeries.js +1 -4
- package/src/series/candlestickSeries.js +5 -4
- package/src/series/gaugeSeries.js +1 -4
- package/src/series/heatmapSeries.js +1 -4
- package/src/series/lineSeries.js +1 -4
- package/src/series/pieSeries.js +5 -3
- package/src/series/radarSeries.js +18 -11
- package/src/series/scatterSeries.js +1 -4
- package/src/series/series.js +18 -9
- package/src/series/stackBarSeries.js +6 -4
- package/src/series/waterfallSeries.js +1 -4
package/dist/jmchart.cjs.js
CHANGED
|
@@ -281,66 +281,75 @@ class jmUtils {
|
|
|
281
281
|
* @param {function} copyHandler 复制对象回调,如果返回undefined,就走后面的逻辑,否则到这里中止
|
|
282
282
|
* @return {object} 参数source的拷贝对象
|
|
283
283
|
*/
|
|
284
|
-
static clone(source, target, deep = false, copyHandler = null, deepIndex = 0) {
|
|
284
|
+
static clone(source, target, deep = false, copyHandler = null, deepIndex = 0, cloned = null) {
|
|
285
285
|
// 如果有指定回调,则用回调处理,否则走后面的复制逻辑
|
|
286
286
|
if(typeof copyHandler === 'function') {
|
|
287
287
|
const obj = copyHandler(source, deep, deepIndex);
|
|
288
288
|
if(obj) return obj;
|
|
289
289
|
}
|
|
290
|
-
|
|
290
|
+
|
|
291
|
+
// 首次调用时初始化克隆映射表(用于处理循环引用)
|
|
292
|
+
if(!cloned) cloned = new WeakMap();
|
|
291
293
|
|
|
292
294
|
if(typeof target === 'boolean') {
|
|
293
295
|
deep = target;
|
|
294
296
|
target = undefined;
|
|
295
297
|
}
|
|
296
298
|
|
|
297
|
-
//
|
|
298
|
-
if(
|
|
299
|
-
return target;
|
|
299
|
+
// 非对象直接返回
|
|
300
|
+
if(!source || typeof source !== 'object') {
|
|
301
|
+
return typeof target !== 'undefined' ? target : source;
|
|
300
302
|
}
|
|
301
303
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
+
// 如果source已经被克隆过,直接返回之前的克隆对象,打破循环引用
|
|
305
|
+
if(cloned.has(source)) return cloned.get(source);
|
|
304
306
|
|
|
307
|
+
// 数组处理
|
|
308
|
+
if(Array.isArray(source)) {
|
|
305
309
|
//如果为当前泛型,则直接new
|
|
306
310
|
if(this.isType(source, jmList)) {
|
|
307
311
|
return new jmList(source);
|
|
308
312
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
dest.push(this.clone(source[i], target[i], deep, copyHandler, deepIndex));
|
|
315
|
-
}
|
|
316
|
-
return dest;
|
|
317
|
-
}
|
|
318
|
-
return source.slice(0);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if(source.__proto__) target.__proto__ = source.__proto__;
|
|
322
|
-
|
|
323
|
-
for(let k in source) {
|
|
324
|
-
if(k === 'constructor') continue;
|
|
325
|
-
const v = source[k];
|
|
326
|
-
// 不复制页面元素和class对象
|
|
327
|
-
if(v && (v.tagName || v.getContext)) {
|
|
328
|
-
target[k] = v;
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// 如果不是对象和空,则采用target的属性
|
|
333
|
-
if(typeof target[k] === 'object' || typeof target[k] === 'undefined') {
|
|
334
|
-
target[k] = this.clone(v, target[k], deep, copyHandler, deepIndex);
|
|
313
|
+
if(deep) {
|
|
314
|
+
let dest = [];
|
|
315
|
+
cloned.set(source, dest);
|
|
316
|
+
for(let i = 0; i < source.length; i++) {
|
|
317
|
+
dest.push(this.clone(source[i], undefined, deep, copyHandler, deepIndex + 1, cloned));
|
|
335
318
|
}
|
|
319
|
+
return dest;
|
|
336
320
|
}
|
|
337
|
-
return
|
|
321
|
+
return source.slice(0);
|
|
338
322
|
}
|
|
339
|
-
|
|
340
|
-
|
|
323
|
+
|
|
324
|
+
// 不复制页面元素和class对象(如jmControl实例等复杂对象保持引用)
|
|
325
|
+
if(source.tagName || source.getContext || source.emit) {
|
|
326
|
+
return source;
|
|
341
327
|
}
|
|
342
328
|
|
|
343
|
-
|
|
329
|
+
// 普通对象处理
|
|
330
|
+
target = target || {};
|
|
331
|
+
cloned.set(source, target);
|
|
332
|
+
|
|
333
|
+
// 保持原型链一致
|
|
334
|
+
if(source.__proto__) target.__proto__ = source.__proto__;
|
|
335
|
+
|
|
336
|
+
// 遍历自身可枚举属性(字符串键 + Symbol键),避免触发原型链上宿主对象的getter
|
|
337
|
+
const keys = Object.keys(source).concat(Object.getOwnPropertySymbols(source));
|
|
338
|
+
for(const k of keys) {
|
|
339
|
+
if(k === 'constructor') continue;
|
|
340
|
+
const v = source[k];
|
|
341
|
+
// 不复制页面元素和class对象
|
|
342
|
+
if(v && (v.tagName || v.getContext || v.emit)) {
|
|
343
|
+
target[k] = v;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 如果不是对象和空,则采用target的属性
|
|
348
|
+
if(typeof target[k] === 'object' || typeof target[k] === 'undefined') {
|
|
349
|
+
target[k] = this.clone(v, target[k], deep, copyHandler, deepIndex + 1, cloned);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return target;
|
|
344
353
|
}
|
|
345
354
|
|
|
346
355
|
/**
|
|
@@ -1110,7 +1119,8 @@ class jmGradient {
|
|
|
1110
1119
|
let sy2 = Number(y2) + bounds.top;
|
|
1111
1120
|
if(this.type === 'linear') {
|
|
1112
1121
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
1113
|
-
|
|
1122
|
+
// WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
|
|
1123
|
+
gradient = control.webglControl.createLinearGradient(sx1, sy1, sx2, sy2, bounds);
|
|
1114
1124
|
gradient.key = this.toString();
|
|
1115
1125
|
}
|
|
1116
1126
|
else {
|
|
@@ -1129,7 +1139,8 @@ class jmGradient {
|
|
|
1129
1139
|
r2 = d * r2;
|
|
1130
1140
|
}
|
|
1131
1141
|
if(control.mode === 'webgl' && control.webglControl) {
|
|
1132
|
-
|
|
1142
|
+
// WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
|
|
1143
|
+
gradient = control.webglControl.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2, bounds);
|
|
1133
1144
|
gradient.key = this.toString();
|
|
1134
1145
|
}
|
|
1135
1146
|
//offsetLine = Math.abs(r2 - r1);//二圆半径差
|
|
@@ -1308,6 +1319,153 @@ class jmShadow {
|
|
|
1308
1319
|
}
|
|
1309
1320
|
}
|
|
1310
1321
|
|
|
1322
|
+
/**
|
|
1323
|
+
* CSS滤镜效果类
|
|
1324
|
+
* 支持的滤镜: blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity
|
|
1325
|
+
*
|
|
1326
|
+
* @class jmFilter
|
|
1327
|
+
* @param {string|object} opt 滤镜参数
|
|
1328
|
+
* 字符串格式: "blur(2px) grayscale(50%) brightness(1.2)"
|
|
1329
|
+
* 对象格式: { blur: 2, grayscale: 0.5, brightness: 1.2 }
|
|
1330
|
+
*/
|
|
1331
|
+
class jmFilter {
|
|
1332
|
+
constructor(opt) {
|
|
1333
|
+
this.filters = [];
|
|
1334
|
+
|
|
1335
|
+
if(typeof opt === 'string') {
|
|
1336
|
+
this.fromString(opt);
|
|
1337
|
+
}
|
|
1338
|
+
else if(opt && typeof opt === 'object') {
|
|
1339
|
+
for(let k in opt) {
|
|
1340
|
+
if(k === 'constructor' || k === 'filters') continue;
|
|
1341
|
+
this.addFilter(k, opt[k]);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/**
|
|
1347
|
+
* 添加单个滤镜
|
|
1348
|
+
* @param {string} name 滤镜名称 (blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity)
|
|
1349
|
+
* @param {number|string} value 滤镜值
|
|
1350
|
+
*/
|
|
1351
|
+
addFilter(name, value) {
|
|
1352
|
+
name = name.toLowerCase().trim();
|
|
1353
|
+
if(typeof value === 'string') {
|
|
1354
|
+
value = parseFloat(value);
|
|
1355
|
+
}
|
|
1356
|
+
if(isNaN(value)) return;
|
|
1357
|
+
|
|
1358
|
+
// 规范化滤镜名称
|
|
1359
|
+
const normalized = {
|
|
1360
|
+
'blur': 'blur',
|
|
1361
|
+
'grayscale': 'grayscale',
|
|
1362
|
+
'greyscale': 'grayscale',
|
|
1363
|
+
'sepia': 'sepia',
|
|
1364
|
+
'brightness': 'brightness',
|
|
1365
|
+
'contrast': 'contrast',
|
|
1366
|
+
'saturate': 'saturate',
|
|
1367
|
+
'hue-rotate': 'hueRotate',
|
|
1368
|
+
'hueRotate': 'hueRotate',
|
|
1369
|
+
'invert': 'invert',
|
|
1370
|
+
'opacity': 'opacity'
|
|
1371
|
+
}[name];
|
|
1372
|
+
|
|
1373
|
+
if(!normalized) return;
|
|
1374
|
+
|
|
1375
|
+
// 检查是否已有同名滤镜,有则更新
|
|
1376
|
+
const existing = this.filters.find(f => f.name === normalized);
|
|
1377
|
+
if(existing) {
|
|
1378
|
+
existing.value = value;
|
|
1379
|
+
}
|
|
1380
|
+
else {
|
|
1381
|
+
this.filters.push({ name: normalized, value: value });
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
/**
|
|
1386
|
+
* 从字符串格式解析滤镜
|
|
1387
|
+
* 格式: "blur(2px) grayscale(50%) brightness(1.2)"
|
|
1388
|
+
* @param {string} s 滤镜字符串
|
|
1389
|
+
*/
|
|
1390
|
+
fromString(s) {
|
|
1391
|
+
if(!s || typeof s !== 'string') return;
|
|
1392
|
+
// 匹配 filterName(value) 模式
|
|
1393
|
+
const regex = /([a-zA-Z-]+)\s*\(\s*([^)]+)\s*\)/g;
|
|
1394
|
+
let match;
|
|
1395
|
+
while((match = regex.exec(s)) !== null) {
|
|
1396
|
+
const name = match[1];
|
|
1397
|
+
const valueStr = match[2].replace(/[a-z%]+$/i, '').trim();
|
|
1398
|
+
const value = parseFloat(valueStr);
|
|
1399
|
+
if(!isNaN(value)) {
|
|
1400
|
+
this.addFilter(name, value);
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
/**
|
|
1406
|
+
* 转换为CSS filter字符串格式
|
|
1407
|
+
* @returns {string}
|
|
1408
|
+
*/
|
|
1409
|
+
toString() {
|
|
1410
|
+
return this.filters.map(f => {
|
|
1411
|
+
switch(f.name) {
|
|
1412
|
+
case 'blur':
|
|
1413
|
+
return `blur(${f.value}px)`;
|
|
1414
|
+
case 'hueRotate':
|
|
1415
|
+
return `hue-rotate(${f.value}deg)`;
|
|
1416
|
+
default:
|
|
1417
|
+
return `${f.name}(${f.value})`;
|
|
1418
|
+
}
|
|
1419
|
+
}).join(' ');
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
/**
|
|
1423
|
+
* 转换为Canvas context.filter可用的字符串
|
|
1424
|
+
* @returns {string}
|
|
1425
|
+
*/
|
|
1426
|
+
toCanvasFilter() {
|
|
1427
|
+
if(this.filters.length === 0) return 'none';
|
|
1428
|
+
return this.toString();
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
/**
|
|
1432
|
+
* 检查是否有指定名称的滤镜
|
|
1433
|
+
* @param {string} name 滤镜名称
|
|
1434
|
+
* @returns {boolean}
|
|
1435
|
+
*/
|
|
1436
|
+
has(name) {
|
|
1437
|
+
return this.filters.some(f => f.name === name);
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
* 获取指定滤镜的值
|
|
1442
|
+
* @param {string} name 滤镜名称
|
|
1443
|
+
* @returns {number|undefined}
|
|
1444
|
+
*/
|
|
1445
|
+
get(name) {
|
|
1446
|
+
const f = this.filters.find(f => f.name === name);
|
|
1447
|
+
return f ? f.value : undefined;
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
/**
|
|
1451
|
+
* 移除指定滤镜
|
|
1452
|
+
* @param {string} name 滤镜名称
|
|
1453
|
+
*/
|
|
1454
|
+
remove(name) {
|
|
1455
|
+
const index = this.filters.findIndex(f => f.name === name);
|
|
1456
|
+
if(index > -1) {
|
|
1457
|
+
this.filters.splice(index, 1);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
/**
|
|
1462
|
+
* 清空所有滤镜
|
|
1463
|
+
*/
|
|
1464
|
+
clear() {
|
|
1465
|
+
this.filters = [];
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1311
1469
|
let control_id_counter = 0;
|
|
1312
1470
|
|
|
1313
1471
|
class jmObject {
|
|
@@ -2129,10 +2287,14 @@ earcut.flatten = function (data) {
|
|
|
2129
2287
|
return result;
|
|
2130
2288
|
};
|
|
2131
2289
|
|
|
2132
|
-
|
|
2290
|
+
const MAX_STOPS = 16;
|
|
2291
|
+
|
|
2292
|
+
/**
|
|
2293
|
+
* WebGL 渐变对象
|
|
2294
|
+
* 支持 GLSL 着色器直接计算渐变色,无需 textureCanvas
|
|
2295
|
+
*/
|
|
2133
2296
|
class WebglGradient {
|
|
2134
|
-
|
|
2135
|
-
constructor(type='linear', params={}) {
|
|
2297
|
+
constructor(type = 'linear', params = {}) {
|
|
2136
2298
|
this.type = type || 'linear';
|
|
2137
2299
|
|
|
2138
2300
|
this.x1 = params.x1 || 0;
|
|
@@ -2152,148 +2314,114 @@ class WebglGradient {
|
|
|
2152
2314
|
this.control = params.control;
|
|
2153
2315
|
|
|
2154
2316
|
this.stops = [];
|
|
2155
|
-
this.
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
init() {
|
|
2159
|
-
const dx = this.x2 - this.x1;
|
|
2160
|
-
const dy = this.y2 - this.y1;
|
|
2161
|
-
|
|
2162
|
-
if(this.type === 'radial') {
|
|
2163
|
-
this.length = this.r2 - this.r1;
|
|
2164
|
-
}
|
|
2165
|
-
else if(dx === 0 && dy === 0) {
|
|
2166
|
-
this.length = 0;
|
|
2167
|
-
}
|
|
2168
|
-
else {
|
|
2169
|
-
// 渐变中心的距离
|
|
2170
|
-
this.length = Math.sqrt(Math.pow(dx, 2), Math.pow(dy, 2));
|
|
2171
|
-
this.sin = dy / this.length;
|
|
2172
|
-
this.cos = dx / this.length;
|
|
2173
|
-
}
|
|
2317
|
+
this._sortedStops = null;
|
|
2318
|
+
this._paramsHash = null;
|
|
2174
2319
|
}
|
|
2175
2320
|
|
|
2176
|
-
|
|
2321
|
+
/**
|
|
2322
|
+
* 添加颜色断点
|
|
2323
|
+
*/
|
|
2177
2324
|
addColorStop(offset, color) {
|
|
2178
2325
|
this.stops.push({
|
|
2179
|
-
offset,
|
|
2326
|
+
offset: Math.max(0, Math.min(1, offset)),
|
|
2180
2327
|
color
|
|
2181
2328
|
});
|
|
2329
|
+
this._sortedStops = null;
|
|
2330
|
+
this._paramsHash = null;
|
|
2182
2331
|
}
|
|
2183
2332
|
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
if(
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
gradient = control.textureContext.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
|
|
2197
|
-
}
|
|
2198
|
-
this.stops.forEach(function(s, i) {
|
|
2199
|
-
const c = control.graph.utils.toColor(s.color);
|
|
2200
|
-
gradient && gradient.addColorStop(s.offset, c);
|
|
2201
|
-
});
|
|
2202
|
-
|
|
2203
|
-
const data = control.toFillTexture(gradient, bounds, points);
|
|
2204
|
-
|
|
2205
|
-
//WebglGradientTextureCache[key] = data;
|
|
2206
|
-
|
|
2207
|
-
return data;
|
|
2208
|
-
}
|
|
2209
|
-
|
|
2210
|
-
// 根据绘制图形的坐标计算出对应点的颜色
|
|
2211
|
-
/*
|
|
2212
|
-
toPointColors(points) {
|
|
2213
|
-
const stops = this.getStops();
|
|
2214
|
-
const colors = [];
|
|
2215
|
-
for(let i=0; i<points.length; i+=2) {
|
|
2216
|
-
const p = {
|
|
2217
|
-
x: points[i],
|
|
2218
|
-
y: points[i+1]
|
|
2219
|
-
}
|
|
2220
|
-
if(this.type === 'radial') {
|
|
2221
|
-
const dx = p.x - this.x1;
|
|
2222
|
-
const dy = p.y - this.y1;
|
|
2223
|
-
const len = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
|
|
2224
|
-
const rang = this.getStopRange(len, stops);
|
|
2225
|
-
if(!rang.start && rang.end) {
|
|
2226
|
-
colors.push(rang.end.color);
|
|
2227
|
-
}
|
|
2228
|
-
else if(!rang.end && rang.start) {
|
|
2229
|
-
colors.push(rang.start.color);
|
|
2333
|
+
/**
|
|
2334
|
+
* 获取排序后的 stops(带解析后的颜色)
|
|
2335
|
+
*/
|
|
2336
|
+
_getSortedStops() {
|
|
2337
|
+
if (this._sortedStops) return this._sortedStops;
|
|
2338
|
+
|
|
2339
|
+
const utils = this.control && this.control.graph && this.control.graph.utils;
|
|
2340
|
+
this._sortedStops = this.stops
|
|
2341
|
+
.map(s => {
|
|
2342
|
+
let c = s.color;
|
|
2343
|
+
if (utils && typeof c === 'string') {
|
|
2344
|
+
c = utils.hexToRGBA(c);
|
|
2230
2345
|
}
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
const
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2346
|
+
if (typeof c === 'object' && c !== null) {
|
|
2347
|
+
// hexToRGBA 返回 r/g/b 为 0~255,a 为 0~1
|
|
2348
|
+
// 但如果已经是 0~1 范围(由 rgbToDecimal 处理过),需要检测
|
|
2349
|
+
const needNormalize = (c.r > 1 || c.g > 1 || c.b > 1) ? 255 : 1;
|
|
2350
|
+
return {
|
|
2351
|
+
offset: s.offset,
|
|
2352
|
+
r: (c.r !== undefined ? c.r : 0) / needNormalize,
|
|
2353
|
+
g: (c.g !== undefined ? c.g : 0) / needNormalize,
|
|
2354
|
+
b: (c.b !== undefined ? c.b : 0) / needNormalize,
|
|
2355
|
+
a: c.a !== undefined ? c.a : 1
|
|
2240
2356
|
};
|
|
2241
|
-
colors.push(color);
|
|
2242
2357
|
}
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2358
|
+
return { offset: s.offset, r: 0, g: 0, b: 0, a: 1 };
|
|
2359
|
+
})
|
|
2360
|
+
.sort((a, b) => a.offset - b.offset);
|
|
2361
|
+
|
|
2362
|
+
return this._sortedStops;
|
|
2246
2363
|
}
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2364
|
+
|
|
2365
|
+
/**
|
|
2366
|
+
* 将渐变参数以 uniform 形式传递给着色器
|
|
2367
|
+
* 返回 { type, start, end, stopCount, stops } 供着色器使用
|
|
2368
|
+
*/
|
|
2369
|
+
toUniformParams() {
|
|
2370
|
+
const stops = this._getSortedStops();
|
|
2371
|
+
const count = Math.min(stops.length, MAX_STOPS);
|
|
2372
|
+
|
|
2373
|
+
// 展平为 Float32Array: [offset, r, g, b, a, ...]
|
|
2374
|
+
const flatStops = new Float32Array(count * 5);
|
|
2375
|
+
for (let i = 0; i < count; i++) {
|
|
2376
|
+
const s = stops[i];
|
|
2377
|
+
flatStops[i * 5 + 0] = s.offset;
|
|
2378
|
+
flatStops[i * 5 + 1] = s.r;
|
|
2379
|
+
flatStops[i * 5 + 2] = s.g;
|
|
2380
|
+
flatStops[i * 5 + 3] = s.b;
|
|
2381
|
+
flatStops[i * 5 + 4] = s.a;
|
|
2259
2382
|
}
|
|
2260
|
-
|
|
2383
|
+
|
|
2384
|
+
return {
|
|
2385
|
+
gradientType: this.type === 'radial' ? 2 : 1,
|
|
2386
|
+
gradientStart: new Float32Array([
|
|
2387
|
+
this.x1, this.y1,
|
|
2388
|
+
this.type === 'radial' ? Math.max(0, this.r1) : 0,
|
|
2389
|
+
0
|
|
2390
|
+
]),
|
|
2391
|
+
gradientEnd: new Float32Array([
|
|
2392
|
+
this.x2, this.y2,
|
|
2393
|
+
this.type === 'radial' ? Math.max(0, this.r2) : 0,
|
|
2394
|
+
0
|
|
2395
|
+
]),
|
|
2396
|
+
stopCount: count,
|
|
2397
|
+
stops: flatStops
|
|
2398
|
+
};
|
|
2261
2399
|
}
|
|
2262
2400
|
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
console.log(s, color);
|
|
2270
|
-
s.color = this.control.graph.utils.rgbToDecimal(color);
|
|
2271
|
-
s.length = s.offset * this.length;
|
|
2272
|
-
}
|
|
2273
|
-
return stops;
|
|
2401
|
+
/**
|
|
2402
|
+
* 使缓存失效
|
|
2403
|
+
*/
|
|
2404
|
+
invalidateCache() {
|
|
2405
|
+
this._sortedStops = null;
|
|
2406
|
+
this._paramsHash = null;
|
|
2274
2407
|
}
|
|
2275
|
-
|
|
2408
|
+
|
|
2276
2409
|
/**
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
this.stops.forEach(function(s) {
|
|
2293
|
-
str += ',' + s.color + ' ' + s.offset;
|
|
2294
|
-
});
|
|
2295
|
-
return str + ')';
|
|
2296
|
-
}
|
|
2410
|
+
* 转换为渐变的字符串表达
|
|
2411
|
+
*/
|
|
2412
|
+
toString() {
|
|
2413
|
+
let str = this.type + '-gradient(';
|
|
2414
|
+
if (this.type == 'linear') {
|
|
2415
|
+
str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
|
|
2416
|
+
}
|
|
2417
|
+
else {
|
|
2418
|
+
str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
|
|
2419
|
+
}
|
|
2420
|
+
this.stops.forEach(function(s) {
|
|
2421
|
+
str += ',' + s.color + ' ' + s.offset;
|
|
2422
|
+
});
|
|
2423
|
+
return str + ')';
|
|
2424
|
+
}
|
|
2297
2425
|
}
|
|
2298
2426
|
|
|
2299
2427
|
// 生成着色器
|
|
@@ -2618,8 +2746,8 @@ const pathVertexSource = `
|
|
|
2618
2746
|
vec4 pos = translatePosition(a_position, a_center_point.x, a_center_point.y);
|
|
2619
2747
|
gl_Position = pos;
|
|
2620
2748
|
v_color = a_color;
|
|
2621
|
-
if(a_type == 2) {
|
|
2622
|
-
v_text_coord =
|
|
2749
|
+
if(a_type == 2 || a_type == 5) {
|
|
2750
|
+
v_text_coord = a_position.xy;
|
|
2623
2751
|
}
|
|
2624
2752
|
}
|
|
2625
2753
|
`;
|
|
@@ -2629,21 +2757,73 @@ const pathFragmentSource = `
|
|
|
2629
2757
|
uniform sampler2D u_sample;
|
|
2630
2758
|
uniform vec4 v_texture_bounds; // 纹理的左上坐标和大小 x,y,z,w
|
|
2631
2759
|
uniform vec4 v_single_color;
|
|
2760
|
+
// GLSL 渐变 uniforms
|
|
2761
|
+
uniform int u_gradient_type; // 0=无 1=线性 2=径向
|
|
2762
|
+
uniform vec4 u_gradient_start; // 线性:{x1,y1,0,0} 径向:{cx,cy,r1,0}
|
|
2763
|
+
uniform vec4 u_gradient_end; // 线性:{x2,y2,0,0} 径向:{cx,cy,r2,0}
|
|
2764
|
+
uniform int u_gradient_stop_count;
|
|
2765
|
+
uniform float u_gradient_offsets[${MAX_STOPS}];
|
|
2766
|
+
uniform vec4 u_gradient_colors[${MAX_STOPS}]; // {r, g, b, a} 0~1 范围
|
|
2632
2767
|
varying float v_type;
|
|
2633
2768
|
varying vec4 v_color;
|
|
2634
2769
|
varying vec2 v_text_coord;
|
|
2635
2770
|
|
|
2636
2771
|
${convertTexturePosition}
|
|
2637
2772
|
|
|
2773
|
+
// 在 sorted stops 中按 t 值采样颜色
|
|
2774
|
+
// 兼容 GLSL ES 1.0:循环仅与常量比较,无 break/continue
|
|
2775
|
+
vec4 sampleGradient(float t) {
|
|
2776
|
+
t = clamp(t, 0.0, 1.0);
|
|
2777
|
+
// 正向扫描:始终遍历 MAX_STOPS-1 次,找到 t 所在段并覆盖结果
|
|
2778
|
+
float localT = 0.0;
|
|
2779
|
+
vec4 c0 = u_gradient_colors[0];
|
|
2780
|
+
vec4 c1 = u_gradient_colors[0];
|
|
2781
|
+
for(int i = 0; i < ${MAX_STOPS - 1}; i++) {
|
|
2782
|
+
float s0 = u_gradient_offsets[i];
|
|
2783
|
+
float s1 = u_gradient_offsets[i + 1];
|
|
2784
|
+
if(t >= s0) {
|
|
2785
|
+
float range = s1 - s0;
|
|
2786
|
+
localT = range > 0.0001 ? clamp((t - s0) / range, 0.0, 1.0) : 0.0;
|
|
2787
|
+
c0 = u_gradient_colors[i];
|
|
2788
|
+
c1 = u_gradient_colors[i + 1];
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
return mix(c0, c1, localT);
|
|
2792
|
+
}
|
|
2793
|
+
|
|
2638
2794
|
void main() {
|
|
2639
2795
|
// 如果是fill,则直接填充颜色
|
|
2640
2796
|
if(v_type == 1.0) {
|
|
2641
2797
|
gl_FragColor = v_single_color;
|
|
2642
2798
|
}
|
|
2643
|
-
// 渐变色
|
|
2799
|
+
// 渐变色 (旧方式,顶点颜色插值)
|
|
2644
2800
|
else if(v_type == 3.0) {
|
|
2645
2801
|
gl_FragColor = v_color;
|
|
2646
2802
|
}
|
|
2803
|
+
// GLSL 渐变填充 (type=5)
|
|
2804
|
+
else if(v_type == 5.0) {
|
|
2805
|
+
float t;
|
|
2806
|
+
if(u_gradient_type == 2) {
|
|
2807
|
+
// 径向渐变
|
|
2808
|
+
vec2 d = v_text_coord - u_gradient_start.xy;
|
|
2809
|
+
float dist = length(d);
|
|
2810
|
+
float r1 = u_gradient_start.z;
|
|
2811
|
+
float r2 = u_gradient_end.z;
|
|
2812
|
+
float range = r2 - r1;
|
|
2813
|
+
t = range > 0.001 ? (dist - r1) / range : 0.0;
|
|
2814
|
+
} else {
|
|
2815
|
+
// 线性渐变
|
|
2816
|
+
vec2 dir = u_gradient_end.xy - u_gradient_start.xy;
|
|
2817
|
+
float lenSq = dot(dir, dir);
|
|
2818
|
+
if(lenSq > 0.001) {
|
|
2819
|
+
vec2 pos = v_text_coord - u_gradient_start.xy;
|
|
2820
|
+
t = dot(pos, dir) / lenSq;
|
|
2821
|
+
} else {
|
|
2822
|
+
t = 0.0;
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
gl_FragColor = sampleGradient(t) * v_single_color.a;
|
|
2826
|
+
}
|
|
2647
2827
|
else if(v_type == 2.0) {
|
|
2648
2828
|
vec2 pos = translateTexturePosition(v_text_coord, v_texture_bounds);
|
|
2649
2829
|
gl_FragColor = texture2D(u_sample, pos);
|
|
@@ -2714,7 +2894,7 @@ class WeblBase {
|
|
|
2714
2894
|
rotate(angle) {
|
|
2715
2895
|
const cos = Math.cos(angle);
|
|
2716
2896
|
const sin = Math.sin(angle);
|
|
2717
|
-
const [a, b, c, d
|
|
2897
|
+
const [a, b, c, d] = this.transformMatrix;
|
|
2718
2898
|
|
|
2719
2899
|
// 更新变换矩阵
|
|
2720
2900
|
this.transformMatrix[0] = a * cos - b * sin;
|
|
@@ -2745,21 +2925,20 @@ class WeblBase {
|
|
|
2745
2925
|
};
|
|
2746
2926
|
}
|
|
2747
2927
|
|
|
2748
|
-
//
|
|
2749
|
-
get
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2928
|
+
// 文本测量用的离屏 canvas context(1x1 单例缓存,不依赖 textureCanvas)
|
|
2929
|
+
get _measureCtx() {
|
|
2930
|
+
if(!this.__measureCtx) {
|
|
2931
|
+
try {
|
|
2932
|
+
if(typeof document !== 'undefined') {
|
|
2933
|
+
const c = document.createElement('canvas');
|
|
2934
|
+
c.width = c.height = 1;
|
|
2935
|
+
this.__measureCtx = c.getContext('2d');
|
|
2936
|
+
}
|
|
2937
|
+
} catch(e) {
|
|
2938
|
+
this.__measureCtx = null;
|
|
2939
|
+
}
|
|
2754
2940
|
}
|
|
2755
|
-
return
|
|
2756
|
-
}
|
|
2757
|
-
// 纹理绘制canvas ctx
|
|
2758
|
-
get textureContext() {
|
|
2759
|
-
const ctx = this.textureCanvas.ctx || (this.textureCanvas.ctx = this.textureCanvas.getContext('2d', {
|
|
2760
|
-
willReadFrequently: true
|
|
2761
|
-
}));
|
|
2762
|
-
return ctx;
|
|
2941
|
+
return this.__measureCtx;
|
|
2763
2942
|
}
|
|
2764
2943
|
|
|
2765
2944
|
// i当前程序
|
|
@@ -2805,29 +2984,42 @@ class WeblBase {
|
|
|
2805
2984
|
// 把传统颜色转为webgl识别的
|
|
2806
2985
|
convertColor(color) {
|
|
2807
2986
|
if(this.isGradient(color)) return color;
|
|
2808
|
-
if(typeof color === 'string')
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
if(['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(style) > -1) {
|
|
2816
|
-
value = this.graph.utils.toColor(value);
|
|
2987
|
+
if(typeof color === 'string') {
|
|
2988
|
+
// 先尝试 hexToRGBA 解析
|
|
2989
|
+
color = this.graph.utils.hexToRGBA(color);
|
|
2990
|
+
// hexToRGBA 对无法识别的格式(如 hsl)会原样返回字符串
|
|
2991
|
+
// 利用离屏 canvas 将任意 CSS 颜色转为 rgba
|
|
2992
|
+
if(typeof color === 'string') {
|
|
2993
|
+
color = this.__parseCSSColor(color);
|
|
2817
2994
|
}
|
|
2818
|
-
this.textureContext[style] = value;
|
|
2819
2995
|
}
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2996
|
+
if(typeof color === 'object' && color.r !== undefined) {
|
|
2997
|
+
return this.graph.utils.rgbToDecimal(color);
|
|
2998
|
+
}
|
|
2999
|
+
return color;
|
|
3000
|
+
}
|
|
3001
|
+
|
|
3002
|
+
// 利用离屏 canvas 解析任意 CSS 颜色(hsl/hsla/命名颜色等)
|
|
3003
|
+
__parseCSSColor(colorStr) {
|
|
3004
|
+
const ctx = this._measureCtx;
|
|
3005
|
+
if(!ctx) return { r: 0, g: 0, b: 0, a: 0 };
|
|
3006
|
+
try {
|
|
3007
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
3008
|
+
ctx.fillStyle = '#000000';
|
|
3009
|
+
ctx.fillStyle = colorStr;
|
|
3010
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
3011
|
+
const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
|
|
3012
|
+
if(ctx.fillStyle === '#000000' && colorStr !== '#000000' && colorStr !== 'black') {
|
|
3013
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
2824
3014
|
}
|
|
3015
|
+
return { r, g, b, a: a / 255 };
|
|
3016
|
+
} catch(e) {
|
|
3017
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
2825
3018
|
}
|
|
2826
3019
|
}
|
|
2827
3020
|
|
|
2828
3021
|
// 创建程序
|
|
2829
3022
|
createProgram(vertexSrc, fragmentSrc) {
|
|
2830
|
-
this.context.lineWidth(1);
|
|
2831
3023
|
return createProgram(this.context, vertexSrc, fragmentSrc);
|
|
2832
3024
|
}
|
|
2833
3025
|
|
|
@@ -2940,7 +3132,9 @@ class WeblBase {
|
|
|
2940
3132
|
// polygonIndices 顶点索引,
|
|
2941
3133
|
earCutPointsToTriangles(points) {
|
|
2942
3134
|
this.earCutCache = this.earCutCache || (this.earCutCache = {});
|
|
2943
|
-
|
|
3135
|
+
// 快速缓存 key:用长度和首尾点坐标
|
|
3136
|
+
const len = points.length;
|
|
3137
|
+
const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
|
|
2944
3138
|
if (this.earCutCache[key]) return this.earCutCache[key];
|
|
2945
3139
|
|
|
2946
3140
|
const ps = this.earCutPoints(points);// 切割得到3角色顶点索引,
|
|
@@ -2995,97 +3189,23 @@ class WeblBase {
|
|
|
2995
3189
|
return obj && obj instanceof WebglGradient;
|
|
2996
3190
|
}
|
|
2997
3191
|
|
|
2998
|
-
|
|
3192
|
+
/**
|
|
2999
3193
|
* 测试获取文本所占大小
|
|
3000
3194
|
*
|
|
3001
3195
|
* @method testSize
|
|
3002
3196
|
* @return {object} 含文本大小的对象
|
|
3003
3197
|
*/
|
|
3004
3198
|
testSize(text, style=this.style) {
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
{width:15};
|
|
3014
|
-
this.textureContext.restore &&this.textureContext.restore();
|
|
3015
|
-
size.height = this.style.fontSize? this.style.fontSize: 15;
|
|
3199
|
+
const ctx = this._measureCtx;
|
|
3200
|
+
if(!ctx) return { width: 15, height: style.fontSize || 15 };
|
|
3201
|
+
|
|
3202
|
+
ctx.save && ctx.save();
|
|
3203
|
+
if(style.font || style.fontSize) ctx.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
|
|
3204
|
+
const size = ctx.measureText ? ctx.measureText(text) : { width: 15 };
|
|
3205
|
+
ctx.restore && ctx.restore();
|
|
3206
|
+
size.height = style.fontSize ? parseInt(style.fontSize) : 15;
|
|
3016
3207
|
return size;
|
|
3017
3208
|
}
|
|
3018
|
-
|
|
3019
|
-
// 使用纹理canvas生成图,
|
|
3020
|
-
// 填充可以是颜色或渐变对象
|
|
3021
|
-
// 如果指定了points,则表明要绘制不规则的图形
|
|
3022
|
-
toFillTexture(fillStyle, bounds, points=null) {
|
|
3023
|
-
const canvas = this.textureCanvas;
|
|
3024
|
-
if(!canvas) {
|
|
3025
|
-
return fillStyle;
|
|
3026
|
-
}
|
|
3027
|
-
canvas.width = bounds.width;
|
|
3028
|
-
canvas.height = bounds.height;
|
|
3029
|
-
|
|
3030
|
-
if(!canvas.width || !canvas.height) {
|
|
3031
|
-
return fillStyle;
|
|
3032
|
-
}
|
|
3033
|
-
|
|
3034
|
-
this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
|
|
3035
|
-
|
|
3036
|
-
this.textureContext.fillStyle = fillStyle;
|
|
3037
|
-
|
|
3038
|
-
this.textureContext.beginPath();
|
|
3039
|
-
if(!points || !points.length) {
|
|
3040
|
-
points = [];
|
|
3041
|
-
points.push({
|
|
3042
|
-
x: bounds.left,
|
|
3043
|
-
y: bounds.top
|
|
3044
|
-
});
|
|
3045
|
-
points.push({
|
|
3046
|
-
x: bounds.left + bounds.width,
|
|
3047
|
-
y: bounds.top
|
|
3048
|
-
});
|
|
3049
|
-
points.push({
|
|
3050
|
-
x: bounds.left + bounds.width,
|
|
3051
|
-
y: bounds.top + bounds.height
|
|
3052
|
-
});
|
|
3053
|
-
points.push({
|
|
3054
|
-
x: bounds.left,
|
|
3055
|
-
y: bounds.top + bounds.height
|
|
3056
|
-
});
|
|
3057
|
-
points.push({
|
|
3058
|
-
x: bounds.left,
|
|
3059
|
-
y: bounds.top
|
|
3060
|
-
});
|
|
3061
|
-
}
|
|
3062
|
-
if(points && points.length) {
|
|
3063
|
-
for(const p of points) {
|
|
3064
|
-
//移至当前坐标
|
|
3065
|
-
if(p.m) {
|
|
3066
|
-
this.textureContext.moveTo(p.x - bounds.left, p.y - bounds.top);
|
|
3067
|
-
}
|
|
3068
|
-
else {
|
|
3069
|
-
this.textureContext.lineTo(p.x - bounds.left, p.y - bounds.top);
|
|
3070
|
-
}
|
|
3071
|
-
}
|
|
3072
|
-
}
|
|
3073
|
-
else {
|
|
3074
|
-
this.textureContext.moveTo(0, 0);
|
|
3075
|
-
this.textureContext.lineTo(bounds.width, 0);
|
|
3076
|
-
this.textureContext.lineTo(bounds.width, bounds.height);
|
|
3077
|
-
this.textureContext.lineTo(0, bounds.height);
|
|
3078
|
-
this.textureContext.lineTo(0, 0);
|
|
3079
|
-
}
|
|
3080
|
-
this.textureContext.closePath();
|
|
3081
|
-
this.textureContext.fill();
|
|
3082
|
-
|
|
3083
|
-
const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
|
|
3084
|
-
return {
|
|
3085
|
-
data,
|
|
3086
|
-
points
|
|
3087
|
-
};
|
|
3088
|
-
}
|
|
3089
3209
|
}
|
|
3090
3210
|
|
|
3091
3211
|
// path 绘制类
|
|
@@ -3097,6 +3217,40 @@ class WebglPath extends WeblBase {
|
|
|
3097
3217
|
this.needCut = option.needCut || false;
|
|
3098
3218
|
this.control = option.control;
|
|
3099
3219
|
this.points = [];
|
|
3220
|
+
// 缓存 buffer 和纹理,避免每帧创建/销毁
|
|
3221
|
+
this.__cachedBuffers = [];
|
|
3222
|
+
this.__cachedTexture = null;
|
|
3223
|
+
this.__cachedTextureKey = null;
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
// 释放缓存的 WebGL 资源
|
|
3227
|
+
dispose() {
|
|
3228
|
+
for(const buf of this.__cachedBuffers) {
|
|
3229
|
+
this.deleteBuffer(buf);
|
|
3230
|
+
}
|
|
3231
|
+
this.__cachedBuffers = [];
|
|
3232
|
+
if(this.__cachedTexture) {
|
|
3233
|
+
this.deleteTexture(this.__cachedTexture);
|
|
3234
|
+
this.__cachedTexture = null;
|
|
3235
|
+
this.__cachedTextureKey = null;
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3238
|
+
|
|
3239
|
+
// 获取或创建 buffer,优先复用缓存
|
|
3240
|
+
getOrCreateBuffer(data, attr) {
|
|
3241
|
+
let buffer = this.__cachedBuffers.find(b => b.attr === attr);
|
|
3242
|
+
if(buffer) {
|
|
3243
|
+
const gl = this.context;
|
|
3244
|
+
const float32 = new Float32Array(data);
|
|
3245
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer.buffer);
|
|
3246
|
+
gl.bufferData(gl.ARRAY_BUFFER, float32, gl.DYNAMIC_DRAW);
|
|
3247
|
+
buffer.data = data;
|
|
3248
|
+
return buffer;
|
|
3249
|
+
}
|
|
3250
|
+
buffer = this.createFloat32Buffer(data);
|
|
3251
|
+
buffer.attr = attr;
|
|
3252
|
+
this.__cachedBuffers.push(buffer);
|
|
3253
|
+
return buffer;
|
|
3100
3254
|
}
|
|
3101
3255
|
|
|
3102
3256
|
// 应用变换到点
|
|
@@ -3109,8 +3263,14 @@ class WebglPath extends WeblBase {
|
|
|
3109
3263
|
//this.useProgram();
|
|
3110
3264
|
|
|
3111
3265
|
if(parentBounds) this.parentAbsoluteBounds = parentBounds;
|
|
3112
|
-
//
|
|
3113
|
-
|
|
3266
|
+
// 缓存中心点值,只在变化时才更新 uniform
|
|
3267
|
+
const cx = this.graph.width / 2;
|
|
3268
|
+
const cy = this.graph.height / 2;
|
|
3269
|
+
if(this.__lastCenterX !== cx || this.__lastCenterY !== cy) {
|
|
3270
|
+
this.context.uniform2f(this.program.uniforms.a_center_point.location, cx, cy);
|
|
3271
|
+
this.__lastCenterX = cx;
|
|
3272
|
+
this.__lastCenterY = cy;
|
|
3273
|
+
}
|
|
3114
3274
|
}
|
|
3115
3275
|
|
|
3116
3276
|
setFragColor(color) {
|
|
@@ -3151,6 +3311,7 @@ class WebglPath extends WeblBase {
|
|
|
3151
3311
|
endDraw() {
|
|
3152
3312
|
if(this.points) delete this.points;
|
|
3153
3313
|
if(this.pathPoints) delete this.pathPoints;
|
|
3314
|
+
// 缓存的纹理保留到下次绘制(渐变可能不变)
|
|
3154
3315
|
}
|
|
3155
3316
|
|
|
3156
3317
|
// 图形封闭
|
|
@@ -3162,21 +3323,47 @@ class WebglPath extends WeblBase {
|
|
|
3162
3323
|
}
|
|
3163
3324
|
}
|
|
3164
3325
|
|
|
3165
|
-
//
|
|
3326
|
+
// 绘制点数组(使用 DYNAMIC_DRAW 复用 buffer,避免每帧 create/delete)
|
|
3166
3327
|
writePoints(points, attr = this.program.attrs.a_position) {
|
|
3167
|
-
|
|
3168
3328
|
const fixedPoints = [];
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3329
|
+
const [a, b, c, d, tx, ty] = this.transformMatrix;
|
|
3330
|
+
const isIdentity = (a === 1 && b === 0 && c === 0 && d === 1 && tx === 0 && ty === 0);
|
|
3331
|
+
const offsetLeft = this.parentAbsoluteBounds.left;
|
|
3332
|
+
const offsetTop = this.parentAbsoluteBounds.top;
|
|
3333
|
+
|
|
3334
|
+
if(isIdentity) {
|
|
3335
|
+
// 单位矩阵时直接加偏移,避免逐点调用 applyTransform
|
|
3336
|
+
for(let i = 0; i < points.length; i++) {
|
|
3337
|
+
fixedPoints.push(points[i].x + offsetLeft, points[i].y + offsetTop);
|
|
3338
|
+
}
|
|
3339
|
+
} else {
|
|
3340
|
+
for(const p of points) {
|
|
3341
|
+
const transformedPoint = this.applyTransform(p);
|
|
3342
|
+
fixedPoints.push(
|
|
3343
|
+
transformedPoint.x + offsetLeft,
|
|
3344
|
+
transformedPoint.y + offsetTop
|
|
3345
|
+
);
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
const float32 = new Float32Array(fixedPoints);
|
|
3349
|
+
const gl = this.context;
|
|
3350
|
+
|
|
3351
|
+
// 复用已有 buffer 或创建新的
|
|
3352
|
+
if(this.__cachedBuffers.length > 0) {
|
|
3353
|
+
// 找一个同 attr 的 buffer 复用
|
|
3354
|
+
let buffer = this.__cachedBuffers.find(b => b.attr === attr);
|
|
3355
|
+
if(buffer) {
|
|
3356
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer.buffer);
|
|
3357
|
+
gl.bufferData(gl.ARRAY_BUFFER, float32, gl.DYNAMIC_DRAW);
|
|
3358
|
+
buffer.data = fixedPoints;
|
|
3359
|
+
this.writeVertexAttrib(buffer, attr, 2, 0, 0);
|
|
3360
|
+
return buffer;
|
|
3361
|
+
}
|
|
3362
|
+
}
|
|
3363
|
+
const vertexBuffer = this.createFloat32Buffer(float32, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
3178
3364
|
this.writeVertexAttrib(vertexBuffer, attr, 2, 0, 0);
|
|
3179
3365
|
vertexBuffer.attr = attr;
|
|
3366
|
+
this.__cachedBuffers.push(vertexBuffer);
|
|
3180
3367
|
return vertexBuffer;
|
|
3181
3368
|
}
|
|
3182
3369
|
|
|
@@ -3225,6 +3412,112 @@ class WebglPath extends WeblBase {
|
|
|
3225
3412
|
equalPoint(p1, p2) {
|
|
3226
3413
|
return p1.x === p2.x && p1.y === p2.y;
|
|
3227
3414
|
}
|
|
3415
|
+
|
|
3416
|
+
// 将带 moveTo 标记的点集拆分为外轮廓和多个洞
|
|
3417
|
+
splitSubPaths(points) {
|
|
3418
|
+
const subPaths = [];
|
|
3419
|
+
let current = [];
|
|
3420
|
+
for(let i = 0; i < points.length; i++) {
|
|
3421
|
+
const p = points[i];
|
|
3422
|
+
if(p.m && current.length > 0) {
|
|
3423
|
+
subPaths.push(current);
|
|
3424
|
+
current = [];
|
|
3425
|
+
}
|
|
3426
|
+
current.push(p);
|
|
3427
|
+
}
|
|
3428
|
+
if(current.length > 0) subPaths.push(current);
|
|
3429
|
+
|
|
3430
|
+
// 面积最大的作为外轮廓,其余作为洞
|
|
3431
|
+
let maxArea = -1;
|
|
3432
|
+
let outerIdx = 0;
|
|
3433
|
+
for(let i = 0; i < subPaths.length; i++) {
|
|
3434
|
+
const area = Math.abs(this.polygonArea(subPaths[i]));
|
|
3435
|
+
if(area > maxArea) {
|
|
3436
|
+
maxArea = area;
|
|
3437
|
+
outerIdx = i;
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
|
|
3441
|
+
const outerPoints = subPaths[outerIdx];
|
|
3442
|
+
const holes = [];
|
|
3443
|
+
for(let i = 0; i < subPaths.length; i++) {
|
|
3444
|
+
if(i !== outerIdx) holes.push(subPaths[i]);
|
|
3445
|
+
}
|
|
3446
|
+
return { outerPoints, holes };
|
|
3447
|
+
}
|
|
3448
|
+
|
|
3449
|
+
// 计算多边形面积(Shoelace 公式)
|
|
3450
|
+
polygonArea(points) {
|
|
3451
|
+
let area = 0;
|
|
3452
|
+
const n = points.length;
|
|
3453
|
+
for(let i = 0; i < n; i++) {
|
|
3454
|
+
const j = (i + 1) % n;
|
|
3455
|
+
area += points[i].x * points[j].y;
|
|
3456
|
+
area -= points[j].x * points[i].y;
|
|
3457
|
+
}
|
|
3458
|
+
return area / 2;
|
|
3459
|
+
}
|
|
3460
|
+
|
|
3461
|
+
// 使用 earcut 带 holes 填充多边形
|
|
3462
|
+
fillWithHoles(outerPoints, holes, isTexture = false) {
|
|
3463
|
+
// 将所有点合并:外轮廓 + 各个洞,并记录洞的起始索引
|
|
3464
|
+
const allPoints = [...outerPoints];
|
|
3465
|
+
const holeIndices = [];
|
|
3466
|
+
for(const hole of holes) {
|
|
3467
|
+
holeIndices.push(allPoints.length);
|
|
3468
|
+
allPoints.push(...hole);
|
|
3469
|
+
}
|
|
3470
|
+
|
|
3471
|
+
const dim = 2;
|
|
3472
|
+
const vertexData = [];
|
|
3473
|
+
for(const p of allPoints) {
|
|
3474
|
+
vertexData.push(p.x, p.y);
|
|
3475
|
+
}
|
|
3476
|
+
|
|
3477
|
+
// 用 earcut 进行带洞三角化
|
|
3478
|
+
const indices = earcut(vertexData, holeIndices, dim);
|
|
3479
|
+
|
|
3480
|
+
if(!indices || indices.length < 3) return;
|
|
3481
|
+
|
|
3482
|
+
// 构建 GPU 顶点数据
|
|
3483
|
+
const allVertices = [];
|
|
3484
|
+
const allTexCoords = [];
|
|
3485
|
+
for(let i = 0; i < indices.length; i++) {
|
|
3486
|
+
const p = allPoints[indices[i]];
|
|
3487
|
+
allVertices.push(p.x, p.y);
|
|
3488
|
+
if(isTexture) allTexCoords.push(p.x, p.y);
|
|
3489
|
+
}
|
|
3490
|
+
|
|
3491
|
+
const gl = this.context;
|
|
3492
|
+
const vertexArr = new Float32Array(allVertices);
|
|
3493
|
+
|
|
3494
|
+
let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
|
|
3495
|
+
if(!posBuffer) {
|
|
3496
|
+
posBuffer = this.createFloat32Buffer(vertexArr, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
3497
|
+
posBuffer.attr = this.program.attrs.a_position;
|
|
3498
|
+
this.__cachedBuffers.push(posBuffer);
|
|
3499
|
+
} else {
|
|
3500
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
|
|
3501
|
+
gl.bufferData(gl.ARRAY_BUFFER, vertexArr, gl.DYNAMIC_DRAW);
|
|
3502
|
+
}
|
|
3503
|
+
this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
|
|
3504
|
+
|
|
3505
|
+
if(isTexture && allTexCoords.length) {
|
|
3506
|
+
const texData = new Float32Array(allTexCoords);
|
|
3507
|
+
let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
|
|
3508
|
+
if(!texBuffer) {
|
|
3509
|
+
texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
3510
|
+
texBuffer.attr = this.program.attrs.a_text_coord;
|
|
3511
|
+
this.__cachedBuffers.push(texBuffer);
|
|
3512
|
+
} else {
|
|
3513
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
|
|
3514
|
+
gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
|
|
3515
|
+
}
|
|
3516
|
+
this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
|
|
3520
|
+
}
|
|
3228
3521
|
// 把path坐标集合转为线段集
|
|
3229
3522
|
pathToLines(points) {
|
|
3230
3523
|
let start = null;
|
|
@@ -3412,9 +3705,10 @@ class WebglPath extends WeblBase {
|
|
|
3412
3705
|
|
|
3413
3706
|
// 分割成一个个规则的三角形,不规则的多边形不全割的话纹理就会没法正确覆盖
|
|
3414
3707
|
getTriangles(points) {
|
|
3415
|
-
|
|
3416
3708
|
this.trianglesCache = this.trianglesCache||(this.trianglesCache={});
|
|
3417
|
-
|
|
3709
|
+
// 快速缓存 key:用长度和首尾点坐标(比 JSON.stringify 快几个数量级)
|
|
3710
|
+
const len = points.length;
|
|
3711
|
+
const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
|
|
3418
3712
|
if(this.trianglesCache[key]) return this.trianglesCache[key];
|
|
3419
3713
|
|
|
3420
3714
|
const res = [];
|
|
@@ -3450,13 +3744,49 @@ class WebglPath extends WeblBase {
|
|
|
3450
3744
|
}
|
|
3451
3745
|
if(points && points.length) {
|
|
3452
3746
|
const regular = lineWidth <= 1.2;
|
|
3453
|
-
|
|
3454
|
-
const
|
|
3455
|
-
|
|
3456
|
-
|
|
3747
|
+
const hasMoveTo = points.some && points.some(p => p.m);
|
|
3748
|
+
const isRing = !hasMoveTo && this.needCut; // 空心形状(jmHArc close=true 时无 m 标记)
|
|
3749
|
+
if(regular && (hasMoveTo || isRing)) {
|
|
3750
|
+
// 有 moveTo 标记或空心形状时,分段绘制每个子路径的 LINE_LOOP
|
|
3751
|
+
// 避免 LINE_LOOP 把不同子路径的点连起来产生拉扯线
|
|
3752
|
+
if(hasMoveTo) {
|
|
3753
|
+
let subPath = [];
|
|
3754
|
+
for(let i = 0; i < points.length; i++) {
|
|
3755
|
+
if(points[i].m && subPath.length > 0) {
|
|
3756
|
+
this.writePoints(subPath);
|
|
3757
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
|
|
3758
|
+
subPath = [];
|
|
3759
|
+
}
|
|
3760
|
+
subPath.push(points[i]);
|
|
3761
|
+
}
|
|
3762
|
+
if(subPath.length > 1) {
|
|
3763
|
+
this.writePoints(subPath);
|
|
3764
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
|
|
3765
|
+
}
|
|
3766
|
+
}
|
|
3767
|
+
else if(isRing) {
|
|
3768
|
+
// 空心形状:前半段为内弧,后半段为外弧(反向),各自 LINE_LOOP
|
|
3769
|
+
const mid = Math.floor(points.length / 2);
|
|
3770
|
+
const inner = points.slice(0, mid);
|
|
3771
|
+
const outer = points.slice(mid);
|
|
3772
|
+
if(inner.length > 1) {
|
|
3773
|
+
this.writePoints(inner);
|
|
3774
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, inner.length);
|
|
3775
|
+
}
|
|
3776
|
+
if(outer.length > 1) {
|
|
3777
|
+
this.writePoints(outer);
|
|
3778
|
+
this.context.drawArrays(this.context.LINE_LOOP, 0, outer.length);
|
|
3779
|
+
}
|
|
3780
|
+
}
|
|
3781
|
+
}
|
|
3782
|
+
else {
|
|
3783
|
+
points = regular? points : this.pathToPoints(points);
|
|
3784
|
+
this.writePoints(points);
|
|
3785
|
+
this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
|
|
3786
|
+
}
|
|
3787
|
+
// buffer 由 endDraw 统一清理
|
|
3457
3788
|
}
|
|
3458
|
-
colorBuffer && this.
|
|
3459
|
-
colorBuffer && this.disableVertexAttribArray(colorBuffer.attr);
|
|
3789
|
+
colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
|
|
3460
3790
|
}
|
|
3461
3791
|
|
|
3462
3792
|
// 填充图形
|
|
@@ -3475,10 +3805,9 @@ class WebglPath extends WeblBase {
|
|
|
3475
3805
|
|
|
3476
3806
|
fillColor(color, points, bounds, type=1) {
|
|
3477
3807
|
|
|
3478
|
-
//
|
|
3808
|
+
// 如果是渐变色,使用 GLSL 着色器直接计算
|
|
3479
3809
|
if(this.isGradient(color)) {
|
|
3480
|
-
|
|
3481
|
-
return this.fillImage(imgData.data, imgData.points, bounds);
|
|
3810
|
+
return this.fillGradient(color, points, bounds);
|
|
3482
3811
|
}
|
|
3483
3812
|
|
|
3484
3813
|
// 标注为fill
|
|
@@ -3487,19 +3816,111 @@ class WebglPath extends WeblBase {
|
|
|
3487
3816
|
|
|
3488
3817
|
this.fillPolygons(points);
|
|
3489
3818
|
|
|
3490
|
-
colorBuffer && this.
|
|
3491
|
-
colorBuffer && this.disableVertexAttribArray(colorBuffer.attr);
|
|
3819
|
+
colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
|
|
3492
3820
|
|
|
3493
3821
|
}
|
|
3494
3822
|
|
|
3823
|
+
/**
|
|
3824
|
+
* 使用 GLSL 着色器渲染渐变填充
|
|
3825
|
+
* 无需 textureCanvas,直接通过 uniform 传递渐变参数给 GPU
|
|
3826
|
+
*/
|
|
3827
|
+
fillGradient(gradient, points, bounds) {
|
|
3828
|
+
const params = gradient.toUniformParams();
|
|
3829
|
+
if(!params) return;
|
|
3830
|
+
|
|
3831
|
+
// 标注为 GLSL 渐变 (type=5)
|
|
3832
|
+
this.context.uniform1i(this.program.uniforms.a_type.location, 5);
|
|
3833
|
+
|
|
3834
|
+
// 设置 globalAlpha(通过 v_single_color.a 传递给着色器)
|
|
3835
|
+
this.context.uniform4f(this.program.uniforms.v_single_color.location, 1.0, 1.0, 1.0, this.style.globalAlpha);
|
|
3836
|
+
|
|
3837
|
+
// 设置渐变类型
|
|
3838
|
+
if(this.program.uniforms.u_gradient_type) {
|
|
3839
|
+
this.context.uniform1i(this.program.uniforms.u_gradient_type.location, params.gradientType);
|
|
3840
|
+
}
|
|
3841
|
+
|
|
3842
|
+
// 设置渐变起点/终点
|
|
3843
|
+
if(this.program.uniforms.u_gradient_start) {
|
|
3844
|
+
this.context.uniform4fv(this.program.uniforms.u_gradient_start.location, params.gradientStart);
|
|
3845
|
+
}
|
|
3846
|
+
if(this.program.uniforms.u_gradient_end) {
|
|
3847
|
+
this.context.uniform4fv(this.program.uniforms.u_gradient_end.location, params.gradientEnd);
|
|
3848
|
+
}
|
|
3849
|
+
|
|
3850
|
+
// 设置颜色断点数量
|
|
3851
|
+
if(this.program.uniforms.u_gradient_stop_count) {
|
|
3852
|
+
this.context.uniform1i(this.program.uniforms.u_gradient_stop_count.location, params.stopCount);
|
|
3853
|
+
}
|
|
3854
|
+
|
|
3855
|
+
// 设置每个 stop 的 offset
|
|
3856
|
+
// 关键:必须填充完整的 MAX_STOPS 长度数组,否则未初始化元素默认为 0
|
|
3857
|
+
// 会导致着色器循环中 t >= 0 始终为 true,返回黑色
|
|
3858
|
+
if(this.program.uniforms.u_gradient_offsets) {
|
|
3859
|
+
const offsets = new Float32Array(MAX_STOPS);
|
|
3860
|
+
for(let i = 0; i < params.stopCount; i++) {
|
|
3861
|
+
offsets[i] = params.stops[i * 5];
|
|
3862
|
+
}
|
|
3863
|
+
// 用 2.0 填充剩余项,使 t(0~1) >= 2.0 为 false,不会被匹配
|
|
3864
|
+
for(let i = params.stopCount; i < MAX_STOPS; i++) {
|
|
3865
|
+
offsets[i] = 2.0;
|
|
3866
|
+
}
|
|
3867
|
+
this.context.uniform1fv(this.program.uniforms.u_gradient_offsets.location, offsets);
|
|
3868
|
+
}
|
|
3869
|
+
|
|
3870
|
+
// 设置每个 stop 的颜色 (rgba)
|
|
3871
|
+
if(this.program.uniforms.u_gradient_colors) {
|
|
3872
|
+
const colors = new Float32Array(MAX_STOPS * 4);
|
|
3873
|
+
for(let i = 0; i < params.stopCount; i++) {
|
|
3874
|
+
colors[i * 4 + 0] = params.stops[i * 5 + 1]; // r
|
|
3875
|
+
colors[i * 4 + 1] = params.stops[i * 5 + 2]; // g
|
|
3876
|
+
colors[i * 4 + 2] = params.stops[i * 5 + 3]; // b
|
|
3877
|
+
colors[i * 4 + 3] = params.stops[i * 5 + 4]; // a
|
|
3878
|
+
}
|
|
3879
|
+
// 用最后一个 stop 的颜色填充剩余项,确保不会返回黑色
|
|
3880
|
+
if(params.stopCount > 0) {
|
|
3881
|
+
const lastR = params.stops[(params.stopCount - 1) * 5 + 1];
|
|
3882
|
+
const lastG = params.stops[(params.stopCount - 1) * 5 + 2];
|
|
3883
|
+
const lastB = params.stops[(params.stopCount - 1) * 5 + 3];
|
|
3884
|
+
const lastA = params.stops[(params.stopCount - 1) * 5 + 4];
|
|
3885
|
+
for(let i = params.stopCount; i < MAX_STOPS; i++) {
|
|
3886
|
+
colors[i * 4 + 0] = lastR;
|
|
3887
|
+
colors[i * 4 + 1] = lastG;
|
|
3888
|
+
colors[i * 4 + 2] = lastB;
|
|
3889
|
+
colors[i * 4 + 3] = lastA;
|
|
3890
|
+
}
|
|
3891
|
+
}
|
|
3892
|
+
this.context.uniform4fv(this.program.uniforms.u_gradient_colors.location, colors);
|
|
3893
|
+
}
|
|
3894
|
+
|
|
3895
|
+
// 填充多边形(需要纹理坐标来计算渐变位置)
|
|
3896
|
+
this.fillPolygons(points, true);
|
|
3897
|
+
this.disableVertexAttribArray(this.program.attrs.a_text_coord);
|
|
3898
|
+
}
|
|
3899
|
+
|
|
3495
3900
|
// 区域填充图片
|
|
3496
3901
|
// points绘制的图形顶点
|
|
3497
3902
|
// 图片整体绘制区域
|
|
3498
3903
|
fillImage(img, points, bounds) {
|
|
3499
3904
|
if(!img) return;
|
|
3500
3905
|
|
|
3501
|
-
//
|
|
3502
|
-
|
|
3906
|
+
// 对于 ImageData,生成缓存 key(基于渐变参数或 bounds),复用纹理
|
|
3907
|
+
let texture = null;
|
|
3908
|
+
if(img instanceof ImageData) {
|
|
3909
|
+
const key = `${img.width}_${img.height}_${bounds.width}_${bounds.height}_${bounds.left}_${bounds.top}`;
|
|
3910
|
+
if(this.__cachedTexture && this.__cachedTextureKey === key) {
|
|
3911
|
+
texture = this.__cachedTexture;
|
|
3912
|
+
} else {
|
|
3913
|
+
texture = this.createDataTexture(img);
|
|
3914
|
+
// 释放旧纹理
|
|
3915
|
+
if(this.__cachedTexture) {
|
|
3916
|
+
this.deleteTexture(this.__cachedTexture);
|
|
3917
|
+
}
|
|
3918
|
+
this.__cachedTexture = texture;
|
|
3919
|
+
this.__cachedTextureKey = key;
|
|
3920
|
+
}
|
|
3921
|
+
} else {
|
|
3922
|
+
texture = this.createImgTexture(img);
|
|
3923
|
+
}
|
|
3503
3924
|
this.context.uniform1i(this.program.uniforms.u_sample.location, 0); // 纹理单元传递给着色器
|
|
3504
3925
|
|
|
3505
3926
|
// 指定纹理区域尺寸
|
|
@@ -3512,7 +3933,10 @@ class WebglPath extends WeblBase {
|
|
|
3512
3933
|
|
|
3513
3934
|
this.fillTexture(points);
|
|
3514
3935
|
|
|
3515
|
-
|
|
3936
|
+
// 仅对非缓存纹理(非 ImageData)立即删除
|
|
3937
|
+
if(!(img instanceof ImageData)) {
|
|
3938
|
+
this.deleteTexture(texture);
|
|
3939
|
+
}
|
|
3516
3940
|
}
|
|
3517
3941
|
|
|
3518
3942
|
fillTexture(points) {
|
|
@@ -3528,23 +3952,90 @@ class WebglPath extends WeblBase {
|
|
|
3528
3952
|
|
|
3529
3953
|
// 进行多边形填充
|
|
3530
3954
|
fillPolygons(points, isTexture = false) {
|
|
3531
|
-
if(points.length
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3955
|
+
if(points.length <= 3) {
|
|
3956
|
+
// 3个点以下的三角形直接画
|
|
3957
|
+
this.writePoints(points);
|
|
3958
|
+
isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
|
|
3959
|
+
this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
|
|
3960
|
+
return;
|
|
3961
|
+
}
|
|
3962
|
+
|
|
3963
|
+
// 规则图形(凸多边形,如圆):直接用 TRIANGLE_FAN 一次性绘制,无需 earcut
|
|
3964
|
+
if(this.isRegular) {
|
|
3965
|
+
// 检查是否有 moveTo 标记,如果有说明路径包含多个子路径(如空心圆弧 jmHArc)
|
|
3966
|
+
const hasMoveTo = points.some && points.some(p => p.m);
|
|
3967
|
+
if(hasMoveTo) {
|
|
3968
|
+
// 有 m 标记:按 m 标记拆分子路径
|
|
3969
|
+
const { outerPoints, holes } = this.splitSubPaths(points);
|
|
3970
|
+
this.fillWithHoles(outerPoints, holes, isTexture);
|
|
3971
|
+
return;
|
|
3972
|
+
}
|
|
3973
|
+
// 无 m 标记但 needCut=true 表示空心形状(如 jmHArc close=true)
|
|
3974
|
+
// 前半段为内弧,后半段为外弧(反向),按中点拆分
|
|
3975
|
+
if(this.needCut && points.length >= 6) {
|
|
3976
|
+
const mid = Math.floor(points.length / 2);
|
|
3977
|
+
const inner = points.slice(0, mid);
|
|
3978
|
+
const outer = points.slice(mid);
|
|
3979
|
+
const innerArea = Math.abs(this.polygonArea(inner));
|
|
3980
|
+
const outerArea = Math.abs(this.polygonArea(outer));
|
|
3981
|
+
if(outerArea >= innerArea) {
|
|
3982
|
+
this.fillWithHoles(outer, [inner], isTexture);
|
|
3983
|
+
} else {
|
|
3984
|
+
this.fillWithHoles(inner, [outer], isTexture);
|
|
3536
3985
|
}
|
|
3986
|
+
return;
|
|
3537
3987
|
}
|
|
3988
|
+
this.writePoints(points);
|
|
3989
|
+
isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
|
|
3990
|
+
this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
|
|
3991
|
+
return;
|
|
3538
3992
|
}
|
|
3539
|
-
else {
|
|
3540
|
-
const buffer = this.writePoints(points);
|
|
3541
|
-
// 纹理坐标
|
|
3542
|
-
const coordBuffer = isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
|
|
3543
3993
|
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3994
|
+
// 不规则图形:需要 earcut 三角化后,合并为一个大的顶点缓冲区,单次 drawArrays
|
|
3995
|
+
const triangles = this.needCut? this.earCutPointsToTriangles(points): this.getTriangles(points);
|
|
3996
|
+
if(!triangles.length) return;
|
|
3997
|
+
|
|
3998
|
+
// 合并所有三角形的顶点到一个数组
|
|
3999
|
+
const allVertices = [];
|
|
4000
|
+
const allTexCoords = [];
|
|
4001
|
+
for(const triangle of triangles) {
|
|
4002
|
+
for(const p of triangle) {
|
|
4003
|
+
allVertices.push(p.x, p.y);
|
|
4004
|
+
if(isTexture) allTexCoords.push(p.x, p.y);
|
|
4005
|
+
}
|
|
4006
|
+
}
|
|
4007
|
+
|
|
4008
|
+
// 一次性上传所有数据并绘制
|
|
4009
|
+
const vertexData = new Float32Array(allVertices);
|
|
4010
|
+
const gl = this.context;
|
|
4011
|
+
|
|
4012
|
+
// 复用或创建 position buffer
|
|
4013
|
+
let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
|
|
4014
|
+
if(!posBuffer) {
|
|
4015
|
+
posBuffer = this.createFloat32Buffer(vertexData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
4016
|
+
posBuffer.attr = this.program.attrs.a_position;
|
|
4017
|
+
this.__cachedBuffers.push(posBuffer);
|
|
4018
|
+
} else {
|
|
4019
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
|
|
4020
|
+
gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.DYNAMIC_DRAW);
|
|
4021
|
+
}
|
|
4022
|
+
this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
|
|
4023
|
+
|
|
4024
|
+
if(isTexture && allTexCoords.length) {
|
|
4025
|
+
const texData = new Float32Array(allTexCoords);
|
|
4026
|
+
let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
|
|
4027
|
+
if(!texBuffer) {
|
|
4028
|
+
texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
|
|
4029
|
+
texBuffer.attr = this.program.attrs.a_text_coord;
|
|
4030
|
+
this.__cachedBuffers.push(texBuffer);
|
|
4031
|
+
} else {
|
|
4032
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
|
|
4033
|
+
gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
|
|
4034
|
+
}
|
|
4035
|
+
this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
|
|
3547
4036
|
}
|
|
4037
|
+
|
|
4038
|
+
gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
|
|
3548
4039
|
}
|
|
3549
4040
|
|
|
3550
4041
|
// 填充图形
|
|
@@ -3561,46 +4052,73 @@ class WebglPath extends WeblBase {
|
|
|
3561
4052
|
}
|
|
3562
4053
|
|
|
3563
4054
|
drawText(text, x, y, bounds) {
|
|
3564
|
-
|
|
4055
|
+
// 文本渲染仍需要 2D canvas 绘制字形,然后作为纹理上传
|
|
4056
|
+
// 使用临时 canvas,不依赖共享的 textureCanvas
|
|
4057
|
+
if(!bounds.width || !bounds.height) return null;
|
|
4058
|
+
if(typeof document === 'undefined') return null;
|
|
4059
|
+
|
|
4060
|
+
let canvas = this.__textCanvas;
|
|
3565
4061
|
if(!canvas) {
|
|
3566
|
-
|
|
4062
|
+
canvas = document.createElement('canvas');
|
|
4063
|
+
this.__textCanvas = canvas;
|
|
3567
4064
|
}
|
|
3568
4065
|
canvas.width = bounds.width;
|
|
3569
4066
|
canvas.height = bounds.height;
|
|
3570
4067
|
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
}
|
|
4068
|
+
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
|
4069
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
3574
4070
|
|
|
3575
|
-
this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
|
|
3576
4071
|
// 修改字体
|
|
3577
|
-
|
|
4072
|
+
ctx.font = this.style.font || (this.style.fontSize + 'px ' + this.style.fontFamily);
|
|
3578
4073
|
|
|
3579
4074
|
x -= bounds.left;
|
|
3580
4075
|
y -= bounds.top;
|
|
3581
4076
|
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
4077
|
+
// 设置文本样式
|
|
4078
|
+
if(this.style.fillStyle) {
|
|
4079
|
+
ctx.fillStyle = this.graph.utils.toColor(this.style.fillStyle);
|
|
4080
|
+
}
|
|
4081
|
+
if(this.style.strokeStyle) {
|
|
4082
|
+
ctx.strokeStyle = this.graph.utils.toColor(this.style.strokeStyle);
|
|
4083
|
+
}
|
|
4084
|
+
if(this.style.shadowColor) {
|
|
4085
|
+
ctx.shadowColor = this.graph.utils.toColor(this.style.shadowColor);
|
|
4086
|
+
}
|
|
4087
|
+
if(this.style.shadowBlur) {
|
|
4088
|
+
ctx.shadowBlur = this.style.shadowBlur;
|
|
4089
|
+
}
|
|
4090
|
+
if(this.style.shadowOffsetX !== undefined) {
|
|
4091
|
+
ctx.shadowOffsetX = this.style.shadowOffsetX;
|
|
4092
|
+
}
|
|
4093
|
+
if(this.style.shadowOffsetY !== undefined) {
|
|
4094
|
+
ctx.shadowOffsetY = this.style.shadowOffsetY;
|
|
4095
|
+
}
|
|
4096
|
+
if(this.style.textAlign) {
|
|
4097
|
+
ctx.textAlign = this.style.textAlign;
|
|
4098
|
+
}
|
|
4099
|
+
if(this.style.textBaseline) {
|
|
4100
|
+
ctx.textBaseline = this.style.textBaseline;
|
|
4101
|
+
}
|
|
3585
4102
|
|
|
4103
|
+
if(this.style.fillStyle && ctx.fillText) {
|
|
3586
4104
|
if(this.style.maxWidth) {
|
|
3587
|
-
|
|
4105
|
+
ctx.fillText(text, x, y, this.style.maxWidth);
|
|
3588
4106
|
}
|
|
3589
4107
|
else {
|
|
3590
|
-
|
|
4108
|
+
ctx.fillText(text, x, y);
|
|
3591
4109
|
}
|
|
3592
4110
|
}
|
|
3593
|
-
if(this.
|
|
3594
|
-
|
|
4111
|
+
if(this.style.strokeStyle && ctx.strokeText) {
|
|
3595
4112
|
if(this.style.maxWidth) {
|
|
3596
|
-
|
|
4113
|
+
ctx.strokeText(text, x, y, this.style.maxWidth);
|
|
3597
4114
|
}
|
|
3598
4115
|
else {
|
|
3599
|
-
|
|
4116
|
+
ctx.strokeText(text, x, y);
|
|
3600
4117
|
}
|
|
3601
4118
|
}
|
|
4119
|
+
|
|
3602
4120
|
// 用纹理图片代替文字
|
|
3603
|
-
const data =
|
|
4121
|
+
const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
3604
4122
|
this.fillImage(data, this.points, bounds);
|
|
3605
4123
|
}
|
|
3606
4124
|
}
|
|
@@ -3626,7 +4144,9 @@ const jmStyleMap = {
|
|
|
3626
4144
|
'shadowOffsetY' : 'shadowOffsetY',
|
|
3627
4145
|
'shadowColor' : 'shadowColor',
|
|
3628
4146
|
'lineJoin': 'lineJoin',
|
|
3629
|
-
'lineCap':'lineCap'
|
|
4147
|
+
'lineCap':'lineCap',
|
|
4148
|
+
'lineDashOffset': 'lineDashOffset',
|
|
4149
|
+
'globalCompositeOperation': 'globalCompositeOperation'
|
|
3630
4150
|
};
|
|
3631
4151
|
|
|
3632
4152
|
class jmControl extends jmProperty {
|
|
@@ -3837,7 +4357,9 @@ class jmControl extends jmProperty {
|
|
|
3837
4357
|
}
|
|
3838
4358
|
|
|
3839
4359
|
setStyle(style) {
|
|
3840
|
-
|
|
4360
|
+
if(!style) {
|
|
4361
|
+
style = this.style;
|
|
4362
|
+
}
|
|
3841
4363
|
if(!style) return;
|
|
3842
4364
|
|
|
3843
4365
|
const __setStyle = (style, name, mpkey) => {
|
|
@@ -3921,6 +4443,120 @@ class jmControl extends jmProperty {
|
|
|
3921
4443
|
this.cursor = styleValue;
|
|
3922
4444
|
break;
|
|
3923
4445
|
}
|
|
4446
|
+
// ===== 新增样式特性 =====
|
|
4447
|
+
|
|
4448
|
+
// 虚线样式:支持自定义lineDash模式 (如 [5, 3, 2] 或 "5,3,2")
|
|
4449
|
+
case 'lineDash' : {
|
|
4450
|
+
if(!this.context.setLineDash) break;
|
|
4451
|
+
let dash;
|
|
4452
|
+
if(typeof styleValue === 'string') {
|
|
4453
|
+
dash = styleValue.split(',').map(v => parseFloat(v.trim())).filter(v => !isNaN(v));
|
|
4454
|
+
}
|
|
4455
|
+
else if(Array.isArray(styleValue)) {
|
|
4456
|
+
dash = styleValue.map(v => parseFloat(v)).filter(v => !isNaN(v));
|
|
4457
|
+
}
|
|
4458
|
+
if(dash && dash.length) {
|
|
4459
|
+
this.context.setLineDash(dash);
|
|
4460
|
+
}
|
|
4461
|
+
else {
|
|
4462
|
+
this.context.setLineDash([]);
|
|
4463
|
+
}
|
|
4464
|
+
break;
|
|
4465
|
+
}
|
|
4466
|
+
// 虚线偏移量
|
|
4467
|
+
case 'lineDashOffset' : {
|
|
4468
|
+
if(!this.context.setLineDash) break;
|
|
4469
|
+
this.context.lineDashOffset = Number(styleValue) || 0;
|
|
4470
|
+
break;
|
|
4471
|
+
}
|
|
4472
|
+
// CSS滤镜效果 (blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity)
|
|
4473
|
+
case 'filter' : {
|
|
4474
|
+
if(this.context.filter === undefined) break;
|
|
4475
|
+
if(styleValue instanceof jmFilter) {
|
|
4476
|
+
this.context.filter = styleValue.toCanvasFilter();
|
|
4477
|
+
}
|
|
4478
|
+
else if(typeof styleValue === 'string') {
|
|
4479
|
+
this.context.filter = styleValue || 'none';
|
|
4480
|
+
}
|
|
4481
|
+
else if(typeof styleValue === 'object') {
|
|
4482
|
+
this.context.filter = (new jmFilter(styleValue)).toCanvasFilter();
|
|
4483
|
+
}
|
|
4484
|
+
break;
|
|
4485
|
+
}
|
|
4486
|
+
// 混合模式 (source-over, multiply, screen, overlay, darken, lighten, etc.)
|
|
4487
|
+
case 'globalCompositeOperation' : {
|
|
4488
|
+
if(!this.context.globalCompositeOperation) break;
|
|
4489
|
+
this.context.globalCompositeOperation = styleValue;
|
|
4490
|
+
break;
|
|
4491
|
+
}
|
|
4492
|
+
// 裁剪路径:通过canvas clip实现
|
|
4493
|
+
case 'clipPath' : {
|
|
4494
|
+
if(!this.context.clip) break;
|
|
4495
|
+
// clipPath可以是一个图形控件实例
|
|
4496
|
+
if(styleValue && styleValue.points && styleValue.points.length > 0) {
|
|
4497
|
+
const bounds = this.parent && this.parent.absoluteBounds ? this.parent.absoluteBounds : this.absoluteBounds;
|
|
4498
|
+
this.context.beginPath();
|
|
4499
|
+
this.context.moveTo(styleValue.points[0].x + (bounds ? bounds.left : 0), styleValue.points[0].y + (bounds ? bounds.top : 0));
|
|
4500
|
+
for(let i = 1; i < styleValue.points.length; i++) {
|
|
4501
|
+
if(styleValue.points[i].m) {
|
|
4502
|
+
this.context.moveTo(styleValue.points[i].x + (bounds ? bounds.left : 0), styleValue.points[i].y + (bounds ? bounds.top : 0));
|
|
4503
|
+
}
|
|
4504
|
+
else {
|
|
4505
|
+
this.context.lineTo(styleValue.points[i].x + (bounds ? bounds.left : 0), styleValue.points[i].y + (bounds ? bounds.top : 0));
|
|
4506
|
+
}
|
|
4507
|
+
}
|
|
4508
|
+
if(styleValue.style && styleValue.style.close) {
|
|
4509
|
+
this.context.closePath();
|
|
4510
|
+
}
|
|
4511
|
+
this.context.clip();
|
|
4512
|
+
}
|
|
4513
|
+
break;
|
|
4514
|
+
}
|
|
4515
|
+
// 遮罩效果:通过globalCompositeOperation + destination-in实现
|
|
4516
|
+
case 'mask' : {
|
|
4517
|
+
if(!this.context.globalCompositeOperation) break;
|
|
4518
|
+
// mask是一个图形控件实例,在绘制前需要先应用mask
|
|
4519
|
+
// 这里只是标记,实际绘制在paint流程中处理
|
|
4520
|
+
this.__mask = styleValue;
|
|
4521
|
+
break;
|
|
4522
|
+
}
|
|
4523
|
+
// 图片阴影描边阴影(WebGL纹理canvas用)
|
|
4524
|
+
case 'shadowColor' : {
|
|
4525
|
+
if(this.webglControl) {
|
|
4526
|
+
this.webglControl.setStyle('shadowColor', styleValue);
|
|
4527
|
+
}
|
|
4528
|
+
else {
|
|
4529
|
+
this.context.shadowColor = jmUtils.toColor(styleValue);
|
|
4530
|
+
}
|
|
4531
|
+
break;
|
|
4532
|
+
}
|
|
4533
|
+
case 'shadowBlur' : {
|
|
4534
|
+
if(this.webglControl) {
|
|
4535
|
+
this.webglControl.setStyle('shadowBlur', styleValue);
|
|
4536
|
+
}
|
|
4537
|
+
else {
|
|
4538
|
+
this.context.shadowBlur = Number(styleValue) || 0;
|
|
4539
|
+
}
|
|
4540
|
+
break;
|
|
4541
|
+
}
|
|
4542
|
+
case 'shadowOffsetX' : {
|
|
4543
|
+
if(this.webglControl) {
|
|
4544
|
+
this.webglControl.setStyle('shadowOffsetX', styleValue);
|
|
4545
|
+
}
|
|
4546
|
+
else {
|
|
4547
|
+
this.context.shadowOffsetX = Number(styleValue) || 0;
|
|
4548
|
+
}
|
|
4549
|
+
break;
|
|
4550
|
+
}
|
|
4551
|
+
case 'shadowOffsetY' : {
|
|
4552
|
+
if(this.webglControl) {
|
|
4553
|
+
this.webglControl.setStyle('shadowOffsetY', styleValue);
|
|
4554
|
+
}
|
|
4555
|
+
else {
|
|
4556
|
+
this.context.shadowOffsetY = Number(styleValue) || 0;
|
|
4557
|
+
}
|
|
4558
|
+
break;
|
|
4559
|
+
}
|
|
3924
4560
|
}
|
|
3925
4561
|
}
|
|
3926
4562
|
}
|
|
@@ -3941,6 +4577,9 @@ class jmControl extends jmProperty {
|
|
|
3941
4577
|
else if(t == 'string' && k == 'shadow') {
|
|
3942
4578
|
style[k] = new jmShadow(style[k]);
|
|
3943
4579
|
}
|
|
4580
|
+
else if(t == 'string' && k == 'filter') {
|
|
4581
|
+
style[k] = new jmFilter(style[k]);
|
|
4582
|
+
}
|
|
3944
4583
|
__setStyle(style[k], k);
|
|
3945
4584
|
}
|
|
3946
4585
|
}
|
|
@@ -4085,25 +4724,30 @@ class jmControl extends jmProperty {
|
|
|
4085
4724
|
* @method getLocation
|
|
4086
4725
|
* @return {object} 当前控件位置参数,包括中心点坐标,右上角坐标,宽高
|
|
4087
4726
|
*/
|
|
4088
|
-
getLocation(
|
|
4727
|
+
getLocation() {
|
|
4089
4728
|
//如果已经计算过则直接返回
|
|
4090
4729
|
//在开画之前会清空此对象
|
|
4091
4730
|
//if(reset !== true && this.location) return this.location;
|
|
4092
4731
|
|
|
4093
4732
|
let local = this.location = {left: 0,top: 0,width: 0,height: 0};
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4097
|
-
|
|
4733
|
+
|
|
4734
|
+
// 检查是否有百分比参数需要解析,没有则直接引用避免克隆开销
|
|
4735
|
+
const needResolve = this.parent && (jmUtils.checkPercent(this.width) || jmUtils.checkPercent(this.height) ||
|
|
4736
|
+
(this.position && jmUtils.checkPercent(this.position.x)) || (this.position && jmUtils.checkPercent(this.position.y)));
|
|
4737
|
+
local.position = typeof this.position == 'function'? this.position(): (needResolve? jmUtils.clone(this.position) : this.position);
|
|
4738
|
+
local.center = this.center && typeof this.center === 'function'?this.center(): (needResolve? jmUtils.clone(this.center) : this.center);//中心
|
|
4739
|
+
local.start = this.start && typeof this.start === 'function'?this.start(): (needResolve? jmUtils.clone(this.start) : this.start);//起点
|
|
4740
|
+
local.end = this.end && typeof this.end === 'function'?this.end(): (needResolve? jmUtils.clone(this.end) : this.end);//起点
|
|
4098
4741
|
local.radius = this.radius;//半径
|
|
4099
4742
|
local.width = this.width;
|
|
4100
4743
|
local.height = this.height;
|
|
4101
4744
|
|
|
4102
|
-
const margin =
|
|
4103
|
-
margin.
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4745
|
+
const margin = this.style.margin;
|
|
4746
|
+
const marginObj = needResolve && margin ? jmUtils.clone(margin, {}) : (margin || {});
|
|
4747
|
+
marginObj.left = (marginObj.left || 0);
|
|
4748
|
+
marginObj.top = (marginObj.top || 0);
|
|
4749
|
+
marginObj.right = (marginObj.right || 0);
|
|
4750
|
+
marginObj.bottom = (marginObj.bottom || 0);
|
|
4107
4751
|
|
|
4108
4752
|
//如果没有指定位置,但指定了margin。则位置取margin偏移量
|
|
4109
4753
|
if(local.position) {
|
|
@@ -4111,8 +4755,8 @@ class jmControl extends jmProperty {
|
|
|
4111
4755
|
local.top = local.position.y;
|
|
4112
4756
|
}
|
|
4113
4757
|
else {
|
|
4114
|
-
local.left =
|
|
4115
|
-
local.top =
|
|
4758
|
+
local.left = marginObj.left;
|
|
4759
|
+
local.top = marginObj.top;
|
|
4116
4760
|
}
|
|
4117
4761
|
|
|
4118
4762
|
if(this.parent) {
|
|
@@ -4374,22 +5018,31 @@ class jmControl extends jmProperty {
|
|
|
4374
5018
|
if(this.webglControl) this.webglControl.closePath();
|
|
4375
5019
|
this.context.closePath && this.context.closePath();
|
|
4376
5020
|
}
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
if(
|
|
4380
|
-
|
|
5021
|
+
|
|
5022
|
+
// 根据渲染模式选择不同的绘制路径
|
|
5023
|
+
if(this.webglControl) {
|
|
5024
|
+
// WebGL 模式:使用 WebGL 绘制
|
|
5025
|
+
const fill = this.style['fill'] || this.style['fillStyle'];
|
|
5026
|
+
if(fill) {
|
|
4381
5027
|
const bounds = this.getBounds();
|
|
4382
5028
|
this.webglControl.fill(bounds);
|
|
4383
5029
|
}
|
|
4384
|
-
this.
|
|
5030
|
+
if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
|
|
5031
|
+
this.webglControl.stroke();
|
|
5032
|
+
}
|
|
5033
|
+
if(this.webglControl.endDraw) this.webglControl.endDraw();
|
|
4385
5034
|
}
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
this.
|
|
5035
|
+
else {
|
|
5036
|
+
// 2D 模式:使用 Canvas 2D API 绘制
|
|
5037
|
+
const fill = this.style['fill'] || this.style['fillStyle'];
|
|
5038
|
+
if(fill) {
|
|
5039
|
+
this.context.fill && this.context.fill();
|
|
5040
|
+
}
|
|
5041
|
+
if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
|
|
5042
|
+
this.context.stroke && this.context.stroke();
|
|
5043
|
+
}
|
|
4389
5044
|
}
|
|
4390
5045
|
|
|
4391
|
-
if(this.webglControl && this.webglControl.endDraw) this.webglControl.endDraw();
|
|
4392
|
-
|
|
4393
5046
|
this.needUpdate = false;
|
|
4394
5047
|
}
|
|
4395
5048
|
|
|
@@ -4405,9 +5058,7 @@ class jmControl extends jmProperty {
|
|
|
4405
5058
|
const bounds = this.parent && this.parent.absoluteBounds?this.parent.absoluteBounds:this.absoluteBounds;
|
|
4406
5059
|
if(this.webglControl) {
|
|
4407
5060
|
this.webglControl.setParentBounds(bounds);
|
|
4408
|
-
this.webglControl.draw(
|
|
4409
|
-
...this.points
|
|
4410
|
-
]);
|
|
5061
|
+
this.webglControl.draw(this.points);
|
|
4411
5062
|
}
|
|
4412
5063
|
else if(this.context && this.context.moveTo) {
|
|
4413
5064
|
this.context.moveTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
|
|
@@ -4452,9 +5103,44 @@ class jmControl extends jmProperty {
|
|
|
4452
5103
|
|
|
4453
5104
|
this.setStyle();//设定样式
|
|
4454
5105
|
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
5106
|
+
// 应用mask遮罩效果:在mask区域内绘制当前控件
|
|
5107
|
+
// 使用 destination-in 合成模式,只保留mask区域内的内容
|
|
5108
|
+
const maskStyle = this.style.mask || this.__mask;
|
|
5109
|
+
if(maskStyle && maskStyle.points && this.context.globalCompositeOperation) {
|
|
5110
|
+
// 先绘制当前控件
|
|
5111
|
+
if(needDraw && this.beginDraw) this.beginDraw();
|
|
5112
|
+
if(needDraw && this.draw) this.draw();
|
|
5113
|
+
if(needDraw && this.endDraw) this.endDraw();
|
|
5114
|
+
|
|
5115
|
+
// 再应用mask裁剪
|
|
5116
|
+
this.context.globalCompositeOperation = 'destination-in';
|
|
5117
|
+
if(maskStyle.initPoints) maskStyle.initPoints();
|
|
5118
|
+
const mBounds = maskStyle.parent && maskStyle.parent.absoluteBounds ? maskStyle.parent.absoluteBounds : this.absoluteBounds;
|
|
5119
|
+
this.context.beginPath();
|
|
5120
|
+
if(maskStyle.points && maskStyle.points.length > 0) {
|
|
5121
|
+
this.context.moveTo(maskStyle.points[0].x + (mBounds ? mBounds.left : 0), maskStyle.points[0].y + (mBounds ? mBounds.top : 0));
|
|
5122
|
+
for(let i = 1; i < maskStyle.points.length; i++) {
|
|
5123
|
+
if(maskStyle.points[i].m) {
|
|
5124
|
+
this.context.moveTo(maskStyle.points[i].x + (mBounds ? mBounds.left : 0), maskStyle.points[i].y + (mBounds ? mBounds.top : 0));
|
|
5125
|
+
}
|
|
5126
|
+
else {
|
|
5127
|
+
this.context.lineTo(maskStyle.points[i].x + (mBounds ? mBounds.left : 0), maskStyle.points[i].y + (mBounds ? mBounds.top : 0));
|
|
5128
|
+
}
|
|
5129
|
+
}
|
|
5130
|
+
if(maskStyle.style && maskStyle.style.close) {
|
|
5131
|
+
this.context.closePath();
|
|
5132
|
+
}
|
|
5133
|
+
}
|
|
5134
|
+
this.context.fillStyle = '#ffffff';
|
|
5135
|
+
this.context.fill();
|
|
5136
|
+
// 恢复合成模式
|
|
5137
|
+
this.context.globalCompositeOperation = 'source-over';
|
|
5138
|
+
}
|
|
5139
|
+
else {
|
|
5140
|
+
if(needDraw && this.beginDraw) this.beginDraw();
|
|
5141
|
+
if(needDraw && this.draw) this.draw();
|
|
5142
|
+
if(needDraw && this.endDraw) this.endDraw();
|
|
5143
|
+
}
|
|
4458
5144
|
|
|
4459
5145
|
if(this.children) {
|
|
4460
5146
|
this.children.each(function(i,item) {
|
|
@@ -4544,7 +5230,15 @@ class jmControl extends jmProperty {
|
|
|
4544
5230
|
* @param {array} args 事件参数数组
|
|
4545
5231
|
*/
|
|
4546
5232
|
emit(...args) {
|
|
4547
|
-
|
|
5233
|
+
// 避免每帧 args.slice(1) 分配临时数组
|
|
5234
|
+
// runEventHandle 内部会把非数组参数包装成数组
|
|
5235
|
+
if(args.length > 2) {
|
|
5236
|
+
this.runEventHandle(args[0], args.slice(1));
|
|
5237
|
+
} else if(args.length === 2) {
|
|
5238
|
+
this.runEventHandle(args[0], [args[1]]);
|
|
5239
|
+
} else {
|
|
5240
|
+
this.runEventHandle(args[0], []);
|
|
5241
|
+
}
|
|
4548
5242
|
return this;
|
|
4549
5243
|
}
|
|
4550
5244
|
|
|
@@ -5184,7 +5878,6 @@ class jmArc extends jmPath {
|
|
|
5184
5878
|
if((mw == 0 && mh == 0) || start == end) return;
|
|
5185
5879
|
|
|
5186
5880
|
let anticlockwise = this.anticlockwise;
|
|
5187
|
-
this.points = [];
|
|
5188
5881
|
let step = 1 / Math.max(mw, mh);
|
|
5189
5882
|
|
|
5190
5883
|
//如果是逆时针绘制,则角度为负数,并且结束角为2Math.PI-end
|
|
@@ -5195,18 +5888,33 @@ class jmArc extends jmPath {
|
|
|
5195
5888
|
}
|
|
5196
5889
|
if(start > end) step = -step;
|
|
5197
5890
|
|
|
5198
|
-
|
|
5891
|
+
// 预计算需要的点数量
|
|
5892
|
+
let pointCount = Math.ceil(Math.abs(end - start) / Math.abs(step)) + 1;
|
|
5893
|
+
if(this.isFan) pointCount++;
|
|
5894
|
+
|
|
5895
|
+
// 复用已有数组,避免每帧分配;大小变化时才重建
|
|
5896
|
+
if(!this.points || this.points.length !== pointCount) {
|
|
5897
|
+
this.points = new Array(pointCount);
|
|
5898
|
+
for(let i = 0; i < pointCount; i++) {
|
|
5899
|
+
this.points[i] = { x: 0, y: 0 };
|
|
5900
|
+
}
|
|
5901
|
+
}
|
|
5902
|
+
|
|
5903
|
+
let idx = 0;
|
|
5904
|
+
if(this.isFan) {
|
|
5905
|
+
this.points[idx].x = location.center.x;
|
|
5906
|
+
this.points[idx].y = location.center.y;
|
|
5907
|
+
idx++;
|
|
5908
|
+
}
|
|
5199
5909
|
|
|
5200
5910
|
//椭圆方程x=a*cos(r) ,y=b*sin(r)
|
|
5201
5911
|
for(let r=start;;r += step) {
|
|
5202
5912
|
if(step > 0 && r > end) r = end;
|
|
5203
5913
|
else if(step < 0 && r < end) r = end;
|
|
5204
5914
|
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
};
|
|
5209
|
-
this.points.push(p);
|
|
5915
|
+
this.points[idx].x = Math.cos(r) * mw + cx;
|
|
5916
|
+
this.points[idx].y = Math.sin(r) * mh + cy;
|
|
5917
|
+
idx++;
|
|
5210
5918
|
|
|
5211
5919
|
if(r == end) break;
|
|
5212
5920
|
}
|
|
@@ -5643,8 +6351,8 @@ class jmHArc extends jmArc {
|
|
|
5643
6351
|
|
|
5644
6352
|
maxps.reverse();//大圆逆序
|
|
5645
6353
|
if(!this.style || !this.style.close) {
|
|
5646
|
-
maxps[0].m = true
|
|
5647
|
-
}
|
|
6354
|
+
maxps[0].m = true;//非闭合时标记 moveTo,分隔内外两个子路径
|
|
6355
|
+
}
|
|
5648
6356
|
this.points = minps.concat(maxps);
|
|
5649
6357
|
}
|
|
5650
6358
|
}
|
|
@@ -5805,6 +6513,7 @@ class jmPrismatic extends jmPath {
|
|
|
5805
6513
|
* @class jmRect
|
|
5806
6514
|
* @extends jmPath
|
|
5807
6515
|
* @param {object} params 参数 position=矩形左上角顶点坐标,width=宽,height=高,radius=边角弧度
|
|
6516
|
+
* radius支持数字(四角相同)或对象 { topLeft, topRight, bottomRight, bottomLeft }
|
|
5808
6517
|
*/
|
|
5809
6518
|
class jmRect extends jmPath {
|
|
5810
6519
|
|
|
@@ -5814,12 +6523,24 @@ class jmRect extends jmPath {
|
|
|
5814
6523
|
super(params, t);
|
|
5815
6524
|
|
|
5816
6525
|
this.style.close = true;
|
|
5817
|
-
|
|
6526
|
+
const r = params.radius || this.style.radius || this.style.borderRadius || 0;
|
|
6527
|
+
if(typeof r === 'object' && r !== null) {
|
|
6528
|
+
// 四角独立圆角
|
|
6529
|
+
this.radius = {
|
|
6530
|
+
topLeft: Number(r.topLeft) || 0,
|
|
6531
|
+
topRight: Number(r.topRight) || 0,
|
|
6532
|
+
bottomRight: Number(r.bottomRight) || 0,
|
|
6533
|
+
bottomLeft: Number(r.bottomLeft) || 0
|
|
6534
|
+
};
|
|
6535
|
+
}
|
|
6536
|
+
else {
|
|
6537
|
+
this.radius = r;
|
|
6538
|
+
}
|
|
5818
6539
|
}
|
|
5819
6540
|
/**
|
|
5820
|
-
*
|
|
6541
|
+
* 圆角半径,支持数字或四角独立对象
|
|
5821
6542
|
* @property radius
|
|
5822
|
-
* @type {number}
|
|
6543
|
+
* @type {number|object}
|
|
5823
6544
|
*/
|
|
5824
6545
|
get radius() {
|
|
5825
6546
|
return this.property('radius');
|
|
@@ -5827,6 +6548,36 @@ class jmRect extends jmPath {
|
|
|
5827
6548
|
set radius(v) {
|
|
5828
6549
|
this.needUpdate = true;
|
|
5829
6550
|
return this.property('radius', v);
|
|
6551
|
+
}
|
|
6552
|
+
|
|
6553
|
+
/**
|
|
6554
|
+
* 获取规范化的圆角值(四角独立)
|
|
6555
|
+
* @returns {object} { topLeft, topRight, bottomRight, bottomLeft }
|
|
6556
|
+
*/
|
|
6557
|
+
getNormalizedRadius() {
|
|
6558
|
+
const r = this.radius;
|
|
6559
|
+
if(typeof r === 'number') {
|
|
6560
|
+
const v = Math.max(0, r);
|
|
6561
|
+
return { topLeft: v, topRight: v, bottomRight: v, bottomLeft: v };
|
|
6562
|
+
}
|
|
6563
|
+
if(typeof r === 'object' && r !== null) {
|
|
6564
|
+
return {
|
|
6565
|
+
topLeft: Math.max(0, Number(r.topLeft) || 0),
|
|
6566
|
+
topRight: Math.max(0, Number(r.topRight) || 0),
|
|
6567
|
+
bottomRight: Math.max(0, Number(r.bottomRight) || 0),
|
|
6568
|
+
bottomLeft: Math.max(0, Number(r.bottomLeft) || 0)
|
|
6569
|
+
};
|
|
6570
|
+
}
|
|
6571
|
+
return { topLeft: 0, topRight: 0, bottomRight: 0, bottomLeft: 0 };
|
|
6572
|
+
}
|
|
6573
|
+
|
|
6574
|
+
/**
|
|
6575
|
+
* 检查是否有圆角
|
|
6576
|
+
* @returns {boolean}
|
|
6577
|
+
*/
|
|
6578
|
+
hasRadius() {
|
|
6579
|
+
const nr = this.getNormalizedRadius();
|
|
6580
|
+
return nr.topLeft > 0 || nr.topRight > 0 || nr.bottomRight > 0 || nr.bottomLeft > 0;
|
|
5830
6581
|
}
|
|
5831
6582
|
|
|
5832
6583
|
/**
|
|
@@ -5889,7 +6640,7 @@ class jmRect extends jmPath {
|
|
|
5889
6640
|
|
|
5890
6641
|
/**
|
|
5891
6642
|
* 初始化图形点
|
|
5892
|
-
*
|
|
6643
|
+
* 支持四角独立圆角,借助圆弧对象计算描点
|
|
5893
6644
|
*
|
|
5894
6645
|
* @method initPoints
|
|
5895
6646
|
* @private
|
|
@@ -5906,32 +6657,67 @@ class jmRect extends jmPath {
|
|
|
5906
6657
|
this.dottedLine = this.graph.createShape(jmLine, {style: this.style});
|
|
5907
6658
|
}
|
|
5908
6659
|
|
|
5909
|
-
|
|
5910
|
-
|
|
6660
|
+
const nr = this.getNormalizedRadius();
|
|
6661
|
+
const hasRadius = this.hasRadius();
|
|
6662
|
+
|
|
6663
|
+
// 如果有圆角(支持四角独立),借助圆弧对象计算描点
|
|
6664
|
+
if(hasRadius) {
|
|
5911
6665
|
let q = Math.PI / 2;
|
|
5912
|
-
|
|
5913
|
-
|
|
5914
|
-
|
|
5915
|
-
|
|
5916
|
-
|
|
5917
|
-
|
|
5918
|
-
|
|
5919
|
-
|
|
5920
|
-
|
|
5921
|
-
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
|
|
5925
|
-
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
|
|
6666
|
+
|
|
6667
|
+
// 限制圆角不超过短边的一半
|
|
6668
|
+
const maxR = Math.min(location.width / 2, location.height / 2);
|
|
6669
|
+
const rtl = Math.min(nr.topLeft, maxR);
|
|
6670
|
+
const rtr = Math.min(nr.topRight, maxR);
|
|
6671
|
+
const rbr = Math.min(nr.bottomRight, maxR);
|
|
6672
|
+
const rbl = Math.min(nr.bottomLeft, maxR);
|
|
6673
|
+
|
|
6674
|
+
// 左上角圆弧
|
|
6675
|
+
if(rtl > 0) {
|
|
6676
|
+
let arc = this.graph.createShape(jmArc,{radius:rtl,anticlockwise:false});
|
|
6677
|
+
arc.center = {x:location.left + rtl, y:location.top + rtl};
|
|
6678
|
+
arc.startAngle = Math.PI;
|
|
6679
|
+
arc.endAngle = Math.PI + q;
|
|
6680
|
+
var ps1 = arc.initPoints();
|
|
6681
|
+
}
|
|
6682
|
+
else {
|
|
6683
|
+
var ps1 = [p1];
|
|
6684
|
+
}
|
|
6685
|
+
|
|
6686
|
+
// 右上角圆弧
|
|
6687
|
+
if(rtr > 0) {
|
|
6688
|
+
let arc = this.graph.createShape(jmArc,{radius:rtr,anticlockwise:false});
|
|
6689
|
+
arc.center = {x:p2.x - rtr, y:p2.y + rtr};
|
|
6690
|
+
arc.startAngle = Math.PI + q;
|
|
6691
|
+
arc.endAngle = Math.PI * 2;
|
|
6692
|
+
var ps2 = arc.initPoints();
|
|
6693
|
+
}
|
|
6694
|
+
else {
|
|
6695
|
+
var ps2 = [p2];
|
|
6696
|
+
}
|
|
6697
|
+
|
|
6698
|
+
// 右下角圆弧
|
|
6699
|
+
if(rbr > 0) {
|
|
6700
|
+
let arc = this.graph.createShape(jmArc,{radius:rbr,anticlockwise:false});
|
|
6701
|
+
arc.center = {x:p3.x - rbr, y:p3.y - rbr};
|
|
6702
|
+
arc.startAngle = 0;
|
|
6703
|
+
arc.endAngle = q;
|
|
6704
|
+
var ps3 = arc.initPoints();
|
|
6705
|
+
}
|
|
6706
|
+
else {
|
|
6707
|
+
var ps3 = [p3];
|
|
6708
|
+
}
|
|
6709
|
+
|
|
6710
|
+
// 左下角圆弧
|
|
6711
|
+
if(rbl > 0) {
|
|
6712
|
+
let arc = this.graph.createShape(jmArc,{radius:rbl,anticlockwise:false});
|
|
6713
|
+
arc.center = {x:p4.x + rbl, y:p4.y - rbl};
|
|
6714
|
+
arc.startAngle = q;
|
|
6715
|
+
arc.endAngle = Math.PI;
|
|
6716
|
+
var ps4 = arc.initPoints();
|
|
6717
|
+
}
|
|
6718
|
+
else {
|
|
6719
|
+
var ps4 = [p4];
|
|
6720
|
+
}
|
|
5935
6721
|
this.points = ps1.concat(ps2,ps3,ps4);
|
|
5936
6722
|
}
|
|
5937
6723
|
else {
|
|
@@ -6270,7 +7056,7 @@ class jmLabel extends jmControl {
|
|
|
6270
7056
|
this.style.textAlign = this.style.textAlign || 'left';
|
|
6271
7057
|
//文字垂直对齐
|
|
6272
7058
|
this.style.textBaseline = this.style.textBaseline || 'middle';
|
|
6273
|
-
this.text = params.text || '';
|
|
7059
|
+
this.text = params.text || params.value || '';
|
|
6274
7060
|
|
|
6275
7061
|
this.center = params.center || null;
|
|
6276
7062
|
}
|
|
@@ -6559,67 +7345,6 @@ class jmLabel extends jmControl {
|
|
|
6559
7345
|
}
|
|
6560
7346
|
}
|
|
6561
7347
|
}
|
|
6562
|
-
//如果有指定边框,则画出边框
|
|
6563
|
-
if(this.style.border) {
|
|
6564
|
-
//如果指定了边框样式
|
|
6565
|
-
if(this.style.border.style) {
|
|
6566
|
-
this.context.save && this.context.save();
|
|
6567
|
-
this.setStyle(this.style.border.style);
|
|
6568
|
-
}
|
|
6569
|
-
if(this.mode === '2d') {
|
|
6570
|
-
this.context.moveTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
|
|
6571
|
-
if(this.style.border.top) {
|
|
6572
|
-
this.context.lineTo(this.points[1].x + bounds.left,this.points[1].y + bounds.top);
|
|
6573
|
-
}
|
|
6574
|
-
|
|
6575
|
-
if(this.style.border.right) {
|
|
6576
|
-
this.context.moveTo(this.points[1].x + bounds.left,this.points[1].y + bounds.top);
|
|
6577
|
-
this.context.lineTo(this.points[2].x + bounds.left,this.points[2].y + bounds.top);
|
|
6578
|
-
}
|
|
6579
|
-
|
|
6580
|
-
if(this.style.border.bottom) {
|
|
6581
|
-
this.context.moveTo(this.points[2].x + bounds.left,this.points[2].y + bounds.top);
|
|
6582
|
-
this.context.lineTo(this.points[3].x + bounds.left,this.points[3].y + bounds.top);
|
|
6583
|
-
}
|
|
6584
|
-
|
|
6585
|
-
if(this.style.border.left) {
|
|
6586
|
-
this.context.moveTo(this.points[3].x + bounds.left,this.points[3].y + bounds.top);
|
|
6587
|
-
this.context.lineTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
|
|
6588
|
-
}
|
|
6589
|
-
}
|
|
6590
|
-
else {
|
|
6591
|
-
const points = [];
|
|
6592
|
-
if(this.style.border.top) {
|
|
6593
|
-
points.push(this.points[0]);
|
|
6594
|
-
points.push(this.points[1]);
|
|
6595
|
-
}
|
|
6596
|
-
|
|
6597
|
-
if(this.style.border.right) {
|
|
6598
|
-
points.push({
|
|
6599
|
-
...this.points[1],
|
|
6600
|
-
m: true
|
|
6601
|
-
});
|
|
6602
|
-
points.push(this.points[2]);
|
|
6603
|
-
}
|
|
6604
|
-
|
|
6605
|
-
if(this.style.border.bottom) {
|
|
6606
|
-
points.push({
|
|
6607
|
-
...this.points[2],
|
|
6608
|
-
m: true
|
|
6609
|
-
});
|
|
6610
|
-
points.push(this.points[3]);
|
|
6611
|
-
}
|
|
6612
|
-
|
|
6613
|
-
if(this.style.border.left) {
|
|
6614
|
-
points.push({
|
|
6615
|
-
...this.points[3],
|
|
6616
|
-
m: true
|
|
6617
|
-
});
|
|
6618
|
-
points.push(this.points[0]);
|
|
6619
|
-
}
|
|
6620
|
-
points.length && this.webglControl && this.webglControl.stroke(points);
|
|
6621
|
-
}
|
|
6622
|
-
}
|
|
6623
7348
|
}
|
|
6624
7349
|
|
|
6625
7350
|
endDraw() {
|
|
@@ -7568,145 +8293,6 @@ class jmKeyEvent {
|
|
|
7568
8293
|
}
|
|
7569
8294
|
}
|
|
7570
8295
|
|
|
7571
|
-
/**
|
|
7572
|
-
* 图层类
|
|
7573
|
-
* 用于组织和管理图形对象,支持可见性和锁定控制
|
|
7574
|
-
* 图层可以包含多个图形对象,并控制它们的显示和交互
|
|
7575
|
-
*
|
|
7576
|
-
* @class jmLayer
|
|
7577
|
-
* @extends jmControl
|
|
7578
|
-
* @param {object} params 图层参数
|
|
7579
|
-
* @param {string} [params.name] 图层名称,默认为 'Layer_${timestamp}'
|
|
7580
|
-
* @param {boolean} [params.visible=true] 图层是否可见
|
|
7581
|
-
* @param {boolean} [params.locked=false] 图层是否锁定(锁定后不可交互)
|
|
7582
|
-
* @param {jmGraph} [params.graph] 所属的画布对象
|
|
7583
|
-
*/
|
|
7584
|
-
class jmLayer extends jmControl {
|
|
7585
|
-
|
|
7586
|
-
constructor(params, t='jmLayer') {
|
|
7587
|
-
params = params || {};
|
|
7588
|
-
params.interactive = false; // 图层本身不响应交互事件
|
|
7589
|
-
super(params, t);
|
|
7590
|
-
|
|
7591
|
-
this.name = params.name || `Layer_${Date.now()}`;
|
|
7592
|
-
this.visible = params.visible !== false;
|
|
7593
|
-
this.locked = params.locked || false;
|
|
7594
|
-
}
|
|
7595
|
-
|
|
7596
|
-
/**
|
|
7597
|
-
* 图层名称
|
|
7598
|
-
* 图层的唯一标识符,用于查找和管理图层
|
|
7599
|
-
*
|
|
7600
|
-
* @property name
|
|
7601
|
-
* @type {string}
|
|
7602
|
-
*/
|
|
7603
|
-
get name() {
|
|
7604
|
-
return this.property('name');
|
|
7605
|
-
}
|
|
7606
|
-
set name(v) {
|
|
7607
|
-
if(!v || typeof v !== 'string') {
|
|
7608
|
-
console.warn('jmLayer: name must be a non-empty string');
|
|
7609
|
-
return;
|
|
7610
|
-
}
|
|
7611
|
-
return this.property('name', v);
|
|
7612
|
-
}
|
|
7613
|
-
|
|
7614
|
-
/**
|
|
7615
|
-
* 图层是否可见
|
|
7616
|
-
* 不可见的图层不会被渲染,但仍然存在于图层列表中
|
|
7617
|
-
*
|
|
7618
|
-
* @property visible
|
|
7619
|
-
* @type {boolean}
|
|
7620
|
-
*/
|
|
7621
|
-
get visible() {
|
|
7622
|
-
return this.property('visible');
|
|
7623
|
-
}
|
|
7624
|
-
set visible(v) {
|
|
7625
|
-
this.needUpdate = true;
|
|
7626
|
-
return this.property('visible', v);
|
|
7627
|
-
}
|
|
7628
|
-
|
|
7629
|
-
/**
|
|
7630
|
-
* 图层是否锁定
|
|
7631
|
-
* 锁定的图层中的图形不可被选中或移动,但仍然可见
|
|
7632
|
-
* 适用于背景图层或参考图层
|
|
7633
|
-
*
|
|
7634
|
-
* @property locked
|
|
7635
|
-
* @type {boolean}
|
|
7636
|
-
*/
|
|
7637
|
-
get locked() {
|
|
7638
|
-
return this.property('locked');
|
|
7639
|
-
}
|
|
7640
|
-
set locked(v) {
|
|
7641
|
-
return this.property('locked', v);
|
|
7642
|
-
}
|
|
7643
|
-
|
|
7644
|
-
/**
|
|
7645
|
-
* 绘制图层
|
|
7646
|
-
* 只有可见的图层才会被绘制
|
|
7647
|
-
*
|
|
7648
|
-
* @method paint
|
|
7649
|
-
* @param {boolean} v 是否需要重绘
|
|
7650
|
-
*/
|
|
7651
|
-
paint(v) {
|
|
7652
|
-
if(this.visible !== false) {
|
|
7653
|
-
super.paint(v);
|
|
7654
|
-
}
|
|
7655
|
-
}
|
|
7656
|
-
|
|
7657
|
-
/**
|
|
7658
|
-
* 检查点是否在图层内
|
|
7659
|
-
* 锁定的图层不会响应鼠标事件
|
|
7660
|
-
*
|
|
7661
|
-
* @method checkPoint
|
|
7662
|
-
* @param {object} p 坐标点 {x, y}
|
|
7663
|
-
* @param {number} [pad] padding,额外的检测范围
|
|
7664
|
-
* @return {boolean} 是否在图层内
|
|
7665
|
-
*/
|
|
7666
|
-
checkPoint(p, pad) {
|
|
7667
|
-
// 锁定的图层不响应交互
|
|
7668
|
-
if(this.locked) return false;
|
|
7669
|
-
return super.checkPoint(p, pad);
|
|
7670
|
-
}
|
|
7671
|
-
|
|
7672
|
-
/**
|
|
7673
|
-
* 清空图层
|
|
7674
|
-
* 移除图层中的所有图形对象
|
|
7675
|
-
*
|
|
7676
|
-
* @method clear
|
|
7677
|
-
*/
|
|
7678
|
-
clear() {
|
|
7679
|
-
this.children.clear();
|
|
7680
|
-
this.needUpdate = true;
|
|
7681
|
-
}
|
|
7682
|
-
|
|
7683
|
-
/**
|
|
7684
|
-
* 获取图层中的图形数量
|
|
7685
|
-
*
|
|
7686
|
-
* @method getShapeCount
|
|
7687
|
-
* @return {number} 图形数量
|
|
7688
|
-
*/
|
|
7689
|
-
getShapeCount() {
|
|
7690
|
-
return this.children.length;
|
|
7691
|
-
}
|
|
7692
|
-
|
|
7693
|
-
/**
|
|
7694
|
-
* 获取图层信息
|
|
7695
|
-
* 返回图层的基本信息,用于调试和日志
|
|
7696
|
-
*
|
|
7697
|
-
* @method getInfo
|
|
7698
|
-
* @return {object} 图层信息对象
|
|
7699
|
-
*/
|
|
7700
|
-
getInfo() {
|
|
7701
|
-
return {
|
|
7702
|
-
name: this.name,
|
|
7703
|
-
visible: this.visible,
|
|
7704
|
-
locked: this.locked,
|
|
7705
|
-
shapeCount: this.getShapeCount()
|
|
7706
|
-
};
|
|
7707
|
-
}
|
|
7708
|
-
}
|
|
7709
|
-
|
|
7710
8296
|
/**
|
|
7711
8297
|
* jmGraph画图类库
|
|
7712
8298
|
* 对canvas画图api进行二次封装,使其更易调用,省去很多重复的工作。
|
|
@@ -7718,7 +8304,7 @@ class jmLayer extends jmControl {
|
|
|
7718
8304
|
* @param {object} option 参数:{width:宽,height:高}
|
|
7719
8305
|
* @param {function} callback 初始化后的回调
|
|
7720
8306
|
*/
|
|
7721
|
-
class jmGraph
|
|
8307
|
+
class jmGraph extends jmControl {
|
|
7722
8308
|
|
|
7723
8309
|
constructor(canvas, option, callback) {
|
|
7724
8310
|
if(typeof option == 'function') {
|
|
@@ -7784,8 +8370,6 @@ class jmGraph$1 extends jmControl {
|
|
|
7784
8370
|
else {
|
|
7785
8371
|
this.context = canvas.getContext(this.mode);
|
|
7786
8372
|
}
|
|
7787
|
-
|
|
7788
|
-
this.textureCanvas = option.textureCanvas || null;
|
|
7789
8373
|
|
|
7790
8374
|
// webgl模式
|
|
7791
8375
|
if(this.mode === 'webgl') {
|
|
@@ -7936,7 +8520,7 @@ class jmGraph$1 extends jmControl {
|
|
|
7936
8520
|
* @return {jmGraph} jmGraph实例对象
|
|
7937
8521
|
*/
|
|
7938
8522
|
static create(...args) {
|
|
7939
|
-
return new jmGraph
|
|
8523
|
+
return new jmGraph(...args);
|
|
7940
8524
|
}
|
|
7941
8525
|
|
|
7942
8526
|
/**
|
|
@@ -7985,9 +8569,7 @@ class jmGraph$1 extends jmControl {
|
|
|
7985
8569
|
if(shape) {
|
|
7986
8570
|
if(!args) args = {};
|
|
7987
8571
|
args.graph = this;
|
|
7988
|
-
|
|
7989
|
-
// 添加到活动图层
|
|
7990
|
-
this.addShapeToLayer(obj);
|
|
8572
|
+
const obj = new shape(args);
|
|
7991
8573
|
return obj;
|
|
7992
8574
|
}
|
|
7993
8575
|
}
|
|
@@ -8113,13 +8695,19 @@ class jmGraph$1 extends jmControl {
|
|
|
8113
8695
|
this.context.clearRect(0, 0, w, h);
|
|
8114
8696
|
}
|
|
8115
8697
|
else if(this.mode === 'webgl' && this.context.clear) {
|
|
8116
|
-
|
|
8117
|
-
|
|
8118
|
-
|
|
8119
|
-
|
|
8120
|
-
a
|
|
8121
|
-
}
|
|
8122
|
-
this.
|
|
8698
|
+
// 缓存 clearColor 对象,避免每帧创建
|
|
8699
|
+
if(this.style && this.style.fill) {
|
|
8700
|
+
const color = this.utils.hexToRGBA(this.style.fill);
|
|
8701
|
+
this.__lastClearColor = color;
|
|
8702
|
+
this.context.clearColor(color.r, color.g, color.b, color.a);
|
|
8703
|
+
}
|
|
8704
|
+
else if(!this.__lastClearColor) {
|
|
8705
|
+
this.__lastClearColor = { r: 0, g: 0, b: 0, a: 0 };
|
|
8706
|
+
this.context.clearColor(0, 0, 0, 0);
|
|
8707
|
+
}
|
|
8708
|
+
else {
|
|
8709
|
+
this.context.clearColor(this.__lastClearColor.r, this.__lastClearColor.g, this.__lastClearColor.b, this.__lastClearColor.a);
|
|
8710
|
+
}
|
|
8123
8711
|
this.context.clear(this.context.COLOR_BUFFER_BIT); // 清空颜色缓冲区,也就是清空画布
|
|
8124
8712
|
}
|
|
8125
8713
|
}
|
|
@@ -8300,240 +8888,14 @@ class jmGraph$1 extends jmControl {
|
|
|
8300
8888
|
*
|
|
8301
8889
|
* @method resetTransform
|
|
8302
8890
|
* @return {jmGraph} 返回当前实例,支持链式调用
|
|
8303
|
-
*/
|
|
8304
|
-
resetTransform() {
|
|
8305
|
-
this.scaleFactor = 1;
|
|
8306
|
-
this.translation = {x: 0, y: 0};
|
|
8307
|
-
this.needUpdate = true;
|
|
8308
|
-
this.redraw();
|
|
8309
|
-
|
|
8310
|
-
return this; // 支持链式调用
|
|
8311
|
-
}
|
|
8312
|
-
|
|
8313
|
-
/**
|
|
8314
|
-
* 初始化图层系统
|
|
8315
|
-
* 创建图层管理的基础结构,包括默认图层
|
|
8316
|
-
*
|
|
8317
|
-
* @method initLayers
|
|
8318
|
-
* @private
|
|
8319
|
-
*/
|
|
8320
|
-
initLayers() {
|
|
8321
|
-
if(!this.layers) {
|
|
8322
|
-
this.layers = new jmList();
|
|
8323
|
-
// 创建默认图层
|
|
8324
|
-
const defaultLayer = this.createLayer('Default Layer');
|
|
8325
|
-
this.activeLayer = defaultLayer;
|
|
8326
|
-
}
|
|
8327
|
-
}
|
|
8328
|
-
|
|
8329
|
-
/**
|
|
8330
|
-
* 创建新图层
|
|
8331
|
-
* 图层用于组织和管理图形对象,支持可见性和锁定控制
|
|
8332
|
-
*
|
|
8333
|
-
* @method createLayer
|
|
8334
|
-
* @param {string} name 图层名称(必须唯一)
|
|
8335
|
-
* @param {object} [options] 图层选项
|
|
8336
|
-
* @param {boolean} [options.visible=true] 图层是否可见
|
|
8337
|
-
* @param {boolean} [options.locked=false] 图层是否锁定(锁定后不可交互)
|
|
8338
|
-
* @return {jmLayer} 新创建的图层
|
|
8339
|
-
*/
|
|
8340
|
-
createLayer(name, options = {}) {
|
|
8341
|
-
// 参数验证
|
|
8342
|
-
if(!name || typeof name !== 'string') {
|
|
8343
|
-
console.warn('jmGraph: createLayer - 图层名称必须是非空字符串');
|
|
8344
|
-
name = `Layer_${Date.now()}`;
|
|
8345
|
-
}
|
|
8346
|
-
|
|
8347
|
-
this.initLayers();
|
|
8348
|
-
|
|
8349
|
-
// 检查图层名称是否已存在
|
|
8350
|
-
const existingLayer = this.getLayer(name);
|
|
8351
|
-
if(existingLayer) {
|
|
8352
|
-
console.warn(`jmGraph: 图层 "${name}" 已存在,将返回现有图层`);
|
|
8353
|
-
return existingLayer;
|
|
8354
|
-
}
|
|
8355
|
-
|
|
8356
|
-
const layer = new jmLayer({
|
|
8357
|
-
name: name,
|
|
8358
|
-
graph: this,
|
|
8359
|
-
...options
|
|
8360
|
-
});
|
|
8361
|
-
|
|
8362
|
-
this.layers.add(layer);
|
|
8363
|
-
this.children.add(layer);
|
|
8364
|
-
this.needUpdate = true;
|
|
8365
|
-
return layer;
|
|
8366
|
-
}
|
|
8367
|
-
|
|
8368
|
-
/**
|
|
8369
|
-
* 获取所有图层
|
|
8370
|
-
*
|
|
8371
|
-
* @method getLayers
|
|
8372
|
-
* @return {jmList} 图层列表
|
|
8373
|
-
*/
|
|
8374
|
-
getLayers() {
|
|
8375
|
-
this.initLayers();
|
|
8376
|
-
return this.layers;
|
|
8377
|
-
}
|
|
8378
|
-
|
|
8379
|
-
/**
|
|
8380
|
-
* 根据名称获取图层
|
|
8381
|
-
*
|
|
8382
|
-
* @method getLayer
|
|
8383
|
-
* @param {string} name 图层名称
|
|
8384
|
-
* @return {jmLayer|null} 图层对象,如果不存在则返回null
|
|
8385
|
-
*/
|
|
8386
|
-
getLayer(name) {
|
|
8387
|
-
this.initLayers();
|
|
8388
|
-
|
|
8389
|
-
if(!name) return null;
|
|
8390
|
-
|
|
8391
|
-
let result = null;
|
|
8392
|
-
this.layers.each((i, layer) => {
|
|
8393
|
-
if(layer.name === name) {
|
|
8394
|
-
result = layer;
|
|
8395
|
-
return false; // 找到后停止遍历
|
|
8396
|
-
}
|
|
8397
|
-
});
|
|
8398
|
-
return result;
|
|
8399
|
-
}
|
|
8400
|
-
|
|
8401
|
-
/**
|
|
8402
|
-
* 设置活动图层
|
|
8403
|
-
* 新创建的图形将自动添加到活动图层
|
|
8404
|
-
*
|
|
8405
|
-
* @method setActiveLayer
|
|
8406
|
-
* @param {string|jmLayer} layer 图层名称或图层对象
|
|
8407
|
-
* @return {jmGraph} 返回当前实例,支持链式调用
|
|
8408
|
-
*/
|
|
8409
|
-
setActiveLayer(layer) {
|
|
8410
|
-
this.initLayers();
|
|
8411
|
-
|
|
8412
|
-
// 支持传入图层名称或图层对象
|
|
8413
|
-
if(typeof layer === 'string') {
|
|
8414
|
-
layer = this.getLayer(layer);
|
|
8415
|
-
}
|
|
8416
|
-
|
|
8417
|
-
if(!layer || !(layer instanceof jmLayer)) {
|
|
8418
|
-
console.warn('jmGraph: setActiveLayer - 无效的图层');
|
|
8419
|
-
return this;
|
|
8420
|
-
}
|
|
8421
|
-
|
|
8422
|
-
this.activeLayer = layer;
|
|
8423
|
-
return this;
|
|
8424
|
-
}
|
|
8425
|
-
|
|
8426
|
-
/**
|
|
8427
|
-
* 获取当前活动图层
|
|
8428
|
-
* 活动图层是新创建图形的默认容器
|
|
8429
|
-
*
|
|
8430
|
-
* @method getActiveLayer
|
|
8431
|
-
* @return {jmLayer} 当前活动图层
|
|
8432
|
-
*/
|
|
8433
|
-
getActiveLayer() {
|
|
8434
|
-
this.initLayers();
|
|
8435
|
-
return this.activeLayer;
|
|
8436
|
-
}
|
|
8437
|
-
|
|
8438
|
-
/**
|
|
8439
|
-
* 移除图层
|
|
8440
|
-
* 删除指定图层及其包含的所有图形
|
|
8441
|
-
* 注意:默认图层不可删除
|
|
8442
|
-
*
|
|
8443
|
-
* @method removeLayer
|
|
8444
|
-
* @param {string|jmLayer} layer 图层名称或图层对象
|
|
8445
|
-
* @return {boolean} 是否成功删除
|
|
8446
|
-
*/
|
|
8447
|
-
removeLayer(layer) {
|
|
8448
|
-
this.initLayers();
|
|
8449
|
-
|
|
8450
|
-
// 支持传入图层名称或图层对象
|
|
8451
|
-
if(typeof layer === 'string') {
|
|
8452
|
-
layer = this.getLayer(layer);
|
|
8453
|
-
}
|
|
8454
|
-
|
|
8455
|
-
if(!layer) {
|
|
8456
|
-
console.warn('jmGraph: removeLayer - 图层不存在');
|
|
8457
|
-
return false;
|
|
8458
|
-
}
|
|
8459
|
-
|
|
8460
|
-
// 禁止删除默认图层
|
|
8461
|
-
if(layer.name === 'Default Layer') {
|
|
8462
|
-
console.warn('jmGraph: 不能删除默认图层');
|
|
8463
|
-
return false;
|
|
8464
|
-
}
|
|
8465
|
-
|
|
8466
|
-
// 如果删除的是当前活动图层,切换到默认图层
|
|
8467
|
-
if(this.activeLayer === layer) {
|
|
8468
|
-
this.activeLayer = this.getLayer('Default Layer');
|
|
8469
|
-
}
|
|
8470
|
-
|
|
8471
|
-
this.layers.remove(layer);
|
|
8472
|
-
this.children.remove(layer);
|
|
8473
|
-
this.needUpdate = true;
|
|
8474
|
-
return true;
|
|
8475
|
-
}
|
|
8476
|
-
|
|
8477
|
-
/**
|
|
8478
|
-
* 将形状添加到指定图层
|
|
8479
|
-
* 如果未指定图层,则添加到当前活动图层
|
|
8480
|
-
*
|
|
8481
|
-
* @method addShapeToLayer
|
|
8482
|
-
* @param {jmControl} shape 要添加的形状对象
|
|
8483
|
-
* @param {string|jmLayer} [layer] 图层名称或图层对象,默认为当前活动图层
|
|
8484
|
-
* @return {jmGraph} 返回当前实例,支持链式调用
|
|
8485
|
-
*/
|
|
8486
|
-
addShapeToLayer(shape, layer) {
|
|
8487
|
-
this.initLayers();
|
|
8488
|
-
|
|
8489
|
-
// 参数验证
|
|
8490
|
-
if(!shape) {
|
|
8491
|
-
console.warn('jmGraph: addShapeToLayer - 无效的形状对象');
|
|
8492
|
-
return this;
|
|
8493
|
-
}
|
|
8494
|
-
|
|
8495
|
-
// 确定目标图层
|
|
8496
|
-
if(!layer) {
|
|
8497
|
-
layer = this.activeLayer;
|
|
8498
|
-
} else if(typeof layer === 'string') {
|
|
8499
|
-
layer = this.getLayer(layer);
|
|
8500
|
-
}
|
|
8501
|
-
|
|
8502
|
-
if(!layer) {
|
|
8503
|
-
console.warn('jmGraph: addShapeToLayer - 图层不存在');
|
|
8504
|
-
return this;
|
|
8505
|
-
}
|
|
8506
|
-
|
|
8507
|
-
layer.children.add(shape);
|
|
8508
|
-
this.needUpdate = true;
|
|
8509
|
-
return this;
|
|
8510
|
-
}
|
|
8511
|
-
|
|
8512
|
-
/**
|
|
8513
|
-
* 从图层中移除形状
|
|
8514
|
-
*
|
|
8515
|
-
* @method removeShapeFromLayer
|
|
8516
|
-
* @param {jmControl} shape 要移除的形状对象
|
|
8517
|
-
* @return {jmGraph} 返回当前实例,支持链式调用
|
|
8518
|
-
*/
|
|
8519
|
-
removeShapeFromLayer(shape) {
|
|
8520
|
-
if(!shape) {
|
|
8521
|
-
console.warn('jmGraph: removeShapeFromLayer - 无效的形状对象');
|
|
8522
|
-
return this;
|
|
8523
|
-
}
|
|
8524
|
-
|
|
8525
|
-
// 从所有图层中查找并移除
|
|
8526
|
-
if(this.layers) {
|
|
8527
|
-
this.layers.each((i, layer) => {
|
|
8528
|
-
if(layer.children.contains(shape)) {
|
|
8529
|
-
layer.children.remove(shape);
|
|
8530
|
-
this.needUpdate = true;
|
|
8531
|
-
return false; // 找到后停止遍历
|
|
8532
|
-
}
|
|
8533
|
-
});
|
|
8534
|
-
}
|
|
8891
|
+
*/
|
|
8892
|
+
resetTransform() {
|
|
8893
|
+
this.scaleFactor = 1;
|
|
8894
|
+
this.translation = {x: 0, y: 0};
|
|
8895
|
+
this.needUpdate = true;
|
|
8896
|
+
this.redraw();
|
|
8535
8897
|
|
|
8536
|
-
return this;
|
|
8898
|
+
return this; // 支持链式调用
|
|
8537
8899
|
}
|
|
8538
8900
|
|
|
8539
8901
|
/**
|
|
@@ -8602,48 +8964,27 @@ class jmGraph$1 extends jmControl {
|
|
|
8602
8964
|
}
|
|
8603
8965
|
|
|
8604
8966
|
/**
|
|
8605
|
-
*
|
|
8606
|
-
*
|
|
8607
|
-
*
|
|
8967
|
+
* 遍历所有形状,生成SVG标记
|
|
8968
|
+
*
|
|
8608
8969
|
* @method toSVG
|
|
8609
8970
|
* @return {string} SVG字符串
|
|
8610
8971
|
*/
|
|
8611
8972
|
toSVG() {
|
|
8612
8973
|
// SVG头部,包含命名空间和画布尺寸
|
|
8613
8974
|
let svg = `<svg width="${this.width}" height="${this.height}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${this.width} ${this.height}">`;
|
|
8614
|
-
|
|
8975
|
+
|
|
8615
8976
|
// 添加背景色(如果有)
|
|
8616
8977
|
if(this.style && this.style.fill) {
|
|
8617
8978
|
svg += `<rect width="100%" height="100%" fill="${this.style.fill}"/>`;
|
|
8618
8979
|
}
|
|
8619
|
-
|
|
8620
|
-
//
|
|
8621
|
-
|
|
8622
|
-
|
|
8623
|
-
|
|
8624
|
-
|
|
8625
|
-
|
|
8626
|
-
|
|
8627
|
-
// 遍历图层中的所有形状
|
|
8628
|
-
layer.children.each((j, shape) => {
|
|
8629
|
-
if(shape.toSVG) {
|
|
8630
|
-
svg += shape.toSVG();
|
|
8631
|
-
}
|
|
8632
|
-
});
|
|
8633
|
-
|
|
8634
|
-
svg += '</g>';
|
|
8635
|
-
}
|
|
8636
|
-
});
|
|
8637
|
-
}
|
|
8638
|
-
else {
|
|
8639
|
-
// 遍历直接添加的形状(兼容没有图层系统的情况)
|
|
8640
|
-
this.children.each((i, shape) => {
|
|
8641
|
-
if(shape.toSVG) {
|
|
8642
|
-
svg += shape.toSVG();
|
|
8643
|
-
}
|
|
8644
|
-
});
|
|
8645
|
-
}
|
|
8646
|
-
|
|
8980
|
+
|
|
8981
|
+
// 遍历所有直接添加的形状
|
|
8982
|
+
this.children.each((i, shape) => {
|
|
8983
|
+
if(shape.toSVG) {
|
|
8984
|
+
svg += shape.toSVG();
|
|
8985
|
+
}
|
|
8986
|
+
});
|
|
8987
|
+
|
|
8647
8988
|
svg += '</svg>';
|
|
8648
8989
|
return svg;
|
|
8649
8990
|
}
|
|
@@ -8693,7 +9034,7 @@ class jmGraph$1 extends jmControl {
|
|
|
8693
9034
|
// 触发刷新事件
|
|
8694
9035
|
self.emit('update', time);
|
|
8695
9036
|
|
|
8696
|
-
|
|
9037
|
+
// 直接 requestAnimationFrame,无需先 cancel
|
|
8697
9038
|
self.__requestAnimationFrameFunHandler = self.requestAnimationFrame(update);
|
|
8698
9039
|
if(callback) callback();
|
|
8699
9040
|
}
|
|
@@ -8728,41 +9069,25 @@ const shapes = {
|
|
|
8728
9069
|
"star": jmStar
|
|
8729
9070
|
};
|
|
8730
9071
|
|
|
8731
|
-
class
|
|
9072
|
+
class jmGraphImpl extends jmGraph {
|
|
8732
9073
|
constructor(canvas, option, callback) {
|
|
8733
|
-
|
|
8734
|
-
const targetType = new.target;
|
|
8735
|
-
|
|
8736
9074
|
// 合并shapes
|
|
8737
9075
|
option = Object.assign({}, option);
|
|
8738
9076
|
option.shapes = Object.assign(shapes, option.shapes||{});
|
|
8739
|
-
|
|
8740
|
-
//不是用new实例化的话,返回一个promise
|
|
8741
|
-
if(!targetType || !(targetType.prototype instanceof jmGraph$1)) {
|
|
8742
|
-
return new Promise(function(resolve, reject){
|
|
8743
|
-
var g = new jmGraph(canvas, option, callback);
|
|
8744
|
-
if(resolve) resolve(g);
|
|
8745
|
-
});
|
|
8746
|
-
}
|
|
8747
9077
|
|
|
8748
9078
|
if(typeof option == 'function') {
|
|
8749
9079
|
callback = option;
|
|
8750
9080
|
option = {};
|
|
8751
|
-
|
|
9081
|
+
}
|
|
8752
9082
|
|
|
8753
9083
|
super(canvas, option, callback);
|
|
8754
9084
|
}
|
|
8755
9085
|
|
|
8756
9086
|
static create(...args) {
|
|
8757
|
-
return
|
|
9087
|
+
return new jmGraphImpl(...args);
|
|
8758
9088
|
}
|
|
8759
9089
|
}
|
|
8760
9090
|
|
|
8761
|
-
//创建实例
|
|
8762
|
-
const createJmGraph = (...args) => {
|
|
8763
|
-
return new jmGraph(...args);
|
|
8764
|
-
};
|
|
8765
|
-
|
|
8766
9091
|
/**
|
|
8767
9092
|
* 基础样式
|
|
8768
9093
|
*
|
|
@@ -9964,13 +10289,16 @@ class jmAxis extends jmArrowLine {
|
|
|
9964
10289
|
clear() {
|
|
9965
10290
|
this._min = null;
|
|
9966
10291
|
this._max = null;
|
|
10292
|
+
this._width = null; // 清除宽度缓存
|
|
9967
10293
|
this.children.each((i, c) => {
|
|
9968
10294
|
c.remove();
|
|
9969
10295
|
}, true);
|
|
9970
10296
|
// 清空栅格线
|
|
9971
|
-
this.gridLines && this.gridLines.
|
|
9972
|
-
|
|
9973
|
-
|
|
10297
|
+
if (this.gridLines && this.gridLines.length) {
|
|
10298
|
+
for (let i = 0; i < this.gridLines.length; i++) {
|
|
10299
|
+
this.gridLines[i].remove();
|
|
10300
|
+
}
|
|
10301
|
+
}
|
|
9974
10302
|
this.labels && this.labels.forEach(label => {
|
|
9975
10303
|
label.remove();
|
|
9976
10304
|
});
|
|
@@ -10182,7 +10510,9 @@ var utils = {
|
|
|
10182
10510
|
}
|
|
10183
10511
|
};
|
|
10184
10512
|
|
|
10185
|
-
|
|
10513
|
+
// 公共常量提取到基类,避免每个子类重复定义
|
|
10514
|
+
const ANIMATION_DATA_THRESHOLD = 100;
|
|
10515
|
+
const DEFAULT_ANIMATION_COUNT = 10;
|
|
10186
10516
|
|
|
10187
10517
|
/**
|
|
10188
10518
|
* 图表系列基类
|
|
@@ -10301,14 +10631,20 @@ class jmSeries extends jmPath {
|
|
|
10301
10631
|
*/
|
|
10302
10632
|
initDataPoint(...args) {
|
|
10303
10633
|
let dataChanged = false;
|
|
10304
|
-
if (this.enableAnimate && this.data && this.data.length < ANIMATION_DATA_THRESHOLD
|
|
10305
|
-
|
|
10306
|
-
|
|
10307
|
-
|
|
10308
|
-
|
|
10309
|
-
|
|
10310
|
-
|
|
10311
|
-
|
|
10634
|
+
if (this.enableAnimate && this.data && this.data.length < ANIMATION_DATA_THRESHOLD) {
|
|
10635
|
+
// 仅在数据引用变化时才做深度比较
|
|
10636
|
+
if (this._lastData !== this.data) {
|
|
10637
|
+
this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, obj => {
|
|
10638
|
+
if (obj instanceof jmControl) return obj;
|
|
10639
|
+
});
|
|
10640
|
+
this.dataPoints = this.createPoints(...args);
|
|
10641
|
+
dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
|
|
10642
|
+
return s.x === t.x && s.y === t.y;
|
|
10643
|
+
});
|
|
10644
|
+
this._lastData = this.data;
|
|
10645
|
+
} else {
|
|
10646
|
+
this.dataPoints = this.createPoints(...args);
|
|
10647
|
+
}
|
|
10312
10648
|
if (dataChanged) {
|
|
10313
10649
|
this.___animateCounter = 0;
|
|
10314
10650
|
}
|
|
@@ -10441,7 +10777,7 @@ class jmSeries extends jmPath {
|
|
|
10441
10777
|
xValue: xv,
|
|
10442
10778
|
xLabel: xv,
|
|
10443
10779
|
points: [],
|
|
10444
|
-
style: this.
|
|
10780
|
+
style: this.style // 共享引用,避免深拷贝;子类如需修改应自行创建副本
|
|
10445
10781
|
};
|
|
10446
10782
|
p.x = xstep * i + this.xAxis.labelStart;
|
|
10447
10783
|
for (let j = 0; j < fields.length; j++) {
|
|
@@ -10607,9 +10943,6 @@ class jmSeries extends jmPath {
|
|
|
10607
10943
|
}
|
|
10608
10944
|
}
|
|
10609
10945
|
|
|
10610
|
-
const ANIMATION_DATA_THRESHOLD$6 = 100;
|
|
10611
|
-
const DEFAULT_ANIMATION_COUNT$6 = 10;
|
|
10612
|
-
|
|
10613
10946
|
/**
|
|
10614
10947
|
* 柱图
|
|
10615
10948
|
*
|
|
@@ -10639,16 +10972,18 @@ class jmBarSeries extends jmSeries {
|
|
|
10639
10972
|
const len = points.length;
|
|
10640
10973
|
if (!len) return;
|
|
10641
10974
|
this.initWidth(len);
|
|
10642
|
-
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD
|
|
10975
|
+
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
|
|
10643
10976
|
let aniIsEnd = true;
|
|
10644
|
-
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT
|
|
10977
|
+
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
|
|
10645
10978
|
for (let i = 0; i < len; i++) {
|
|
10646
10979
|
const point = points[i];
|
|
10647
10980
|
if (typeof point.y === 'undefined' || point.y === null) {
|
|
10648
10981
|
continue;
|
|
10649
10982
|
}
|
|
10650
|
-
|
|
10651
|
-
|
|
10983
|
+
const style = Object.assign({}, point.style, {
|
|
10984
|
+
fill: this.getColor(point)
|
|
10985
|
+
});
|
|
10986
|
+
const sp = this.addShape(this.graph.createPath(null, style));
|
|
10652
10987
|
const p1 = {
|
|
10653
10988
|
x: point.x - this.barTotalWidth / 2 + this.barWidth * this.barIndex,
|
|
10654
10989
|
y: this.baseY
|
|
@@ -10783,16 +11118,20 @@ class jmStackBarSeries extends jmBarSeries {
|
|
|
10783
11118
|
let topStartY = this.baseY;
|
|
10784
11119
|
let bottomStartY = this.baseY;
|
|
10785
11120
|
for (let index = 0; index < point.points.length; index++) {
|
|
10786
|
-
const style = this.graph.utils.clone(this.style);
|
|
10787
11121
|
const p = point.points[index];
|
|
10788
|
-
|
|
10789
|
-
|
|
11122
|
+
let fillColor;
|
|
11123
|
+
if (this.style.color && typeof this.style.color === 'function') {
|
|
11124
|
+
fillColor = this.style.color.call(this, {
|
|
10790
11125
|
index,
|
|
10791
11126
|
point: p
|
|
10792
11127
|
});
|
|
10793
11128
|
} else {
|
|
10794
|
-
|
|
11129
|
+
fillColor = this.graph.getColor(index);
|
|
10795
11130
|
}
|
|
11131
|
+
// 使用浅拷贝而非深拷贝
|
|
11132
|
+
const style = Object.assign({}, this.style, {
|
|
11133
|
+
fill: fillColor
|
|
11134
|
+
});
|
|
10796
11135
|
const sp = this.addShape(this.graph.createPath(null, style));
|
|
10797
11136
|
let startY = topStartY;
|
|
10798
11137
|
if (p.yValue < this.baseYValue) startY = bottomStartY;
|
|
@@ -10901,7 +11240,7 @@ class jmPieSeries extends jmSeries {
|
|
|
10901
11240
|
this.totalValue = 0;
|
|
10902
11241
|
//计算最大值和最小值
|
|
10903
11242
|
if (this.data) {
|
|
10904
|
-
for (
|
|
11243
|
+
for (let i = 0; i < this.data.length; i++) {
|
|
10905
11244
|
const s = this.data[i];
|
|
10906
11245
|
const vy = s[this.field];
|
|
10907
11246
|
if (vy) {
|
|
@@ -10998,15 +11337,20 @@ class jmPieSeries extends jmSeries {
|
|
|
10998
11337
|
yLabel: yv,
|
|
10999
11338
|
step: Math.abs(yv / this.totalValue),
|
|
11000
11339
|
// 每个数值点比
|
|
11001
|
-
style: this.
|
|
11340
|
+
style: this.style,
|
|
11341
|
+
// 共享引用,仅在需要修改时才拷贝
|
|
11002
11342
|
anticlockwise
|
|
11003
11343
|
};
|
|
11004
11344
|
points.push(p);
|
|
11005
11345
|
//p.style.color = this.graph.getColor(index);
|
|
11006
11346
|
if (p.style.color && typeof p.style.color === 'function') {
|
|
11347
|
+
p.style = this.graph.utils.clone(this.style);
|
|
11007
11348
|
p.style.fill = p.style.color.call(this, p);
|
|
11008
11349
|
} else {
|
|
11009
|
-
|
|
11350
|
+
// fill 需要独立,创建轻量样式覆盖
|
|
11351
|
+
p.style = Object.assign({}, this.style, {
|
|
11352
|
+
fill: this.graph.getColor(index)
|
|
11353
|
+
});
|
|
11010
11354
|
}
|
|
11011
11355
|
const start = startAni; // 上一个扇形的结束角度为当前的起始角度
|
|
11012
11356
|
// 计算当前结束角度, 同时也是下一个的起始角度
|
|
@@ -11212,11 +11556,12 @@ class jmRadarSeries extends jmSeries {
|
|
|
11212
11556
|
const rotateStep = Math.PI * 2 / yCount;
|
|
11213
11557
|
|
|
11214
11558
|
// 清空除了一个默认外的所有Y轴
|
|
11215
|
-
|
|
11216
|
-
|
|
11559
|
+
const yAxisKeys = Object.keys(this.graph.yAxises || {});
|
|
11560
|
+
for (let k = 0; k < yAxisKeys.length; k++) {
|
|
11561
|
+
const axis = this.graph.yAxises[yAxisKeys[k]];
|
|
11217
11562
|
if (!axis || axis === this.yAxis) continue;
|
|
11218
11563
|
axis.remove();
|
|
11219
|
-
delete this.graph.yAxises[
|
|
11564
|
+
delete this.graph.yAxises[yAxisKeys[k]];
|
|
11220
11565
|
}
|
|
11221
11566
|
for (let index = 0; index < yCount; index++) {
|
|
11222
11567
|
if (!this.field[index]) continue;
|
|
@@ -11363,21 +11708,30 @@ class jmRadarSeries extends jmSeries {
|
|
|
11363
11708
|
const points = [];
|
|
11364
11709
|
for (var i = 0; i < this.data.length; i++) {
|
|
11365
11710
|
const s = this.data[i];
|
|
11366
|
-
|
|
11367
|
-
if (style.color && typeof style.color === 'function') {
|
|
11368
|
-
|
|
11711
|
+
let strokeColor;
|
|
11712
|
+
if (this.style.color && typeof this.style.color === 'function') {
|
|
11713
|
+
strokeColor = this.style.color.call(this, {
|
|
11369
11714
|
data: s,
|
|
11370
11715
|
index: i
|
|
11371
11716
|
});
|
|
11372
11717
|
} else {
|
|
11373
|
-
|
|
11718
|
+
strokeColor = this.graph.getColor(i);
|
|
11374
11719
|
}
|
|
11375
|
-
|
|
11376
|
-
|
|
11720
|
+
let fillColor;
|
|
11721
|
+
if (typeof this.style.fill === 'function') {
|
|
11722
|
+
fillColor = this.style.fill.call(this, {
|
|
11723
|
+
...this.style,
|
|
11724
|
+
stroke: strokeColor
|
|
11725
|
+
});
|
|
11377
11726
|
} else {
|
|
11378
|
-
const color = this.graph.utils.hexToRGBA(
|
|
11379
|
-
|
|
11727
|
+
const color = this.graph.utils.hexToRGBA(strokeColor);
|
|
11728
|
+
fillColor = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
|
|
11380
11729
|
}
|
|
11730
|
+
// 使用浅拷贝而非深拷贝
|
|
11731
|
+
const style = Object.assign({}, this.style, {
|
|
11732
|
+
stroke: strokeColor,
|
|
11733
|
+
fill: fillColor
|
|
11734
|
+
});
|
|
11381
11735
|
const shapePoints = [];
|
|
11382
11736
|
for (const axis of this.axises) {
|
|
11383
11737
|
if (!axis || !axis.field) continue;
|
|
@@ -11471,9 +11825,6 @@ jmRadarSeries.prototype.createLegend = function (point) {
|
|
|
11471
11825
|
});
|
|
11472
11826
|
};
|
|
11473
11827
|
|
|
11474
|
-
const ANIMATION_DATA_THRESHOLD$5 = 100;
|
|
11475
|
-
const DEFAULT_ANIMATION_COUNT$5 = 10;
|
|
11476
|
-
|
|
11477
11828
|
/**
|
|
11478
11829
|
* 线图
|
|
11479
11830
|
*
|
|
@@ -11505,9 +11856,9 @@ class jmLineSeries extends jmSeries {
|
|
|
11505
11856
|
if (!len) return;
|
|
11506
11857
|
this.style.stroke = this.style.color;
|
|
11507
11858
|
this.style.item.stroke = this.style.color;
|
|
11508
|
-
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD
|
|
11859
|
+
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
|
|
11509
11860
|
let shapePoints = [];
|
|
11510
|
-
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT
|
|
11861
|
+
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
|
|
11511
11862
|
const aniStep = Math.floor(len / aniCount) || 1;
|
|
11512
11863
|
for (let i = 0; i < len; i++) {
|
|
11513
11864
|
const p = points[i];
|
|
@@ -11890,7 +12241,6 @@ class jmCandlestickSeries extends jmSeries {
|
|
|
11890
12241
|
console.warn('K线图数据不完整,需要4个字段(开盘、收盘、最高、最低)');
|
|
11891
12242
|
continue;
|
|
11892
12243
|
}
|
|
11893
|
-
const sp = this.addShape(this.graph.createPath([], p.style));
|
|
11894
12244
|
const bl = {
|
|
11895
12245
|
x: p.x - w,
|
|
11896
12246
|
y: p.points[0].y
|
|
@@ -11909,14 +12259,18 @@ class jmCandlestickSeries extends jmSeries {
|
|
|
11909
12259
|
};
|
|
11910
12260
|
let tm = p.points[1];
|
|
11911
12261
|
let bm = p.points[0];
|
|
11912
|
-
|
|
12262
|
+
|
|
12263
|
+
// 使用浅拷贝避免污染共享引用
|
|
12264
|
+
const style = Object.assign({}, p.style);
|
|
12265
|
+
style.stroke = style.fill = style.masculineColor || 'red';
|
|
11913
12266
|
if (p.points[0].yValue > p.points[1].yValue) {
|
|
11914
|
-
|
|
12267
|
+
style.stroke = style.fill = style.negativeColor || 'green';
|
|
11915
12268
|
bl.y = br.y = p.points[1].y;
|
|
11916
12269
|
tl.y = tr.y = p.points[0].y;
|
|
11917
12270
|
tm = p.points[0];
|
|
11918
12271
|
bm = p.points[1];
|
|
11919
12272
|
}
|
|
12273
|
+
const sp = this.addShape(this.graph.createPath([], style));
|
|
11920
12274
|
sp.points.push(p.points[2], tm, tl, bl, bm, p.points[3], bm, br, tr, tm, p.points[2]);
|
|
11921
12275
|
this.emit('onPointCreated', p);
|
|
11922
12276
|
}
|
|
@@ -11941,9 +12295,6 @@ class jmCandlestickSeries extends jmSeries {
|
|
|
11941
12295
|
}
|
|
11942
12296
|
}
|
|
11943
12297
|
|
|
11944
|
-
const ANIMATION_DATA_THRESHOLD$4 = 100;
|
|
11945
|
-
const DEFAULT_ANIMATION_COUNT$4 = 10;
|
|
11946
|
-
|
|
11947
12298
|
/**
|
|
11948
12299
|
* 散点图
|
|
11949
12300
|
*
|
|
@@ -11975,8 +12326,8 @@ class jmScatterSeries extends jmSeries {
|
|
|
11975
12326
|
if (!len) return;
|
|
11976
12327
|
this.style.stroke = this.style.color;
|
|
11977
12328
|
this.style.item.stroke = this.style.color;
|
|
11978
|
-
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD
|
|
11979
|
-
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT
|
|
12329
|
+
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
|
|
12330
|
+
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
|
|
11980
12331
|
const aniStep = Math.floor(len / aniCount) || 1;
|
|
11981
12332
|
for (let i = 0; i < len; i++) {
|
|
11982
12333
|
const p = points[i];
|
|
@@ -12049,9 +12400,6 @@ class jmScatterSeries extends jmSeries {
|
|
|
12049
12400
|
}
|
|
12050
12401
|
}
|
|
12051
12402
|
|
|
12052
|
-
const ANIMATION_DATA_THRESHOLD$3 = 100;
|
|
12053
|
-
const DEFAULT_ANIMATION_COUNT$3 = 10;
|
|
12054
|
-
|
|
12055
12403
|
/**
|
|
12056
12404
|
* 气泡图
|
|
12057
12405
|
*
|
|
@@ -12083,8 +12431,8 @@ class jmBubbleSeries extends jmSeries {
|
|
|
12083
12431
|
if (!len) return;
|
|
12084
12432
|
this.style.stroke = this.style.color;
|
|
12085
12433
|
this.style.item.stroke = this.style.color;
|
|
12086
|
-
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD
|
|
12087
|
-
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT
|
|
12434
|
+
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
|
|
12435
|
+
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
|
|
12088
12436
|
const aniStep = Math.floor(len / aniCount) || 1;
|
|
12089
12437
|
for (let i = 0; i < len; i++) {
|
|
12090
12438
|
const p = points[i];
|
|
@@ -12163,9 +12511,6 @@ class jmBubbleSeries extends jmSeries {
|
|
|
12163
12511
|
}
|
|
12164
12512
|
}
|
|
12165
12513
|
|
|
12166
|
-
const ANIMATION_DATA_THRESHOLD$2 = 100;
|
|
12167
|
-
const DEFAULT_ANIMATION_COUNT$2 = 10;
|
|
12168
|
-
|
|
12169
12514
|
/**
|
|
12170
12515
|
* 热力图
|
|
12171
12516
|
*
|
|
@@ -12208,8 +12553,8 @@ class jmHeatmapSeries extends jmSeries {
|
|
|
12208
12553
|
}
|
|
12209
12554
|
this.minValue = minValue;
|
|
12210
12555
|
this.maxValue = maxValue;
|
|
12211
|
-
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD
|
|
12212
|
-
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT
|
|
12556
|
+
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
|
|
12557
|
+
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
|
|
12213
12558
|
const aniStep = Math.floor(len / aniCount) || 1;
|
|
12214
12559
|
for (let i = 0; i < len; i++) {
|
|
12215
12560
|
const p = points[i];
|
|
@@ -12576,20 +12921,15 @@ class jmGaugeSeries extends jmSeries {
|
|
|
12576
12921
|
}
|
|
12577
12922
|
}
|
|
12578
12923
|
|
|
12579
|
-
const ANIMATION_DATA_THRESHOLD$1 = 100;
|
|
12580
|
-
const DEFAULT_ANIMATION_COUNT$1 = 10;
|
|
12581
|
-
|
|
12582
12924
|
/**
|
|
12583
12925
|
* 面积图
|
|
12926
|
+
* 继承自折线图,默认启用区域填充
|
|
12584
12927
|
*
|
|
12585
12928
|
* @class jmAreaSeries
|
|
12586
12929
|
* @module jmChart
|
|
12587
|
-
* @
|
|
12588
|
-
* @param {array} mappings 图形字段映射
|
|
12589
|
-
* @param {style} style 样式
|
|
12930
|
+
* @extends jmLineSeries
|
|
12590
12931
|
*/
|
|
12591
|
-
|
|
12592
|
-
class jmAreaSeries extends jmSeries {
|
|
12932
|
+
class jmAreaSeries extends jmLineSeries {
|
|
12593
12933
|
constructor(options) {
|
|
12594
12934
|
options.style = options.style || options.graph.style.area;
|
|
12595
12935
|
super(options);
|
|
@@ -12597,193 +12937,13 @@ class jmAreaSeries extends jmSeries {
|
|
|
12597
12937
|
this.style.area = true;
|
|
12598
12938
|
}
|
|
12599
12939
|
|
|
12600
|
-
/**
|
|
12601
|
-
* 初始化面积图
|
|
12602
|
-
*
|
|
12603
|
-
* @method init
|
|
12604
|
-
* @for jmAreaSeries
|
|
12605
|
-
*/
|
|
12606
|
-
init() {
|
|
12607
|
-
const {
|
|
12608
|
-
points,
|
|
12609
|
-
dataChanged
|
|
12610
|
-
} = this.initDataPoint();
|
|
12611
|
-
const len = points.length;
|
|
12612
|
-
if (!len) return;
|
|
12613
|
-
this.style.stroke = this.style.color;
|
|
12614
|
-
this.style.item.stroke = this.style.color;
|
|
12615
|
-
const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$1;
|
|
12616
|
-
let shapePoints = [];
|
|
12617
|
-
const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$1;
|
|
12618
|
-
const aniStep = Math.floor(len / aniCount) || 1;
|
|
12619
|
-
for (let i = 0; i < len; i++) {
|
|
12620
|
-
const p = points[i];
|
|
12621
|
-
if (typeof p.y === 'undefined' || p.y === null) {
|
|
12622
|
-
continue;
|
|
12623
|
-
}
|
|
12624
|
-
if (isRunningAni && i > this.___animateCounter) {
|
|
12625
|
-
break;
|
|
12626
|
-
}
|
|
12627
|
-
if (this.style.showItem) {
|
|
12628
|
-
this.createPointItem(p);
|
|
12629
|
-
}
|
|
12630
|
-
if (this.style.curve) {
|
|
12631
|
-
shapePoints = this.createCurePoints(shapePoints, p);
|
|
12632
|
-
} else if (this.style.lineType === 'dotted') {
|
|
12633
|
-
shapePoints = this.createDotLine(shapePoints, p);
|
|
12634
|
-
}
|
|
12635
|
-
shapePoints.push(p);
|
|
12636
|
-
this.createItemLabel(p);
|
|
12637
|
-
this.emit('onPointCreated', p);
|
|
12638
|
-
}
|
|
12639
|
-
if (this.___animateCounter >= len - 1) {
|
|
12640
|
-
this.___animateCounter = 0;
|
|
12641
|
-
} else if (isRunningAni) {
|
|
12642
|
-
this.___animateCounter += aniStep;
|
|
12643
|
-
this.graph.utils.requestAnimationFrame(() => {
|
|
12644
|
-
this.needUpdate = true;
|
|
12645
|
-
});
|
|
12646
|
-
}
|
|
12647
|
-
this.points = shapePoints;
|
|
12648
|
-
this.createArea(shapePoints);
|
|
12649
|
-
}
|
|
12650
|
-
|
|
12651
|
-
/**
|
|
12652
|
-
* 生成点的小圆圈
|
|
12653
|
-
*
|
|
12654
|
-
* @method createPointItem
|
|
12655
|
-
* @for jmAreaSeries
|
|
12656
|
-
* @param {object} p 数据点
|
|
12657
|
-
*/
|
|
12658
|
-
createPointItem(p) {
|
|
12659
|
-
const pointShape = this.graph.createShape('circle', {
|
|
12660
|
-
style: this.style.item,
|
|
12661
|
-
center: p,
|
|
12662
|
-
radius: this.style.radius || 3
|
|
12663
|
-
});
|
|
12664
|
-
pointShape.zIndex = (pointShape.style.zIndex || 1) + 1;
|
|
12665
|
-
return this.addShape(pointShape);
|
|
12666
|
-
}
|
|
12667
|
-
|
|
12668
|
-
/**
|
|
12669
|
-
* 创建曲线点
|
|
12670
|
-
*
|
|
12671
|
-
* @method createCurePoints
|
|
12672
|
-
* @for jmAreaSeries
|
|
12673
|
-
* @param {array} shapePoints 形状点数组
|
|
12674
|
-
* @param {object} p 当前点
|
|
12675
|
-
* @return {array} 新的形状点数组
|
|
12676
|
-
*/
|
|
12677
|
-
createCurePoints(shapePoints, p) {
|
|
12678
|
-
const startPoint = shapePoints[shapePoints.length - 1];
|
|
12679
|
-
if (!startPoint || !p) return shapePoints;
|
|
12680
|
-
if (startPoint.x === undefined || startPoint.x === null || startPoint.y === undefined || startPoint.y === null || p.x === undefined || p.x === null || p.y === undefined || p.y === null) {
|
|
12681
|
-
return shapePoints;
|
|
12682
|
-
}
|
|
12683
|
-
const p1 = {
|
|
12684
|
-
x: startPoint.x + (p.x - startPoint.x) / 5,
|
|
12685
|
-
y: startPoint.y
|
|
12686
|
-
};
|
|
12687
|
-
const p2 = {
|
|
12688
|
-
x: startPoint.x + (p.x - startPoint.x) / 2,
|
|
12689
|
-
y: p.y - (p.y - startPoint.y) / 2
|
|
12690
|
-
};
|
|
12691
|
-
const p3 = {
|
|
12692
|
-
x: p.x - (p.x - startPoint.x) / 5,
|
|
12693
|
-
y: p.y
|
|
12694
|
-
};
|
|
12695
|
-
this.__bezier = this.__bezier || this.graph.createShape('bezier');
|
|
12696
|
-
this.__bezier.cpoints = [startPoint, p1, p2, p3, p];
|
|
12697
|
-
const bzpoints = this.__bezier.initPoints();
|
|
12698
|
-
if (bzpoints && bzpoints.length) {
|
|
12699
|
-
shapePoints = shapePoints.concat(bzpoints);
|
|
12700
|
-
}
|
|
12701
|
-
return shapePoints;
|
|
12702
|
-
}
|
|
12703
|
-
|
|
12704
|
-
/**
|
|
12705
|
-
* 创建虚线点
|
|
12706
|
-
*
|
|
12707
|
-
* @method createDotLine
|
|
12708
|
-
* @for jmAreaSeries
|
|
12709
|
-
* @param {array} shapePoints 形状点数组
|
|
12710
|
-
* @param {object} p 当前点
|
|
12711
|
-
* @return {array} 新的形状点数组
|
|
12712
|
-
*/
|
|
12713
|
-
createDotLine(shapePoints, p) {
|
|
12714
|
-
const startPoint = shapePoints[shapePoints.length - 1];
|
|
12715
|
-
if (!startPoint || !p) return shapePoints;
|
|
12716
|
-
if (startPoint.x === undefined || startPoint.x === null || startPoint.y === undefined || startPoint.y === null || p.x === undefined || p.x === null || p.y === undefined || p.y === null) {
|
|
12717
|
-
return shapePoints;
|
|
12718
|
-
}
|
|
12719
|
-
this.__line = this.__line || this.graph.createShape('line', {
|
|
12720
|
-
style: this.style
|
|
12721
|
-
});
|
|
12722
|
-
this.__line.start = startPoint;
|
|
12723
|
-
this.__line.end = p;
|
|
12724
|
-
const dots = this.__line.initPoints();
|
|
12725
|
-
if (dots && dots.length) {
|
|
12726
|
-
shapePoints = shapePoints.concat(dots);
|
|
12727
|
-
}
|
|
12728
|
-
return shapePoints;
|
|
12729
|
-
}
|
|
12730
|
-
|
|
12731
|
-
/**
|
|
12732
|
-
* 生成面积效果
|
|
12733
|
-
*
|
|
12734
|
-
* @method createArea
|
|
12735
|
-
* @for jmAreaSeries
|
|
12736
|
-
* @param {array} points 数据点数组
|
|
12737
|
-
* @param {boolean} needClosePoint 是否需要闭合点
|
|
12738
|
-
*/
|
|
12739
|
-
createArea(points, needClosePoint = true) {
|
|
12740
|
-
// 有指定绘制区域效果才展示
|
|
12741
|
-
if (!this.style.area || points.length < 2) return;
|
|
12742
|
-
const start = points[0];
|
|
12743
|
-
const end = points[points.length - 1];
|
|
12744
|
-
const style = this.graph.utils.clone(this.style.area, {}, true);
|
|
12745
|
-
// 连框颜色如果没指定,就透明
|
|
12746
|
-
style.stroke = style.stroke || 'transparent';
|
|
12747
|
-
if (!style.fill) {
|
|
12748
|
-
const color = this.graph.utils.hexToRGBA(this.style.stroke);
|
|
12749
|
-
style.fill = `linear-gradient(50% 0 50% 100%,
|
|
12750
|
-
rgba(${color.r},${color.g},${color.b}, 0) 1,
|
|
12751
|
-
rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
|
|
12752
|
-
rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
|
|
12753
|
-
} else if (typeof style.fill === 'function') {
|
|
12754
|
-
style.fill = style.fill.call(this, style);
|
|
12755
|
-
}
|
|
12756
|
-
const area = this.graph.createShape('path', {
|
|
12757
|
-
points: this.graph.utils.clone(points, true),
|
|
12758
|
-
style,
|
|
12759
|
-
width: this.graph.chartArea.width,
|
|
12760
|
-
height: this.graph.chartArea.height
|
|
12761
|
-
});
|
|
12762
|
-
|
|
12763
|
-
// 在点集合前后加上落地到X轴的点就可以组成一个封闭的图形area
|
|
12764
|
-
if (needClosePoint) {
|
|
12765
|
-
area.points.unshift({
|
|
12766
|
-
x: start.x,
|
|
12767
|
-
y: this.baseY
|
|
12768
|
-
});
|
|
12769
|
-
area.points.push({
|
|
12770
|
-
x: end.x,
|
|
12771
|
-
y: this.baseY
|
|
12772
|
-
});
|
|
12773
|
-
}
|
|
12774
|
-
this.addShape(area);
|
|
12775
|
-
}
|
|
12776
|
-
|
|
12777
12940
|
/**
|
|
12778
12941
|
* 生成图例
|
|
12779
|
-
*
|
|
12780
|
-
* @method createLegend
|
|
12781
|
-
* @for jmAreaSeries
|
|
12782
12942
|
*/
|
|
12783
12943
|
createLegend() {
|
|
12784
|
-
// 生成图例前的图标
|
|
12785
12944
|
var style = this.graph.utils.clone(this.style);
|
|
12786
12945
|
style.stroke = style.color;
|
|
12946
|
+
style.fill = style.color;
|
|
12787
12947
|
var shape = this.graph.createShape('path', {
|
|
12788
12948
|
style: style
|
|
12789
12949
|
});
|
|
@@ -12805,8 +12965,7 @@ class jmAreaSeries extends jmSeries {
|
|
|
12805
12965
|
y: 0
|
|
12806
12966
|
};
|
|
12807
12967
|
this.__bezier = this.__bezier || this.graph.createShape('bezier');
|
|
12808
|
-
this.__bezier.cpoints = [p1, p2, p3, p4];
|
|
12809
|
-
|
|
12968
|
+
this.__bezier.cpoints = [p1, p2, p3, p4];
|
|
12810
12969
|
shape.points = this.__bezier.initPoints();
|
|
12811
12970
|
} else {
|
|
12812
12971
|
shape.points = [{
|
|
@@ -12821,8 +12980,6 @@ class jmAreaSeries extends jmSeries {
|
|
|
12821
12980
|
}
|
|
12822
12981
|
}
|
|
12823
12982
|
|
|
12824
|
-
const ANIMATION_DATA_THRESHOLD = 100;
|
|
12825
|
-
const DEFAULT_ANIMATION_COUNT = 10;
|
|
12826
12983
|
class jmWaterfallSeries extends jmSeries {
|
|
12827
12984
|
constructor(options) {
|
|
12828
12985
|
options.style = options.style || options.graph.style.waterfall;
|
|
@@ -14255,12 +14412,11 @@ class jmMarkLineManager {
|
|
|
14255
14412
|
if (chart.style.markLine.longtap) {
|
|
14256
14413
|
longtap = 1;
|
|
14257
14414
|
longtapHandler && graph.utils.cancelAnimationFrame(longtapHandler);
|
|
14258
|
-
|
|
14415
|
+
const tapStartTime = Date.now();
|
|
14259
14416
|
const reqFun = () => {
|
|
14260
|
-
const elapsed = Date.now() - tapStartTime;
|
|
14261
14417
|
if (longtap === 1 || longtap === 2) {
|
|
14262
14418
|
// 如果还未过一定时间,则继续等待
|
|
14263
|
-
if (
|
|
14419
|
+
if (Date.now() - tapStartTime < 500) {
|
|
14264
14420
|
longtapHandler = graph.utils.requestAnimationFrame(reqFun);
|
|
14265
14421
|
return;
|
|
14266
14422
|
}
|
|
@@ -14271,9 +14427,6 @@ class jmMarkLineManager {
|
|
|
14271
14427
|
};
|
|
14272
14428
|
// 如果一定时间后还没有取消,则表示长按了
|
|
14273
14429
|
longtapHandler = graph.utils.requestAnimationFrame(reqFun);
|
|
14274
|
-
|
|
14275
|
-
//args.event.stopPropagation();
|
|
14276
|
-
//args.event.preventDefault();// 阻止默认行为
|
|
14277
14430
|
} else {
|
|
14278
14431
|
this.startMove(args);
|
|
14279
14432
|
}
|
|
@@ -14380,7 +14533,7 @@ class jmMarkLineManager {
|
|
|
14380
14533
|
* @module jmChart
|
|
14381
14534
|
* @param {element} container 图表容器
|
|
14382
14535
|
*/
|
|
14383
|
-
class jmChart extends
|
|
14536
|
+
class jmChart extends jmGraphImpl {
|
|
14384
14537
|
constructor(container, options) {
|
|
14385
14538
|
options = options || {};
|
|
14386
14539
|
const enableAnimate = !!options.enableAnimate;
|
|
@@ -14474,25 +14627,27 @@ class jmChart extends jmGraph {
|
|
|
14474
14627
|
|
|
14475
14628
|
// 绑定点击事件
|
|
14476
14629
|
this.on('click', args => {
|
|
14477
|
-
if (this.option.onClick)
|
|
14478
|
-
|
|
14479
|
-
|
|
14480
|
-
|
|
14481
|
-
|
|
14482
|
-
|
|
14483
|
-
|
|
14484
|
-
|
|
14485
|
-
|
|
14486
|
-
|
|
14487
|
-
|
|
14488
|
-
|
|
14489
|
-
|
|
14490
|
-
|
|
14630
|
+
if (!this.option.onClick) return;
|
|
14631
|
+
// 找到点击位置的数据点
|
|
14632
|
+
let closestPoint = null;
|
|
14633
|
+
let minDistance = 20; // 20px 范围,同时作为阈值,无需再比较小于阈值
|
|
14634
|
+
|
|
14635
|
+
this.series.each((i, serie) => {
|
|
14636
|
+
if (!serie.dataPoints) return;
|
|
14637
|
+
const points = serie.dataPoints;
|
|
14638
|
+
for (let j = 0, len = points.length; j < len; j++) {
|
|
14639
|
+
const point = points[j];
|
|
14640
|
+
const dx = args.position.x - point.x;
|
|
14641
|
+
const dy = args.position.y - point.y;
|
|
14642
|
+
const distance = dx * dx + dy * dy; // 避免开方,直接比较平方
|
|
14643
|
+
if (distance < minDistance * minDistance) {
|
|
14644
|
+
minDistance = distance;
|
|
14645
|
+
closestPoint = point;
|
|
14491
14646
|
}
|
|
14492
|
-
});
|
|
14493
|
-
if (closestPoint) {
|
|
14494
|
-
this.option.onClick(closestPoint);
|
|
14495
14647
|
}
|
|
14648
|
+
});
|
|
14649
|
+
if (closestPoint) {
|
|
14650
|
+
this.option.onClick(closestPoint);
|
|
14496
14651
|
}
|
|
14497
14652
|
});
|
|
14498
14653
|
}
|
|
@@ -14522,7 +14677,7 @@ class jmChart extends jmGraph {
|
|
|
14522
14677
|
options = this.utils.clone(options, {
|
|
14523
14678
|
autoRefresh: true
|
|
14524
14679
|
}, true);
|
|
14525
|
-
this.touchGraph = new
|
|
14680
|
+
this.touchGraph = new jmGraphImpl(cn, options);
|
|
14526
14681
|
this.touchGraph.chartGraph = this;
|
|
14527
14682
|
this.on('propertyChange', (name, args) => {
|
|
14528
14683
|
if (['width', 'height'].includes(name)) {
|
|
@@ -14562,8 +14717,9 @@ class jmChart extends jmGraph {
|
|
|
14562
14717
|
delete this.xAxis;
|
|
14563
14718
|
}
|
|
14564
14719
|
if (this.yAxises) {
|
|
14565
|
-
|
|
14566
|
-
|
|
14720
|
+
const keys = Object.keys(this.yAxises);
|
|
14721
|
+
for (let k = 0; k < keys.length; k++) {
|
|
14722
|
+
this.yAxises[keys[k]].remove();
|
|
14567
14723
|
}
|
|
14568
14724
|
delete this.yAxises;
|
|
14569
14725
|
}
|
|
@@ -14575,18 +14731,9 @@ class jmChart extends jmGraph {
|
|
|
14575
14731
|
* @param {int} index 颜色索引
|
|
14576
14732
|
*/
|
|
14577
14733
|
getColor(index) {
|
|
14578
|
-
const
|
|
14579
|
-
|
|
14580
|
-
|
|
14581
|
-
}
|
|
14582
|
-
let color;
|
|
14583
|
-
if (index >= this.style.chartColors.length) {
|
|
14584
|
-
color = this.style.chartColors[index % this.style.chartColors.length];
|
|
14585
|
-
} else {
|
|
14586
|
-
color = this.style.chartColors[index];
|
|
14587
|
-
}
|
|
14588
|
-
this._cache.set(cacheKey, color);
|
|
14589
|
-
return color;
|
|
14734
|
+
const colors = this.style.chartColors;
|
|
14735
|
+
const colorIndex = index % colors.length;
|
|
14736
|
+
return colors[colorIndex];
|
|
14590
14737
|
}
|
|
14591
14738
|
|
|
14592
14739
|
/**
|
|
@@ -14604,40 +14751,43 @@ class jmChart extends jmGraph {
|
|
|
14604
14751
|
this.xAxis.clear();
|
|
14605
14752
|
}
|
|
14606
14753
|
if (this.yAxises) {
|
|
14607
|
-
|
|
14608
|
-
|
|
14754
|
+
const keys = Object.keys(this.yAxises);
|
|
14755
|
+
for (let k = 0; k < keys.length; k++) {
|
|
14756
|
+
this.yAxises[keys[k]].clear();
|
|
14609
14757
|
}
|
|
14610
14758
|
}
|
|
14611
14759
|
this.barSeriesCount = 0;
|
|
14760
|
+
// 合并两次 series 遍历为一次,减少遍历开销
|
|
14761
|
+
const seriesInited = [];
|
|
14612
14762
|
this.series.each((i, serie) => {
|
|
14613
14763
|
if (!serie.style.color) {
|
|
14614
14764
|
serie.style.color = serie.graph.getColor(i);
|
|
14615
14765
|
}
|
|
14616
|
-
if (serie
|
|
14617
|
-
if (serie
|
|
14766
|
+
if (serie instanceof jmBarSeries) {
|
|
14767
|
+
if (serie.graph.style.layout != 'inside') {
|
|
14618
14768
|
serie.graph.style.layout = 'inside';
|
|
14619
14769
|
}
|
|
14620
|
-
}
|
|
14621
|
-
if (serie instanceof jmBarSeries) {
|
|
14622
14770
|
serie.barIndex = serie.graph.barSeriesCount;
|
|
14623
14771
|
serie.graph.barSeriesCount++;
|
|
14624
14772
|
}
|
|
14625
14773
|
serie.reset();
|
|
14774
|
+
seriesInited.push(serie);
|
|
14626
14775
|
});
|
|
14627
14776
|
if (this.legend) {
|
|
14628
14777
|
this.legend.reset();
|
|
14629
14778
|
}
|
|
14630
14779
|
if (this.yAxises) {
|
|
14631
|
-
|
|
14632
|
-
|
|
14780
|
+
const keys = Object.keys(this.yAxises);
|
|
14781
|
+
for (let k = 0; k < keys.length; k++) {
|
|
14782
|
+
this.yAxises[keys[k]].reset();
|
|
14633
14783
|
}
|
|
14634
14784
|
}
|
|
14635
14785
|
if (this.xAxis) {
|
|
14636
14786
|
this.xAxis.reset();
|
|
14637
14787
|
}
|
|
14638
|
-
|
|
14639
|
-
|
|
14640
|
-
}
|
|
14788
|
+
for (let i = 0; i < seriesInited.length; i++) {
|
|
14789
|
+
seriesInited[i].init && seriesInited[i].init();
|
|
14790
|
+
}
|
|
14641
14791
|
}
|
|
14642
14792
|
|
|
14643
14793
|
/**
|
|
@@ -14815,14 +14965,35 @@ var vchart = {
|
|
|
14815
14965
|
this.initChart();
|
|
14816
14966
|
},
|
|
14817
14967
|
updated() {
|
|
14818
|
-
this.
|
|
14968
|
+
this.refresh();
|
|
14819
14969
|
},
|
|
14820
14970
|
destroyed() {
|
|
14821
14971
|
this.chartInstance && this.chartInstance.destroy();
|
|
14822
14972
|
},
|
|
14823
14973
|
watch: {
|
|
14824
|
-
chartData:
|
|
14825
|
-
|
|
14974
|
+
chartData: {
|
|
14975
|
+
handler: function (newData, oldData) {
|
|
14976
|
+
this.refresh();
|
|
14977
|
+
},
|
|
14978
|
+
deep: false // 仅监听引用变化,由使用者显式替换数组时触发
|
|
14979
|
+
},
|
|
14980
|
+
chartOptions: {
|
|
14981
|
+
handler: function (newOptions, oldOptions) {
|
|
14982
|
+
// 配置变化时需要重建图表
|
|
14983
|
+
if (this.chartInstance) {
|
|
14984
|
+
this.chartInstance.destroy();
|
|
14985
|
+
this.chartInstance = null;
|
|
14986
|
+
}
|
|
14987
|
+
this.option = Object.assign({
|
|
14988
|
+
enableAnimate: false,
|
|
14989
|
+
legendPosition: 'top',
|
|
14990
|
+
legendVisible: true,
|
|
14991
|
+
width: this.width,
|
|
14992
|
+
height: this.height
|
|
14993
|
+
}, newOptions);
|
|
14994
|
+
this.$nextTick(() => this.initChart());
|
|
14995
|
+
},
|
|
14996
|
+
deep: false
|
|
14826
14997
|
},
|
|
14827
14998
|
width: function (newWidth, oldWidth) {
|
|
14828
14999
|
if (!this.chartInstance) return;
|