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