jmchart 1.2.13 → 1.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
  /**
@@ -1106,7 +1115,8 @@ class jmGradient {
1106
1115
  let sy2 = Number(y2) + bounds.top;
1107
1116
  if(this.type === 'linear') {
1108
1117
  if(control.mode === 'webgl' && control.webglControl) {
1109
- gradient = control.webglControl.createLinearGradient(x1, y1, x2, y2, bounds);
1118
+ // WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
1119
+ gradient = control.webglControl.createLinearGradient(sx1, sy1, sx2, sy2, bounds);
1110
1120
  gradient.key = this.toString();
1111
1121
  }
1112
1122
  else {
@@ -1125,7 +1135,8 @@ class jmGradient {
1125
1135
  r2 = d * r2;
1126
1136
  }
1127
1137
  if(control.mode === 'webgl' && control.webglControl) {
1128
- gradient = control.webglControl.createRadialGradient(x1, y1, r1, x2, y2, r2, bounds);
1138
+ // WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
1139
+ gradient = control.webglControl.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2, bounds);
1129
1140
  gradient.key = this.toString();
1130
1141
  }
1131
1142
  //offsetLine = Math.abs(r2 - r1);//二圆半径差
@@ -1304,6 +1315,153 @@ class jmShadow {
1304
1315
  }
1305
1316
  }
1306
1317
 
1318
+ /**
1319
+ * CSS滤镜效果类
1320
+ * 支持的滤镜: blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity
1321
+ *
1322
+ * @class jmFilter
1323
+ * @param {string|object} opt 滤镜参数
1324
+ * 字符串格式: "blur(2px) grayscale(50%) brightness(1.2)"
1325
+ * 对象格式: { blur: 2, grayscale: 0.5, brightness: 1.2 }
1326
+ */
1327
+ class jmFilter {
1328
+ constructor(opt) {
1329
+ this.filters = [];
1330
+
1331
+ if(typeof opt === 'string') {
1332
+ this.fromString(opt);
1333
+ }
1334
+ else if(opt && typeof opt === 'object') {
1335
+ for(let k in opt) {
1336
+ if(k === 'constructor' || k === 'filters') continue;
1337
+ this.addFilter(k, opt[k]);
1338
+ }
1339
+ }
1340
+ }
1341
+
1342
+ /**
1343
+ * 添加单个滤镜
1344
+ * @param {string} name 滤镜名称 (blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity)
1345
+ * @param {number|string} value 滤镜值
1346
+ */
1347
+ addFilter(name, value) {
1348
+ name = name.toLowerCase().trim();
1349
+ if(typeof value === 'string') {
1350
+ value = parseFloat(value);
1351
+ }
1352
+ if(isNaN(value)) return;
1353
+
1354
+ // 规范化滤镜名称
1355
+ const normalized = {
1356
+ 'blur': 'blur',
1357
+ 'grayscale': 'grayscale',
1358
+ 'greyscale': 'grayscale',
1359
+ 'sepia': 'sepia',
1360
+ 'brightness': 'brightness',
1361
+ 'contrast': 'contrast',
1362
+ 'saturate': 'saturate',
1363
+ 'hue-rotate': 'hueRotate',
1364
+ 'hueRotate': 'hueRotate',
1365
+ 'invert': 'invert',
1366
+ 'opacity': 'opacity'
1367
+ }[name];
1368
+
1369
+ if(!normalized) return;
1370
+
1371
+ // 检查是否已有同名滤镜,有则更新
1372
+ const existing = this.filters.find(f => f.name === normalized);
1373
+ if(existing) {
1374
+ existing.value = value;
1375
+ }
1376
+ else {
1377
+ this.filters.push({ name: normalized, value: value });
1378
+ }
1379
+ }
1380
+
1381
+ /**
1382
+ * 从字符串格式解析滤镜
1383
+ * 格式: "blur(2px) grayscale(50%) brightness(1.2)"
1384
+ * @param {string} s 滤镜字符串
1385
+ */
1386
+ fromString(s) {
1387
+ if(!s || typeof s !== 'string') return;
1388
+ // 匹配 filterName(value) 模式
1389
+ const regex = /([a-zA-Z-]+)\s*\(\s*([^)]+)\s*\)/g;
1390
+ let match;
1391
+ while((match = regex.exec(s)) !== null) {
1392
+ const name = match[1];
1393
+ const valueStr = match[2].replace(/[a-z%]+$/i, '').trim();
1394
+ const value = parseFloat(valueStr);
1395
+ if(!isNaN(value)) {
1396
+ this.addFilter(name, value);
1397
+ }
1398
+ }
1399
+ }
1400
+
1401
+ /**
1402
+ * 转换为CSS filter字符串格式
1403
+ * @returns {string}
1404
+ */
1405
+ toString() {
1406
+ return this.filters.map(f => {
1407
+ switch(f.name) {
1408
+ case 'blur':
1409
+ return `blur(${f.value}px)`;
1410
+ case 'hueRotate':
1411
+ return `hue-rotate(${f.value}deg)`;
1412
+ default:
1413
+ return `${f.name}(${f.value})`;
1414
+ }
1415
+ }).join(' ');
1416
+ }
1417
+
1418
+ /**
1419
+ * 转换为Canvas context.filter可用的字符串
1420
+ * @returns {string}
1421
+ */
1422
+ toCanvasFilter() {
1423
+ if(this.filters.length === 0) return 'none';
1424
+ return this.toString();
1425
+ }
1426
+
1427
+ /**
1428
+ * 检查是否有指定名称的滤镜
1429
+ * @param {string} name 滤镜名称
1430
+ * @returns {boolean}
1431
+ */
1432
+ has(name) {
1433
+ return this.filters.some(f => f.name === name);
1434
+ }
1435
+
1436
+ /**
1437
+ * 获取指定滤镜的值
1438
+ * @param {string} name 滤镜名称
1439
+ * @returns {number|undefined}
1440
+ */
1441
+ get(name) {
1442
+ const f = this.filters.find(f => f.name === name);
1443
+ return f ? f.value : undefined;
1444
+ }
1445
+
1446
+ /**
1447
+ * 移除指定滤镜
1448
+ * @param {string} name 滤镜名称
1449
+ */
1450
+ remove(name) {
1451
+ const index = this.filters.findIndex(f => f.name === name);
1452
+ if(index > -1) {
1453
+ this.filters.splice(index, 1);
1454
+ }
1455
+ }
1456
+
1457
+ /**
1458
+ * 清空所有滤镜
1459
+ */
1460
+ clear() {
1461
+ this.filters = [];
1462
+ }
1463
+ }
1464
+
1307
1465
  let control_id_counter = 0;
1308
1466
 
1309
1467
  class jmObject {
@@ -2125,10 +2283,14 @@ earcut.flatten = function (data) {
2125
2283
  return result;
2126
2284
  };
2127
2285
 
2128
- // 渐变
2286
+ const MAX_STOPS = 16;
2287
+
2288
+ /**
2289
+ * WebGL 渐变对象
2290
+ * 支持 GLSL 着色器直接计算渐变色,无需 textureCanvas
2291
+ */
2129
2292
  class WebglGradient {
2130
- // type:[linear= 线性渐变,radial=放射性渐变]
2131
- constructor(type='linear', params={}) {
2293
+ constructor(type = 'linear', params = {}) {
2132
2294
  this.type = type || 'linear';
2133
2295
 
2134
2296
  this.x1 = params.x1 || 0;
@@ -2148,148 +2310,114 @@ class WebglGradient {
2148
2310
  this.control = params.control;
2149
2311
 
2150
2312
  this.stops = [];
2151
- this.init();
2152
- }
2153
-
2154
- init() {
2155
- const dx = this.x2 - this.x1;
2156
- const dy = this.y2 - this.y1;
2157
-
2158
- if(this.type === 'radial') {
2159
- this.length = this.r2 - this.r1;
2160
- }
2161
- else if(dx === 0 && dy === 0) {
2162
- this.length = 0;
2163
- }
2164
- else {
2165
- // 渐变中心的距离
2166
- this.length = Math.sqrt(Math.pow(dx, 2), Math.pow(dy, 2));
2167
- this.sin = dy / this.length;
2168
- this.cos = dx / this.length;
2169
- }
2313
+ this._sortedStops = null;
2314
+ this._paramsHash = null;
2170
2315
  }
2171
2316
 
2172
- // 渐变颜色
2317
+ /**
2318
+ * 添加颜色断点
2319
+ */
2173
2320
  addColorStop(offset, color) {
2174
2321
  this.stops.push({
2175
- offset,
2322
+ offset: Math.max(0, Math.min(1, offset)),
2176
2323
  color
2177
2324
  });
2325
+ this._sortedStops = null;
2326
+ this._paramsHash = null;
2178
2327
  }
2179
2328
 
2180
- // 转为渐变为纹理
2181
- toImageData(control, bounds, points=null) {
2182
- //const key = this.key || this.toString();
2183
- //if(WebglGradientTextureCache[key]) return WebglGradientTextureCache[key];
2184
- if(!control.textureContext) {
2185
- return null;
2186
- }
2187
- let gradient = null;
2188
- if(this.type === 'linear') {
2189
- gradient = control.textureContext.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
2190
- }
2191
- else {
2192
- gradient = control.textureContext.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
2193
- }
2194
- this.stops.forEach(function(s, i) {
2195
- const c = control.graph.utils.toColor(s.color);
2196
- gradient && gradient.addColorStop(s.offset, c);
2197
- });
2198
-
2199
- const data = control.toFillTexture(gradient, bounds, points);
2200
-
2201
- //WebglGradientTextureCache[key] = data;
2202
-
2203
- return data;
2204
- }
2205
-
2206
- // 根据绘制图形的坐标计算出对应点的颜色
2207
- /*
2208
- toPointColors(points) {
2209
- const stops = this.getStops();
2210
- const colors = [];
2211
- for(let i=0; i<points.length; i+=2) {
2212
- const p = {
2213
- x: points[i],
2214
- y: points[i+1]
2215
- }
2216
- if(this.type === 'radial') {
2217
- const dx = p.x - this.x1;
2218
- const dy = p.y - this.y1;
2219
- const len = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
2220
- const rang = this.getStopRange(len, stops);
2221
- if(!rang.start && rang.end) {
2222
- colors.push(rang.end.color);
2223
- }
2224
- else if(!rang.end && rang.start) {
2225
- colors.push(rang.start.color);
2329
+ /**
2330
+ * 获取排序后的 stops(带解析后的颜色)
2331
+ */
2332
+ _getSortedStops() {
2333
+ if (this._sortedStops) return this._sortedStops;
2334
+
2335
+ const utils = this.control && this.control.graph && this.control.graph.utils;
2336
+ this._sortedStops = this.stops
2337
+ .map(s => {
2338
+ let c = s.color;
2339
+ if (utils && typeof c === 'string') {
2340
+ c = utils.hexToRGBA(c);
2226
2341
  }
2227
- else {
2228
- const rangLength = rang.end.length - rang.start.length;
2229
- const offlen = len - rang.start.length;
2230
- const per = offlen / rangLength;
2231
- const color = {
2232
- r: rang.start.color.r + (rang.end.color.r - rang.start.color.r) * per,
2233
- g: rang.start.color.g + (rang.end.color.g - rang.start.color.g) * per,
2234
- b: rang.start.color.b + (rang.end.color.b - rang.start.color.b) * per,
2235
- a: rang.start.color.a + (rang.end.color.a - rang.start.color.a) * per,
2342
+ if (typeof c === 'object' && c !== null) {
2343
+ // hexToRGBA 返回 r/g/b 为 0~255,a 为 0~1
2344
+ // 但如果已经是 0~1 范围(由 rgbToDecimal 处理过),需要检测
2345
+ const needNormalize = (c.r > 1 || c.g > 1 || c.b > 1) ? 255 : 1;
2346
+ return {
2347
+ offset: s.offset,
2348
+ r: (c.r !== undefined ? c.r : 0) / needNormalize,
2349
+ g: (c.g !== undefined ? c.g : 0) / needNormalize,
2350
+ b: (c.b !== undefined ? c.b : 0) / needNormalize,
2351
+ a: c.a !== undefined ? c.a : 1
2236
2352
  };
2237
- colors.push(color);
2238
2353
  }
2239
- }
2240
- }
2241
- return colors;
2354
+ return { offset: s.offset, r: 0, g: 0, b: 0, a: 1 };
2355
+ })
2356
+ .sort((a, b) => a.offset - b.offset);
2357
+
2358
+ return this._sortedStops;
2242
2359
  }
2243
- */
2244
- // 根据起点距离获取边界stop
2245
- /*
2246
- getStopRange(len, stops) {
2247
- const res = {};
2248
- for(const s of stops) {
2249
- if(s.length <= len) {
2250
- res.start = s;
2251
- }
2252
- else {
2253
- res.end = s;
2254
- }
2360
+
2361
+ /**
2362
+ * 将渐变参数以 uniform 形式传递给着色器
2363
+ * 返回 { type, start, end, stopCount, stops } 供着色器使用
2364
+ */
2365
+ toUniformParams() {
2366
+ const stops = this._getSortedStops();
2367
+ const count = Math.min(stops.length, MAX_STOPS);
2368
+
2369
+ // 展平为 Float32Array: [offset, r, g, b, a, ...]
2370
+ const flatStops = new Float32Array(count * 5);
2371
+ for (let i = 0; i < count; i++) {
2372
+ const s = stops[i];
2373
+ flatStops[i * 5 + 0] = s.offset;
2374
+ flatStops[i * 5 + 1] = s.r;
2375
+ flatStops[i * 5 + 2] = s.g;
2376
+ flatStops[i * 5 + 3] = s.b;
2377
+ flatStops[i * 5 + 4] = s.a;
2255
2378
  }
2256
- return res;
2379
+
2380
+ return {
2381
+ gradientType: this.type === 'radial' ? 2 : 1,
2382
+ gradientStart: new Float32Array([
2383
+ this.x1, this.y1,
2384
+ this.type === 'radial' ? Math.max(0, this.r1) : 0,
2385
+ 0
2386
+ ]),
2387
+ gradientEnd: new Float32Array([
2388
+ this.x2, this.y2,
2389
+ this.type === 'radial' ? Math.max(0, this.r2) : 0,
2390
+ 0
2391
+ ]),
2392
+ stopCount: count,
2393
+ stops: flatStops
2394
+ };
2257
2395
  }
2258
2396
 
2259
- // 根据stop计算offset长度
2260
- getStops() {
2261
- const stops = this.stops.sort((p1, p2) => p1.offset - p2.offset); // 渐变色排序从小于大
2262
- for(const s of stops) {
2263
-
2264
- const color = typeof s.color === 'string'? this.control.graph.utils.hexToRGBA(s.color) : s.color;
2265
- console.log(s, color);
2266
- s.color = this.control.graph.utils.rgbToDecimal(color);
2267
- s.length = s.offset * this.length;
2268
- }
2269
- return stops;
2397
+ /**
2398
+ * 使缓存失效
2399
+ */
2400
+ invalidateCache() {
2401
+ this._sortedStops = null;
2402
+ this._paramsHash = null;
2270
2403
  }
2271
- */
2404
+
2272
2405
  /**
2273
- * 转换为渐变的字符串表达
2274
- *
2275
- * @method toString
2276
- * @for jmGradient
2277
- * @return {string} linear-gradient(x1 y1 x2 y2, color1 step, color2 step, ...); //radial-gradient(x1 y1 r1 x2 y2 r2, color1 step,color2 step, ...);
2278
- */
2279
- toString() {
2280
- let str = this.type + '-gradient(';
2281
- if(this.type == 'linear') {
2282
- str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
2283
- }
2284
- else {
2285
- str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
2286
- }
2287
- //颜色渐变
2288
- this.stops.forEach(function(s) {
2289
- str += ',' + s.color + ' ' + s.offset;
2290
- });
2291
- return str + ')';
2292
- }
2406
+ * 转换为渐变的字符串表达
2407
+ */
2408
+ toString() {
2409
+ let str = this.type + '-gradient(';
2410
+ if (this.type == 'linear') {
2411
+ str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
2412
+ }
2413
+ else {
2414
+ str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
2415
+ }
2416
+ this.stops.forEach(function(s) {
2417
+ str += ',' + s.color + ' ' + s.offset;
2418
+ });
2419
+ return str + ')';
2420
+ }
2293
2421
  }
2294
2422
 
2295
2423
  // 生成着色器
@@ -2614,8 +2742,8 @@ const pathVertexSource = `
2614
2742
  vec4 pos = translatePosition(a_position, a_center_point.x, a_center_point.y);
2615
2743
  gl_Position = pos;
2616
2744
  v_color = a_color;
2617
- if(a_type == 2) {
2618
- v_text_coord = a_text_coord;
2745
+ if(a_type == 2 || a_type == 5) {
2746
+ v_text_coord = a_position.xy;
2619
2747
  }
2620
2748
  }
2621
2749
  `;
@@ -2625,21 +2753,73 @@ const pathFragmentSource = `
2625
2753
  uniform sampler2D u_sample;
2626
2754
  uniform vec4 v_texture_bounds; // 纹理的左上坐标和大小 x,y,z,w
2627
2755
  uniform vec4 v_single_color;
2756
+ // GLSL 渐变 uniforms
2757
+ uniform int u_gradient_type; // 0=无 1=线性 2=径向
2758
+ uniform vec4 u_gradient_start; // 线性:{x1,y1,0,0} 径向:{cx,cy,r1,0}
2759
+ uniform vec4 u_gradient_end; // 线性:{x2,y2,0,0} 径向:{cx,cy,r2,0}
2760
+ uniform int u_gradient_stop_count;
2761
+ uniform float u_gradient_offsets[${MAX_STOPS}];
2762
+ uniform vec4 u_gradient_colors[${MAX_STOPS}]; // {r, g, b, a} 0~1 范围
2628
2763
  varying float v_type;
2629
2764
  varying vec4 v_color;
2630
2765
  varying vec2 v_text_coord;
2631
2766
 
2632
2767
  ${convertTexturePosition}
2633
2768
 
2769
+ // 在 sorted stops 中按 t 值采样颜色
2770
+ // 兼容 GLSL ES 1.0:循环仅与常量比较,无 break/continue
2771
+ vec4 sampleGradient(float t) {
2772
+ t = clamp(t, 0.0, 1.0);
2773
+ // 正向扫描:始终遍历 MAX_STOPS-1 次,找到 t 所在段并覆盖结果
2774
+ float localT = 0.0;
2775
+ vec4 c0 = u_gradient_colors[0];
2776
+ vec4 c1 = u_gradient_colors[0];
2777
+ for(int i = 0; i < ${MAX_STOPS - 1}; i++) {
2778
+ float s0 = u_gradient_offsets[i];
2779
+ float s1 = u_gradient_offsets[i + 1];
2780
+ if(t >= s0) {
2781
+ float range = s1 - s0;
2782
+ localT = range > 0.0001 ? clamp((t - s0) / range, 0.0, 1.0) : 0.0;
2783
+ c0 = u_gradient_colors[i];
2784
+ c1 = u_gradient_colors[i + 1];
2785
+ }
2786
+ }
2787
+ return mix(c0, c1, localT);
2788
+ }
2789
+
2634
2790
  void main() {
2635
2791
  // 如果是fill,则直接填充颜色
2636
2792
  if(v_type == 1.0) {
2637
2793
  gl_FragColor = v_single_color;
2638
2794
  }
2639
- // 渐变色
2795
+ // 渐变色 (旧方式,顶点颜色插值)
2640
2796
  else if(v_type == 3.0) {
2641
2797
  gl_FragColor = v_color;
2642
2798
  }
2799
+ // GLSL 渐变填充 (type=5)
2800
+ else if(v_type == 5.0) {
2801
+ float t;
2802
+ if(u_gradient_type == 2) {
2803
+ // 径向渐变
2804
+ vec2 d = v_text_coord - u_gradient_start.xy;
2805
+ float dist = length(d);
2806
+ float r1 = u_gradient_start.z;
2807
+ float r2 = u_gradient_end.z;
2808
+ float range = r2 - r1;
2809
+ t = range > 0.001 ? (dist - r1) / range : 0.0;
2810
+ } else {
2811
+ // 线性渐变
2812
+ vec2 dir = u_gradient_end.xy - u_gradient_start.xy;
2813
+ float lenSq = dot(dir, dir);
2814
+ if(lenSq > 0.001) {
2815
+ vec2 pos = v_text_coord - u_gradient_start.xy;
2816
+ t = dot(pos, dir) / lenSq;
2817
+ } else {
2818
+ t = 0.0;
2819
+ }
2820
+ }
2821
+ gl_FragColor = sampleGradient(t) * v_single_color.a;
2822
+ }
2643
2823
  else if(v_type == 2.0) {
2644
2824
  vec2 pos = translateTexturePosition(v_text_coord, v_texture_bounds);
2645
2825
  gl_FragColor = texture2D(u_sample, pos);
@@ -2710,7 +2890,7 @@ class WeblBase {
2710
2890
  rotate(angle) {
2711
2891
  const cos = Math.cos(angle);
2712
2892
  const sin = Math.sin(angle);
2713
- const [a, b, c, d, tx, ty] = this.transformMatrix;
2893
+ const [a, b, c, d] = this.transformMatrix;
2714
2894
 
2715
2895
  // 更新变换矩阵
2716
2896
  this.transformMatrix[0] = a * cos - b * sin;
@@ -2741,21 +2921,20 @@ class WeblBase {
2741
2921
  };
2742
2922
  }
2743
2923
 
2744
- // 纹理绘制canvas
2745
- get textureCanvas() {
2746
- let canvas = this.graph.textureCanvas;
2747
- if(!canvas) {
2748
- if(typeof document === 'undefined') return null;
2749
- canvas = this.graph.textureCanvas = document.createElement('canvas');
2924
+ // 文本测量用的离屏 canvas context(1x1 单例缓存,不依赖 textureCanvas)
2925
+ get _measureCtx() {
2926
+ if(!this.__measureCtx) {
2927
+ try {
2928
+ if(typeof document !== 'undefined') {
2929
+ const c = document.createElement('canvas');
2930
+ c.width = c.height = 1;
2931
+ this.__measureCtx = c.getContext('2d');
2932
+ }
2933
+ } catch(e) {
2934
+ this.__measureCtx = null;
2935
+ }
2750
2936
  }
2751
- return canvas;
2752
- }
2753
- // 纹理绘制canvas ctx
2754
- get textureContext() {
2755
- const ctx = this.textureCanvas.ctx || (this.textureCanvas.ctx = this.textureCanvas.getContext('2d', {
2756
- willReadFrequently: true
2757
- }));
2758
- return ctx;
2937
+ return this.__measureCtx;
2759
2938
  }
2760
2939
 
2761
2940
  // i当前程序
@@ -2801,29 +2980,42 @@ class WeblBase {
2801
2980
  // 把传统颜色转为webgl识别的
2802
2981
  convertColor(color) {
2803
2982
  if(this.isGradient(color)) return color;
2804
- if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
2805
- return this.graph.utils.rgbToDecimal(color);
2806
- }
2807
-
2808
- setTextureStyle(style, value='') {
2809
-
2810
- if(typeof style === 'string') {
2811
- if(['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(style) > -1) {
2812
- value = this.graph.utils.toColor(value);
2983
+ if(typeof color === 'string') {
2984
+ // 先尝试 hexToRGBA 解析
2985
+ color = this.graph.utils.hexToRGBA(color);
2986
+ // hexToRGBA 对无法识别的格式(如 hsl)会原样返回字符串
2987
+ // 利用离屏 canvas 将任意 CSS 颜色转为 rgba
2988
+ if(typeof color === 'string') {
2989
+ color = this.__parseCSSColor(color);
2813
2990
  }
2814
- this.textureContext[style] = value;
2815
2991
  }
2816
- else {
2817
- for(const name in style) {
2818
- if(name === 'constructor') continue;
2819
- this.setTextureStyle(name, style[name]);
2992
+ if(typeof color === 'object' && color.r !== undefined) {
2993
+ return this.graph.utils.rgbToDecimal(color);
2994
+ }
2995
+ return color;
2996
+ }
2997
+
2998
+ // 利用离屏 canvas 解析任意 CSS 颜色(hsl/hsla/命名颜色等)
2999
+ __parseCSSColor(colorStr) {
3000
+ const ctx = this._measureCtx;
3001
+ if(!ctx) return { r: 0, g: 0, b: 0, a: 0 };
3002
+ try {
3003
+ ctx.clearRect(0, 0, 1, 1);
3004
+ ctx.fillStyle = '#000000';
3005
+ ctx.fillStyle = colorStr;
3006
+ ctx.fillRect(0, 0, 1, 1);
3007
+ const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
3008
+ if(ctx.fillStyle === '#000000' && colorStr !== '#000000' && colorStr !== 'black') {
3009
+ return { r: 0, g: 0, b: 0, a: 0 };
2820
3010
  }
3011
+ return { r, g, b, a: a / 255 };
3012
+ } catch(e) {
3013
+ return { r: 0, g: 0, b: 0, a: 0 };
2821
3014
  }
2822
3015
  }
2823
3016
 
2824
3017
  // 创建程序
2825
3018
  createProgram(vertexSrc, fragmentSrc) {
2826
- this.context.lineWidth(1);
2827
3019
  return createProgram(this.context, vertexSrc, fragmentSrc);
2828
3020
  }
2829
3021
 
@@ -2936,7 +3128,9 @@ class WeblBase {
2936
3128
  // polygonIndices 顶点索引,
2937
3129
  earCutPointsToTriangles(points) {
2938
3130
  this.earCutCache = this.earCutCache || (this.earCutCache = {});
2939
- const key = JSON.stringify(points);
3131
+ // 快速缓存 key:用长度和首尾点坐标
3132
+ const len = points.length;
3133
+ const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
2940
3134
  if (this.earCutCache[key]) return this.earCutCache[key];
2941
3135
 
2942
3136
  const ps = this.earCutPoints(points);// 切割得到3角色顶点索引,
@@ -2991,97 +3185,23 @@ class WeblBase {
2991
3185
  return obj && obj instanceof WebglGradient;
2992
3186
  }
2993
3187
 
2994
- /**
3188
+ /**
2995
3189
  * 测试获取文本所占大小
2996
3190
  *
2997
3191
  * @method testSize
2998
3192
  * @return {object} 含文本大小的对象
2999
3193
  */
3000
3194
  testSize(text, style=this.style) {
3001
-
3002
- this.textureContext.save && this.textureContext.save();
3003
- // 修改字体,用来计算
3004
- if(style.font || style.fontSize) this.textureContext.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
3005
-
3006
- //计算宽度
3007
- const size = this.textureContext.measureText?
3008
- this.textureContext.measureText(text):
3009
- {width:15};
3010
- this.textureContext.restore &&this.textureContext.restore();
3011
- size.height = this.style.fontSize? this.style.fontSize: 15;
3195
+ const ctx = this._measureCtx;
3196
+ if(!ctx) return { width: 15, height: style.fontSize || 15 };
3197
+
3198
+ ctx.save && ctx.save();
3199
+ if(style.font || style.fontSize) ctx.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
3200
+ const size = ctx.measureText ? ctx.measureText(text) : { width: 15 };
3201
+ ctx.restore && ctx.restore();
3202
+ size.height = style.fontSize ? parseInt(style.fontSize) : 15;
3012
3203
  return size;
3013
3204
  }
3014
-
3015
- // 使用纹理canvas生成图,
3016
- // 填充可以是颜色或渐变对象
3017
- // 如果指定了points,则表明要绘制不规则的图形
3018
- toFillTexture(fillStyle, bounds, points=null) {
3019
- const canvas = this.textureCanvas;
3020
- if(!canvas) {
3021
- return fillStyle;
3022
- }
3023
- canvas.width = bounds.width;
3024
- canvas.height = bounds.height;
3025
-
3026
- if(!canvas.width || !canvas.height) {
3027
- return fillStyle;
3028
- }
3029
-
3030
- this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
3031
-
3032
- this.textureContext.fillStyle = fillStyle;
3033
-
3034
- this.textureContext.beginPath();
3035
- 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) {
3059
- for(const p of points) {
3060
- //移至当前坐标
3061
- if(p.m) {
3062
- this.textureContext.moveTo(p.x - bounds.left, p.y - bounds.top);
3063
- }
3064
- else {
3065
- this.textureContext.lineTo(p.x - bounds.left, p.y - bounds.top);
3066
- }
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);
3075
- }
3076
- this.textureContext.closePath();
3077
- this.textureContext.fill();
3078
-
3079
- const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
3080
- return {
3081
- data,
3082
- points
3083
- };
3084
- }
3085
3205
  }
3086
3206
 
3087
3207
  // path 绘制类
@@ -3093,6 +3213,40 @@ class WebglPath extends WeblBase {
3093
3213
  this.needCut = option.needCut || false;
3094
3214
  this.control = option.control;
3095
3215
  this.points = [];
3216
+ // 缓存 buffer 和纹理,避免每帧创建/销毁
3217
+ this.__cachedBuffers = [];
3218
+ this.__cachedTexture = null;
3219
+ this.__cachedTextureKey = null;
3220
+ }
3221
+
3222
+ // 释放缓存的 WebGL 资源
3223
+ dispose() {
3224
+ for(const buf of this.__cachedBuffers) {
3225
+ this.deleteBuffer(buf);
3226
+ }
3227
+ this.__cachedBuffers = [];
3228
+ if(this.__cachedTexture) {
3229
+ this.deleteTexture(this.__cachedTexture);
3230
+ this.__cachedTexture = null;
3231
+ this.__cachedTextureKey = null;
3232
+ }
3233
+ }
3234
+
3235
+ // 获取或创建 buffer,优先复用缓存
3236
+ getOrCreateBuffer(data, attr) {
3237
+ let buffer = this.__cachedBuffers.find(b => b.attr === attr);
3238
+ if(buffer) {
3239
+ const gl = this.context;
3240
+ const float32 = new Float32Array(data);
3241
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer.buffer);
3242
+ gl.bufferData(gl.ARRAY_BUFFER, float32, gl.DYNAMIC_DRAW);
3243
+ buffer.data = data;
3244
+ return buffer;
3245
+ }
3246
+ buffer = this.createFloat32Buffer(data);
3247
+ buffer.attr = attr;
3248
+ this.__cachedBuffers.push(buffer);
3249
+ return buffer;
3096
3250
  }
3097
3251
 
3098
3252
  // 应用变换到点
@@ -3105,8 +3259,14 @@ class WebglPath extends WeblBase {
3105
3259
  //this.useProgram();
3106
3260
 
3107
3261
  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);
3262
+ // 缓存中心点值,只在变化时才更新 uniform
3263
+ const cx = this.graph.width / 2;
3264
+ const cy = this.graph.height / 2;
3265
+ if(this.__lastCenterX !== cx || this.__lastCenterY !== cy) {
3266
+ this.context.uniform2f(this.program.uniforms.a_center_point.location, cx, cy);
3267
+ this.__lastCenterX = cx;
3268
+ this.__lastCenterY = cy;
3269
+ }
3110
3270
  }
3111
3271
 
3112
3272
  setFragColor(color) {
@@ -3147,6 +3307,7 @@ class WebglPath extends WeblBase {
3147
3307
  endDraw() {
3148
3308
  if(this.points) delete this.points;
3149
3309
  if(this.pathPoints) delete this.pathPoints;
3310
+ // 缓存的纹理保留到下次绘制(渐变可能不变)
3150
3311
  }
3151
3312
 
3152
3313
  // 图形封闭
@@ -3158,21 +3319,47 @@ class WebglPath extends WeblBase {
3158
3319
  }
3159
3320
  }
3160
3321
 
3161
- // 绘制点数组
3322
+ // 绘制点数组(使用 DYNAMIC_DRAW 复用 buffer,避免每帧 create/delete)
3162
3323
  writePoints(points, attr = this.program.attrs.a_position) {
3163
-
3164
3324
  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);
3325
+ const [a, b, c, d, tx, ty] = this.transformMatrix;
3326
+ const isIdentity = (a === 1 && b === 0 && c === 0 && d === 1 && tx === 0 && ty === 0);
3327
+ const offsetLeft = this.parentAbsoluteBounds.left;
3328
+ const offsetTop = this.parentAbsoluteBounds.top;
3329
+
3330
+ if(isIdentity) {
3331
+ // 单位矩阵时直接加偏移,避免逐点调用 applyTransform
3332
+ for(let i = 0; i < points.length; i++) {
3333
+ fixedPoints.push(points[i].x + offsetLeft, points[i].y + offsetTop);
3334
+ }
3335
+ } else {
3336
+ for(const p of points) {
3337
+ const transformedPoint = this.applyTransform(p);
3338
+ fixedPoints.push(
3339
+ transformedPoint.x + offsetLeft,
3340
+ transformedPoint.y + offsetTop
3341
+ );
3342
+ }
3343
+ }
3344
+ const float32 = new Float32Array(fixedPoints);
3345
+ const gl = this.context;
3346
+
3347
+ // 复用已有 buffer 或创建新的
3348
+ if(this.__cachedBuffers.length > 0) {
3349
+ // 找一个同 attr 的 buffer 复用
3350
+ let buffer = this.__cachedBuffers.find(b => b.attr === attr);
3351
+ if(buffer) {
3352
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer.buffer);
3353
+ gl.bufferData(gl.ARRAY_BUFFER, float32, gl.DYNAMIC_DRAW);
3354
+ buffer.data = fixedPoints;
3355
+ this.writeVertexAttrib(buffer, attr, 2, 0, 0);
3356
+ return buffer;
3357
+ }
3358
+ }
3359
+ const vertexBuffer = this.createFloat32Buffer(float32, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3174
3360
  this.writeVertexAttrib(vertexBuffer, attr, 2, 0, 0);
3175
3361
  vertexBuffer.attr = attr;
3362
+ this.__cachedBuffers.push(vertexBuffer);
3176
3363
  return vertexBuffer;
3177
3364
  }
3178
3365
 
@@ -3221,6 +3408,112 @@ class WebglPath extends WeblBase {
3221
3408
  equalPoint(p1, p2) {
3222
3409
  return p1.x === p2.x && p1.y === p2.y;
3223
3410
  }
3411
+
3412
+ // 将带 moveTo 标记的点集拆分为外轮廓和多个洞
3413
+ splitSubPaths(points) {
3414
+ const subPaths = [];
3415
+ let current = [];
3416
+ for(let i = 0; i < points.length; i++) {
3417
+ const p = points[i];
3418
+ if(p.m && current.length > 0) {
3419
+ subPaths.push(current);
3420
+ current = [];
3421
+ }
3422
+ current.push(p);
3423
+ }
3424
+ if(current.length > 0) subPaths.push(current);
3425
+
3426
+ // 面积最大的作为外轮廓,其余作为洞
3427
+ let maxArea = -1;
3428
+ let outerIdx = 0;
3429
+ for(let i = 0; i < subPaths.length; i++) {
3430
+ const area = Math.abs(this.polygonArea(subPaths[i]));
3431
+ if(area > maxArea) {
3432
+ maxArea = area;
3433
+ outerIdx = i;
3434
+ }
3435
+ }
3436
+
3437
+ const outerPoints = subPaths[outerIdx];
3438
+ const holes = [];
3439
+ for(let i = 0; i < subPaths.length; i++) {
3440
+ if(i !== outerIdx) holes.push(subPaths[i]);
3441
+ }
3442
+ return { outerPoints, holes };
3443
+ }
3444
+
3445
+ // 计算多边形面积(Shoelace 公式)
3446
+ polygonArea(points) {
3447
+ let area = 0;
3448
+ const n = points.length;
3449
+ for(let i = 0; i < n; i++) {
3450
+ const j = (i + 1) % n;
3451
+ area += points[i].x * points[j].y;
3452
+ area -= points[j].x * points[i].y;
3453
+ }
3454
+ return area / 2;
3455
+ }
3456
+
3457
+ // 使用 earcut 带 holes 填充多边形
3458
+ fillWithHoles(outerPoints, holes, isTexture = false) {
3459
+ // 将所有点合并:外轮廓 + 各个洞,并记录洞的起始索引
3460
+ const allPoints = [...outerPoints];
3461
+ const holeIndices = [];
3462
+ for(const hole of holes) {
3463
+ holeIndices.push(allPoints.length);
3464
+ allPoints.push(...hole);
3465
+ }
3466
+
3467
+ const dim = 2;
3468
+ const vertexData = [];
3469
+ for(const p of allPoints) {
3470
+ vertexData.push(p.x, p.y);
3471
+ }
3472
+
3473
+ // 用 earcut 进行带洞三角化
3474
+ const indices = earcut(vertexData, holeIndices, dim);
3475
+
3476
+ if(!indices || indices.length < 3) return;
3477
+
3478
+ // 构建 GPU 顶点数据
3479
+ const allVertices = [];
3480
+ const allTexCoords = [];
3481
+ for(let i = 0; i < indices.length; i++) {
3482
+ const p = allPoints[indices[i]];
3483
+ allVertices.push(p.x, p.y);
3484
+ if(isTexture) allTexCoords.push(p.x, p.y);
3485
+ }
3486
+
3487
+ const gl = this.context;
3488
+ const vertexArr = new Float32Array(allVertices);
3489
+
3490
+ let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
3491
+ if(!posBuffer) {
3492
+ posBuffer = this.createFloat32Buffer(vertexArr, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3493
+ posBuffer.attr = this.program.attrs.a_position;
3494
+ this.__cachedBuffers.push(posBuffer);
3495
+ } else {
3496
+ gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
3497
+ gl.bufferData(gl.ARRAY_BUFFER, vertexArr, gl.DYNAMIC_DRAW);
3498
+ }
3499
+ this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
3500
+
3501
+ if(isTexture && allTexCoords.length) {
3502
+ const texData = new Float32Array(allTexCoords);
3503
+ let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
3504
+ if(!texBuffer) {
3505
+ texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3506
+ texBuffer.attr = this.program.attrs.a_text_coord;
3507
+ this.__cachedBuffers.push(texBuffer);
3508
+ } else {
3509
+ gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
3510
+ gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
3511
+ }
3512
+ this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
3513
+ }
3514
+
3515
+ gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
3516
+ }
3224
3517
  // 把path坐标集合转为线段集
3225
3518
  pathToLines(points) {
3226
3519
  let start = null;
@@ -3408,9 +3701,10 @@ class WebglPath extends WeblBase {
3408
3701
 
3409
3702
  // 分割成一个个规则的三角形,不规则的多边形不全割的话纹理就会没法正确覆盖
3410
3703
  getTriangles(points) {
3411
-
3412
3704
  this.trianglesCache = this.trianglesCache||(this.trianglesCache={});
3413
- const key = JSON.stringify(points);
3705
+ // 快速缓存 key:用长度和首尾点坐标(比 JSON.stringify 快几个数量级)
3706
+ const len = points.length;
3707
+ const key = len + '_' + points[0].x + '_' + points[0].y + '_' + points[len-1].x + '_' + points[len-1].y;
3414
3708
  if(this.trianglesCache[key]) return this.trianglesCache[key];
3415
3709
 
3416
3710
  const res = [];
@@ -3446,13 +3740,49 @@ class WebglPath extends WeblBase {
3446
3740
  }
3447
3741
  if(points && points.length) {
3448
3742
  const regular = lineWidth <= 1.2;
3449
- points = regular? points : this.pathToPoints(points);
3450
- const buffer = this.writePoints(points);
3451
- this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
3452
- this.deleteBuffer(buffer);
3743
+ const hasMoveTo = points.some && points.some(p => p.m);
3744
+ const isRing = !hasMoveTo && this.needCut; // 空心形状(jmHArc close=true 时无 m 标记)
3745
+ if(regular && (hasMoveTo || isRing)) {
3746
+ // 有 moveTo 标记或空心形状时,分段绘制每个子路径的 LINE_LOOP
3747
+ // 避免 LINE_LOOP 把不同子路径的点连起来产生拉扯线
3748
+ if(hasMoveTo) {
3749
+ let subPath = [];
3750
+ for(let i = 0; i < points.length; i++) {
3751
+ if(points[i].m && subPath.length > 0) {
3752
+ this.writePoints(subPath);
3753
+ this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
3754
+ subPath = [];
3755
+ }
3756
+ subPath.push(points[i]);
3757
+ }
3758
+ if(subPath.length > 1) {
3759
+ this.writePoints(subPath);
3760
+ this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
3761
+ }
3762
+ }
3763
+ else if(isRing) {
3764
+ // 空心形状:前半段为内弧,后半段为外弧(反向),各自 LINE_LOOP
3765
+ const mid = Math.floor(points.length / 2);
3766
+ const inner = points.slice(0, mid);
3767
+ const outer = points.slice(mid);
3768
+ if(inner.length > 1) {
3769
+ this.writePoints(inner);
3770
+ this.context.drawArrays(this.context.LINE_LOOP, 0, inner.length);
3771
+ }
3772
+ if(outer.length > 1) {
3773
+ this.writePoints(outer);
3774
+ this.context.drawArrays(this.context.LINE_LOOP, 0, outer.length);
3775
+ }
3776
+ }
3777
+ }
3778
+ else {
3779
+ points = regular? points : this.pathToPoints(points);
3780
+ this.writePoints(points);
3781
+ this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
3782
+ }
3783
+ // buffer 由 endDraw 统一清理
3453
3784
  }
3454
- colorBuffer && this.deleteBuffer(colorBuffer);
3455
- colorBuffer && this.disableVertexAttribArray(colorBuffer.attr);
3785
+ colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
3456
3786
  }
3457
3787
 
3458
3788
  // 填充图形
@@ -3471,10 +3801,9 @@ class WebglPath extends WeblBase {
3471
3801
 
3472
3802
  fillColor(color, points, bounds, type=1) {
3473
3803
 
3474
- // 如果是渐变色,则需要计算偏移量的颜色
3804
+ // 如果是渐变色,使用 GLSL 着色器直接计算
3475
3805
  if(this.isGradient(color)) {
3476
- const imgData = color.toImageData(this, bounds, points);
3477
- return this.fillImage(imgData.data, imgData.points, bounds);
3806
+ return this.fillGradient(color, points, bounds);
3478
3807
  }
3479
3808
 
3480
3809
  // 标注为fill
@@ -3483,19 +3812,111 @@ class WebglPath extends WeblBase {
3483
3812
 
3484
3813
  this.fillPolygons(points);
3485
3814
 
3486
- colorBuffer && this.deleteBuffer(colorBuffer);
3487
- colorBuffer && this.disableVertexAttribArray(colorBuffer.attr);
3815
+ colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
3488
3816
 
3489
3817
  }
3490
3818
 
3819
+ /**
3820
+ * 使用 GLSL 着色器渲染渐变填充
3821
+ * 无需 textureCanvas,直接通过 uniform 传递渐变参数给 GPU
3822
+ */
3823
+ fillGradient(gradient, points, bounds) {
3824
+ const params = gradient.toUniformParams();
3825
+ if(!params) return;
3826
+
3827
+ // 标注为 GLSL 渐变 (type=5)
3828
+ this.context.uniform1i(this.program.uniforms.a_type.location, 5);
3829
+
3830
+ // 设置 globalAlpha(通过 v_single_color.a 传递给着色器)
3831
+ this.context.uniform4f(this.program.uniforms.v_single_color.location, 1.0, 1.0, 1.0, this.style.globalAlpha);
3832
+
3833
+ // 设置渐变类型
3834
+ if(this.program.uniforms.u_gradient_type) {
3835
+ this.context.uniform1i(this.program.uniforms.u_gradient_type.location, params.gradientType);
3836
+ }
3837
+
3838
+ // 设置渐变起点/终点
3839
+ if(this.program.uniforms.u_gradient_start) {
3840
+ this.context.uniform4fv(this.program.uniforms.u_gradient_start.location, params.gradientStart);
3841
+ }
3842
+ if(this.program.uniforms.u_gradient_end) {
3843
+ this.context.uniform4fv(this.program.uniforms.u_gradient_end.location, params.gradientEnd);
3844
+ }
3845
+
3846
+ // 设置颜色断点数量
3847
+ if(this.program.uniforms.u_gradient_stop_count) {
3848
+ this.context.uniform1i(this.program.uniforms.u_gradient_stop_count.location, params.stopCount);
3849
+ }
3850
+
3851
+ // 设置每个 stop 的 offset
3852
+ // 关键:必须填充完整的 MAX_STOPS 长度数组,否则未初始化元素默认为 0
3853
+ // 会导致着色器循环中 t >= 0 始终为 true,返回黑色
3854
+ if(this.program.uniforms.u_gradient_offsets) {
3855
+ const offsets = new Float32Array(MAX_STOPS);
3856
+ for(let i = 0; i < params.stopCount; i++) {
3857
+ offsets[i] = params.stops[i * 5];
3858
+ }
3859
+ // 用 2.0 填充剩余项,使 t(0~1) >= 2.0 为 false,不会被匹配
3860
+ for(let i = params.stopCount; i < MAX_STOPS; i++) {
3861
+ offsets[i] = 2.0;
3862
+ }
3863
+ this.context.uniform1fv(this.program.uniforms.u_gradient_offsets.location, offsets);
3864
+ }
3865
+
3866
+ // 设置每个 stop 的颜色 (rgba)
3867
+ if(this.program.uniforms.u_gradient_colors) {
3868
+ const colors = new Float32Array(MAX_STOPS * 4);
3869
+ for(let i = 0; i < params.stopCount; i++) {
3870
+ colors[i * 4 + 0] = params.stops[i * 5 + 1]; // r
3871
+ colors[i * 4 + 1] = params.stops[i * 5 + 2]; // g
3872
+ colors[i * 4 + 2] = params.stops[i * 5 + 3]; // b
3873
+ colors[i * 4 + 3] = params.stops[i * 5 + 4]; // a
3874
+ }
3875
+ // 用最后一个 stop 的颜色填充剩余项,确保不会返回黑色
3876
+ if(params.stopCount > 0) {
3877
+ const lastR = params.stops[(params.stopCount - 1) * 5 + 1];
3878
+ const lastG = params.stops[(params.stopCount - 1) * 5 + 2];
3879
+ const lastB = params.stops[(params.stopCount - 1) * 5 + 3];
3880
+ const lastA = params.stops[(params.stopCount - 1) * 5 + 4];
3881
+ for(let i = params.stopCount; i < MAX_STOPS; i++) {
3882
+ colors[i * 4 + 0] = lastR;
3883
+ colors[i * 4 + 1] = lastG;
3884
+ colors[i * 4 + 2] = lastB;
3885
+ colors[i * 4 + 3] = lastA;
3886
+ }
3887
+ }
3888
+ this.context.uniform4fv(this.program.uniforms.u_gradient_colors.location, colors);
3889
+ }
3890
+
3891
+ // 填充多边形(需要纹理坐标来计算渐变位置)
3892
+ this.fillPolygons(points, true);
3893
+ this.disableVertexAttribArray(this.program.attrs.a_text_coord);
3894
+ }
3895
+
3491
3896
  // 区域填充图片
3492
3897
  // points绘制的图形顶点
3493
3898
  // 图片整体绘制区域
3494
3899
  fillImage(img, points, bounds) {
3495
3900
  if(!img) return;
3496
3901
 
3497
- // 设置纹理
3498
- const texture = img instanceof ImageData? this.createDataTexture(img) : this.createImgTexture(img);
3902
+ // 对于 ImageData,生成缓存 key(基于渐变参数或 bounds),复用纹理
3903
+ let texture = null;
3904
+ if(img instanceof ImageData) {
3905
+ const key = `${img.width}_${img.height}_${bounds.width}_${bounds.height}_${bounds.left}_${bounds.top}`;
3906
+ if(this.__cachedTexture && this.__cachedTextureKey === key) {
3907
+ texture = this.__cachedTexture;
3908
+ } else {
3909
+ texture = this.createDataTexture(img);
3910
+ // 释放旧纹理
3911
+ if(this.__cachedTexture) {
3912
+ this.deleteTexture(this.__cachedTexture);
3913
+ }
3914
+ this.__cachedTexture = texture;
3915
+ this.__cachedTextureKey = key;
3916
+ }
3917
+ } else {
3918
+ texture = this.createImgTexture(img);
3919
+ }
3499
3920
  this.context.uniform1i(this.program.uniforms.u_sample.location, 0); // 纹理单元传递给着色器
3500
3921
 
3501
3922
  // 指定纹理区域尺寸
@@ -3508,7 +3929,10 @@ class WebglPath extends WeblBase {
3508
3929
 
3509
3930
  this.fillTexture(points);
3510
3931
 
3511
- this.deleteTexture(texture);
3932
+ // 仅对非缓存纹理(非 ImageData)立即删除
3933
+ if(!(img instanceof ImageData)) {
3934
+ this.deleteTexture(texture);
3935
+ }
3512
3936
  }
3513
3937
 
3514
3938
  fillTexture(points) {
@@ -3524,23 +3948,90 @@ class WebglPath extends WeblBase {
3524
3948
 
3525
3949
  // 进行多边形填充
3526
3950
  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);// 这里就变成了规则的图形了
3951
+ if(points.length <= 3) {
3952
+ // 3个点以下的三角形直接画
3953
+ this.writePoints(points);
3954
+ isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3955
+ this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
3956
+ return;
3957
+ }
3958
+
3959
+ // 规则图形(凸多边形,如圆):直接用 TRIANGLE_FAN 一次性绘制,无需 earcut
3960
+ if(this.isRegular) {
3961
+ // 检查是否有 moveTo 标记,如果有说明路径包含多个子路径(如空心圆弧 jmHArc)
3962
+ const hasMoveTo = points.some && points.some(p => p.m);
3963
+ if(hasMoveTo) {
3964
+ // 有 m 标记:按 m 标记拆分子路径
3965
+ const { outerPoints, holes } = this.splitSubPaths(points);
3966
+ this.fillWithHoles(outerPoints, holes, isTexture);
3967
+ return;
3968
+ }
3969
+ // 无 m 标记但 needCut=true 表示空心形状(如 jmHArc close=true)
3970
+ // 前半段为内弧,后半段为外弧(反向),按中点拆分
3971
+ if(this.needCut && points.length >= 6) {
3972
+ const mid = Math.floor(points.length / 2);
3973
+ const inner = points.slice(0, mid);
3974
+ const outer = points.slice(mid);
3975
+ const innerArea = Math.abs(this.polygonArea(inner));
3976
+ const outerArea = Math.abs(this.polygonArea(outer));
3977
+ if(outerArea >= innerArea) {
3978
+ this.fillWithHoles(outer, [inner], isTexture);
3979
+ } else {
3980
+ this.fillWithHoles(inner, [outer], isTexture);
3532
3981
  }
3982
+ return;
3533
3983
  }
3984
+ this.writePoints(points);
3985
+ isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3986
+ this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
3987
+ return;
3534
3988
  }
3535
- else {
3536
- const buffer = this.writePoints(points);
3537
- // 纹理坐标
3538
- const coordBuffer = isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3539
3989
 
3540
- this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
3541
- this.deleteBuffer(buffer);
3542
- coordBuffer && this.deleteBuffer(coordBuffer);
3990
+ // 不规则图形:需要 earcut 三角化后,合并为一个大的顶点缓冲区,单次 drawArrays
3991
+ const triangles = this.needCut? this.earCutPointsToTriangles(points): this.getTriangles(points);
3992
+ if(!triangles.length) return;
3993
+
3994
+ // 合并所有三角形的顶点到一个数组
3995
+ const allVertices = [];
3996
+ const allTexCoords = [];
3997
+ for(const triangle of triangles) {
3998
+ for(const p of triangle) {
3999
+ allVertices.push(p.x, p.y);
4000
+ if(isTexture) allTexCoords.push(p.x, p.y);
4001
+ }
4002
+ }
4003
+
4004
+ // 一次性上传所有数据并绘制
4005
+ const vertexData = new Float32Array(allVertices);
4006
+ const gl = this.context;
4007
+
4008
+ // 复用或创建 position buffer
4009
+ let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
4010
+ if(!posBuffer) {
4011
+ posBuffer = this.createFloat32Buffer(vertexData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
4012
+ posBuffer.attr = this.program.attrs.a_position;
4013
+ this.__cachedBuffers.push(posBuffer);
4014
+ } else {
4015
+ gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
4016
+ gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.DYNAMIC_DRAW);
4017
+ }
4018
+ this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
4019
+
4020
+ if(isTexture && allTexCoords.length) {
4021
+ const texData = new Float32Array(allTexCoords);
4022
+ let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
4023
+ if(!texBuffer) {
4024
+ texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
4025
+ texBuffer.attr = this.program.attrs.a_text_coord;
4026
+ this.__cachedBuffers.push(texBuffer);
4027
+ } else {
4028
+ gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
4029
+ gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
4030
+ }
4031
+ this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
3543
4032
  }
4033
+
4034
+ gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
3544
4035
  }
3545
4036
 
3546
4037
  // 填充图形
@@ -3557,46 +4048,73 @@ class WebglPath extends WeblBase {
3557
4048
  }
3558
4049
 
3559
4050
  drawText(text, x, y, bounds) {
3560
- let canvas = this.textureCanvas;
4051
+ // 文本渲染仍需要 2D canvas 绘制字形,然后作为纹理上传
4052
+ // 使用临时 canvas,不依赖共享的 textureCanvas
4053
+ if(!bounds.width || !bounds.height) return null;
4054
+ if(typeof document === 'undefined') return null;
4055
+
4056
+ let canvas = this.__textCanvas;
3561
4057
  if(!canvas) {
3562
- return null;
4058
+ canvas = document.createElement('canvas');
4059
+ this.__textCanvas = canvas;
3563
4060
  }
3564
4061
  canvas.width = bounds.width;
3565
4062
  canvas.height = bounds.height;
3566
4063
 
3567
- if(!canvas.width || !canvas.height) {
3568
- return null;
3569
- }
4064
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
4065
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
3570
4066
 
3571
- this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
3572
4067
  // 修改字体
3573
- this.textureContext.font = this.style.font || (this.style.fontSize + 'px ' + this.style.fontFamily);
4068
+ ctx.font = this.style.font || (this.style.fontSize + 'px ' + this.style.fontFamily);
3574
4069
 
3575
4070
  x -= bounds.left;
3576
4071
  y -= bounds.top;
3577
4072
 
3578
- this.setTextureStyle(this.style);
3579
-
3580
- if(this.style.fillStyle && this.textureContext.fillText) {
4073
+ // 设置文本样式
4074
+ if(this.style.fillStyle) {
4075
+ ctx.fillStyle = this.graph.utils.toColor(this.style.fillStyle);
4076
+ }
4077
+ if(this.style.strokeStyle) {
4078
+ ctx.strokeStyle = this.graph.utils.toColor(this.style.strokeStyle);
4079
+ }
4080
+ if(this.style.shadowColor) {
4081
+ ctx.shadowColor = this.graph.utils.toColor(this.style.shadowColor);
4082
+ }
4083
+ if(this.style.shadowBlur) {
4084
+ ctx.shadowBlur = this.style.shadowBlur;
4085
+ }
4086
+ if(this.style.shadowOffsetX !== undefined) {
4087
+ ctx.shadowOffsetX = this.style.shadowOffsetX;
4088
+ }
4089
+ if(this.style.shadowOffsetY !== undefined) {
4090
+ ctx.shadowOffsetY = this.style.shadowOffsetY;
4091
+ }
4092
+ if(this.style.textAlign) {
4093
+ ctx.textAlign = this.style.textAlign;
4094
+ }
4095
+ if(this.style.textBaseline) {
4096
+ ctx.textBaseline = this.style.textBaseline;
4097
+ }
3581
4098
 
4099
+ if(this.style.fillStyle && ctx.fillText) {
3582
4100
  if(this.style.maxWidth) {
3583
- this.textureContext.fillText(text, x, y, this.style.maxWidth);
4101
+ ctx.fillText(text, x, y, this.style.maxWidth);
3584
4102
  }
3585
4103
  else {
3586
- this.textureContext.fillText(text, x, y);
4104
+ ctx.fillText(text, x, y);
3587
4105
  }
3588
4106
  }
3589
- if(this.textureContext.strokeText) {
3590
-
4107
+ if(this.style.strokeStyle && ctx.strokeText) {
3591
4108
  if(this.style.maxWidth) {
3592
- this.textureContext.strokeText(text, x, y, this.style.maxWidth);
4109
+ ctx.strokeText(text, x, y, this.style.maxWidth);
3593
4110
  }
3594
4111
  else {
3595
- this.textureContext.strokeText(text, x, y);
4112
+ ctx.strokeText(text, x, y);
3596
4113
  }
3597
4114
  }
4115
+
3598
4116
  // 用纹理图片代替文字
3599
- const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
4117
+ const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
3600
4118
  this.fillImage(data, this.points, bounds);
3601
4119
  }
3602
4120
  }
@@ -3622,7 +4140,9 @@ const jmStyleMap = {
3622
4140
  'shadowOffsetY' : 'shadowOffsetY',
3623
4141
  'shadowColor' : 'shadowColor',
3624
4142
  'lineJoin': 'lineJoin',
3625
- 'lineCap':'lineCap'
4143
+ 'lineCap':'lineCap',
4144
+ 'lineDashOffset': 'lineDashOffset',
4145
+ 'globalCompositeOperation': 'globalCompositeOperation'
3626
4146
  };
3627
4147
 
3628
4148
  class jmControl extends jmProperty {
@@ -3833,7 +4353,9 @@ class jmControl extends jmProperty {
3833
4353
  }
3834
4354
 
3835
4355
  setStyle(style) {
3836
- style = style || jmUtils.clone(this.style, true);
4356
+ if(!style) {
4357
+ style = this.style;
4358
+ }
3837
4359
  if(!style) return;
3838
4360
 
3839
4361
  const __setStyle = (style, name, mpkey) => {
@@ -3917,6 +4439,120 @@ class jmControl extends jmProperty {
3917
4439
  this.cursor = styleValue;
3918
4440
  break;
3919
4441
  }
4442
+ // ===== 新增样式特性 =====
4443
+
4444
+ // 虚线样式:支持自定义lineDash模式 (如 [5, 3, 2] 或 "5,3,2")
4445
+ case 'lineDash' : {
4446
+ if(!this.context.setLineDash) break;
4447
+ let dash;
4448
+ if(typeof styleValue === 'string') {
4449
+ dash = styleValue.split(',').map(v => parseFloat(v.trim())).filter(v => !isNaN(v));
4450
+ }
4451
+ else if(Array.isArray(styleValue)) {
4452
+ dash = styleValue.map(v => parseFloat(v)).filter(v => !isNaN(v));
4453
+ }
4454
+ if(dash && dash.length) {
4455
+ this.context.setLineDash(dash);
4456
+ }
4457
+ else {
4458
+ this.context.setLineDash([]);
4459
+ }
4460
+ break;
4461
+ }
4462
+ // 虚线偏移量
4463
+ case 'lineDashOffset' : {
4464
+ if(!this.context.setLineDash) break;
4465
+ this.context.lineDashOffset = Number(styleValue) || 0;
4466
+ break;
4467
+ }
4468
+ // CSS滤镜效果 (blur, grayscale, sepia, brightness, contrast, saturate, hue-rotate, invert, opacity)
4469
+ case 'filter' : {
4470
+ if(this.context.filter === undefined) break;
4471
+ if(styleValue instanceof jmFilter) {
4472
+ this.context.filter = styleValue.toCanvasFilter();
4473
+ }
4474
+ else if(typeof styleValue === 'string') {
4475
+ this.context.filter = styleValue || 'none';
4476
+ }
4477
+ else if(typeof styleValue === 'object') {
4478
+ this.context.filter = (new jmFilter(styleValue)).toCanvasFilter();
4479
+ }
4480
+ break;
4481
+ }
4482
+ // 混合模式 (source-over, multiply, screen, overlay, darken, lighten, etc.)
4483
+ case 'globalCompositeOperation' : {
4484
+ if(!this.context.globalCompositeOperation) break;
4485
+ this.context.globalCompositeOperation = styleValue;
4486
+ break;
4487
+ }
4488
+ // 裁剪路径:通过canvas clip实现
4489
+ case 'clipPath' : {
4490
+ if(!this.context.clip) break;
4491
+ // clipPath可以是一个图形控件实例
4492
+ if(styleValue && styleValue.points && styleValue.points.length > 0) {
4493
+ const bounds = this.parent && this.parent.absoluteBounds ? this.parent.absoluteBounds : this.absoluteBounds;
4494
+ this.context.beginPath();
4495
+ this.context.moveTo(styleValue.points[0].x + (bounds ? bounds.left : 0), styleValue.points[0].y + (bounds ? bounds.top : 0));
4496
+ for(let i = 1; i < styleValue.points.length; i++) {
4497
+ if(styleValue.points[i].m) {
4498
+ this.context.moveTo(styleValue.points[i].x + (bounds ? bounds.left : 0), styleValue.points[i].y + (bounds ? bounds.top : 0));
4499
+ }
4500
+ else {
4501
+ this.context.lineTo(styleValue.points[i].x + (bounds ? bounds.left : 0), styleValue.points[i].y + (bounds ? bounds.top : 0));
4502
+ }
4503
+ }
4504
+ if(styleValue.style && styleValue.style.close) {
4505
+ this.context.closePath();
4506
+ }
4507
+ this.context.clip();
4508
+ }
4509
+ break;
4510
+ }
4511
+ // 遮罩效果:通过globalCompositeOperation + destination-in实现
4512
+ case 'mask' : {
4513
+ if(!this.context.globalCompositeOperation) break;
4514
+ // mask是一个图形控件实例,在绘制前需要先应用mask
4515
+ // 这里只是标记,实际绘制在paint流程中处理
4516
+ this.__mask = styleValue;
4517
+ break;
4518
+ }
4519
+ // 图片阴影描边阴影(WebGL纹理canvas用)
4520
+ case 'shadowColor' : {
4521
+ if(this.webglControl) {
4522
+ this.webglControl.setStyle('shadowColor', styleValue);
4523
+ }
4524
+ else {
4525
+ this.context.shadowColor = jmUtils.toColor(styleValue);
4526
+ }
4527
+ break;
4528
+ }
4529
+ case 'shadowBlur' : {
4530
+ if(this.webglControl) {
4531
+ this.webglControl.setStyle('shadowBlur', styleValue);
4532
+ }
4533
+ else {
4534
+ this.context.shadowBlur = Number(styleValue) || 0;
4535
+ }
4536
+ break;
4537
+ }
4538
+ case 'shadowOffsetX' : {
4539
+ if(this.webglControl) {
4540
+ this.webglControl.setStyle('shadowOffsetX', styleValue);
4541
+ }
4542
+ else {
4543
+ this.context.shadowOffsetX = Number(styleValue) || 0;
4544
+ }
4545
+ break;
4546
+ }
4547
+ case 'shadowOffsetY' : {
4548
+ if(this.webglControl) {
4549
+ this.webglControl.setStyle('shadowOffsetY', styleValue);
4550
+ }
4551
+ else {
4552
+ this.context.shadowOffsetY = Number(styleValue) || 0;
4553
+ }
4554
+ break;
4555
+ }
3920
4556
  }
3921
4557
  }
3922
4558
  }
@@ -3937,6 +4573,9 @@ class jmControl extends jmProperty {
3937
4573
  else if(t == 'string' && k == 'shadow') {
3938
4574
  style[k] = new jmShadow(style[k]);
3939
4575
  }
4576
+ else if(t == 'string' && k == 'filter') {
4577
+ style[k] = new jmFilter(style[k]);
4578
+ }
3940
4579
  __setStyle(style[k], k);
3941
4580
  }
3942
4581
  }
@@ -4081,25 +4720,30 @@ class jmControl extends jmProperty {
4081
4720
  * @method getLocation
4082
4721
  * @return {object} 当前控件位置参数,包括中心点坐标,右上角坐标,宽高
4083
4722
  */
4084
- getLocation(clone=true) {
4723
+ getLocation() {
4085
4724
  //如果已经计算过则直接返回
4086
4725
  //在开画之前会清空此对象
4087
4726
  //if(reset !== true && this.location) return this.location;
4088
4727
 
4089
4728
  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);//起点
4729
+
4730
+ // 检查是否有百分比参数需要解析,没有则直接引用避免克隆开销
4731
+ const needResolve = this.parent && (jmUtils.checkPercent(this.width) || jmUtils.checkPercent(this.height) ||
4732
+ (this.position && jmUtils.checkPercent(this.position.x)) || (this.position && jmUtils.checkPercent(this.position.y)));
4733
+ local.position = typeof this.position == 'function'? this.position(): (needResolve? jmUtils.clone(this.position) : this.position);
4734
+ local.center = this.center && typeof this.center === 'function'?this.center(): (needResolve? jmUtils.clone(this.center) : this.center);//中心
4735
+ local.start = this.start && typeof this.start === 'function'?this.start(): (needResolve? jmUtils.clone(this.start) : this.start);//起点
4736
+ local.end = this.end && typeof this.end === 'function'?this.end(): (needResolve? jmUtils.clone(this.end) : this.end);//起点
4094
4737
  local.radius = this.radius;//半径
4095
4738
  local.width = this.width;
4096
4739
  local.height = this.height;
4097
4740
 
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);
4741
+ const margin = this.style.margin;
4742
+ const marginObj = needResolve && margin ? jmUtils.clone(margin, {}) : (margin || {});
4743
+ marginObj.left = (marginObj.left || 0);
4744
+ marginObj.top = (marginObj.top || 0);
4745
+ marginObj.right = (marginObj.right || 0);
4746
+ marginObj.bottom = (marginObj.bottom || 0);
4103
4747
 
4104
4748
  //如果没有指定位置,但指定了margin。则位置取margin偏移量
4105
4749
  if(local.position) {
@@ -4107,8 +4751,8 @@ class jmControl extends jmProperty {
4107
4751
  local.top = local.position.y;
4108
4752
  }
4109
4753
  else {
4110
- local.left = margin.left;
4111
- local.top = margin.top;
4754
+ local.left = marginObj.left;
4755
+ local.top = marginObj.top;
4112
4756
  }
4113
4757
 
4114
4758
  if(this.parent) {
@@ -4370,22 +5014,31 @@ class jmControl extends jmProperty {
4370
5014
  if(this.webglControl) this.webglControl.closePath();
4371
5015
  this.context.closePath && this.context.closePath();
4372
5016
  }
4373
-
4374
- const fill = this.style['fill'] || this.style['fillStyle'];
4375
- if(fill) {
4376
- if(this.webglControl) {
5017
+
5018
+ // 根据渲染模式选择不同的绘制路径
5019
+ if(this.webglControl) {
5020
+ // WebGL 模式:使用 WebGL 绘制
5021
+ const fill = this.style['fill'] || this.style['fillStyle'];
5022
+ if(fill) {
4377
5023
  const bounds = this.getBounds();
4378
5024
  this.webglControl.fill(bounds);
4379
5025
  }
4380
- this.context.fill && this.context.fill();
5026
+ if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
5027
+ this.webglControl.stroke();
5028
+ }
5029
+ if(this.webglControl.endDraw) this.webglControl.endDraw();
4381
5030
  }
4382
- if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
4383
- if(this.webglControl) this.webglControl.stroke();
4384
- this.context.stroke && this.context.stroke();
5031
+ else {
5032
+ // 2D 模式:使用 Canvas 2D API 绘制
5033
+ const fill = this.style['fill'] || this.style['fillStyle'];
5034
+ if(fill) {
5035
+ this.context.fill && this.context.fill();
5036
+ }
5037
+ if(this.style['stroke'] || (!fill && !this.is('jmGraph'))) {
5038
+ this.context.stroke && this.context.stroke();
5039
+ }
4385
5040
  }
4386
5041
 
4387
- if(this.webglControl && this.webglControl.endDraw) this.webglControl.endDraw();
4388
-
4389
5042
  this.needUpdate = false;
4390
5043
  }
4391
5044
 
@@ -4401,9 +5054,7 @@ class jmControl extends jmProperty {
4401
5054
  const bounds = this.parent && this.parent.absoluteBounds?this.parent.absoluteBounds:this.absoluteBounds;
4402
5055
  if(this.webglControl) {
4403
5056
  this.webglControl.setParentBounds(bounds);
4404
- this.webglControl.draw([
4405
- ...this.points
4406
- ]);
5057
+ this.webglControl.draw(this.points);
4407
5058
  }
4408
5059
  else if(this.context && this.context.moveTo) {
4409
5060
  this.context.moveTo(this.points[0].x + bounds.left,this.points[0].y + bounds.top);
@@ -4448,9 +5099,44 @@ class jmControl extends jmProperty {
4448
5099
 
4449
5100
  this.setStyle();//设定样式
4450
5101
 
4451
- if(needDraw && this.beginDraw) this.beginDraw();
4452
- if(needDraw && this.draw) this.draw();
4453
- if(needDraw && this.endDraw) this.endDraw();
5102
+ // 应用mask遮罩效果:在mask区域内绘制当前控件
5103
+ // 使用 destination-in 合成模式,只保留mask区域内的内容
5104
+ const maskStyle = this.style.mask || this.__mask;
5105
+ if(maskStyle && maskStyle.points && this.context.globalCompositeOperation) {
5106
+ // 先绘制当前控件
5107
+ if(needDraw && this.beginDraw) this.beginDraw();
5108
+ if(needDraw && this.draw) this.draw();
5109
+ if(needDraw && this.endDraw) this.endDraw();
5110
+
5111
+ // 再应用mask裁剪
5112
+ this.context.globalCompositeOperation = 'destination-in';
5113
+ if(maskStyle.initPoints) maskStyle.initPoints();
5114
+ const mBounds = maskStyle.parent && maskStyle.parent.absoluteBounds ? maskStyle.parent.absoluteBounds : this.absoluteBounds;
5115
+ this.context.beginPath();
5116
+ if(maskStyle.points && maskStyle.points.length > 0) {
5117
+ this.context.moveTo(maskStyle.points[0].x + (mBounds ? mBounds.left : 0), maskStyle.points[0].y + (mBounds ? mBounds.top : 0));
5118
+ for(let i = 1; i < maskStyle.points.length; i++) {
5119
+ if(maskStyle.points[i].m) {
5120
+ this.context.moveTo(maskStyle.points[i].x + (mBounds ? mBounds.left : 0), maskStyle.points[i].y + (mBounds ? mBounds.top : 0));
5121
+ }
5122
+ else {
5123
+ this.context.lineTo(maskStyle.points[i].x + (mBounds ? mBounds.left : 0), maskStyle.points[i].y + (mBounds ? mBounds.top : 0));
5124
+ }
5125
+ }
5126
+ if(maskStyle.style && maskStyle.style.close) {
5127
+ this.context.closePath();
5128
+ }
5129
+ }
5130
+ this.context.fillStyle = '#ffffff';
5131
+ this.context.fill();
5132
+ // 恢复合成模式
5133
+ this.context.globalCompositeOperation = 'source-over';
5134
+ }
5135
+ else {
5136
+ if(needDraw && this.beginDraw) this.beginDraw();
5137
+ if(needDraw && this.draw) this.draw();
5138
+ if(needDraw && this.endDraw) this.endDraw();
5139
+ }
4454
5140
 
4455
5141
  if(this.children) {
4456
5142
  this.children.each(function(i,item) {
@@ -4540,7 +5226,15 @@ class jmControl extends jmProperty {
4540
5226
  * @param {array} args 事件参数数组
4541
5227
  */
4542
5228
  emit(...args) {
4543
- this.runEventHandle(args[0], args.slice(1));
5229
+ // 避免每帧 args.slice(1) 分配临时数组
5230
+ // runEventHandle 内部会把非数组参数包装成数组
5231
+ if(args.length > 2) {
5232
+ this.runEventHandle(args[0], args.slice(1));
5233
+ } else if(args.length === 2) {
5234
+ this.runEventHandle(args[0], [args[1]]);
5235
+ } else {
5236
+ this.runEventHandle(args[0], []);
5237
+ }
4544
5238
  return this;
4545
5239
  }
4546
5240
 
@@ -5180,7 +5874,6 @@ class jmArc extends jmPath {
5180
5874
  if((mw == 0 && mh == 0) || start == end) return;
5181
5875
 
5182
5876
  let anticlockwise = this.anticlockwise;
5183
- this.points = [];
5184
5877
  let step = 1 / Math.max(mw, mh);
5185
5878
 
5186
5879
  //如果是逆时针绘制,则角度为负数,并且结束角为2Math.PI-end
@@ -5191,18 +5884,33 @@ class jmArc extends jmPath {
5191
5884
  }
5192
5885
  if(start > end) step = -step;
5193
5886
 
5194
- if(this.isFan) this.points.push(location.center);// 如果是扇形,则从中心开始画
5887
+ // 预计算需要的点数量
5888
+ let pointCount = Math.ceil(Math.abs(end - start) / Math.abs(step)) + 1;
5889
+ if(this.isFan) pointCount++;
5890
+
5891
+ // 复用已有数组,避免每帧分配;大小变化时才重建
5892
+ if(!this.points || this.points.length !== pointCount) {
5893
+ this.points = new Array(pointCount);
5894
+ for(let i = 0; i < pointCount; i++) {
5895
+ this.points[i] = { x: 0, y: 0 };
5896
+ }
5897
+ }
5898
+
5899
+ let idx = 0;
5900
+ if(this.isFan) {
5901
+ this.points[idx].x = location.center.x;
5902
+ this.points[idx].y = location.center.y;
5903
+ idx++;
5904
+ }
5195
5905
 
5196
5906
  //椭圆方程x=a*cos(r) ,y=b*sin(r)
5197
5907
  for(let r=start;;r += step) {
5198
5908
  if(step > 0 && r > end) r = end;
5199
5909
  else if(step < 0 && r < end) r = end;
5200
5910
 
5201
- const p = {
5202
- x : Math.cos(r) * mw + cx,
5203
- y : Math.sin(r) * mh + cy
5204
- };
5205
- this.points.push(p);
5911
+ this.points[idx].x = Math.cos(r) * mw + cx;
5912
+ this.points[idx].y = Math.sin(r) * mh + cy;
5913
+ idx++;
5206
5914
 
5207
5915
  if(r == end) break;
5208
5916
  }
@@ -5639,8 +6347,8 @@ class jmHArc extends jmArc {
5639
6347
 
5640
6348
  maxps.reverse();//大圆逆序
5641
6349
  if(!this.style || !this.style.close) {
5642
- maxps[0].m = true;//开始画大圆时表示为移动
5643
- }
6350
+ maxps[0].m = true;//非闭合时标记 moveTo,分隔内外两个子路径
6351
+ }
5644
6352
  this.points = minps.concat(maxps);
5645
6353
  }
5646
6354
  }
@@ -5801,6 +6509,7 @@ class jmPrismatic extends jmPath {
5801
6509
  * @class jmRect
5802
6510
  * @extends jmPath
5803
6511
  * @param {object} params 参数 position=矩形左上角顶点坐标,width=宽,height=高,radius=边角弧度
6512
+ * radius支持数字(四角相同)或对象 { topLeft, topRight, bottomRight, bottomLeft }
5804
6513
  */
5805
6514
  class jmRect extends jmPath {
5806
6515
 
@@ -5810,12 +6519,24 @@ class jmRect extends jmPath {
5810
6519
  super(params, t);
5811
6520
 
5812
6521
  this.style.close = true;
5813
- this.radius = params.radius || this.style.radius || 0;
6522
+ const r = params.radius || this.style.radius || this.style.borderRadius || 0;
6523
+ if(typeof r === 'object' && r !== null) {
6524
+ // 四角独立圆角
6525
+ this.radius = {
6526
+ topLeft: Number(r.topLeft) || 0,
6527
+ topRight: Number(r.topRight) || 0,
6528
+ bottomRight: Number(r.bottomRight) || 0,
6529
+ bottomLeft: Number(r.bottomLeft) || 0
6530
+ };
6531
+ }
6532
+ else {
6533
+ this.radius = r;
6534
+ }
5814
6535
  }
5815
6536
  /**
5816
- * 圆角半径
6537
+ * 圆角半径,支持数字或四角独立对象
5817
6538
  * @property radius
5818
- * @type {number}
6539
+ * @type {number|object}
5819
6540
  */
5820
6541
  get radius() {
5821
6542
  return this.property('radius');
@@ -5823,6 +6544,36 @@ class jmRect extends jmPath {
5823
6544
  set radius(v) {
5824
6545
  this.needUpdate = true;
5825
6546
  return this.property('radius', v);
6547
+ }
6548
+
6549
+ /**
6550
+ * 获取规范化的圆角值(四角独立)
6551
+ * @returns {object} { topLeft, topRight, bottomRight, bottomLeft }
6552
+ */
6553
+ getNormalizedRadius() {
6554
+ const r = this.radius;
6555
+ if(typeof r === 'number') {
6556
+ const v = Math.max(0, r);
6557
+ return { topLeft: v, topRight: v, bottomRight: v, bottomLeft: v };
6558
+ }
6559
+ if(typeof r === 'object' && r !== null) {
6560
+ return {
6561
+ topLeft: Math.max(0, Number(r.topLeft) || 0),
6562
+ topRight: Math.max(0, Number(r.topRight) || 0),
6563
+ bottomRight: Math.max(0, Number(r.bottomRight) || 0),
6564
+ bottomLeft: Math.max(0, Number(r.bottomLeft) || 0)
6565
+ };
6566
+ }
6567
+ return { topLeft: 0, topRight: 0, bottomRight: 0, bottomLeft: 0 };
6568
+ }
6569
+
6570
+ /**
6571
+ * 检查是否有圆角
6572
+ * @returns {boolean}
6573
+ */
6574
+ hasRadius() {
6575
+ const nr = this.getNormalizedRadius();
6576
+ return nr.topLeft > 0 || nr.topRight > 0 || nr.bottomRight > 0 || nr.bottomLeft > 0;
5826
6577
  }
5827
6578
 
5828
6579
  /**
@@ -5885,7 +6636,7 @@ class jmRect extends jmPath {
5885
6636
 
5886
6637
  /**
5887
6638
  * 初始化图形点
5888
- * 如果有边角弧度则类型圆绝计算其描点
6639
+ * 支持四角独立圆角,借助圆弧对象计算描点
5889
6640
  *
5890
6641
  * @method initPoints
5891
6642
  * @private
@@ -5902,32 +6653,67 @@ class jmRect extends jmPath {
5902
6653
  this.dottedLine = this.graph.createShape(jmLine, {style: this.style});
5903
6654
  }
5904
6655
 
5905
- //如果有边界弧度则借助圆弧对象计算描点
5906
- if(location.radius && location.radius < location.width/2 && location.radius < location.height/2) {
6656
+ const nr = this.getNormalizedRadius();
6657
+ const hasRadius = this.hasRadius();
6658
+
6659
+ // 如果有圆角(支持四角独立),借助圆弧对象计算描点
6660
+ if(hasRadius) {
5907
6661
  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();
6662
+
6663
+ // 限制圆角不超过短边的一半
6664
+ const maxR = Math.min(location.width / 2, location.height / 2);
6665
+ const rtl = Math.min(nr.topLeft, maxR);
6666
+ const rtr = Math.min(nr.topRight, maxR);
6667
+ const rbr = Math.min(nr.bottomRight, maxR);
6668
+ const rbl = Math.min(nr.bottomLeft, maxR);
6669
+
6670
+ // 左上角圆弧
6671
+ if(rtl > 0) {
6672
+ let arc = this.graph.createShape(jmArc,{radius:rtl,anticlockwise:false});
6673
+ arc.center = {x:location.left + rtl, y:location.top + rtl};
6674
+ arc.startAngle = Math.PI;
6675
+ arc.endAngle = Math.PI + q;
6676
+ var ps1 = arc.initPoints();
6677
+ }
6678
+ else {
6679
+ var ps1 = [p1];
6680
+ }
6681
+
6682
+ // 右上角圆弧
6683
+ if(rtr > 0) {
6684
+ let arc = this.graph.createShape(jmArc,{radius:rtr,anticlockwise:false});
6685
+ arc.center = {x:p2.x - rtr, y:p2.y + rtr};
6686
+ arc.startAngle = Math.PI + q;
6687
+ arc.endAngle = Math.PI * 2;
6688
+ var ps2 = arc.initPoints();
6689
+ }
6690
+ else {
6691
+ var ps2 = [p2];
6692
+ }
6693
+
6694
+ // 右下角圆弧
6695
+ if(rbr > 0) {
6696
+ let arc = this.graph.createShape(jmArc,{radius:rbr,anticlockwise:false});
6697
+ arc.center = {x:p3.x - rbr, y:p3.y - rbr};
6698
+ arc.startAngle = 0;
6699
+ arc.endAngle = q;
6700
+ var ps3 = arc.initPoints();
6701
+ }
6702
+ else {
6703
+ var ps3 = [p3];
6704
+ }
6705
+
6706
+ // 左下角圆弧
6707
+ if(rbl > 0) {
6708
+ let arc = this.graph.createShape(jmArc,{radius:rbl,anticlockwise:false});
6709
+ arc.center = {x:p4.x + rbl, y:p4.y - rbl};
6710
+ arc.startAngle = q;
6711
+ arc.endAngle = Math.PI;
6712
+ var ps4 = arc.initPoints();
6713
+ }
6714
+ else {
6715
+ var ps4 = [p4];
6716
+ }
5931
6717
  this.points = ps1.concat(ps2,ps3,ps4);
5932
6718
  }
5933
6719
  else {
@@ -6266,7 +7052,7 @@ class jmLabel extends jmControl {
6266
7052
  this.style.textAlign = this.style.textAlign || 'left';
6267
7053
  //文字垂直对齐
6268
7054
  this.style.textBaseline = this.style.textBaseline || 'middle';
6269
- this.text = params.text || '';
7055
+ this.text = params.text || params.value || '';
6270
7056
 
6271
7057
  this.center = params.center || null;
6272
7058
  }
@@ -6555,67 +7341,6 @@ class jmLabel extends jmControl {
6555
7341
  }
6556
7342
  }
6557
7343
  }
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
7344
  }
6620
7345
 
6621
7346
  endDraw() {
@@ -7564,145 +8289,6 @@ class jmKeyEvent {
7564
8289
  }
7565
8290
  }
7566
8291
 
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
8292
  /**
7707
8293
  * jmGraph画图类库
7708
8294
  * 对canvas画图api进行二次封装,使其更易调用,省去很多重复的工作。
@@ -7714,7 +8300,7 @@ class jmLayer extends jmControl {
7714
8300
  * @param {object} option 参数:{width:宽,height:高}
7715
8301
  * @param {function} callback 初始化后的回调
7716
8302
  */
7717
- class jmGraph$1 extends jmControl {
8303
+ class jmGraph extends jmControl {
7718
8304
 
7719
8305
  constructor(canvas, option, callback) {
7720
8306
  if(typeof option == 'function') {
@@ -7780,8 +8366,6 @@ class jmGraph$1 extends jmControl {
7780
8366
  else {
7781
8367
  this.context = canvas.getContext(this.mode);
7782
8368
  }
7783
-
7784
- this.textureCanvas = option.textureCanvas || null;
7785
8369
 
7786
8370
  // webgl模式
7787
8371
  if(this.mode === 'webgl') {
@@ -7932,7 +8516,7 @@ class jmGraph$1 extends jmControl {
7932
8516
  * @return {jmGraph} jmGraph实例对象
7933
8517
  */
7934
8518
  static create(...args) {
7935
- return new jmGraph$1(...args);
8519
+ return new jmGraph(...args);
7936
8520
  }
7937
8521
 
7938
8522
  /**
@@ -7981,9 +8565,7 @@ class jmGraph$1 extends jmControl {
7981
8565
  if(shape) {
7982
8566
  if(!args) args = {};
7983
8567
  args.graph = this;
7984
- let obj = new shape(args);
7985
- // 添加到活动图层
7986
- this.addShapeToLayer(obj);
8568
+ const obj = new shape(args);
7987
8569
  return obj;
7988
8570
  }
7989
8571
  }
@@ -8109,13 +8691,19 @@ class jmGraph$1 extends jmControl {
8109
8691
  this.context.clearRect(0, 0, w, h);
8110
8692
  }
8111
8693
  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); // 设置清空颜色缓冲时的颜色值
8694
+ // 缓存 clearColor 对象,避免每帧创建
8695
+ if(this.style && this.style.fill) {
8696
+ const color = this.utils.hexToRGBA(this.style.fill);
8697
+ this.__lastClearColor = color;
8698
+ this.context.clearColor(color.r, color.g, color.b, color.a);
8699
+ }
8700
+ else if(!this.__lastClearColor) {
8701
+ this.__lastClearColor = { r: 0, g: 0, b: 0, a: 0 };
8702
+ this.context.clearColor(0, 0, 0, 0);
8703
+ }
8704
+ else {
8705
+ this.context.clearColor(this.__lastClearColor.r, this.__lastClearColor.g, this.__lastClearColor.b, this.__lastClearColor.a);
8706
+ }
8119
8707
  this.context.clear(this.context.COLOR_BUFFER_BIT); // 清空颜色缓冲区,也就是清空画布
8120
8708
  }
8121
8709
  }
@@ -8296,240 +8884,14 @@ class jmGraph$1 extends jmControl {
8296
8884
  *
8297
8885
  * @method resetTransform
8298
8886
  * @return {jmGraph} 返回当前实例,支持链式调用
8299
- */
8300
- resetTransform() {
8301
- this.scaleFactor = 1;
8302
- this.translation = {x: 0, y: 0};
8303
- this.needUpdate = true;
8304
- this.redraw();
8305
-
8306
- return this; // 支持链式调用
8307
- }
8308
-
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
- }
8887
+ */
8888
+ resetTransform() {
8889
+ this.scaleFactor = 1;
8890
+ this.translation = {x: 0, y: 0};
8891
+ this.needUpdate = true;
8892
+ this.redraw();
8531
8893
 
8532
- return this;
8894
+ return this; // 支持链式调用
8533
8895
  }
8534
8896
 
8535
8897
  /**
@@ -8598,48 +8960,27 @@ class jmGraph$1 extends jmControl {
8598
8960
  }
8599
8961
 
8600
8962
  /**
8601
- * 转换为SVG字符串
8602
- * 遍历所有图层和形状,生成SVG标记
8603
- *
8963
+ * 遍历所有形状,生成SVG标记
8964
+ *
8604
8965
  * @method toSVG
8605
8966
  * @return {string} SVG字符串
8606
8967
  */
8607
8968
  toSVG() {
8608
8969
  // SVG头部,包含命名空间和画布尺寸
8609
8970
  let svg = `<svg width="${this.width}" height="${this.height}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${this.width} ${this.height}">`;
8610
-
8971
+
8611
8972
  // 添加背景色(如果有)
8612
8973
  if(this.style && this.style.fill) {
8613
8974
  svg += `<rect width="100%" height="100%" fill="${this.style.fill}"/>`;
8614
8975
  }
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
-
8976
+
8977
+ // 遍历所有直接添加的形状
8978
+ this.children.each((i, shape) => {
8979
+ if(shape.toSVG) {
8980
+ svg += shape.toSVG();
8981
+ }
8982
+ });
8983
+
8643
8984
  svg += '</svg>';
8644
8985
  return svg;
8645
8986
  }
@@ -8689,7 +9030,7 @@ class jmGraph$1 extends jmControl {
8689
9030
  // 触发刷新事件
8690
9031
  self.emit('update', time);
8691
9032
 
8692
- self.__requestAnimationFrameFunHandler && self.cancelAnimationFrame(self.__requestAnimationFrameFunHandler);
9033
+ // 直接 requestAnimationFrame,无需先 cancel
8693
9034
  self.__requestAnimationFrameFunHandler = self.requestAnimationFrame(update);
8694
9035
  if(callback) callback();
8695
9036
  }
@@ -8724,41 +9065,25 @@ const shapes = {
8724
9065
  "star": jmStar
8725
9066
  };
8726
9067
 
8727
- class jmGraph extends jmGraph$1 {
9068
+ class jmGraphImpl extends jmGraph {
8728
9069
  constructor(canvas, option, callback) {
8729
-
8730
- const targetType = new.target;
8731
-
8732
9070
  // 合并shapes
8733
9071
  option = Object.assign({}, option);
8734
9072
  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
9073
 
8744
9074
  if(typeof option == 'function') {
8745
9075
  callback = option;
8746
9076
  option = {};
8747
- }
9077
+ }
8748
9078
 
8749
9079
  super(canvas, option, callback);
8750
9080
  }
8751
9081
 
8752
9082
  static create(...args) {
8753
- return createJmGraph(...args);
9083
+ return new jmGraphImpl(...args);
8754
9084
  }
8755
9085
  }
8756
9086
 
8757
- //创建实例
8758
- const createJmGraph = (...args) => {
8759
- return new jmGraph(...args);
8760
- };
8761
-
8762
9087
  /**
8763
9088
  * 基础样式
8764
9089
  *
@@ -9960,13 +10285,16 @@ class jmAxis extends jmArrowLine {
9960
10285
  clear() {
9961
10286
  this._min = null;
9962
10287
  this._max = null;
10288
+ this._width = null; // 清除宽度缓存
9963
10289
  this.children.each((i, c) => {
9964
10290
  c.remove();
9965
10291
  }, true);
9966
10292
  // 清空栅格线
9967
- this.gridLines && this.gridLines.forEach(line => {
9968
- line.remove();
9969
- });
10293
+ if (this.gridLines && this.gridLines.length) {
10294
+ for (let i = 0; i < this.gridLines.length; i++) {
10295
+ this.gridLines[i].remove();
10296
+ }
10297
+ }
9970
10298
  this.labels && this.labels.forEach(label => {
9971
10299
  label.remove();
9972
10300
  });
@@ -10178,7 +10506,9 @@ var utils = {
10178
10506
  }
10179
10507
  };
10180
10508
 
10181
- const ANIMATION_DATA_THRESHOLD$7 = 100;
10509
+ // 公共常量提取到基类,避免每个子类重复定义
10510
+ const ANIMATION_DATA_THRESHOLD = 100;
10511
+ const DEFAULT_ANIMATION_COUNT = 10;
10182
10512
 
10183
10513
  /**
10184
10514
  * 图表系列基类
@@ -10297,14 +10627,20 @@ class jmSeries extends jmPath {
10297
10627
  */
10298
10628
  initDataPoint(...args) {
10299
10629
  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
- });
10630
+ if (this.enableAnimate && this.data && this.data.length < ANIMATION_DATA_THRESHOLD) {
10631
+ // 仅在数据引用变化时才做深度比较
10632
+ if (this._lastData !== this.data) {
10633
+ this.lastPoints = this.graph.utils.clone(this.dataPoints, null, true, obj => {
10634
+ if (obj instanceof jmControl) return obj;
10635
+ });
10636
+ this.dataPoints = this.createPoints(...args);
10637
+ dataChanged = utils.arrayIsChange(this.lastPoints, this.dataPoints, (s, t) => {
10638
+ return s.x === t.x && s.y === t.y;
10639
+ });
10640
+ this._lastData = this.data;
10641
+ } else {
10642
+ this.dataPoints = this.createPoints(...args);
10643
+ }
10308
10644
  if (dataChanged) {
10309
10645
  this.___animateCounter = 0;
10310
10646
  }
@@ -10437,7 +10773,7 @@ class jmSeries extends jmPath {
10437
10773
  xValue: xv,
10438
10774
  xLabel: xv,
10439
10775
  points: [],
10440
- style: this.graph.utils.clone(this.style)
10776
+ style: this.style // 共享引用,避免深拷贝;子类如需修改应自行创建副本
10441
10777
  };
10442
10778
  p.x = xstep * i + this.xAxis.labelStart;
10443
10779
  for (let j = 0; j < fields.length; j++) {
@@ -10603,9 +10939,6 @@ class jmSeries extends jmPath {
10603
10939
  }
10604
10940
  }
10605
10941
 
10606
- const ANIMATION_DATA_THRESHOLD$6 = 100;
10607
- const DEFAULT_ANIMATION_COUNT$6 = 10;
10608
-
10609
10942
  /**
10610
10943
  * 柱图
10611
10944
  *
@@ -10635,16 +10968,18 @@ class jmBarSeries extends jmSeries {
10635
10968
  const len = points.length;
10636
10969
  if (!len) return;
10637
10970
  this.initWidth(len);
10638
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$6;
10971
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
10639
10972
  let aniIsEnd = true;
10640
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$6;
10973
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
10641
10974
  for (let i = 0; i < len; i++) {
10642
10975
  const point = points[i];
10643
10976
  if (typeof point.y === 'undefined' || point.y === null) {
10644
10977
  continue;
10645
10978
  }
10646
- point.style.fill = this.getColor(point);
10647
- const sp = this.addShape(this.graph.createPath(null, point.style));
10979
+ const style = Object.assign({}, point.style, {
10980
+ fill: this.getColor(point)
10981
+ });
10982
+ const sp = this.addShape(this.graph.createPath(null, style));
10648
10983
  const p1 = {
10649
10984
  x: point.x - this.barTotalWidth / 2 + this.barWidth * this.barIndex,
10650
10985
  y: this.baseY
@@ -10779,16 +11114,20 @@ class jmStackBarSeries extends jmBarSeries {
10779
11114
  let topStartY = this.baseY;
10780
11115
  let bottomStartY = this.baseY;
10781
11116
  for (let index = 0; index < point.points.length; index++) {
10782
- const style = this.graph.utils.clone(this.style);
10783
11117
  const p = point.points[index];
10784
- if (style.color && typeof style.color === 'function') {
10785
- style.fill = style.color.call(this, {
11118
+ let fillColor;
11119
+ if (this.style.color && typeof this.style.color === 'function') {
11120
+ fillColor = this.style.color.call(this, {
10786
11121
  index,
10787
11122
  point: p
10788
11123
  });
10789
11124
  } else {
10790
- style.fill = this.graph.getColor(index);
11125
+ fillColor = this.graph.getColor(index);
10791
11126
  }
11127
+ // 使用浅拷贝而非深拷贝
11128
+ const style = Object.assign({}, this.style, {
11129
+ fill: fillColor
11130
+ });
10792
11131
  const sp = this.addShape(this.graph.createPath(null, style));
10793
11132
  let startY = topStartY;
10794
11133
  if (p.yValue < this.baseYValue) startY = bottomStartY;
@@ -10897,7 +11236,7 @@ class jmPieSeries extends jmSeries {
10897
11236
  this.totalValue = 0;
10898
11237
  //计算最大值和最小值
10899
11238
  if (this.data) {
10900
- for (const i in this.data) {
11239
+ for (let i = 0; i < this.data.length; i++) {
10901
11240
  const s = this.data[i];
10902
11241
  const vy = s[this.field];
10903
11242
  if (vy) {
@@ -10994,15 +11333,20 @@ class jmPieSeries extends jmSeries {
10994
11333
  yLabel: yv,
10995
11334
  step: Math.abs(yv / this.totalValue),
10996
11335
  // 每个数值点比
10997
- style: this.graph.utils.clone(this.style),
11336
+ style: this.style,
11337
+ // 共享引用,仅在需要修改时才拷贝
10998
11338
  anticlockwise
10999
11339
  };
11000
11340
  points.push(p);
11001
11341
  //p.style.color = this.graph.getColor(index);
11002
11342
  if (p.style.color && typeof p.style.color === 'function') {
11343
+ p.style = this.graph.utils.clone(this.style);
11003
11344
  p.style.fill = p.style.color.call(this, p);
11004
11345
  } else {
11005
- p.style.fill = this.graph.getColor(index);
11346
+ // fill 需要独立,创建轻量样式覆盖
11347
+ p.style = Object.assign({}, this.style, {
11348
+ fill: this.graph.getColor(index)
11349
+ });
11006
11350
  }
11007
11351
  const start = startAni; // 上一个扇形的结束角度为当前的起始角度
11008
11352
  // 计算当前结束角度, 同时也是下一个的起始角度
@@ -11208,11 +11552,12 @@ class jmRadarSeries extends jmSeries {
11208
11552
  const rotateStep = Math.PI * 2 / yCount;
11209
11553
 
11210
11554
  // 清空除了一个默认外的所有Y轴
11211
- for (let index in this.graph.yAxises) {
11212
- const axis = this.graph.yAxises[index];
11555
+ const yAxisKeys = Object.keys(this.graph.yAxises || {});
11556
+ for (let k = 0; k < yAxisKeys.length; k++) {
11557
+ const axis = this.graph.yAxises[yAxisKeys[k]];
11213
11558
  if (!axis || axis === this.yAxis) continue;
11214
11559
  axis.remove();
11215
- delete this.graph.yAxises[index];
11560
+ delete this.graph.yAxises[yAxisKeys[k]];
11216
11561
  }
11217
11562
  for (let index = 0; index < yCount; index++) {
11218
11563
  if (!this.field[index]) continue;
@@ -11359,21 +11704,30 @@ class jmRadarSeries extends jmSeries {
11359
11704
  const points = [];
11360
11705
  for (var i = 0; i < this.data.length; i++) {
11361
11706
  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, {
11707
+ let strokeColor;
11708
+ if (this.style.color && typeof this.style.color === 'function') {
11709
+ strokeColor = this.style.color.call(this, {
11365
11710
  data: s,
11366
11711
  index: i
11367
11712
  });
11368
11713
  } else {
11369
- style.stroke = this.graph.getColor(i);
11714
+ strokeColor = this.graph.getColor(i);
11370
11715
  }
11371
- if (typeof style.fill === 'function') {
11372
- style.fill = style.fill.call(this, style);
11716
+ let fillColor;
11717
+ if (typeof this.style.fill === 'function') {
11718
+ fillColor = this.style.fill.call(this, {
11719
+ ...this.style,
11720
+ stroke: strokeColor
11721
+ });
11373
11722
  } else {
11374
- const color = this.graph.utils.hexToRGBA(style.stroke);
11375
- style.fill = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
11723
+ const color = this.graph.utils.hexToRGBA(strokeColor);
11724
+ fillColor = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
11376
11725
  }
11726
+ // 使用浅拷贝而非深拷贝
11727
+ const style = Object.assign({}, this.style, {
11728
+ stroke: strokeColor,
11729
+ fill: fillColor
11730
+ });
11377
11731
  const shapePoints = [];
11378
11732
  for (const axis of this.axises) {
11379
11733
  if (!axis || !axis.field) continue;
@@ -11467,9 +11821,6 @@ jmRadarSeries.prototype.createLegend = function (point) {
11467
11821
  });
11468
11822
  };
11469
11823
 
11470
- const ANIMATION_DATA_THRESHOLD$5 = 100;
11471
- const DEFAULT_ANIMATION_COUNT$5 = 10;
11472
-
11473
11824
  /**
11474
11825
  * 线图
11475
11826
  *
@@ -11501,9 +11852,9 @@ class jmLineSeries extends jmSeries {
11501
11852
  if (!len) return;
11502
11853
  this.style.stroke = this.style.color;
11503
11854
  this.style.item.stroke = this.style.color;
11504
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD$5;
11855
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
11505
11856
  let shapePoints = [];
11506
- const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT$5;
11857
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
11507
11858
  const aniStep = Math.floor(len / aniCount) || 1;
11508
11859
  for (let i = 0; i < len; i++) {
11509
11860
  const p = points[i];
@@ -11886,7 +12237,6 @@ class jmCandlestickSeries extends jmSeries {
11886
12237
  console.warn('K线图数据不完整,需要4个字段(开盘、收盘、最高、最低)');
11887
12238
  continue;
11888
12239
  }
11889
- const sp = this.addShape(this.graph.createPath([], p.style));
11890
12240
  const bl = {
11891
12241
  x: p.x - w,
11892
12242
  y: p.points[0].y
@@ -11905,14 +12255,18 @@ class jmCandlestickSeries extends jmSeries {
11905
12255
  };
11906
12256
  let tm = p.points[1];
11907
12257
  let bm = p.points[0];
11908
- p.style.stroke = p.style.fill = p.style.masculineColor || 'red';
12258
+
12259
+ // 使用浅拷贝避免污染共享引用
12260
+ const style = Object.assign({}, p.style);
12261
+ style.stroke = style.fill = style.masculineColor || 'red';
11909
12262
  if (p.points[0].yValue > p.points[1].yValue) {
11910
- p.style.stroke = p.style.fill = p.style.negativeColor || 'green';
12263
+ style.stroke = style.fill = style.negativeColor || 'green';
11911
12264
  bl.y = br.y = p.points[1].y;
11912
12265
  tl.y = tr.y = p.points[0].y;
11913
12266
  tm = p.points[0];
11914
12267
  bm = p.points[1];
11915
12268
  }
12269
+ const sp = this.addShape(this.graph.createPath([], style));
11916
12270
  sp.points.push(p.points[2], tm, tl, bl, bm, p.points[3], bm, br, tr, tm, p.points[2]);
11917
12271
  this.emit('onPointCreated', p);
11918
12272
  }
@@ -11937,9 +12291,6 @@ class jmCandlestickSeries extends jmSeries {
11937
12291
  }
11938
12292
  }
11939
12293
 
11940
- const ANIMATION_DATA_THRESHOLD$4 = 100;
11941
- const DEFAULT_ANIMATION_COUNT$4 = 10;
11942
-
11943
12294
  /**
11944
12295
  * 散点图
11945
12296
  *
@@ -11971,8 +12322,8 @@ class jmScatterSeries extends jmSeries {
11971
12322
  if (!len) return;
11972
12323
  this.style.stroke = this.style.color;
11973
12324
  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;
12325
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
12326
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
11976
12327
  const aniStep = Math.floor(len / aniCount) || 1;
11977
12328
  for (let i = 0; i < len; i++) {
11978
12329
  const p = points[i];
@@ -12045,9 +12396,6 @@ class jmScatterSeries extends jmSeries {
12045
12396
  }
12046
12397
  }
12047
12398
 
12048
- const ANIMATION_DATA_THRESHOLD$3 = 100;
12049
- const DEFAULT_ANIMATION_COUNT$3 = 10;
12050
-
12051
12399
  /**
12052
12400
  * 气泡图
12053
12401
  *
@@ -12079,8 +12427,8 @@ class jmBubbleSeries extends jmSeries {
12079
12427
  if (!len) return;
12080
12428
  this.style.stroke = this.style.color;
12081
12429
  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;
12430
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
12431
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
12084
12432
  const aniStep = Math.floor(len / aniCount) || 1;
12085
12433
  for (let i = 0; i < len; i++) {
12086
12434
  const p = points[i];
@@ -12159,9 +12507,6 @@ class jmBubbleSeries extends jmSeries {
12159
12507
  }
12160
12508
  }
12161
12509
 
12162
- const ANIMATION_DATA_THRESHOLD$2 = 100;
12163
- const DEFAULT_ANIMATION_COUNT$2 = 10;
12164
-
12165
12510
  /**
12166
12511
  * 热力图
12167
12512
  *
@@ -12204,8 +12549,8 @@ class jmHeatmapSeries extends jmSeries {
12204
12549
  }
12205
12550
  this.minValue = minValue;
12206
12551
  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;
12552
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
12553
+ const aniCount = this.style.aniCount || DEFAULT_ANIMATION_COUNT;
12209
12554
  const aniStep = Math.floor(len / aniCount) || 1;
12210
12555
  for (let i = 0; i < len; i++) {
12211
12556
  const p = points[i];
@@ -12572,20 +12917,15 @@ class jmGaugeSeries extends jmSeries {
12572
12917
  }
12573
12918
  }
12574
12919
 
12575
- const ANIMATION_DATA_THRESHOLD$1 = 100;
12576
- const DEFAULT_ANIMATION_COUNT$1 = 10;
12577
-
12578
12920
  /**
12579
12921
  * 面积图
12922
+ * 继承自折线图,默认启用区域填充
12580
12923
  *
12581
12924
  * @class jmAreaSeries
12582
12925
  * @module jmChart
12583
- * @param {jmChart} chart 当前图表
12584
- * @param {array} mappings 图形字段映射
12585
- * @param {style} style 样式
12926
+ * @extends jmLineSeries
12586
12927
  */
12587
-
12588
- class jmAreaSeries extends jmSeries {
12928
+ class jmAreaSeries extends jmLineSeries {
12589
12929
  constructor(options) {
12590
12930
  options.style = options.style || options.graph.style.area;
12591
12931
  super(options);
@@ -12593,193 +12933,13 @@ class jmAreaSeries extends jmSeries {
12593
12933
  this.style.area = true;
12594
12934
  }
12595
12935
 
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
12936
  /**
12774
12937
  * 生成图例
12775
- *
12776
- * @method createLegend
12777
- * @for jmAreaSeries
12778
12938
  */
12779
12939
  createLegend() {
12780
- // 生成图例前的图标
12781
12940
  var style = this.graph.utils.clone(this.style);
12782
12941
  style.stroke = style.color;
12942
+ style.fill = style.color;
12783
12943
  var shape = this.graph.createShape('path', {
12784
12944
  style: style
12785
12945
  });
@@ -12801,8 +12961,7 @@ class jmAreaSeries extends jmSeries {
12801
12961
  y: 0
12802
12962
  };
12803
12963
  this.__bezier = this.__bezier || this.graph.createShape('bezier');
12804
- this.__bezier.cpoints = [p1, p2, p3, p4]; //设置控制点
12805
-
12964
+ this.__bezier.cpoints = [p1, p2, p3, p4];
12806
12965
  shape.points = this.__bezier.initPoints();
12807
12966
  } else {
12808
12967
  shape.points = [{
@@ -12817,8 +12976,6 @@ class jmAreaSeries extends jmSeries {
12817
12976
  }
12818
12977
  }
12819
12978
 
12820
- const ANIMATION_DATA_THRESHOLD = 100;
12821
- const DEFAULT_ANIMATION_COUNT = 10;
12822
12979
  class jmWaterfallSeries extends jmSeries {
12823
12980
  constructor(options) {
12824
12981
  options.style = options.style || options.graph.style.waterfall;
@@ -14251,12 +14408,11 @@ class jmMarkLineManager {
14251
14408
  if (chart.style.markLine.longtap) {
14252
14409
  longtap = 1;
14253
14410
  longtapHandler && graph.utils.cancelAnimationFrame(longtapHandler);
14254
- let tapStartTime = Date.now();
14411
+ const tapStartTime = Date.now();
14255
14412
  const reqFun = () => {
14256
- const elapsed = Date.now() - tapStartTime;
14257
14413
  if (longtap === 1 || longtap === 2) {
14258
14414
  // 如果还未过一定时间,则继续等待
14259
- if (elapsed < 500) {
14415
+ if (Date.now() - tapStartTime < 500) {
14260
14416
  longtapHandler = graph.utils.requestAnimationFrame(reqFun);
14261
14417
  return;
14262
14418
  }
@@ -14267,9 +14423,6 @@ class jmMarkLineManager {
14267
14423
  };
14268
14424
  // 如果一定时间后还没有取消,则表示长按了
14269
14425
  longtapHandler = graph.utils.requestAnimationFrame(reqFun);
14270
-
14271
- //args.event.stopPropagation();
14272
- //args.event.preventDefault();// 阻止默认行为
14273
14426
  } else {
14274
14427
  this.startMove(args);
14275
14428
  }
@@ -14376,7 +14529,7 @@ class jmMarkLineManager {
14376
14529
  * @module jmChart
14377
14530
  * @param {element} container 图表容器
14378
14531
  */
14379
- class jmChart extends jmGraph {
14532
+ class jmChart extends jmGraphImpl {
14380
14533
  constructor(container, options) {
14381
14534
  options = options || {};
14382
14535
  const enableAnimate = !!options.enableAnimate;
@@ -14470,25 +14623,27 @@ class jmChart extends jmGraph {
14470
14623
 
14471
14624
  // 绑定点击事件
14472
14625
  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
- });
14626
+ if (!this.option.onClick) return;
14627
+ // 找到点击位置的数据点
14628
+ let closestPoint = null;
14629
+ let minDistance = 20; // 20px 范围,同时作为阈值,无需再比较小于阈值
14630
+
14631
+ this.series.each((i, serie) => {
14632
+ if (!serie.dataPoints) return;
14633
+ const points = serie.dataPoints;
14634
+ for (let j = 0, len = points.length; j < len; j++) {
14635
+ const point = points[j];
14636
+ const dx = args.position.x - point.x;
14637
+ const dy = args.position.y - point.y;
14638
+ const distance = dx * dx + dy * dy; // 避免开方,直接比较平方
14639
+ if (distance < minDistance * minDistance) {
14640
+ minDistance = distance;
14641
+ closestPoint = point;
14487
14642
  }
14488
- });
14489
- if (closestPoint) {
14490
- this.option.onClick(closestPoint);
14491
14643
  }
14644
+ });
14645
+ if (closestPoint) {
14646
+ this.option.onClick(closestPoint);
14492
14647
  }
14493
14648
  });
14494
14649
  }
@@ -14518,7 +14673,7 @@ class jmChart extends jmGraph {
14518
14673
  options = this.utils.clone(options, {
14519
14674
  autoRefresh: true
14520
14675
  }, true);
14521
- this.touchGraph = new jmGraph(cn, options);
14676
+ this.touchGraph = new jmGraphImpl(cn, options);
14522
14677
  this.touchGraph.chartGraph = this;
14523
14678
  this.on('propertyChange', (name, args) => {
14524
14679
  if (['width', 'height'].includes(name)) {
@@ -14558,8 +14713,9 @@ class jmChart extends jmGraph {
14558
14713
  delete this.xAxis;
14559
14714
  }
14560
14715
  if (this.yAxises) {
14561
- for (let i in this.yAxises) {
14562
- this.yAxises[i].remove();
14716
+ const keys = Object.keys(this.yAxises);
14717
+ for (let k = 0; k < keys.length; k++) {
14718
+ this.yAxises[keys[k]].remove();
14563
14719
  }
14564
14720
  delete this.yAxises;
14565
14721
  }
@@ -14571,18 +14727,9 @@ class jmChart extends jmGraph {
14571
14727
  * @param {int} index 颜色索引
14572
14728
  */
14573
14729
  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;
14730
+ const colors = this.style.chartColors;
14731
+ const colorIndex = index % colors.length;
14732
+ return colors[colorIndex];
14586
14733
  }
14587
14734
 
14588
14735
  /**
@@ -14600,40 +14747,43 @@ class jmChart extends jmGraph {
14600
14747
  this.xAxis.clear();
14601
14748
  }
14602
14749
  if (this.yAxises) {
14603
- for (let i in this.yAxises) {
14604
- this.yAxises[i].clear();
14750
+ const keys = Object.keys(this.yAxises);
14751
+ for (let k = 0; k < keys.length; k++) {
14752
+ this.yAxises[keys[k]].clear();
14605
14753
  }
14606
14754
  }
14607
14755
  this.barSeriesCount = 0;
14756
+ // 合并两次 series 遍历为一次,减少遍历开销
14757
+ const seriesInited = [];
14608
14758
  this.series.each((i, serie) => {
14609
14759
  if (!serie.style.color) {
14610
14760
  serie.style.color = serie.graph.getColor(i);
14611
14761
  }
14612
- if (serie.graph.style.layout != 'inside') {
14613
- if (serie instanceof jmBarSeries) {
14762
+ if (serie instanceof jmBarSeries) {
14763
+ if (serie.graph.style.layout != 'inside') {
14614
14764
  serie.graph.style.layout = 'inside';
14615
14765
  }
14616
- }
14617
- if (serie instanceof jmBarSeries) {
14618
14766
  serie.barIndex = serie.graph.barSeriesCount;
14619
14767
  serie.graph.barSeriesCount++;
14620
14768
  }
14621
14769
  serie.reset();
14770
+ seriesInited.push(serie);
14622
14771
  });
14623
14772
  if (this.legend) {
14624
14773
  this.legend.reset();
14625
14774
  }
14626
14775
  if (this.yAxises) {
14627
- for (let i in this.yAxises) {
14628
- this.yAxises[i].reset();
14776
+ const keys = Object.keys(this.yAxises);
14777
+ for (let k = 0; k < keys.length; k++) {
14778
+ this.yAxises[keys[k]].reset();
14629
14779
  }
14630
14780
  }
14631
14781
  if (this.xAxis) {
14632
14782
  this.xAxis.reset();
14633
14783
  }
14634
- this.series.each((i, serie) => {
14635
- serie.init && serie.init();
14636
- });
14784
+ for (let i = 0; i < seriesInited.length; i++) {
14785
+ seriesInited[i].init && seriesInited[i].init();
14786
+ }
14637
14787
  }
14638
14788
 
14639
14789
  /**
@@ -14811,14 +14961,35 @@ var vchart = {
14811
14961
  this.initChart();
14812
14962
  },
14813
14963
  updated() {
14814
- this.initChart();
14964
+ this.refresh();
14815
14965
  },
14816
14966
  destroyed() {
14817
14967
  this.chartInstance && this.chartInstance.destroy();
14818
14968
  },
14819
14969
  watch: {
14820
- chartData: function (newData, oldData) {
14821
- this.refresh();
14970
+ chartData: {
14971
+ handler: function (newData, oldData) {
14972
+ this.refresh();
14973
+ },
14974
+ deep: false // 仅监听引用变化,由使用者显式替换数组时触发
14975
+ },
14976
+ chartOptions: {
14977
+ handler: function (newOptions, oldOptions) {
14978
+ // 配置变化时需要重建图表
14979
+ if (this.chartInstance) {
14980
+ this.chartInstance.destroy();
14981
+ this.chartInstance = null;
14982
+ }
14983
+ this.option = Object.assign({
14984
+ enableAnimate: false,
14985
+ legendPosition: 'top',
14986
+ legendVisible: true,
14987
+ width: this.width,
14988
+ height: this.height
14989
+ }, newOptions);
14990
+ this.$nextTick(() => this.initChart());
14991
+ },
14992
+ deep: false
14822
14993
  },
14823
14994
  width: function (newWidth, oldWidth) {
14824
14995
  if (!this.chartInstance) return;