jmchart 1.2.13 → 1.2.15

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.
@@ -277,66 +277,75 @@ class jmUtils {
277
277
  * @param {function} copyHandler 复制对象回调,如果返回undefined,就走后面的逻辑,否则到这里中止
278
278
  * @return {object} 参数source的拷贝对象
279
279
  */
280
- static clone(source, target, deep = false, copyHandler = null, deepIndex = 0) {
280
+ static clone(source, target, deep = false, copyHandler = null, deepIndex = 0, cloned = null) {
281
281
  // 如果有指定回调,则用回调处理,否则走后面的复制逻辑
282
282
  if(typeof copyHandler === 'function') {
283
283
  const obj = copyHandler(source, deep, deepIndex);
284
284
  if(obj) return obj;
285
285
  }
286
- deepIndex++; // 每执行一次,需要判断最大拷贝深度
286
+
287
+ // 首次调用时初始化克隆映射表(用于处理循环引用)
288
+ if(!cloned) cloned = new WeakMap();
287
289
 
288
290
  if(typeof target === 'boolean') {
289
291
  deep = target;
290
292
  target = undefined;
291
293
  }
292
294
 
293
- // 超过100拷贝深度,直接返回
294
- if(deepIndex > 100) {
295
- return target;
295
+ // 非对象直接返回
296
+ if(!source || typeof source !== 'object') {
297
+ return typeof target !== 'undefined' ? target : source;
296
298
  }
297
299
 
298
- if(source && typeof source === 'object') {
299
- target = target || {};
300
+ // 如果source已经被克隆过,直接返回之前的克隆对象,打破循环引用
301
+ if(cloned.has(source)) return cloned.get(source);
300
302
 
303
+ // 数组处理
304
+ if(Array.isArray(source)) {
301
305
  //如果为当前泛型,则直接new
302
306
  if(this.isType(source, jmList)) {
303
307
  return new jmList(source);
304
308
  }
305
- else if(Array.isArray(source)) {
306
- //如果是深度复,则拷贝每个对象
307
- if(deep) {
308
- let dest = [];
309
- for(let i=0; i<source.length; i++) {
310
- dest.push(this.clone(source[i], target[i], deep, copyHandler, deepIndex));
311
- }
312
- return dest;
313
- }
314
- return source.slice(0);
315
- }
316
-
317
- if(source.__proto__) target.__proto__ = source.__proto__;
318
-
319
- for(let k in source) {
320
- if(k === 'constructor') continue;
321
- const v = source[k];
322
- // 不复制页面元素和class对象
323
- if(v && (v.tagName || v.getContext)) {
324
- target[k] = v;
325
- continue;
326
- }
327
-
328
- // 如果不是对象和空,则采用target的属性
329
- if(typeof target[k] === 'object' || typeof target[k] === 'undefined') {
330
- target[k] = this.clone(v, target[k], deep, copyHandler, deepIndex);
309
+ if(deep) {
310
+ let dest = [];
311
+ cloned.set(source, dest);
312
+ for(let i = 0; i < source.length; i++) {
313
+ dest.push(this.clone(source[i], undefined, deep, copyHandler, deepIndex + 1, cloned));
331
314
  }
315
+ return dest;
332
316
  }
333
- return target;
317
+ return source.slice(0);
334
318
  }
335
- else if(typeof target != 'undefined') {
336
- return target;
319
+
320
+ // 不复制页面元素和class对象(如jmControl实例等复杂对象保持引用)
321
+ if(source.tagName || source.getContext || source.emit) {
322
+ return source;
337
323
  }
338
324
 
339
- return source;
325
+ // 普通对象处理
326
+ target = target || {};
327
+ cloned.set(source, target);
328
+
329
+ // 保持原型链一致
330
+ if(source.__proto__) target.__proto__ = source.__proto__;
331
+
332
+ // 遍历自身可枚举属性(字符串键 + Symbol键),避免触发原型链上宿主对象的getter
333
+ const keys = Object.keys(source).concat(Object.getOwnPropertySymbols(source));
334
+ for(const k of keys) {
335
+ if(k === 'constructor') continue;
336
+ const v = source[k];
337
+ // 不复制页面元素和class对象
338
+ if(v && (v.tagName || v.getContext || v.emit)) {
339
+ target[k] = v;
340
+ continue;
341
+ }
342
+
343
+ // 如果不是对象和空,则采用target的属性
344
+ if(typeof target[k] === 'object' || typeof target[k] === 'undefined') {
345
+ target[k] = this.clone(v, target[k], deep, copyHandler, deepIndex + 1, cloned);
346
+ }
347
+ }
348
+ return target;
340
349
  }
341
350
 
342
351
  /**
@@ -1304,6 +1313,153 @@ class jmShadow {
1304
1313
  }
1305
1314
  }
1306
1315
 
1316
+ /**
1317
+ * CSS滤镜效果类
1318
+ * 支持的滤镜: blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity
1319
+ *
1320
+ * @class jmFilter
1321
+ * @param {string|object} opt 滤镜参数
1322
+ * 字符串格式: "blur(2px) grayscale(50%) brightness(1.2)"
1323
+ * 对象格式: { blur: 2, grayscale: 0.5, brightness: 1.2 }
1324
+ */
1325
+ class jmFilter {
1326
+ constructor(opt) {
1327
+ this.filters = [];
1328
+
1329
+ if(typeof opt === 'string') {
1330
+ this.fromString(opt);
1331
+ }
1332
+ else if(opt && typeof opt === 'object') {
1333
+ for(let k in opt) {
1334
+ if(k === 'constructor' || k === 'filters') continue;
1335
+ this.addFilter(k, opt[k]);
1336
+ }
1337
+ }
1338
+ }
1339
+
1340
+ /**
1341
+ * 添加单个滤镜
1342
+ * @param {string} name 滤镜名称 (blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity)
1343
+ * @param {number|string} value 滤镜值
1344
+ */
1345
+ addFilter(name, value) {
1346
+ name = name.toLowerCase().trim();
1347
+ if(typeof value === 'string') {
1348
+ value = parseFloat(value);
1349
+ }
1350
+ if(isNaN(value)) return;
1351
+
1352
+ // 规范化滤镜名称
1353
+ const normalized = {
1354
+ 'blur': 'blur',
1355
+ 'grayscale': 'grayscale',
1356
+ 'greyscale': 'grayscale',
1357
+ 'sepia': 'sepia',
1358
+ 'brightness': 'brightness',
1359
+ 'contrast': 'contrast',
1360
+ 'saturate': 'saturate',
1361
+ 'hue-rotate': 'hueRotate',
1362
+ 'hueRotate': 'hueRotate',
1363
+ 'invert': 'invert',
1364
+ 'opacity': 'opacity'
1365
+ }[name];
1366
+
1367
+ if(!normalized) return;
1368
+
1369
+ // 检查是否已有同名滤镜,有则更新
1370
+ const existing = this.filters.find(f => f.name === normalized);
1371
+ if(existing) {
1372
+ existing.value = value;
1373
+ }
1374
+ else {
1375
+ this.filters.push({ name: normalized, value: value });
1376
+ }
1377
+ }
1378
+
1379
+ /**
1380
+ * 从字符串格式解析滤镜
1381
+ * 格式: "blur(2px) grayscale(50%) brightness(1.2)"
1382
+ * @param {string} s 滤镜字符串
1383
+ */
1384
+ fromString(s) {
1385
+ if(!s || typeof s !== 'string') return;
1386
+ // 匹配 filterName(value) 模式
1387
+ const regex = /([a-zA-Z-]+)\s*\(\s*([^)]+)\s*\)/g;
1388
+ let match;
1389
+ while((match = regex.exec(s)) !== null) {
1390
+ const name = match[1];
1391
+ const valueStr = match[2].replace(/[a-z%]+$/i, '').trim();
1392
+ const value = parseFloat(valueStr);
1393
+ if(!isNaN(value)) {
1394
+ this.addFilter(name, value);
1395
+ }
1396
+ }
1397
+ }
1398
+
1399
+ /**
1400
+ * 转换为CSS filter字符串格式
1401
+ * @returns {string}
1402
+ */
1403
+ toString() {
1404
+ return this.filters.map(f => {
1405
+ switch(f.name) {
1406
+ case 'blur':
1407
+ return `blur(${f.value}px)`;
1408
+ case 'hueRotate':
1409
+ return `hue-rotate(${f.value}deg)`;
1410
+ default:
1411
+ return `${f.name}(${f.value})`;
1412
+ }
1413
+ }).join(' ');
1414
+ }
1415
+
1416
+ /**
1417
+ * 转换为Canvas context.filter可用的字符串
1418
+ * @returns {string}
1419
+ */
1420
+ toCanvasFilter() {
1421
+ if(this.filters.length === 0) return 'none';
1422
+ return this.toString();
1423
+ }
1424
+
1425
+ /**
1426
+ * 检查是否有指定名称的滤镜
1427
+ * @param {string} name 滤镜名称
1428
+ * @returns {boolean}
1429
+ */
1430
+ has(name) {
1431
+ return this.filters.some(f => f.name === name);
1432
+ }
1433
+
1434
+ /**
1435
+ * 获取指定滤镜的值
1436
+ * @param {string} name 滤镜名称
1437
+ * @returns {number|undefined}
1438
+ */
1439
+ get(name) {
1440
+ const f = this.filters.find(f => f.name === name);
1441
+ return f ? f.value : undefined;
1442
+ }
1443
+
1444
+ /**
1445
+ * 移除指定滤镜
1446
+ * @param {string} name 滤镜名称
1447
+ */
1448
+ remove(name) {
1449
+ const index = this.filters.findIndex(f => f.name === name);
1450
+ if(index > -1) {
1451
+ this.filters.splice(index, 1);
1452
+ }
1453
+ }
1454
+
1455
+ /**
1456
+ * 清空所有滤镜
1457
+ */
1458
+ clear() {
1459
+ this.filters = [];
1460
+ }
1461
+ }
1462
+
1307
1463
  let control_id_counter = 0;
1308
1464
 
1309
1465
  class jmObject {
@@ -2179,8 +2335,14 @@ class WebglGradient {
2179
2335
 
2180
2336
  // 转为渐变为纹理
2181
2337
  toImageData(control, bounds, points=null) {
2182
- //const key = this.key || this.toString();
2183
- //if(WebglGradientTextureCache[key]) return WebglGradientTextureCache[key];
2338
+ // 缓存基于渐变参数(不含 bounds,因为同一个渐变只是位置不同时纹理相同)
2339
+ const gradientKey = this.toString();
2340
+ if(this.__cachedData && this.__cacheKey === gradientKey &&
2341
+ this.__cachedData.data && this.__cachedData.data.width === Math.ceil(bounds.width) &&
2342
+ this.__cachedData.data.data && this.__cachedData.data.data.height === Math.ceil(bounds.height)) {
2343
+ return this.__cachedData;
2344
+ }
2345
+
2184
2346
  if(!control.textureContext) {
2185
2347
  return null;
2186
2348
  }
@@ -2198,11 +2360,18 @@ class WebglGradient {
2198
2360
 
2199
2361
  const data = control.toFillTexture(gradient, bounds, points);
2200
2362
 
2201
- //WebglGradientTextureCache[key] = data;
2363
+ this.__cachedData = data;
2364
+ this.__cacheKey = gradientKey;
2202
2365
 
2203
2366
  return data;
2204
2367
  }
2205
2368
 
2369
+ // 当渐变参数变化时使缓存失效
2370
+ invalidateCache() {
2371
+ this.__cachedData = null;
2372
+ this.__cacheKey = null;
2373
+ }
2374
+
2206
2375
  // 根据绘制图形的坐标计算出对应点的颜色
2207
2376
  /*
2208
2377
  toPointColors(points) {
@@ -2823,7 +2992,6 @@ class WeblBase {
2823
2992
 
2824
2993
  // 创建程序
2825
2994
  createProgram(vertexSrc, fragmentSrc) {
2826
- this.context.lineWidth(1);
2827
2995
  return createProgram(this.context, vertexSrc, fragmentSrc);
2828
2996
  }
2829
2997
 
@@ -2936,7 +3104,9 @@ class WeblBase {
2936
3104
  // polygonIndices 顶点索引,
2937
3105
  earCutPointsToTriangles(points) {
2938
3106
  this.earCutCache = this.earCutCache || (this.earCutCache = {});
2939
- const key = JSON.stringify(points);
3107
+ // 快速缓存 key:用长度和首尾点坐标
3108
+ const len = points.length;
3109
+ const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
2940
3110
  if (this.earCutCache[key]) return this.earCutCache[key];
2941
3111
 
2942
3112
  const ps = this.earCutPoints(points);// 切割得到3角色顶点索引,
@@ -3031,31 +3201,11 @@ class WeblBase {
3031
3201
 
3032
3202
  this.textureContext.fillStyle = fillStyle;
3033
3203
 
3034
- this.textureContext.beginPath();
3204
+ // 规则图形用 fillRect,比 beginPath/lineTo/fill 快
3035
3205
  if(!points || !points.length) {
3036
- points = [];
3037
- points.push({
3038
- x: bounds.left,
3039
- y: bounds.top
3040
- });
3041
- points.push({
3042
- x: bounds.left + bounds.width,
3043
- y: bounds.top
3044
- });
3045
- points.push({
3046
- x: bounds.left + bounds.width,
3047
- y: bounds.top + bounds.height
3048
- });
3049
- points.push({
3050
- x: bounds.left,
3051
- y: bounds.top + bounds.height
3052
- });
3053
- points.push({
3054
- x: bounds.left,
3055
- y: bounds.top
3056
- });
3057
- }
3058
- if(points && points.length) {
3206
+ this.textureContext.fillRect(0, 0, bounds.width, bounds.height);
3207
+ } else {
3208
+ this.textureContext.beginPath();
3059
3209
  for(const p of points) {
3060
3210
  //移至当前坐标
3061
3211
  if(p.m) {
@@ -3064,17 +3214,10 @@ class WeblBase {
3064
3214
  else {
3065
3215
  this.textureContext.lineTo(p.x - bounds.left, p.y - bounds.top);
3066
3216
  }
3067
- }
3068
- }
3069
- else {
3070
- this.textureContext.moveTo(0, 0);
3071
- this.textureContext.lineTo(bounds.width, 0);
3072
- this.textureContext.lineTo(bounds.width, bounds.height);
3073
- this.textureContext.lineTo(0, bounds.height);
3074
- this.textureContext.lineTo(0, 0);
3217
+ }
3218
+ this.textureContext.closePath();
3219
+ this.textureContext.fill();
3075
3220
  }
3076
- this.textureContext.closePath();
3077
- this.textureContext.fill();
3078
3221
 
3079
3222
  const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
3080
3223
  return {
@@ -3093,6 +3236,40 @@ class WebglPath extends WeblBase {
3093
3236
  this.needCut = option.needCut || false;
3094
3237
  this.control = option.control;
3095
3238
  this.points = [];
3239
+ // 缓存 buffer 和纹理,避免每帧创建/销毁
3240
+ this.__cachedBuffers = [];
3241
+ this.__cachedTexture = null;
3242
+ this.__cachedTextureKey = null;
3243
+ }
3244
+
3245
+ // 释放缓存的 WebGL 资源
3246
+ dispose() {
3247
+ for(const buf of this.__cachedBuffers) {
3248
+ this.deleteBuffer(buf);
3249
+ }
3250
+ this.__cachedBuffers = [];
3251
+ if(this.__cachedTexture) {
3252
+ this.deleteTexture(this.__cachedTexture);
3253
+ this.__cachedTexture = null;
3254
+ this.__cachedTextureKey = null;
3255
+ }
3256
+ }
3257
+
3258
+ // 获取或创建 buffer,优先复用缓存
3259
+ getOrCreateBuffer(data, attr) {
3260
+ let buffer = this.__cachedBuffers.find(b => b.attr === attr);
3261
+ if(buffer) {
3262
+ const gl = this.context;
3263
+ const float32 = new Float32Array(data);
3264
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer.buffer);
3265
+ gl.bufferData(gl.ARRAY_BUFFER, float32, gl.DYNAMIC_DRAW);
3266
+ buffer.data = data;
3267
+ return buffer;
3268
+ }
3269
+ buffer = this.createFloat32Buffer(data);
3270
+ buffer.attr = attr;
3271
+ this.__cachedBuffers.push(buffer);
3272
+ return buffer;
3096
3273
  }
3097
3274
 
3098
3275
  // 应用变换到点
@@ -3105,8 +3282,14 @@ class WebglPath extends WeblBase {
3105
3282
  //this.useProgram();
3106
3283
 
3107
3284
  if(parentBounds) this.parentAbsoluteBounds = parentBounds;
3108
- // 写入当前canvas大小
3109
- this.context.uniform2f(this.program.uniforms.a_center_point.location, this.graph.width / 2, this.graph.height / 2);
3285
+ // 缓存中心点值,只在变化时才更新 uniform
3286
+ const cx = this.graph.width / 2;
3287
+ const cy = this.graph.height / 2;
3288
+ if(this.__lastCenterX !== cx || this.__lastCenterY !== cy) {
3289
+ this.context.uniform2f(this.program.uniforms.a_center_point.location, cx, cy);
3290
+ this.__lastCenterX = cx;
3291
+ this.__lastCenterY = cy;
3292
+ }
3110
3293
  }
3111
3294
 
3112
3295
  setFragColor(color) {
@@ -3147,6 +3330,7 @@ class WebglPath extends WeblBase {
3147
3330
  endDraw() {
3148
3331
  if(this.points) delete this.points;
3149
3332
  if(this.pathPoints) delete this.pathPoints;
3333
+ // 缓存的纹理保留到下次绘制(渐变可能不变)
3150
3334
  }
3151
3335
 
3152
3336
  // 图形封闭
@@ -3158,21 +3342,47 @@ class WebglPath extends WeblBase {
3158
3342
  }
3159
3343
  }
3160
3344
 
3161
- // 绘制点数组
3345
+ // 绘制点数组(使用 DYNAMIC_DRAW 复用 buffer,避免每帧 create/delete)
3162
3346
  writePoints(points, attr = this.program.attrs.a_position) {
3163
-
3164
3347
  const fixedPoints = [];
3165
- for(const p of points) {
3166
- // 应用变换矩阵
3167
- const transformedPoint = this.applyTransform(p);
3168
- fixedPoints.push(
3169
- transformedPoint.x + this.parentAbsoluteBounds.left,
3170
- transformedPoint.y + this.parentAbsoluteBounds.top
3171
- );
3172
- }
3173
- const vertexBuffer = this.createFloat32Buffer(fixedPoints);
3348
+ const [a, b, c, d, tx, ty] = this.transformMatrix;
3349
+ const isIdentity = (a === 1 && b === 0 && c === 0 && d === 1 && tx === 0 && ty === 0);
3350
+ const offsetLeft = this.parentAbsoluteBounds.left;
3351
+ const offsetTop = this.parentAbsoluteBounds.top;
3352
+
3353
+ if(isIdentity) {
3354
+ // 单位矩阵时直接加偏移,避免逐点调用 applyTransform
3355
+ for(let i = 0; i < points.length; i++) {
3356
+ fixedPoints.push(points[i].x + offsetLeft, points[i].y + offsetTop);
3357
+ }
3358
+ } else {
3359
+ for(const p of points) {
3360
+ const transformedPoint = this.applyTransform(p);
3361
+ fixedPoints.push(
3362
+ transformedPoint.x + offsetLeft,
3363
+ transformedPoint.y + offsetTop
3364
+ );
3365
+ }
3366
+ }
3367
+ const float32 = new Float32Array(fixedPoints);
3368
+ const gl = this.context;
3369
+
3370
+ // 复用已有 buffer 或创建新的
3371
+ if(this.__cachedBuffers.length > 0) {
3372
+ // 找一个同 attr 的 buffer 复用
3373
+ let buffer = this.__cachedBuffers.find(b => b.attr === attr);
3374
+ if(buffer) {
3375
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer.buffer);
3376
+ gl.bufferData(gl.ARRAY_BUFFER, float32, gl.DYNAMIC_DRAW);
3377
+ buffer.data = fixedPoints;
3378
+ this.writeVertexAttrib(buffer, attr, 2, 0, 0);
3379
+ return buffer;
3380
+ }
3381
+ }
3382
+ const vertexBuffer = this.createFloat32Buffer(float32, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3174
3383
  this.writeVertexAttrib(vertexBuffer, attr, 2, 0, 0);
3175
3384
  vertexBuffer.attr = attr;
3385
+ this.__cachedBuffers.push(vertexBuffer);
3176
3386
  return vertexBuffer;
3177
3387
  }
3178
3388
 
@@ -3408,9 +3618,10 @@ class WebglPath extends WeblBase {
3408
3618
 
3409
3619
  // 分割成一个个规则的三角形,不规则的多边形不全割的话纹理就会没法正确覆盖
3410
3620
  getTriangles(points) {
3411
-
3412
3621
  this.trianglesCache = this.trianglesCache||(this.trianglesCache={});
3413
- const key = JSON.stringify(points);
3622
+ // 快速缓存 key:用长度和首尾点坐标(比 JSON.stringify 快几个数量级)
3623
+ const len = points.length;
3624
+ const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
3414
3625
  if(this.trianglesCache[key]) return this.trianglesCache[key];
3415
3626
 
3416
3627
  const res = [];
@@ -3447,12 +3658,11 @@ class WebglPath extends WeblBase {
3447
3658
  if(points && points.length) {
3448
3659
  const regular = lineWidth <= 1.2;
3449
3660
  points = regular? points : this.pathToPoints(points);
3450
- const buffer = this.writePoints(points);
3661
+ this.writePoints(points);
3451
3662
  this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
3452
- this.deleteBuffer(buffer);
3663
+ // buffer 由 endDraw 统一清理
3453
3664
  }
3454
- colorBuffer && this.deleteBuffer(colorBuffer);
3455
- colorBuffer && this.disableVertexAttribArray(colorBuffer.attr);
3665
+ colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
3456
3666
  }
3457
3667
 
3458
3668
  // 填充图形
@@ -3483,8 +3693,7 @@ class WebglPath extends WeblBase {
3483
3693
 
3484
3694
  this.fillPolygons(points);
3485
3695
 
3486
- colorBuffer && this.deleteBuffer(colorBuffer);
3487
- colorBuffer && this.disableVertexAttribArray(colorBuffer.attr);
3696
+ colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
3488
3697
 
3489
3698
  }
3490
3699
 
@@ -3494,8 +3703,24 @@ class WebglPath extends WeblBase {
3494
3703
  fillImage(img, points, bounds) {
3495
3704
  if(!img) return;
3496
3705
 
3497
- // 设置纹理
3498
- const texture = img instanceof ImageData? this.createDataTexture(img) : this.createImgTexture(img);
3706
+ // 对于 ImageData,生成缓存 key(基于渐变参数或 bounds),复用纹理
3707
+ let texture = null;
3708
+ if(img instanceof ImageData) {
3709
+ const key = `${img.width}_${img.height}_${bounds.width}_${bounds.height}_${bounds.left}_${bounds.top}`;
3710
+ if(this.__cachedTexture && this.__cachedTextureKey === key) {
3711
+ texture = this.__cachedTexture;
3712
+ } else {
3713
+ texture = this.createDataTexture(img);
3714
+ // 释放旧纹理
3715
+ if(this.__cachedTexture) {
3716
+ this.deleteTexture(this.__cachedTexture);
3717
+ }
3718
+ this.__cachedTexture = texture;
3719
+ this.__cachedTextureKey = key;
3720
+ }
3721
+ } else {
3722
+ texture = this.createImgTexture(img);
3723
+ }
3499
3724
  this.context.uniform1i(this.program.uniforms.u_sample.location, 0); // 纹理单元传递给着色器
3500
3725
 
3501
3726
  // 指定纹理区域尺寸
@@ -3508,7 +3733,10 @@ class WebglPath extends WeblBase {
3508
3733
 
3509
3734
  this.fillTexture(points);
3510
3735
 
3511
- this.deleteTexture(texture);
3736
+ // 仅对非缓存纹理(非 ImageData)立即删除
3737
+ if(!(img instanceof ImageData)) {
3738
+ this.deleteTexture(texture);
3739
+ }
3512
3740
  }
3513
3741
 
3514
3742
  fillTexture(points) {
@@ -3524,23 +3752,67 @@ class WebglPath extends WeblBase {
3524
3752
 
3525
3753
  // 进行多边形填充
3526
3754
  fillPolygons(points, isTexture = false) {
3527
- if(points.length > 3) {
3528
- const triangles = this.needCut? this.earCutPointsToTriangles(points): this.getTriangles(points);
3529
- if(triangles.length) {
3530
- for(const triangle of triangles) {
3531
- this.fillPolygons(triangle, isTexture);// 这里就变成了规则的图形了
3532
- }
3533
- }
3755
+ if(points.length <= 3) {
3756
+ // 3个点以下的三角形直接画
3757
+ this.writePoints(points);
3758
+ isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3759
+ this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
3760
+ return;
3534
3761
  }
3535
- else {
3536
- const buffer = this.writePoints(points);
3537
- // 纹理坐标
3538
- const coordBuffer = isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3539
3762
 
3763
+ // 规则图形(凸多边形,如圆):直接用 TRIANGLE_FAN 一次性绘制,无需 earcut
3764
+ if(this.isRegular) {
3765
+ this.writePoints(points);
3766
+ isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3540
3767
  this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
3541
- this.deleteBuffer(buffer);
3542
- coordBuffer && this.deleteBuffer(coordBuffer);
3768
+ return;
3769
+ }
3770
+
3771
+ // 不规则图形:需要 earcut 三角化后,合并为一个大的顶点缓冲区,单次 drawArrays
3772
+ const triangles = this.needCut? this.earCutPointsToTriangles(points): this.getTriangles(points);
3773
+ if(!triangles.length) return;
3774
+
3775
+ // 合并所有三角形的顶点到一个数组
3776
+ const allVertices = [];
3777
+ const allTexCoords = [];
3778
+ for(const triangle of triangles) {
3779
+ for(const p of triangle) {
3780
+ allVertices.push(p.x, p.y);
3781
+ if(isTexture) allTexCoords.push(p.x, p.y);
3782
+ }
3783
+ }
3784
+
3785
+ // 一次性上传所有数据并绘制
3786
+ const vertexData = new Float32Array(allVertices);
3787
+ const gl = this.context;
3788
+
3789
+ // 复用或创建 position buffer
3790
+ let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
3791
+ if(!posBuffer) {
3792
+ posBuffer = this.createFloat32Buffer(vertexData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3793
+ posBuffer.attr = this.program.attrs.a_position;
3794
+ this.__cachedBuffers.push(posBuffer);
3795
+ } else {
3796
+ gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
3797
+ gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.DYNAMIC_DRAW);
3798
+ }
3799
+ this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
3800
+
3801
+ if(isTexture && allTexCoords.length) {
3802
+ const texData = new Float32Array(allTexCoords);
3803
+ let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
3804
+ if(!texBuffer) {
3805
+ texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3806
+ texBuffer.attr = this.program.attrs.a_text_coord;
3807
+ this.__cachedBuffers.push(texBuffer);
3808
+ } else {
3809
+ gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
3810
+ gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
3811
+ }
3812
+ this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
3543
3813
  }
3814
+
3815
+ gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
3544
3816
  }
3545
3817
 
3546
3818
  // 填充图形
@@ -3622,7 +3894,9 @@ const jmStyleMap = {
3622
3894
  'shadowOffsetY' : 'shadowOffsetY',
3623
3895
  'shadowColor' : 'shadowColor',
3624
3896
  'lineJoin': 'lineJoin',
3625
- 'lineCap':'lineCap'
3897
+ 'lineCap':'lineCap',
3898
+ 'lineDashOffset': 'lineDashOffset',
3899
+ 'globalCompositeOperation': 'globalCompositeOperation'
3626
3900
  };
3627
3901
 
3628
3902
  class jmControl extends jmProperty {
@@ -3833,7 +4107,9 @@ class jmControl extends jmProperty {
3833
4107
  }
3834
4108
 
3835
4109
  setStyle(style) {
3836
- style = style || jmUtils.clone(this.style, true);
4110
+ if(!style) {
4111
+ style = this.style;
4112
+ }
3837
4113
  if(!style) return;
3838
4114
 
3839
4115
  const __setStyle = (style, name, mpkey) => {
@@ -3917,6 +4193,120 @@ class jmControl extends jmProperty {
3917
4193
  this.cursor = styleValue;
3918
4194
  break;
3919
4195
  }
4196
+ // ===== 新增样式特性 =====
4197
+
4198
+ // 虚线样式:支持自定义lineDash模式 (如 [5, 3, 2] 或 "5,3,2")
4199
+ case 'lineDash' : {
4200
+ if(!this.context.setLineDash) break;
4201
+ let dash;
4202
+ if(typeof styleValue === 'string') {
4203
+ dash = styleValue.split(',').map(v => parseFloat(v.trim())).filter(v => !isNaN(v));
4204
+ }
4205
+ else if(Array.isArray(styleValue)) {
4206
+ dash = styleValue.map(v => parseFloat(v)).filter(v => !isNaN(v));
4207
+ }
4208
+ if(dash && dash.length) {
4209
+ this.context.setLineDash(dash);
4210
+ }
4211
+ else {
4212
+ this.context.setLineDash([]);
4213
+ }
4214
+ break;
4215
+ }
4216
+ // 虚线偏移量
4217
+ case 'lineDashOffset' : {
4218
+ if(!this.context.setLineDash) break;
4219
+ this.context.lineDashOffset = Number(styleValue) || 0;
4220
+ break;
4221
+ }
4222
+ // CSS滤镜效果 (blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity)
4223
+ case 'filter' : {
4224
+ if(this.context.filter === undefined) break;
4225
+ if(styleValue instanceof jmFilter) {
4226
+ this.context.filter = styleValue.toCanvasFilter();
4227
+ }
4228
+ else if(typeof styleValue === 'string') {
4229
+ this.context.filter = styleValue || 'none';
4230
+ }
4231
+ else if(typeof styleValue === 'object') {
4232
+ this.context.filter = (new jmFilter(styleValue)).toCanvasFilter();
4233
+ }
4234
+ break;
4235
+ }
4236
+ // 混合模式 (source-over, multiply, screen, overlay, darken, lighten, etc.)
4237
+ case 'globalCompositeOperation' : {
4238
+ if(!this.context.globalCompositeOperation) break;
4239
+ this.context.globalCompositeOperation = styleValue;
4240
+ break;
4241
+ }
4242
+ // 裁剪路径:通过canvas clip实现
4243
+ case 'clipPath' : {
4244
+ if(!this.context.clip) break;
4245
+ // clipPath可以是一个图形控件实例
4246
+ if(styleValue && styleValue.points && styleValue.points.length > 0) {
4247
+ const bounds = this.parent && this.parent.absoluteBounds ? this.parent.absoluteBounds : this.absoluteBounds;
4248
+ this.context.beginPath();
4249
+ this.context.moveTo(styleValue.points[0].x + (bounds ? bounds.left : 0), styleValue.points[0].y + (bounds ? bounds.top : 0));
4250
+ for(let i = 1; i < styleValue.points.length; i++) {
4251
+ if(styleValue.points[i].m) {
4252
+ this.context.moveTo(styleValue.points[i].x + (bounds ? bounds.left : 0), styleValue.points[i].y + (bounds ? bounds.top : 0));
4253
+ }
4254
+ else {
4255
+ this.context.lineTo(styleValue.points[i].x + (bounds ? bounds.left : 0), styleValue.points[i].y + (bounds ? bounds.top : 0));
4256
+ }
4257
+ }
4258
+ if(styleValue.style && styleValue.style.close) {
4259
+ this.context.closePath();
4260
+ }
4261
+ this.context.clip();
4262
+ }
4263
+ break;
4264
+ }
4265
+ // 遮罩效果:通过globalCompositeOperation + destination-in实现
4266
+ case 'mask' : {
4267
+ if(!this.context.globalCompositeOperation) break;
4268
+ // mask是一个图形控件实例,在绘制前需要先应用mask
4269
+ // 这里只是标记,实际绘制在paint流程中处理
4270
+ this.__mask = styleValue;
4271
+ break;
4272
+ }
4273
+ // 图片阴影描边阴影(WebGL纹理canvas用)
4274
+ case 'shadowColor' : {
4275
+ if(this.webglControl) {
4276
+ this.webglControl.setStyle('shadowColor', styleValue);
4277
+ }
4278
+ else {
4279
+ this.context.shadowColor = jmUtils.toColor(styleValue);
4280
+ }
4281
+ break;
4282
+ }
4283
+ case 'shadowBlur' : {
4284
+ if(this.webglControl) {
4285
+ this.webglControl.setStyle('shadowBlur', styleValue);
4286
+ }
4287
+ else {
4288
+ this.context.shadowBlur = Number(styleValue) || 0;
4289
+ }
4290
+ break;
4291
+ }
4292
+ case 'shadowOffsetX' : {
4293
+ if(this.webglControl) {
4294
+ this.webglControl.setStyle('shadowOffsetX', styleValue);
4295
+ }
4296
+ else {
4297
+ this.context.shadowOffsetX = Number(styleValue) || 0;
4298
+ }
4299
+ break;
4300
+ }
4301
+ case 'shadowOffsetY' : {
4302
+ if(this.webglControl) {
4303
+ this.webglControl.setStyle('shadowOffsetY', styleValue);
4304
+ }
4305
+ else {
4306
+ this.context.shadowOffsetY = Number(styleValue) || 0;
4307
+ }
4308
+ break;
4309
+ }
3920
4310
  }
3921
4311
  }
3922
4312
  }
@@ -3937,6 +4327,9 @@ class jmControl extends jmProperty {
3937
4327
  else if(t == 'string' && k == 'shadow') {
3938
4328
  style[k] = new jmShadow(style[k]);
3939
4329
  }
4330
+ else if(t == 'string' && k == 'filter') {
4331
+ style[k] = new jmFilter(style[k]);
4332
+ }
3940
4333
  __setStyle(style[k], k);
3941
4334
  }
3942
4335
  }
@@ -4081,25 +4474,30 @@ class jmControl extends jmProperty {
4081
4474
  * @method getLocation
4082
4475
  * @return {object} 当前控件位置参数,包括中心点坐标,右上角坐标,宽高
4083
4476
  */
4084
- getLocation(clone=true) {
4477
+ getLocation() {
4085
4478
  //如果已经计算过则直接返回
4086
4479
  //在开画之前会清空此对象
4087
4480
  //if(reset !== true && this.location) return this.location;
4088
4481
 
4089
4482
  let local = this.location = {left: 0,top: 0,width: 0,height: 0};
4090
- local.position = typeof this.position == 'function'? this.position(): jmUtils.clone(this.position);
4091
- local.center = this.center && typeof this.center === 'function'?this.center(): jmUtils.clone(this.center);//中心
4092
- local.start = this.start && typeof this.start === 'function'?this.start(): jmUtils.clone(this.start);//起点
4093
- local.end = this.end && typeof this.end === 'function'?this.end(): jmUtils.clone(this.end);//起点
4483
+
4484
+ // 检查是否有百分比参数需要解析,没有则直接引用避免克隆开销
4485
+ const needResolve = this.parent && (jmUtils.checkPercent(this.width) || jmUtils.checkPercent(this.height) ||
4486
+ (this.position && jmUtils.checkPercent(this.position.x)) || (this.position && jmUtils.checkPercent(this.position.y)));
4487
+ local.position = typeof this.position == 'function'? this.position(): (needResolve? jmUtils.clone(this.position) : this.position);
4488
+ local.center = this.center && typeof this.center === 'function'?this.center(): (needResolve? jmUtils.clone(this.center) : this.center);//中心
4489
+ local.start = this.start && typeof this.start === 'function'?this.start(): (needResolve? jmUtils.clone(this.start) : this.start);//起点
4490
+ local.end = this.end && typeof this.end === 'function'?this.end(): (needResolve? jmUtils.clone(this.end) : this.end);//起点
4094
4491
  local.radius = this.radius;//半径
4095
4492
  local.width = this.width;
4096
4493
  local.height = this.height;
4097
4494
 
4098
- const margin = jmUtils.clone(this.style.margin, {});
4099
- margin.left = (margin.left || 0);
4100
- margin.top = (margin.top || 0);
4101
- margin.right = (margin.right || 0);
4102
- margin.bottom = (margin.bottom || 0);
4495
+ const margin = this.style.margin;
4496
+ const marginObj = needResolve && margin ? jmUtils.clone(margin, {}) : (margin || {});
4497
+ marginObj.left = (marginObj.left || 0);
4498
+ marginObj.top = (marginObj.top || 0);
4499
+ marginObj.right = (marginObj.right || 0);
4500
+ marginObj.bottom = (marginObj.bottom || 0);
4103
4501
 
4104
4502
  //如果没有指定位置,但指定了margin。则位置取margin偏移量
4105
4503
  if(local.position) {
@@ -4107,8 +4505,8 @@ class jmControl extends jmProperty {
4107
4505
  local.top = local.position.y;
4108
4506
  }
4109
4507
  else {
4110
- local.left = margin.left;
4111
- local.top = margin.top;
4508
+ local.left = marginObj.left;
4509
+ local.top = marginObj.top;
4112
4510
  }
4113
4511
 
4114
4512
  if(this.parent) {
@@ -4370,22 +4768,31 @@ class jmControl extends jmProperty {
4370
4768
  if(this.webglControl) this.webglControl.closePath();
4371
4769
  this.context.closePath && this.context.closePath();
4372
4770
  }
4373
-
4374
- const fill = this.style['fill'] || this.style['fillStyle'];
4375
- if(fill) {
4376
- if(this.webglControl) {
4771
+
4772
+ // 根据渲染模式选择不同的绘制路径
4773
+ if(this.webglControl) {
4774
+ // WebGL 模式:使用 WebGL 绘制
4775
+ const fill = this.style['fill'] || this.style['fillStyle'];
4776
+ if(fill) {
4377
4777
  const bounds = this.getBounds();
4378
4778
  this.webglControl.fill(bounds);
4379
4779
  }
4380
- this.context.fill && this.context.fill();
4780
+ if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
4781
+ this.webglControl.stroke();
4782
+ }
4783
+ if(this.webglControl.endDraw) this.webglControl.endDraw();
4381
4784
  }
4382
- if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
4383
- if(this.webglControl) this.webglControl.stroke();
4384
- this.context.stroke && this.context.stroke();
4785
+ else {
4786
+ // 2D 模式:使用 Canvas 2D API 绘制
4787
+ const fill = this.style['fill'] || this.style['fillStyle'];
4788
+ if(fill) {
4789
+ this.context.fill && this.context.fill();
4790
+ }
4791
+ if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
4792
+ this.context.stroke && this.context.stroke();
4793
+ }
4385
4794
  }
4386
4795
 
4387
- if(this.webglControl && this.webglControl.endDraw) this.webglControl.endDraw();
4388
-
4389
4796
  this.needUpdate = false;
4390
4797
  }
4391
4798
 
@@ -4401,9 +4808,7 @@ class jmControl extends jmProperty {
4401
4808
  const bounds = this.parent && this.parent.absoluteBounds?this.parent.absoluteBounds:this.absoluteBounds;
4402
4809
  if(this.webglControl) {
4403
4810
  this.webglControl.setParentBounds(bounds);
4404
- this.webglControl.draw([
4405
- ...this.points
4406
- ]);
4811
+ this.webglControl.draw(this.points);
4407
4812
  }
4408
4813
  else if(this.context && this.context.moveTo) {
4409
4814
  this.context.moveTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
@@ -4448,9 +4853,44 @@ class jmControl extends jmProperty {
4448
4853
 
4449
4854
  this.setStyle();//设定样式
4450
4855
 
4451
- if(needDraw && this.beginDraw) this.beginDraw();
4452
- if(needDraw && this.draw) this.draw();
4453
- if(needDraw && this.endDraw) this.endDraw();
4856
+ // 应用mask遮罩效果:在mask区域内绘制当前控件
4857
+ // 使用 destination-in 合成模式,只保留mask区域内的内容
4858
+ const maskStyle = this.style.mask || this.__mask;
4859
+ if(maskStyle && maskStyle.points && this.context.globalCompositeOperation) {
4860
+ // 先绘制当前控件
4861
+ if(needDraw && this.beginDraw) this.beginDraw();
4862
+ if(needDraw && this.draw) this.draw();
4863
+ if(needDraw && this.endDraw) this.endDraw();
4864
+
4865
+ // 再应用mask裁剪
4866
+ this.context.globalCompositeOperation = 'destination-in';
4867
+ if(maskStyle.initPoints) maskStyle.initPoints();
4868
+ const mBounds = maskStyle.parent && maskStyle.parent.absoluteBounds ? maskStyle.parent.absoluteBounds : this.absoluteBounds;
4869
+ this.context.beginPath();
4870
+ if(maskStyle.points && maskStyle.points.length > 0) {
4871
+ this.context.moveTo(maskStyle.points[0].x + (mBounds ? mBounds.left : 0), maskStyle.points[0].y + (mBounds ? mBounds.top : 0));
4872
+ for(let i = 1; i < maskStyle.points.length; i++) {
4873
+ if(maskStyle.points[i].m) {
4874
+ this.context.moveTo(maskStyle.points[i].x + (mBounds ? mBounds.left : 0), maskStyle.points[i].y + (mBounds ? mBounds.top : 0));
4875
+ }
4876
+ else {
4877
+ this.context.lineTo(maskStyle.points[i].x + (mBounds ? mBounds.left : 0), maskStyle.points[i].y + (mBounds ? mBounds.top : 0));
4878
+ }
4879
+ }
4880
+ if(maskStyle.style && maskStyle.style.close) {
4881
+ this.context.closePath();
4882
+ }
4883
+ }
4884
+ this.context.fillStyle = '#ffffff';
4885
+ this.context.fill();
4886
+ // 恢复合成模式
4887
+ this.context.globalCompositeOperation = 'source-over';
4888
+ }
4889
+ else {
4890
+ if(needDraw && this.beginDraw) this.beginDraw();
4891
+ if(needDraw && this.draw) this.draw();
4892
+ if(needDraw && this.endDraw) this.endDraw();
4893
+ }
4454
4894
 
4455
4895
  if(this.children) {
4456
4896
  this.children.each(function(i,item) {
@@ -4540,7 +4980,15 @@ class jmControl extends jmProperty {
4540
4980
  * @param {array} args 事件参数数组
4541
4981
  */
4542
4982
  emit(...args) {
4543
- this.runEventHandle(args[0], args.slice(1));
4983
+ // 避免每帧 args.slice(1) 分配临时数组
4984
+ // runEventHandle 内部会把非数组参数包装成数组
4985
+ if(args.length > 2) {
4986
+ this.runEventHandle(args[0], args.slice(1));
4987
+ } else if(args.length === 2) {
4988
+ this.runEventHandle(args[0], [args[1]]);
4989
+ } else {
4990
+ this.runEventHandle(args[0], []);
4991
+ }
4544
4992
  return this;
4545
4993
  }
4546
4994
 
@@ -5180,7 +5628,6 @@ class jmArc extends jmPath {
5180
5628
  if((mw == 0 && mh == 0) || start == end) return;
5181
5629
 
5182
5630
  let anticlockwise = this.anticlockwise;
5183
- this.points = [];
5184
5631
  let step = 1 / Math.max(mw, mh);
5185
5632
 
5186
5633
  //如果是逆时针绘制,则角度为负数,并且结束角为2Math.PI-end
@@ -5191,18 +5638,33 @@ class jmArc extends jmPath {
5191
5638
  }
5192
5639
  if(start > end) step = -step;
5193
5640
 
5194
- if(this.isFan) this.points.push(location.center);// 如果是扇形,则从中心开始画
5641
+ // 预计算需要的点数量
5642
+ let pointCount = Math.ceil(Math.abs(end - start) / Math.abs(step)) + 1;
5643
+ if(this.isFan) pointCount++;
5644
+
5645
+ // 复用已有数组,避免每帧分配;大小变化时才重建
5646
+ if(!this.points || this.points.length !== pointCount) {
5647
+ this.points = new Array(pointCount);
5648
+ for(let i = 0; i < pointCount; i++) {
5649
+ this.points[i] = { x: 0, y: 0 };
5650
+ }
5651
+ }
5652
+
5653
+ let idx = 0;
5654
+ if(this.isFan) {
5655
+ this.points[idx].x = location.center.x;
5656
+ this.points[idx].y = location.center.y;
5657
+ idx++;
5658
+ }
5195
5659
 
5196
5660
  //椭圆方程x=a*cos(r) ,y=b*sin(r)
5197
5661
  for(let r=start;;r += step) {
5198
5662
  if(step > 0 && r > end) r = end;
5199
5663
  else if(step < 0 && r < end) r = end;
5200
5664
 
5201
- const p = {
5202
- x : Math.cos(r) * mw + cx,
5203
- y : Math.sin(r) * mh + cy
5204
- };
5205
- this.points.push(p);
5665
+ this.points[idx].x = Math.cos(r) * mw + cx;
5666
+ this.points[idx].y = Math.sin(r) * mh + cy;
5667
+ idx++;
5206
5668
 
5207
5669
  if(r == end) break;
5208
5670
  }
@@ -5801,6 +6263,7 @@ class jmPrismatic extends jmPath {
5801
6263
  * @class jmRect
5802
6264
  * @extends jmPath
5803
6265
  * @param {object} params 参数 position=矩形左上角顶点坐标,width=宽,height=高,radius=边角弧度
6266
+ * radius支持数字(四角相同)或对象 { topLeft, topRight, bottomRight, bottomLeft }
5804
6267
  */
5805
6268
  class jmRect extends jmPath {
5806
6269
 
@@ -5810,12 +6273,24 @@ class jmRect extends jmPath {
5810
6273
  super(params, t);
5811
6274
 
5812
6275
  this.style.close = true;
5813
- this.radius = params.radius || this.style.radius || 0;
6276
+ const r = params.radius || this.style.radius || this.style.borderRadius || 0;
6277
+ if(typeof r === 'object' && r !== null) {
6278
+ // 四角独立圆角
6279
+ this.radius = {
6280
+ topLeft: Number(r.topLeft) || 0,
6281
+ topRight: Number(r.topRight) || 0,
6282
+ bottomRight: Number(r.bottomRight) || 0,
6283
+ bottomLeft: Number(r.bottomLeft) || 0
6284
+ };
6285
+ }
6286
+ else {
6287
+ this.radius = r;
6288
+ }
5814
6289
  }
5815
6290
  /**
5816
- * 圆角半径
6291
+ * 圆角半径,支持数字或四角独立对象
5817
6292
  * @property radius
5818
- * @type {number}
6293
+ * @type {number|object}
5819
6294
  */
5820
6295
  get radius() {
5821
6296
  return this.property('radius');
@@ -5823,6 +6298,36 @@ class jmRect extends jmPath {
5823
6298
  set radius(v) {
5824
6299
  this.needUpdate = true;
5825
6300
  return this.property('radius', v);
6301
+ }
6302
+
6303
+ /**
6304
+ * 获取规范化的圆角值(四角独立)
6305
+ * @returns {object} { topLeft, topRight, bottomRight, bottomLeft }
6306
+ */
6307
+ getNormalizedRadius() {
6308
+ const r = this.radius;
6309
+ if(typeof r === 'number') {
6310
+ const v = Math.max(0, r);
6311
+ return { topLeft: v, topRight: v, bottomRight: v, bottomLeft: v };
6312
+ }
6313
+ if(typeof r === 'object' && r !== null) {
6314
+ return {
6315
+ topLeft: Math.max(0, Number(r.topLeft) || 0),
6316
+ topRight: Math.max(0, Number(r.topRight) || 0),
6317
+ bottomRight: Math.max(0, Number(r.bottomRight) || 0),
6318
+ bottomLeft: Math.max(0, Number(r.bottomLeft) || 0)
6319
+ };
6320
+ }
6321
+ return { topLeft: 0, topRight: 0, bottomRight: 0, bottomLeft: 0 };
6322
+ }
6323
+
6324
+ /**
6325
+ * 检查是否有圆角
6326
+ * @returns {boolean}
6327
+ */
6328
+ hasRadius() {
6329
+ const nr = this.getNormalizedRadius();
6330
+ return nr.topLeft > 0 || nr.topRight > 0 || nr.bottomRight > 0 || nr.bottomLeft > 0;
5826
6331
  }
5827
6332
 
5828
6333
  /**
@@ -5885,7 +6390,7 @@ class jmRect extends jmPath {
5885
6390
 
5886
6391
  /**
5887
6392
  * 初始化图形点
5888
- * 如果有边角弧度则类型圆绝计算其描点
6393
+ * 支持四角独立圆角,借助圆弧对象计算描点
5889
6394
  *
5890
6395
  * @method initPoints
5891
6396
  * @private
@@ -5902,32 +6407,67 @@ class jmRect extends jmPath {
5902
6407
  this.dottedLine = this.graph.createShape(jmLine, {style: this.style});
5903
6408
  }
5904
6409
 
5905
- //如果有边界弧度则借助圆弧对象计算描点
5906
- if(location.radius && location.radius < location.width/2 && location.radius < location.height/2) {
6410
+ const nr = this.getNormalizedRadius();
6411
+ const hasRadius = this.hasRadius();
6412
+
6413
+ // 如果有圆角(支持四角独立),借助圆弧对象计算描点
6414
+ if(hasRadius) {
5907
6415
  let q = Math.PI / 2;
5908
- let arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
5909
- arc.center = {x:location.left + location.radius,y:location.top+location.radius};
5910
- arc.startAngle = Math.PI;
5911
- arc.endAngle = Math.PI + q;
5912
- let ps1 = arc.initPoints();
5913
-
5914
- arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
5915
- arc.center = {x:p2.x - location.radius,y:p2.y + location.radius};
5916
- arc.startAngle = Math.PI + q;
5917
- arc.endAngle = Math.PI * 2;
5918
- let ps2 = arc.initPoints();
5919
-
5920
- arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
5921
- arc.center = {x:p3.x - location.radius,y:p3.y - location.radius};
5922
- arc.startAngle = 0;
5923
- arc.endAngle = q;
5924
- let ps3 = arc.initPoints();
5925
-
5926
- arc = this.graph.createShape(jmArc,{radius:location.radius,anticlockwise:false});
5927
- arc.center = {x:p4.x + location.radius,y:p4.y - location.radius};
5928
- arc.startAngle = q;
5929
- arc.endAngle = Math.PI;
5930
- let ps4 = arc.initPoints();
6416
+
6417
+ // 限制圆角不超过短边的一半
6418
+ const maxR = Math.min(location.width / 2, location.height / 2);
6419
+ const rtl = Math.min(nr.topLeft, maxR);
6420
+ const rtr = Math.min(nr.topRight, maxR);
6421
+ const rbr = Math.min(nr.bottomRight, maxR);
6422
+ const rbl = Math.min(nr.bottomLeft, maxR);
6423
+
6424
+ // 左上角圆弧
6425
+ if(rtl > 0) {
6426
+ let arc = this.graph.createShape(jmArc,{radius:rtl,anticlockwise:false});
6427
+ arc.center = {x:location.left + rtl, y:location.top + rtl};
6428
+ arc.startAngle = Math.PI;
6429
+ arc.endAngle = Math.PI + q;
6430
+ var ps1 = arc.initPoints();
6431
+ }
6432
+ else {
6433
+ var ps1 = [p1];
6434
+ }
6435
+
6436
+ // 右上角圆弧
6437
+ if(rtr > 0) {
6438
+ let arc = this.graph.createShape(jmArc,{radius:rtr,anticlockwise:false});
6439
+ arc.center = {x:p2.x - rtr, y:p2.y + rtr};
6440
+ arc.startAngle = Math.PI + q;
6441
+ arc.endAngle = Math.PI * 2;
6442
+ var ps2 = arc.initPoints();
6443
+ }
6444
+ else {
6445
+ var ps2 = [p2];
6446
+ }
6447
+
6448
+ // 右下角圆弧
6449
+ if(rbr > 0) {
6450
+ let arc = this.graph.createShape(jmArc,{radius:rbr,anticlockwise:false});
6451
+ arc.center = {x:p3.x - rbr, y:p3.y - rbr};
6452
+ arc.startAngle = 0;
6453
+ arc.endAngle = q;
6454
+ var ps3 = arc.initPoints();
6455
+ }
6456
+ else {
6457
+ var ps3 = [p3];
6458
+ }
6459
+
6460
+ // 左下角圆弧
6461
+ if(rbl > 0) {
6462
+ let arc = this.graph.createShape(jmArc,{radius:rbl,anticlockwise:false});
6463
+ arc.center = {x:p4.x + rbl, y:p4.y - rbl};
6464
+ arc.startAngle = q;
6465
+ arc.endAngle = Math.PI;
6466
+ var ps4 = arc.initPoints();
6467
+ }
6468
+ else {
6469
+ var ps4 = [p4];
6470
+ }
5931
6471
  this.points = ps1.concat(ps2,ps3,ps4);
5932
6472
  }
5933
6473
  else {
@@ -6266,7 +6806,7 @@ class jmLabel extends jmControl {
6266
6806
  this.style.textAlign = this.style.textAlign || 'left';
6267
6807
  //文字垂直对齐
6268
6808
  this.style.textBaseline = this.style.textBaseline || 'middle';
6269
- this.text = params.text || '';
6809
+ this.text = params.text || params.value || '';
6270
6810
 
6271
6811
  this.center = params.center || null;
6272
6812
  }
@@ -6555,67 +7095,6 @@ class jmLabel extends jmControl {
6555
7095
  }
6556
7096
  }
6557
7097
  }
6558
- //如果有指定边框,则画出边框
6559
- if(this.style.border) {
6560
- //如果指定了边框样式
6561
- if(this.style.border.style) {
6562
- this.context.save && this.context.save();
6563
- this.setStyle(this.style.border.style);
6564
- }
6565
- if(this.mode === '2d') {
6566
- this.context.moveTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
6567
- if(this.style.border.top) {
6568
- this.context.lineTo(this.points[1].x + bounds.left,this.points[1].y + bounds.top);
6569
- }
6570
-
6571
- if(this.style.border.right) {
6572
- this.context.moveTo(this.points[1].x + bounds.left,this.points[1].y + bounds.top);
6573
- this.context.lineTo(this.points[2].x + bounds.left,this.points[2].y + bounds.top);
6574
- }
6575
-
6576
- if(this.style.border.bottom) {
6577
- this.context.moveTo(this.points[2].x + bounds.left,this.points[2].y + bounds.top);
6578
- this.context.lineTo(this.points[3].x + bounds.left,this.points[3].y + bounds.top);
6579
- }
6580
-
6581
- if(this.style.border.left) {
6582
- this.context.moveTo(this.points[3].x + bounds.left,this.points[3].y + bounds.top);
6583
- this.context.lineTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
6584
- }
6585
- }
6586
- else {
6587
- const points = [];
6588
- if(this.style.border.top) {
6589
- points.push(this.points[0]);
6590
- points.push(this.points[1]);
6591
- }
6592
-
6593
- if(this.style.border.right) {
6594
- points.push({
6595
- ...this.points[1],
6596
- m: true
6597
- });
6598
- points.push(this.points[2]);
6599
- }
6600
-
6601
- if(this.style.border.bottom) {
6602
- points.push({
6603
- ...this.points[2],
6604
- m: true
6605
- });
6606
- points.push(this.points[3]);
6607
- }
6608
-
6609
- if(this.style.border.left) {
6610
- points.push({
6611
- ...this.points[3],
6612
- m: true
6613
- });
6614
- points.push(this.points[0]);
6615
- }
6616
- points.length && this.webglControl && this.webglControl.stroke(points);
6617
- }
6618
- }
6619
7098
  }
6620
7099
 
6621
7100
  endDraw() {
@@ -7564,145 +8043,6 @@ class jmKeyEvent {
7564
8043
  }
7565
8044
  }
7566
8045
 
7567
- /**
7568
- * 图层类
7569
- * 用于组织和管理图形对象,支持可见性和锁定控制
7570
- * 图层可以包含多个图形对象,并控制它们的显示和交互
7571
- *
7572
- * @class jmLayer
7573
- * @extends jmControl
7574
- * @param {object} params 图层参数
7575
- * @param {string} [params.name] 图层名称,默认为 'Layer_${timestamp}'
7576
- * @param {boolean} [params.visible=true] 图层是否可见
7577
- * @param {boolean} [params.locked=false] 图层是否锁定(锁定后不可交互)
7578
- * @param {jmGraph} [params.graph] 所属的画布对象
7579
- */
7580
- class jmLayer extends jmControl {
7581
-
7582
- constructor(params, t='jmLayer') {
7583
- params = params || {};
7584
- params.interactive = false; // 图层本身不响应交互事件
7585
- super(params, t);
7586
-
7587
- this.name = params.name || `Layer_${Date.now()}`;
7588
- this.visible = params.visible !== false;
7589
- this.locked = params.locked || false;
7590
- }
7591
-
7592
- /**
7593
- * 图层名称
7594
- * 图层的唯一标识符,用于查找和管理图层
7595
- *
7596
- * @property name
7597
- * @type {string}
7598
- */
7599
- get name() {
7600
- return this.property('name');
7601
- }
7602
- set name(v) {
7603
- if(!v || typeof v !== 'string') {
7604
- console.warn('jmLayer: name must be a non-empty string');
7605
- return;
7606
- }
7607
- return this.property('name', v);
7608
- }
7609
-
7610
- /**
7611
- * 图层是否可见
7612
- * 不可见的图层不会被渲染,但仍然存在于图层列表中
7613
- *
7614
- * @property visible
7615
- * @type {boolean}
7616
- */
7617
- get visible() {
7618
- return this.property('visible');
7619
- }
7620
- set visible(v) {
7621
- this.needUpdate = true;
7622
- return this.property('visible', v);
7623
- }
7624
-
7625
- /**
7626
- * 图层是否锁定
7627
- * 锁定的图层中的图形不可被选中或移动,但仍然可见
7628
- * 适用于背景图层或参考图层
7629
- *
7630
- * @property locked
7631
- * @type {boolean}
7632
- */
7633
- get locked() {
7634
- return this.property('locked');
7635
- }
7636
- set locked(v) {
7637
- return this.property('locked', v);
7638
- }
7639
-
7640
- /**
7641
- * 绘制图层
7642
- * 只有可见的图层才会被绘制
7643
- *
7644
- * @method paint
7645
- * @param {boolean} v 是否需要重绘
7646
- */
7647
- paint(v) {
7648
- if(this.visible !== false) {
7649
- super.paint(v);
7650
- }
7651
- }
7652
-
7653
- /**
7654
- * 检查点是否在图层内
7655
- * 锁定的图层不会响应鼠标事件
7656
- *
7657
- * @method checkPoint
7658
- * @param {object} p 坐标点 {x, y}
7659
- * @param {number} [pad] padding,额外的检测范围
7660
- * @return {boolean} 是否在图层内
7661
- */
7662
- checkPoint(p, pad) {
7663
- // 锁定的图层不响应交互
7664
- if(this.locked) return false;
7665
- return super.checkPoint(p, pad);
7666
- }
7667
-
7668
- /**
7669
- * 清空图层
7670
- * 移除图层中的所有图形对象
7671
- *
7672
- * @method clear
7673
- */
7674
- clear() {
7675
- this.children.clear();
7676
- this.needUpdate = true;
7677
- }
7678
-
7679
- /**
7680
- * 获取图层中的图形数量
7681
- *
7682
- * @method getShapeCount
7683
- * @return {number} 图形数量
7684
- */
7685
- getShapeCount() {
7686
- return this.children.length;
7687
- }
7688
-
7689
- /**
7690
- * 获取图层信息
7691
- * 返回图层的基本信息,用于调试和日志
7692
- *
7693
- * @method getInfo
7694
- * @return {object} 图层信息对象
7695
- */
7696
- getInfo() {
7697
- return {
7698
- name: this.name,
7699
- visible: this.visible,
7700
- locked: this.locked,
7701
- shapeCount: this.getShapeCount()
7702
- };
7703
- }
7704
- }
7705
-
7706
8046
  /**
7707
8047
  * jmGraph画图类库
7708
8048
  * 对canvas画图api进行二次封装,使其更易调用,省去很多重复的工作。
@@ -7714,7 +8054,7 @@ class jmLayer extends jmControl {
7714
8054
  * @param {object} option 参数:{width:宽,height:高}
7715
8055
  * @param {function} callback 初始化后的回调
7716
8056
  */
7717
- class jmGraph$1 extends jmControl {
8057
+ class jmGraph extends jmControl {
7718
8058
 
7719
8059
  constructor(canvas, option, callback) {
7720
8060
  if(typeof option == 'function') {
@@ -7932,7 +8272,7 @@ class jmGraph$1 extends jmControl {
7932
8272
  * @return {jmGraph} jmGraph实例对象
7933
8273
  */
7934
8274
  static create(...args) {
7935
- return new jmGraph$1(...args);
8275
+ return new jmGraph(...args);
7936
8276
  }
7937
8277
 
7938
8278
  /**
@@ -7981,9 +8321,7 @@ class jmGraph$1 extends jmControl {
7981
8321
  if(shape) {
7982
8322
  if(!args) args = {};
7983
8323
  args.graph = this;
7984
- let obj = new shape(args);
7985
- // 添加到活动图层
7986
- this.addShapeToLayer(obj);
8324
+ const obj = new shape(args);
7987
8325
  return obj;
7988
8326
  }
7989
8327
  }
@@ -8109,13 +8447,19 @@ class jmGraph$1 extends jmControl {
8109
8447
  this.context.clearRect(0, 0, w, h);
8110
8448
  }
8111
8449
  else if(this.mode === 'webgl' && this.context.clear) {
8112
- const color = this.style && this.style.fill? this.utils.hexToRGBA(this.style.fill): {
8113
- r: 0,
8114
- g: 0,
8115
- b: 0,
8116
- a: 0
8117
- };
8118
- this.context.clearColor(color.r, color.g, color.b, color.a); // 设置清空颜色缓冲时的颜色值
8450
+ // 缓存 clearColor 对象,避免每帧创建
8451
+ if(this.style && this.style.fill) {
8452
+ const color = this.utils.hexToRGBA(this.style.fill);
8453
+ this.__lastClearColor = color;
8454
+ this.context.clearColor(color.r, color.g, color.b, color.a);
8455
+ }
8456
+ else if(!this.__lastClearColor) {
8457
+ this.__lastClearColor = { r: 0, g: 0, b: 0, a: 0 };
8458
+ this.context.clearColor(0, 0, 0, 0);
8459
+ }
8460
+ else {
8461
+ this.context.clearColor(this.__lastClearColor.r, this.__lastClearColor.g, this.__lastClearColor.b, this.__lastClearColor.a);
8462
+ }
8119
8463
  this.context.clear(this.context.COLOR_BUFFER_BIT); // 清空颜色缓冲区,也就是清空画布
8120
8464
  }
8121
8465
  }
@@ -8306,232 +8650,6 @@ class jmGraph$1 extends jmControl {
8306
8650
  return this; // 支持链式调用
8307
8651
  }
8308
8652
 
8309
- /**
8310
- * 初始化图层系统
8311
- * 创建图层管理的基础结构,包括默认图层
8312
- *
8313
- * @method initLayers
8314
- * @private
8315
- */
8316
- initLayers() {
8317
- if(!this.layers) {
8318
- this.layers = new jmList();
8319
- // 创建默认图层
8320
- const defaultLayer = this.createLayer('Default Layer');
8321
- this.activeLayer = defaultLayer;
8322
- }
8323
- }
8324
-
8325
- /**
8326
- * 创建新图层
8327
- * 图层用于组织和管理图形对象,支持可见性和锁定控制
8328
- *
8329
- * @method createLayer
8330
- * @param {string} name 图层名称(必须唯一)
8331
- * @param {object} [options] 图层选项
8332
- * @param {boolean} [options.visible=true] 图层是否可见
8333
- * @param {boolean} [options.locked=false] 图层是否锁定(锁定后不可交互)
8334
- * @return {jmLayer} 新创建的图层
8335
- */
8336
- createLayer(name, options = {}) {
8337
- // 参数验证
8338
- if(!name || typeof name !== 'string') {
8339
- console.warn('jmGraph: createLayer - 图层名称必须是非空字符串');
8340
- name = `Layer_${Date.now()}`;
8341
- }
8342
-
8343
- this.initLayers();
8344
-
8345
- // 检查图层名称是否已存在
8346
- const existingLayer = this.getLayer(name);
8347
- if(existingLayer) {
8348
- console.warn(`jmGraph: 图层 "${name}" 已存在,将返回现有图层`);
8349
- return existingLayer;
8350
- }
8351
-
8352
- const layer = new jmLayer({
8353
- name: name,
8354
- graph: this,
8355
- ...options
8356
- });
8357
-
8358
- this.layers.add(layer);
8359
- this.children.add(layer);
8360
- this.needUpdate = true;
8361
- return layer;
8362
- }
8363
-
8364
- /**
8365
- * 获取所有图层
8366
- *
8367
- * @method getLayers
8368
- * @return {jmList} 图层列表
8369
- */
8370
- getLayers() {
8371
- this.initLayers();
8372
- return this.layers;
8373
- }
8374
-
8375
- /**
8376
- * 根据名称获取图层
8377
- *
8378
- * @method getLayer
8379
- * @param {string} name 图层名称
8380
- * @return {jmLayer|null} 图层对象,如果不存在则返回null
8381
- */
8382
- getLayer(name) {
8383
- this.initLayers();
8384
-
8385
- if(!name) return null;
8386
-
8387
- let result = null;
8388
- this.layers.each((i, layer) => {
8389
- if(layer.name === name) {
8390
- result = layer;
8391
- return false; // 找到后停止遍历
8392
- }
8393
- });
8394
- return result;
8395
- }
8396
-
8397
- /**
8398
- * 设置活动图层
8399
- * 新创建的图形将自动添加到活动图层
8400
- *
8401
- * @method setActiveLayer
8402
- * @param {string|jmLayer} layer 图层名称或图层对象
8403
- * @return {jmGraph} 返回当前实例,支持链式调用
8404
- */
8405
- setActiveLayer(layer) {
8406
- this.initLayers();
8407
-
8408
- // 支持传入图层名称或图层对象
8409
- if(typeof layer === 'string') {
8410
- layer = this.getLayer(layer);
8411
- }
8412
-
8413
- if(!layer || !(layer instanceof jmLayer)) {
8414
- console.warn('jmGraph: setActiveLayer - 无效的图层');
8415
- return this;
8416
- }
8417
-
8418
- this.activeLayer = layer;
8419
- return this;
8420
- }
8421
-
8422
- /**
8423
- * 获取当前活动图层
8424
- * 活动图层是新创建图形的默认容器
8425
- *
8426
- * @method getActiveLayer
8427
- * @return {jmLayer} 当前活动图层
8428
- */
8429
- getActiveLayer() {
8430
- this.initLayers();
8431
- return this.activeLayer;
8432
- }
8433
-
8434
- /**
8435
- * 移除图层
8436
- * 删除指定图层及其包含的所有图形
8437
- * 注意:默认图层不可删除
8438
- *
8439
- * @method removeLayer
8440
- * @param {string|jmLayer} layer 图层名称或图层对象
8441
- * @return {boolean} 是否成功删除
8442
- */
8443
- removeLayer(layer) {
8444
- this.initLayers();
8445
-
8446
- // 支持传入图层名称或图层对象
8447
- if(typeof layer === 'string') {
8448
- layer = this.getLayer(layer);
8449
- }
8450
-
8451
- if(!layer) {
8452
- console.warn('jmGraph: removeLayer - 图层不存在');
8453
- return false;
8454
- }
8455
-
8456
- // 禁止删除默认图层
8457
- if(layer.name === 'Default Layer') {
8458
- console.warn('jmGraph: 不能删除默认图层');
8459
- return false;
8460
- }
8461
-
8462
- // 如果删除的是当前活动图层,切换到默认图层
8463
- if(this.activeLayer === layer) {
8464
- this.activeLayer = this.getLayer('Default Layer');
8465
- }
8466
-
8467
- this.layers.remove(layer);
8468
- this.children.remove(layer);
8469
- this.needUpdate = true;
8470
- return true;
8471
- }
8472
-
8473
- /**
8474
- * 将形状添加到指定图层
8475
- * 如果未指定图层,则添加到当前活动图层
8476
- *
8477
- * @method addShapeToLayer
8478
- * @param {jmControl} shape 要添加的形状对象
8479
- * @param {string|jmLayer} [layer] 图层名称或图层对象,默认为当前活动图层
8480
- * @return {jmGraph} 返回当前实例,支持链式调用
8481
- */
8482
- addShapeToLayer(shape, layer) {
8483
- this.initLayers();
8484
-
8485
- // 参数验证
8486
- if(!shape) {
8487
- console.warn('jmGraph: addShapeToLayer - 无效的形状对象');
8488
- return this;
8489
- }
8490
-
8491
- // 确定目标图层
8492
- if(!layer) {
8493
- layer = this.activeLayer;
8494
- } else if(typeof layer === 'string') {
8495
- layer = this.getLayer(layer);
8496
- }
8497
-
8498
- if(!layer) {
8499
- console.warn('jmGraph: addShapeToLayer - 图层不存在');
8500
- return this;
8501
- }
8502
-
8503
- layer.children.add(shape);
8504
- this.needUpdate = true;
8505
- return this;
8506
- }
8507
-
8508
- /**
8509
- * 从图层中移除形状
8510
- *
8511
- * @method removeShapeFromLayer
8512
- * @param {jmControl} shape 要移除的形状对象
8513
- * @return {jmGraph} 返回当前实例,支持链式调用
8514
- */
8515
- removeShapeFromLayer(shape) {
8516
- if(!shape) {
8517
- console.warn('jmGraph: removeShapeFromLayer - 无效的形状对象');
8518
- return this;
8519
- }
8520
-
8521
- // 从所有图层中查找并移除
8522
- if(this.layers) {
8523
- this.layers.each((i, layer) => {
8524
- if(layer.children.contains(shape)) {
8525
- layer.children.remove(shape);
8526
- this.needUpdate = true;
8527
- return false; // 找到后停止遍历
8528
- }
8529
- });
8530
- }
8531
-
8532
- return this;
8533
- }
8534
-
8535
8653
  /**
8536
8654
  * 保存为base64图形数据
8537
8655
  *
@@ -8598,48 +8716,27 @@ class jmGraph$1 extends jmControl {
8598
8716
  }
8599
8717
 
8600
8718
  /**
8601
- * 转换为SVG字符串
8602
- * 遍历所有图层和形状,生成SVG标记
8603
- *
8719
+ * 遍历所有形状,生成SVG标记
8720
+ *
8604
8721
  * @method toSVG
8605
8722
  * @return {string} SVG字符串
8606
8723
  */
8607
8724
  toSVG() {
8608
8725
  // SVG头部,包含命名空间和画布尺寸
8609
8726
  let svg = `<svg width="${this.width}" height="${this.height}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${this.width} ${this.height}">`;
8610
-
8727
+
8611
8728
  // 添加背景色(如果有)
8612
8729
  if(this.style && this.style.fill) {
8613
8730
  svg += `<rect width="100%" height="100%" fill="${this.style.fill}"/>`;
8614
8731
  }
8615
-
8616
- // 遍历所有图层
8617
- if(this.layers) {
8618
- this.layers.each((i, layer) => {
8619
- if(layer.visible) {
8620
- // 添加图层组,方便管理
8621
- svg += `<g id="${layer.name}" opacity="${layer.opacity || 1}">`;
8622
-
8623
- // 遍历图层中的所有形状
8624
- layer.children.each((j, shape) => {
8625
- if(shape.toSVG) {
8626
- svg += shape.toSVG();
8627
- }
8628
- });
8629
-
8630
- svg += '</g>';
8631
- }
8632
- });
8633
- }
8634
- else {
8635
- // 遍历直接添加的形状(兼容没有图层系统的情况)
8636
- this.children.each((i, shape) => {
8637
- if(shape.toSVG) {
8638
- svg += shape.toSVG();
8639
- }
8640
- });
8641
- }
8642
-
8732
+
8733
+ // 遍历所有直接添加的形状
8734
+ this.children.each((i, shape) => {
8735
+ if(shape.toSVG) {
8736
+ svg += shape.toSVG();
8737
+ }
8738
+ });
8739
+
8643
8740
  svg += '</svg>';
8644
8741
  return svg;
8645
8742
  }
@@ -8689,7 +8786,7 @@ class jmGraph$1 extends jmControl {
8689
8786
  // 触发刷新事件
8690
8787
  self.emit('update', time);
8691
8788
 
8692
- self.__requestAnimationFrameFunHandler && self.cancelAnimationFrame(self.__requestAnimationFrameFunHandler);
8789
+ // 直接 requestAnimationFrame,无需先 cancel
8693
8790
  self.__requestAnimationFrameFunHandler = self.requestAnimationFrame(update);
8694
8791
  if(callback) callback();
8695
8792
  }
@@ -8724,22 +8821,11 @@ const shapes = {
8724
8821
  "star": jmStar
8725
8822
  };
8726
8823
 
8727
- class jmGraph extends jmGraph$1 {
8824
+ class jmGraphImpl extends jmGraph {
8728
8825
  constructor(canvas, option, callback) {
8729
-
8730
- const targetType = new.target;
8731
-
8732
8826
  // 合并shapes
8733
8827
  option = Object.assign({}, option);
8734
8828
  option.shapes = Object.assign(shapes, option.shapes||{});
8735
-
8736
- //不是用new实例化的话,返回一个promise
8737
- if(!targetType || !(targetType.prototype instanceof jmGraph$1)) {
8738
- return new Promise(function(resolve, reject){
8739
- var g = new jmGraph(canvas, option, callback);
8740
- if(resolve) resolve(g);
8741
- });
8742
- }
8743
8829
 
8744
8830
  if(typeof option == 'function') {
8745
8831
  callback = option;
@@ -8748,17 +8834,8 @@ class jmGraph extends jmGraph$1 {
8748
8834
 
8749
8835
  super(canvas, option, callback);
8750
8836
  }
8751
-
8752
- static create(...args) {
8753
- return createJmGraph(...args);
8754
- }
8755
8837
  }
8756
8838
 
8757
- //创建实例
8758
- const createJmGraph = (...args) => {
8759
- return new jmGraph(...args);
8760
- };
8761
-
8762
8839
  /**
8763
8840
  * 基础样式
8764
8841
  *
@@ -9960,13 +10037,16 @@ class jmAxis extends jmArrowLine {
9960
10037
  clear() {
9961
10038
  this._min = null;
9962
10039
  this._max = null;
10040
+ this._width = null; // 清除宽度缓存
9963
10041
  this.children.each((i, c) => {
9964
10042
  c.remove();
9965
10043
  }, true);
9966
10044
  // 清空栅格线
9967
- this.gridLines && this.gridLines.forEach(line => {
9968
- line.remove();
9969
- });
10045
+ if (this.gridLines && this.gridLines.length) {
10046
+ for (let i = 0; i < this.gridLines.length; i++) {
10047
+ this.gridLines[i].remove();
10048
+ }
10049
+ }
9970
10050
  this.labels && this.labels.forEach(label => {
9971
10051
  label.remove();
9972
10052
  });
@@ -10178,7 +10258,9 @@ var utils = {
10178
10258
  }
10179
10259
  };
10180
10260
 
10181
- const ANIMATION_DATA_THRESHOLD$7 = 100;
10261
+ // 公共常量提取到基类,避免每个子类重复定义
10262
+ const ANIMATION_DATA_THRESHOLD = 100;
10263
+ const DEFAULT_ANIMATION_COUNT = 10;
10182
10264
 
10183
10265
  /**
10184
10266
  * 图表系列基类
@@ -10297,14 +10379,20 @@ class jmSeries extends jmPath {
10297
10379
  */
10298
10380
  initDataPoint(...args) {
10299
10381
  let dataChanged = false;
10300
- if (this.enableAnimate && this.data && this.data.length < ANIMATION_DATA_THRESHOLD$7) {
10301
- this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, obj => {
10302
- if (obj instanceof jmControl) return obj;
10303
- });
10304
- this.dataPoints = this.createPoints(...args);
10305
- dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
10306
- return s.x === t.x && s.y === t.y;
10307
- });
10382
+ if (this.enableAnimate && this.data && this.data.length < ANIMATION_DATA_THRESHOLD) {
10383
+ // 仅在数据引用变化时才做深度比较
10384
+ if (this._lastData !== this.data) {
10385
+ this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, obj => {
10386
+ if (obj instanceof jmControl) return obj;
10387
+ });
10388
+ this.dataPoints = this.createPoints(...args);
10389
+ dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
10390
+ return s.x === t.x && s.y === t.y;
10391
+ });
10392
+ this._lastData = this.data;
10393
+ } else {
10394
+ this.dataPoints = this.createPoints(...args);
10395
+ }
10308
10396
  if (dataChanged) {
10309
10397
  this.___animateCounter = 0;
10310
10398
  }
@@ -10437,7 +10525,7 @@ class jmSeries extends jmPath {
10437
10525
  xValue: xv,
10438
10526
  xLabel: xv,
10439
10527
  points: [],
10440
- style: this.graph.utils.clone(this.style)
10528
+ style: this.style // 共享引用,避免深拷贝;子类如需修改应自行创建副本
10441
10529
  };
10442
10530
  p.x = xstep * i + this.xAxis.labelStart;
10443
10531
  for (let j = 0; j < fields.length; j++) {
@@ -10603,9 +10691,6 @@ class jmSeries extends jmPath {
10603
10691
  }
10604
10692
  }
10605
10693
 
10606
- const ANIMATION_DATA_THRESHOLD$6 = 100;
10607
- const DEFAULT_ANIMATION_COUNT$6 = 10;
10608
-
10609
10694
  /**
10610
10695
  * 柱图
10611
10696
  *
@@ -10635,16 +10720,18 @@ class jmBarSeries extends jmSeries {
10635
10720
  const len = points.length;
10636
10721
  if (!len) return;
10637
10722
  this.initWidth(len);
10638
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$6;
10723
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
10639
10724
  let aniIsEnd = true;
10640
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$6;
10725
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
10641
10726
  for (let i = 0; i < len; i++) {
10642
10727
  const point = points[i];
10643
10728
  if (typeof point.y === 'undefined' || point.y === null) {
10644
10729
  continue;
10645
10730
  }
10646
- point.style.fill = this.getColor(point);
10647
- const sp = this.addShape(this.graph.createPath(null, point.style));
10731
+ const style = Object.assign({}, point.style, {
10732
+ fill: this.getColor(point)
10733
+ });
10734
+ const sp = this.addShape(this.graph.createPath(null, style));
10648
10735
  const p1 = {
10649
10736
  x: point.x - this.barTotalWidth / 2 + this.barWidth * this.barIndex,
10650
10737
  y: this.baseY
@@ -10779,16 +10866,20 @@ class jmStackBarSeries extends jmBarSeries {
10779
10866
  let topStartY = this.baseY;
10780
10867
  let bottomStartY = this.baseY;
10781
10868
  for (let index = 0; index < point.points.length; index++) {
10782
- const style = this.graph.utils.clone(this.style);
10783
10869
  const p = point.points[index];
10784
- if (style.color && typeof style.color === 'function') {
10785
- style.fill = style.color.call(this, {
10870
+ let fillColor;
10871
+ if (this.style.color && typeof this.style.color === 'function') {
10872
+ fillColor = this.style.color.call(this, {
10786
10873
  index,
10787
10874
  point: p
10788
10875
  });
10789
10876
  } else {
10790
- style.fill = this.graph.getColor(index);
10877
+ fillColor = this.graph.getColor(index);
10791
10878
  }
10879
+ // 使用浅拷贝而非深拷贝
10880
+ const style = Object.assign({}, this.style, {
10881
+ fill: fillColor
10882
+ });
10792
10883
  const sp = this.addShape(this.graph.createPath(null, style));
10793
10884
  let startY = topStartY;
10794
10885
  if (p.yValue < this.baseYValue) startY = bottomStartY;
@@ -10897,7 +10988,7 @@ class jmPieSeries extends jmSeries {
10897
10988
  this.totalValue = 0;
10898
10989
  //计算最大值和最小值
10899
10990
  if (this.data) {
10900
- for (const i in this.data) {
10991
+ for (let i = 0; i < this.data.length; i++) {
10901
10992
  const s = this.data[i];
10902
10993
  const vy = s[this.field];
10903
10994
  if (vy) {
@@ -10994,15 +11085,20 @@ class jmPieSeries extends jmSeries {
10994
11085
  yLabel: yv,
10995
11086
  step: Math.abs(yv / this.totalValue),
10996
11087
  // 每个数值点比
10997
- style: this.graph.utils.clone(this.style),
11088
+ style: this.style,
11089
+ // 共享引用,仅在需要修改时才拷贝
10998
11090
  anticlockwise
10999
11091
  };
11000
11092
  points.push(p);
11001
11093
  //p.style.color = this.graph.getColor(index);
11002
11094
  if (p.style.color && typeof p.style.color === 'function') {
11095
+ p.style = this.graph.utils.clone(this.style);
11003
11096
  p.style.fill = p.style.color.call(this, p);
11004
11097
  } else {
11005
- p.style.fill = this.graph.getColor(index);
11098
+ // fill 需要独立,创建轻量样式覆盖
11099
+ p.style = Object.assign({}, this.style, {
11100
+ fill: this.graph.getColor(index)
11101
+ });
11006
11102
  }
11007
11103
  const start = startAni; // 上一个扇形的结束角度为当前的起始角度
11008
11104
  // 计算当前结束角度, 同时也是下一个的起始角度
@@ -11208,11 +11304,12 @@ class jmRadarSeries extends jmSeries {
11208
11304
  const rotateStep = Math.PI * 2 / yCount;
11209
11305
 
11210
11306
  // 清空除了一个默认外的所有Y轴
11211
- for (let index in this.graph.yAxises) {
11212
- const axis = this.graph.yAxises[index];
11307
+ const yAxisKeys = Object.keys(this.graph.yAxises || {});
11308
+ for (let k = 0; k < yAxisKeys.length; k++) {
11309
+ const axis = this.graph.yAxises[yAxisKeys[k]];
11213
11310
  if (!axis || axis === this.yAxis) continue;
11214
11311
  axis.remove();
11215
- delete this.graph.yAxises[index];
11312
+ delete this.graph.yAxises[yAxisKeys[k]];
11216
11313
  }
11217
11314
  for (let index = 0; index < yCount; index++) {
11218
11315
  if (!this.field[index]) continue;
@@ -11359,21 +11456,30 @@ class jmRadarSeries extends jmSeries {
11359
11456
  const points = [];
11360
11457
  for (var i = 0; i < this.data.length; i++) {
11361
11458
  const s = this.data[i];
11362
- const style = this.graph.utils.clone(this.style);
11363
- if (style.color && typeof style.color === 'function') {
11364
- style.stroke = style.color.call(this, {
11459
+ let strokeColor;
11460
+ if (this.style.color && typeof this.style.color === 'function') {
11461
+ strokeColor = this.style.color.call(this, {
11365
11462
  data: s,
11366
11463
  index: i
11367
11464
  });
11368
11465
  } else {
11369
- style.stroke = this.graph.getColor(i);
11466
+ strokeColor = this.graph.getColor(i);
11370
11467
  }
11371
- if (typeof style.fill === 'function') {
11372
- style.fill = style.fill.call(this, style);
11468
+ let fillColor;
11469
+ if (typeof this.style.fill === 'function') {
11470
+ fillColor = this.style.fill.call(this, {
11471
+ ...this.style,
11472
+ stroke: strokeColor
11473
+ });
11373
11474
  } else {
11374
- const color = this.graph.utils.hexToRGBA(style.stroke);
11375
- style.fill = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
11475
+ const color = this.graph.utils.hexToRGBA(strokeColor);
11476
+ fillColor = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
11376
11477
  }
11478
+ // 使用浅拷贝而非深拷贝
11479
+ const style = Object.assign({}, this.style, {
11480
+ stroke: strokeColor,
11481
+ fill: fillColor
11482
+ });
11377
11483
  const shapePoints = [];
11378
11484
  for (const axis of this.axises) {
11379
11485
  if (!axis || !axis.field) continue;
@@ -11467,9 +11573,6 @@ jmRadarSeries.prototype.createLegend = function (point) {
11467
11573
  });
11468
11574
  };
11469
11575
 
11470
- const ANIMATION_DATA_THRESHOLD$5 = 100;
11471
- const DEFAULT_ANIMATION_COUNT$5 = 10;
11472
-
11473
11576
  /**
11474
11577
  * 线图
11475
11578
  *
@@ -11501,9 +11604,9 @@ class jmLineSeries extends jmSeries {
11501
11604
  if (!len) return;
11502
11605
  this.style.stroke = this.style.color;
11503
11606
  this.style.item.stroke = this.style.color;
11504
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$5;
11607
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
11505
11608
  let shapePoints = [];
11506
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$5;
11609
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
11507
11610
  const aniStep = Math.floor(len / aniCount) || 1;
11508
11611
  for (let i = 0; i < len; i++) {
11509
11612
  const p = points[i];
@@ -11886,7 +11989,6 @@ class jmCandlestickSeries extends jmSeries {
11886
11989
  console.warn('K线图数据不完整,需要4个字段(开盘、收盘、最高、最低)');
11887
11990
  continue;
11888
11991
  }
11889
- const sp = this.addShape(this.graph.createPath([], p.style));
11890
11992
  const bl = {
11891
11993
  x: p.x - w,
11892
11994
  y: p.points[0].y
@@ -11905,14 +12007,18 @@ class jmCandlestickSeries extends jmSeries {
11905
12007
  };
11906
12008
  let tm = p.points[1];
11907
12009
  let bm = p.points[0];
11908
- p.style.stroke = p.style.fill = p.style.masculineColor || 'red';
12010
+
12011
+ // 使用浅拷贝避免污染共享引用
12012
+ const style = Object.assign({}, p.style);
12013
+ style.stroke = style.fill = style.masculineColor || 'red';
11909
12014
  if (p.points[0].yValue > p.points[1].yValue) {
11910
- p.style.stroke = p.style.fill = p.style.negativeColor || 'green';
12015
+ style.stroke = style.fill = style.negativeColor || 'green';
11911
12016
  bl.y = br.y = p.points[1].y;
11912
12017
  tl.y = tr.y = p.points[0].y;
11913
12018
  tm = p.points[0];
11914
12019
  bm = p.points[1];
11915
12020
  }
12021
+ const sp = this.addShape(this.graph.createPath([], style));
11916
12022
  sp.points.push(p.points[2], tm, tl, bl, bm, p.points[3], bm, br, tr, tm, p.points[2]);
11917
12023
  this.emit('onPointCreated', p);
11918
12024
  }
@@ -11937,9 +12043,6 @@ class jmCandlestickSeries extends jmSeries {
11937
12043
  }
11938
12044
  }
11939
12045
 
11940
- const ANIMATION_DATA_THRESHOLD$4 = 100;
11941
- const DEFAULT_ANIMATION_COUNT$4 = 10;
11942
-
11943
12046
  /**
11944
12047
  * 散点图
11945
12048
  *
@@ -11971,8 +12074,8 @@ class jmScatterSeries extends jmSeries {
11971
12074
  if (!len) return;
11972
12075
  this.style.stroke = this.style.color;
11973
12076
  this.style.item.stroke = this.style.color;
11974
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$4;
11975
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$4;
12077
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
12078
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
11976
12079
  const aniStep = Math.floor(len / aniCount) || 1;
11977
12080
  for (let i = 0; i < len; i++) {
11978
12081
  const p = points[i];
@@ -12045,9 +12148,6 @@ class jmScatterSeries extends jmSeries {
12045
12148
  }
12046
12149
  }
12047
12150
 
12048
- const ANIMATION_DATA_THRESHOLD$3 = 100;
12049
- const DEFAULT_ANIMATION_COUNT$3 = 10;
12050
-
12051
12151
  /**
12052
12152
  * 气泡图
12053
12153
  *
@@ -12079,8 +12179,8 @@ class jmBubbleSeries extends jmSeries {
12079
12179
  if (!len) return;
12080
12180
  this.style.stroke = this.style.color;
12081
12181
  this.style.item.stroke = this.style.color;
12082
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$3;
12083
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$3;
12182
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
12183
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
12084
12184
  const aniStep = Math.floor(len / aniCount) || 1;
12085
12185
  for (let i = 0; i < len; i++) {
12086
12186
  const p = points[i];
@@ -12159,9 +12259,6 @@ class jmBubbleSeries extends jmSeries {
12159
12259
  }
12160
12260
  }
12161
12261
 
12162
- const ANIMATION_DATA_THRESHOLD$2 = 100;
12163
- const DEFAULT_ANIMATION_COUNT$2 = 10;
12164
-
12165
12262
  /**
12166
12263
  * 热力图
12167
12264
  *
@@ -12204,8 +12301,8 @@ class jmHeatmapSeries extends jmSeries {
12204
12301
  }
12205
12302
  this.minValue = minValue;
12206
12303
  this.maxValue = maxValue;
12207
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$2;
12208
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$2;
12304
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
12305
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
12209
12306
  const aniStep = Math.floor(len / aniCount) || 1;
12210
12307
  for (let i = 0; i < len; i++) {
12211
12308
  const p = points[i];
@@ -12572,20 +12669,15 @@ class jmGaugeSeries extends jmSeries {
12572
12669
  }
12573
12670
  }
12574
12671
 
12575
- const ANIMATION_DATA_THRESHOLD$1 = 100;
12576
- const DEFAULT_ANIMATION_COUNT$1 = 10;
12577
-
12578
12672
  /**
12579
12673
  * 面积图
12674
+ * 继承自折线图,默认启用区域填充
12580
12675
  *
12581
12676
  * @class jmAreaSeries
12582
12677
  * @module jmChart
12583
- * @param {jmChart} chart 当前图表
12584
- * @param {array} mappings 图形字段映射
12585
- * @param {style} style 样式
12678
+ * @extends jmLineSeries
12586
12679
  */
12587
-
12588
- class jmAreaSeries extends jmSeries {
12680
+ class jmAreaSeries extends jmLineSeries {
12589
12681
  constructor(options) {
12590
12682
  options.style = options.style || options.graph.style.area;
12591
12683
  super(options);
@@ -12593,193 +12685,13 @@ class jmAreaSeries extends jmSeries {
12593
12685
  this.style.area = true;
12594
12686
  }
12595
12687
 
12596
- /**
12597
- * 初始化面积图
12598
- *
12599
- * @method init
12600
- * @for jmAreaSeries
12601
- */
12602
- init() {
12603
- const {
12604
- points,
12605
- dataChanged
12606
- } = this.initDataPoint();
12607
- const len = points.length;
12608
- if (!len) return;
12609
- this.style.stroke = this.style.color;
12610
- this.style.item.stroke = this.style.color;
12611
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$1;
12612
- let shapePoints = [];
12613
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$1;
12614
- const aniStep = Math.floor(len / aniCount) || 1;
12615
- for (let i = 0; i < len; i++) {
12616
- const p = points[i];
12617
- if (typeof p.y === 'undefined' || p.y === null) {
12618
- continue;
12619
- }
12620
- if (isRunningAni && i > this.___animateCounter) {
12621
- break;
12622
- }
12623
- if (this.style.showItem) {
12624
- this.createPointItem(p);
12625
- }
12626
- if (this.style.curve) {
12627
- shapePoints = this.createCurePoints(shapePoints, p);
12628
- } else if (this.style.lineType === 'dotted') {
12629
- shapePoints = this.createDotLine(shapePoints, p);
12630
- }
12631
- shapePoints.push(p);
12632
- this.createItemLabel(p);
12633
- this.emit('onPointCreated', p);
12634
- }
12635
- if (this.___animateCounter >= len - 1) {
12636
- this.___animateCounter = 0;
12637
- } else if (isRunningAni) {
12638
- this.___animateCounter += aniStep;
12639
- this.graph.utils.requestAnimationFrame(() => {
12640
- this.needUpdate = true;
12641
- });
12642
- }
12643
- this.points = shapePoints;
12644
- this.createArea(shapePoints);
12645
- }
12646
-
12647
- /**
12648
- * 生成点的小圆圈
12649
- *
12650
- * @method createPointItem
12651
- * @for jmAreaSeries
12652
- * @param {object} p 数据点
12653
- */
12654
- createPointItem(p) {
12655
- const pointShape = this.graph.createShape('circle', {
12656
- style: this.style.item,
12657
- center: p,
12658
- radius: this.style.radius || 3
12659
- });
12660
- pointShape.zIndex = (pointShape.style.zIndex || 1) + 1;
12661
- return this.addShape(pointShape);
12662
- }
12663
-
12664
- /**
12665
- * 创建曲线点
12666
- *
12667
- * @method createCurePoints
12668
- * @for jmAreaSeries
12669
- * @param {array} shapePoints 形状点数组
12670
- * @param {object} p 当前点
12671
- * @return {array} 新的形状点数组
12672
- */
12673
- createCurePoints(shapePoints, p) {
12674
- const startPoint = shapePoints[shapePoints.length - 1];
12675
- if (!startPoint || !p) return shapePoints;
12676
- 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) {
12677
- return shapePoints;
12678
- }
12679
- const p1 = {
12680
- x: startPoint.x + (p.x - startPoint.x) / 5,
12681
- y: startPoint.y
12682
- };
12683
- const p2 = {
12684
- x: startPoint.x + (p.x - startPoint.x) / 2,
12685
- y: p.y - (p.y - startPoint.y) / 2
12686
- };
12687
- const p3 = {
12688
- x: p.x - (p.x - startPoint.x) / 5,
12689
- y: p.y
12690
- };
12691
- this.__bezier = this.__bezier || this.graph.createShape('bezier');
12692
- this.__bezier.cpoints = [startPoint, p1, p2, p3, p];
12693
- const bzpoints = this.__bezier.initPoints();
12694
- if (bzpoints && bzpoints.length) {
12695
- shapePoints = shapePoints.concat(bzpoints);
12696
- }
12697
- return shapePoints;
12698
- }
12699
-
12700
- /**
12701
- * 创建虚线点
12702
- *
12703
- * @method createDotLine
12704
- * @for jmAreaSeries
12705
- * @param {array} shapePoints 形状点数组
12706
- * @param {object} p 当前点
12707
- * @return {array} 新的形状点数组
12708
- */
12709
- createDotLine(shapePoints, p) {
12710
- const startPoint = shapePoints[shapePoints.length - 1];
12711
- if (!startPoint || !p) return shapePoints;
12712
- 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) {
12713
- return shapePoints;
12714
- }
12715
- this.__line = this.__line || this.graph.createShape('line', {
12716
- style: this.style
12717
- });
12718
- this.__line.start = startPoint;
12719
- this.__line.end = p;
12720
- const dots = this.__line.initPoints();
12721
- if (dots && dots.length) {
12722
- shapePoints = shapePoints.concat(dots);
12723
- }
12724
- return shapePoints;
12725
- }
12726
-
12727
- /**
12728
- * 生成面积效果
12729
- *
12730
- * @method createArea
12731
- * @for jmAreaSeries
12732
- * @param {array} points 数据点数组
12733
- * @param {boolean} needClosePoint 是否需要闭合点
12734
- */
12735
- createArea(points, needClosePoint = true) {
12736
- // 有指定绘制区域效果才展示
12737
- if (!this.style.area || points.length < 2) return;
12738
- const start = points[0];
12739
- const end = points[points.length - 1];
12740
- const style = this.graph.utils.clone(this.style.area, {}, true);
12741
- // 连框颜色如果没指定,就透明
12742
- style.stroke = style.stroke || 'transparent';
12743
- if (!style.fill) {
12744
- const color = this.graph.utils.hexToRGBA(this.style.stroke);
12745
- style.fill = `linear-gradient(50% 0 50% 100%,
12746
- rgba(${color.r},${color.g},${color.b}, 0) 1,
12747
- rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
12748
- rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
12749
- } else if (typeof style.fill === 'function') {
12750
- style.fill = style.fill.call(this, style);
12751
- }
12752
- const area = this.graph.createShape('path', {
12753
- points: this.graph.utils.clone(points, true),
12754
- style,
12755
- width: this.graph.chartArea.width,
12756
- height: this.graph.chartArea.height
12757
- });
12758
-
12759
- // 在点集合前后加上落地到X轴的点就可以组成一个封闭的图形area
12760
- if (needClosePoint) {
12761
- area.points.unshift({
12762
- x: start.x,
12763
- y: this.baseY
12764
- });
12765
- area.points.push({
12766
- x: end.x,
12767
- y: this.baseY
12768
- });
12769
- }
12770
- this.addShape(area);
12771
- }
12772
-
12773
12688
  /**
12774
12689
  * 生成图例
12775
- *
12776
- * @method createLegend
12777
- * @for jmAreaSeries
12778
12690
  */
12779
12691
  createLegend() {
12780
- // 生成图例前的图标
12781
12692
  var style = this.graph.utils.clone(this.style);
12782
12693
  style.stroke = style.color;
12694
+ style.fill = style.color;
12783
12695
  var shape = this.graph.createShape('path', {
12784
12696
  style: style
12785
12697
  });
@@ -12801,8 +12713,7 @@ class jmAreaSeries extends jmSeries {
12801
12713
  y: 0
12802
12714
  };
12803
12715
  this.__bezier = this.__bezier || this.graph.createShape('bezier');
12804
- this.__bezier.cpoints = [p1, p2, p3, p4]; //设置控制点
12805
-
12716
+ this.__bezier.cpoints = [p1, p2, p3, p4];
12806
12717
  shape.points = this.__bezier.initPoints();
12807
12718
  } else {
12808
12719
  shape.points = [{
@@ -12817,8 +12728,6 @@ class jmAreaSeries extends jmSeries {
12817
12728
  }
12818
12729
  }
12819
12730
 
12820
- const ANIMATION_DATA_THRESHOLD = 100;
12821
- const DEFAULT_ANIMATION_COUNT = 10;
12822
12731
  class jmWaterfallSeries extends jmSeries {
12823
12732
  constructor(options) {
12824
12733
  options.style = options.style || options.graph.style.waterfall;
@@ -14251,12 +14160,11 @@ class jmMarkLineManager {
14251
14160
  if (chart.style.markLine.longtap) {
14252
14161
  longtap = 1;
14253
14162
  longtapHandler && graph.utils.cancelAnimationFrame(longtapHandler);
14254
- let tapStartTime = Date.now();
14163
+ const tapStartTime = Date.now();
14255
14164
  const reqFun = () => {
14256
- const elapsed = Date.now() - tapStartTime;
14257
14165
  if (longtap === 1 || longtap === 2) {
14258
14166
  // 如果还未过一定时间,则继续等待
14259
- if (elapsed < 500) {
14167
+ if (Date.now() - tapStartTime < 500) {
14260
14168
  longtapHandler = graph.utils.requestAnimationFrame(reqFun);
14261
14169
  return;
14262
14170
  }
@@ -14267,9 +14175,6 @@ class jmMarkLineManager {
14267
14175
  };
14268
14176
  // 如果一定时间后还没有取消,则表示长按了
14269
14177
  longtapHandler = graph.utils.requestAnimationFrame(reqFun);
14270
-
14271
- //args.event.stopPropagation();
14272
- //args.event.preventDefault();// 阻止默认行为
14273
14178
  } else {
14274
14179
  this.startMove(args);
14275
14180
  }
@@ -14376,7 +14281,7 @@ class jmMarkLineManager {
14376
14281
  * @module jmChart
14377
14282
  * @param {element} container 图表容器
14378
14283
  */
14379
- class jmChart extends jmGraph {
14284
+ class jmChart extends jmGraphImpl {
14380
14285
  constructor(container, options) {
14381
14286
  options = options || {};
14382
14287
  const enableAnimate = !!options.enableAnimate;
@@ -14470,25 +14375,27 @@ class jmChart extends jmGraph {
14470
14375
 
14471
14376
  // 绑定点击事件
14472
14377
  this.on('click', args => {
14473
- if (this.option.onClick) {
14474
- // 找到点击位置的数据点
14475
- let closestPoint = null;
14476
- let minDistance = Infinity;
14477
- this.series.each((i, serie) => {
14478
- if (serie.dataPoints) {
14479
- serie.dataPoints.forEach(point => {
14480
- const distance = Math.sqrt(Math.pow(args.position.x - point.x, 2) + Math.pow(args.position.y - point.y, 2));
14481
- if (distance < minDistance && distance < 20) {
14482
- // 20px 范围内的点
14483
- minDistance = distance;
14484
- closestPoint = point;
14485
- }
14486
- });
14378
+ if (!this.option.onClick) return;
14379
+ // 找到点击位置的数据点
14380
+ let closestPoint = null;
14381
+ let minDistance = 20; // 20px 范围,同时作为阈值,无需再比较小于阈值
14382
+
14383
+ this.series.each((i, serie) => {
14384
+ if (!serie.dataPoints) return;
14385
+ const points = serie.dataPoints;
14386
+ for (let j = 0, len = points.length; j < len; j++) {
14387
+ const point = points[j];
14388
+ const dx = args.position.x - point.x;
14389
+ const dy = args.position.y - point.y;
14390
+ const distance = dx * dx + dy * dy; // 避免开方,直接比较平方
14391
+ if (distance < minDistance * minDistance) {
14392
+ minDistance = distance;
14393
+ closestPoint = point;
14487
14394
  }
14488
- });
14489
- if (closestPoint) {
14490
- this.option.onClick(closestPoint);
14491
14395
  }
14396
+ });
14397
+ if (closestPoint) {
14398
+ this.option.onClick(closestPoint);
14492
14399
  }
14493
14400
  });
14494
14401
  }
@@ -14518,7 +14425,7 @@ class jmChart extends jmGraph {
14518
14425
  options = this.utils.clone(options, {
14519
14426
  autoRefresh: true
14520
14427
  }, true);
14521
- this.touchGraph = new jmGraph(cn, options);
14428
+ this.touchGraph = new jmGraphImpl(cn, options);
14522
14429
  this.touchGraph.chartGraph = this;
14523
14430
  this.on('propertyChange', (name, args) => {
14524
14431
  if (['width', 'height'].includes(name)) {
@@ -14558,8 +14465,9 @@ class jmChart extends jmGraph {
14558
14465
  delete this.xAxis;
14559
14466
  }
14560
14467
  if (this.yAxises) {
14561
- for (let i in this.yAxises) {
14562
- this.yAxises[i].remove();
14468
+ const keys = Object.keys(this.yAxises);
14469
+ for (let k = 0; k < keys.length; k++) {
14470
+ this.yAxises[keys[k]].remove();
14563
14471
  }
14564
14472
  delete this.yAxises;
14565
14473
  }
@@ -14571,18 +14479,9 @@ class jmChart extends jmGraph {
14571
14479
  * @param {int} index 颜色索引
14572
14480
  */
14573
14481
  getColor(index) {
14574
- const cacheKey = `color_${index}`;
14575
- if (this._cache.has(cacheKey)) {
14576
- return this._cache.get(cacheKey);
14577
- }
14578
- let color;
14579
- if (index >= this.style.chartColors.length) {
14580
- color = this.style.chartColors[index % this.style.chartColors.length];
14581
- } else {
14582
- color = this.style.chartColors[index];
14583
- }
14584
- this._cache.set(cacheKey, color);
14585
- return color;
14482
+ const colors = this.style.chartColors;
14483
+ const colorIndex = index % colors.length;
14484
+ return colors[colorIndex];
14586
14485
  }
14587
14486
 
14588
14487
  /**
@@ -14600,40 +14499,43 @@ class jmChart extends jmGraph {
14600
14499
  this.xAxis.clear();
14601
14500
  }
14602
14501
  if (this.yAxises) {
14603
- for (let i in this.yAxises) {
14604
- this.yAxises[i].clear();
14502
+ const keys = Object.keys(this.yAxises);
14503
+ for (let k = 0; k < keys.length; k++) {
14504
+ this.yAxises[keys[k]].clear();
14605
14505
  }
14606
14506
  }
14607
14507
  this.barSeriesCount = 0;
14508
+ // 合并两次 series 遍历为一次,减少遍历开销
14509
+ const seriesInited = [];
14608
14510
  this.series.each((i, serie) => {
14609
14511
  if (!serie.style.color) {
14610
14512
  serie.style.color = serie.graph.getColor(i);
14611
14513
  }
14612
- if (serie.graph.style.layout != 'inside') {
14613
- if (serie instanceof jmBarSeries) {
14514
+ if (serie instanceof jmBarSeries) {
14515
+ if (serie.graph.style.layout != 'inside') {
14614
14516
  serie.graph.style.layout = 'inside';
14615
14517
  }
14616
- }
14617
- if (serie instanceof jmBarSeries) {
14618
14518
  serie.barIndex = serie.graph.barSeriesCount;
14619
14519
  serie.graph.barSeriesCount++;
14620
14520
  }
14621
14521
  serie.reset();
14522
+ seriesInited.push(serie);
14622
14523
  });
14623
14524
  if (this.legend) {
14624
14525
  this.legend.reset();
14625
14526
  }
14626
14527
  if (this.yAxises) {
14627
- for (let i in this.yAxises) {
14628
- this.yAxises[i].reset();
14528
+ const keys = Object.keys(this.yAxises);
14529
+ for (let k = 0; k < keys.length; k++) {
14530
+ this.yAxises[keys[k]].reset();
14629
14531
  }
14630
14532
  }
14631
14533
  if (this.xAxis) {
14632
14534
  this.xAxis.reset();
14633
14535
  }
14634
- this.series.each((i, serie) => {
14635
- serie.init && serie.init();
14636
- });
14536
+ for (let i = 0; i < seriesInited.length; i++) {
14537
+ seriesInited[i].init && seriesInited[i].init();
14538
+ }
14637
14539
  }
14638
14540
 
14639
14541
  /**
@@ -14811,14 +14713,35 @@ var vchart = {
14811
14713
  this.initChart();
14812
14714
  },
14813
14715
  updated() {
14814
- this.initChart();
14716
+ this.refresh();
14815
14717
  },
14816
14718
  destroyed() {
14817
14719
  this.chartInstance && this.chartInstance.destroy();
14818
14720
  },
14819
14721
  watch: {
14820
- chartData: function (newData, oldData) {
14821
- this.refresh();
14722
+ chartData: {
14723
+ handler: function (newData, oldData) {
14724
+ this.refresh();
14725
+ },
14726
+ deep: false // 仅监听引用变化,由使用者显式替换数组时触发
14727
+ },
14728
+ chartOptions: {
14729
+ handler: function (newOptions, oldOptions) {
14730
+ // 配置变化时需要重建图表
14731
+ if (this.chartInstance) {
14732
+ this.chartInstance.destroy();
14733
+ this.chartInstance = null;
14734
+ }
14735
+ this.option = Object.assign({
14736
+ enableAnimate: false,
14737
+ legendPosition: 'top',
14738
+ legendVisible: true,
14739
+ width: this.width,
14740
+ height: this.height
14741
+ }, newOptions);
14742
+ this.$nextTick(() => this.initChart());
14743
+ },
14744
+ deep: false
14822
14745
  },
14823
14746
  width: function (newWidth, oldWidth) {
14824
14747
  if (!this.chartInstance) return;