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