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