jmchart 1.2.15 → 1.2.17

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.
@@ -1119,7 +1119,8 @@ class jmGradient {
1119
1119
  let sy2 = Number(y2) + bounds.top;
1120
1120
  if(this.type === 'linear') {
1121
1121
  if(control.mode === 'webgl' && control.webglControl) {
1122
- gradient = control.webglControl.createLinearGradient(x1, y1, x2, y2, bounds);
1122
+ // WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
1123
+ gradient = control.webglControl.createLinearGradient(sx1, sy1, sx2, sy2, bounds);
1123
1124
  gradient.key = this.toString();
1124
1125
  }
1125
1126
  else {
@@ -1138,7 +1139,8 @@ class jmGradient {
1138
1139
  r2 = d * r2;
1139
1140
  }
1140
1141
  if(control.mode === 'webgl' && control.webglControl) {
1141
- gradient = control.webglControl.createRadialGradient(x1, y1, r1, x2, y2, r2, bounds);
1142
+ // WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
1143
+ gradient = control.webglControl.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2, bounds);
1142
1144
  gradient.key = this.toString();
1143
1145
  }
1144
1146
  //offsetLine = Math.abs(r2 - r1);//二圆半径差
@@ -2285,10 +2287,14 @@ earcut.flatten = function (data) {
2285
2287
  return result;
2286
2288
  };
2287
2289
 
2288
- // 渐变
2290
+ const MAX_STOPS = 16;
2291
+
2292
+ /**
2293
+ * WebGL 渐变对象
2294
+ * 支持 GLSL 着色器直接计算渐变色,无需 textureCanvas
2295
+ */
2289
2296
  class WebglGradient {
2290
- // type:[linear= 线性渐变,radial=放射性渐变]
2291
- constructor(type='linear', params={}) {
2297
+ constructor(type = 'linear', params = {}) {
2292
2298
  this.type = type || 'linear';
2293
2299
 
2294
2300
  this.x1 = params.x1 || 0;
@@ -2308,161 +2314,114 @@ class WebglGradient {
2308
2314
  this.control = params.control;
2309
2315
 
2310
2316
  this.stops = [];
2311
- this.init();
2317
+ this._sortedStops = null;
2318
+ this._paramsHash = null;
2312
2319
  }
2313
2320
 
2314
- init() {
2315
- const dx = this.x2 - this.x1;
2316
- const dy = this.y2 - this.y1;
2317
-
2318
- if(this.type === 'radial') {
2319
- this.length = this.r2 - this.r1;
2320
- }
2321
- else if(dx === 0 && dy === 0) {
2322
- this.length = 0;
2323
- }
2324
- else {
2325
- // 渐变中心的距离
2326
- this.length = Math.sqrt(Math.pow(dx, 2), Math.pow(dy, 2));
2327
- this.sin = dy / this.length;
2328
- this.cos = dx / this.length;
2329
- }
2330
- }
2331
-
2332
- // 渐变颜色
2321
+ /**
2322
+ * 添加颜色断点
2323
+ */
2333
2324
  addColorStop(offset, color) {
2334
2325
  this.stops.push({
2335
- offset,
2326
+ offset: Math.max(0, Math.min(1, offset)),
2336
2327
  color
2337
2328
  });
2329
+ this._sortedStops = null;
2330
+ this._paramsHash = null;
2338
2331
  }
2339
2332
 
2340
- // 转为渐变为纹理
2341
- toImageData(control, bounds, points=null) {
2342
- // 缓存基于渐变参数(不含 bounds,因为同一个渐变只是位置不同时纹理相同)
2343
- const gradientKey = this.toString();
2344
- if(this.__cachedData && this.__cacheKey === gradientKey &&
2345
- this.__cachedData.data && this.__cachedData.data.width === Math.ceil(bounds.width) &&
2346
- this.__cachedData.data.data && this.__cachedData.data.data.height === Math.ceil(bounds.height)) {
2347
- return this.__cachedData;
2348
- }
2333
+ /**
2334
+ * 获取排序后的 stops(带解析后的颜色)
2335
+ */
2336
+ _getSortedStops() {
2337
+ if (this._sortedStops) return this._sortedStops;
2338
+
2339
+ const utils = this.control && this.control.graph && this.control.graph.utils;
2340
+ this._sortedStops = this.stops
2341
+ .map(s => {
2342
+ let c = s.color;
2343
+ if (utils && typeof c === 'string') {
2344
+ c = utils.hexToRGBA(c);
2345
+ }
2346
+ if (typeof c === 'object' && c !== null) {
2347
+ // hexToRGBA 返回 r/g/b 为 0~255,a 为 0~1
2348
+ // 但如果已经是 0~1 范围(由 rgbToDecimal 处理过),需要检测
2349
+ const needNormalize = (c.r > 1 || c.g > 1 || c.b > 1) ? 255 : 1;
2350
+ return {
2351
+ offset: s.offset,
2352
+ r: (c.r !== undefined ? c.r : 0) / needNormalize,
2353
+ g: (c.g !== undefined ? c.g : 0) / needNormalize,
2354
+ b: (c.b !== undefined ? c.b : 0) / needNormalize,
2355
+ a: c.a !== undefined ? c.a : 1
2356
+ };
2357
+ }
2358
+ return { offset: s.offset, r: 0, g: 0, b: 0, a: 1 };
2359
+ })
2360
+ .sort((a, b) => a.offset - b.offset);
2349
2361
 
2350
- if(!control.textureContext) {
2351
- return null;
2352
- }
2353
- let gradient = null;
2354
- if(this.type === 'linear') {
2355
- gradient = control.textureContext.createLinearGradient(this.x1, this.y1, this.x2, this.y2);
2356
- }
2357
- else {
2358
- gradient = control.textureContext.createRadialGradient(this.x1, this.y1, this.r1, this.x2, this.y2, this.r2);
2359
- }
2360
- this.stops.forEach(function(s, i) {
2361
- const c = control.graph.utils.toColor(s.color);
2362
- gradient && gradient.addColorStop(s.offset, c);
2363
- });
2364
-
2365
- const data = control.toFillTexture(gradient, bounds, points);
2362
+ return this._sortedStops;
2363
+ }
2366
2364
 
2367
- this.__cachedData = data;
2368
- this.__cacheKey = gradientKey;
2365
+ /**
2366
+ * 将渐变参数以 uniform 形式传递给着色器
2367
+ * 返回 { type, start, end, stopCount, stops } 供着色器使用
2368
+ */
2369
+ toUniformParams() {
2370
+ const stops = this._getSortedStops();
2371
+ const count = Math.min(stops.length, MAX_STOPS);
2372
+
2373
+ // 展平为 Float32Array: [offset, r, g, b, a, ...]
2374
+ const flatStops = new Float32Array(count * 5);
2375
+ for (let i = 0; i < count; i++) {
2376
+ const s = stops[i];
2377
+ flatStops[i * 5 + 0] = s.offset;
2378
+ flatStops[i * 5 + 1] = s.r;
2379
+ flatStops[i * 5 + 2] = s.g;
2380
+ flatStops[i * 5 + 3] = s.b;
2381
+ flatStops[i * 5 + 4] = s.a;
2382
+ }
2369
2383
 
2370
- return data;
2384
+ return {
2385
+ gradientType: this.type === 'radial' ? 2 : 1,
2386
+ gradientStart: new Float32Array([
2387
+ this.x1, this.y1,
2388
+ this.type === 'radial' ? Math.max(0, this.r1) : 0,
2389
+ 0
2390
+ ]),
2391
+ gradientEnd: new Float32Array([
2392
+ this.x2, this.y2,
2393
+ this.type === 'radial' ? Math.max(0, this.r2) : 0,
2394
+ 0
2395
+ ]),
2396
+ stopCount: count,
2397
+ stops: flatStops
2398
+ };
2371
2399
  }
2372
2400
 
2373
- // 当渐变参数变化时使缓存失效
2401
+ /**
2402
+ * 使缓存失效
2403
+ */
2374
2404
  invalidateCache() {
2375
- this.__cachedData = null;
2376
- this.__cacheKey = null;
2377
- }
2378
-
2379
- // 根据绘制图形的坐标计算出对应点的颜色
2380
- /*
2381
- toPointColors(points) {
2382
- const stops = this.getStops();
2383
- const colors = [];
2384
- for(let i=0; i<points.length; i+=2) {
2385
- const p = {
2386
- x: points[i],
2387
- y: points[i+1]
2388
- }
2389
- if(this.type === 'radial') {
2390
- const dx = p.x - this.x1;
2391
- const dy = p.y - this.y1;
2392
- const len = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
2393
- const rang = this.getStopRange(len, stops);
2394
- if(!rang.start && rang.end) {
2395
- colors.push(rang.end.color);
2396
- }
2397
- else if(!rang.end && rang.start) {
2398
- colors.push(rang.start.color);
2399
- }
2400
- else {
2401
- const rangLength = rang.end.length - rang.start.length;
2402
- const offlen = len - rang.start.length;
2403
- const per = offlen / rangLength;
2404
- const color = {
2405
- r: rang.start.color.r + (rang.end.color.r - rang.start.color.r) * per,
2406
- g: rang.start.color.g + (rang.end.color.g - rang.start.color.g) * per,
2407
- b: rang.start.color.b + (rang.end.color.b - rang.start.color.b) * per,
2408
- a: rang.start.color.a + (rang.end.color.a - rang.start.color.a) * per,
2409
- };
2410
- colors.push(color);
2411
- }
2412
- }
2413
- }
2414
- return colors;
2415
- }
2416
- */
2417
- // 根据起点距离获取边界stop
2418
- /*
2419
- getStopRange(len, stops) {
2420
- const res = {};
2421
- for(const s of stops) {
2422
- if(s.length <= len) {
2423
- res.start = s;
2424
- }
2425
- else {
2426
- res.end = s;
2427
- }
2428
- }
2429
- return res;
2405
+ this._sortedStops = null;
2406
+ this._paramsHash = null;
2430
2407
  }
2431
2408
 
2432
- // 根据stop计算offset长度
2433
- getStops() {
2434
- const stops = this.stops.sort((p1, p2) => p1.offset - p2.offset); // 渐变色排序从小于大
2435
- for(const s of stops) {
2436
-
2437
- const color = typeof s.color === 'string'? this.control.graph.utils.hexToRGBA(s.color) : s.color;
2438
- console.log(s, color);
2439
- s.color = this.control.graph.utils.rgbToDecimal(color);
2440
- s.length = s.offset * this.length;
2409
+ /**
2410
+ * 转换为渐变的字符串表达
2411
+ */
2412
+ toString() {
2413
+ let str = this.type + '-gradient(';
2414
+ if (this.type == 'linear') {
2415
+ str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
2441
2416
  }
2442
- return stops;
2417
+ else {
2418
+ str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
2419
+ }
2420
+ this.stops.forEach(function(s) {
2421
+ str += ',' + s.color + ' ' + s.offset;
2422
+ });
2423
+ return str + ')';
2443
2424
  }
2444
- */
2445
- /**
2446
- * 转换为渐变的字符串表达
2447
- *
2448
- * @method toString
2449
- * @for jmGradient
2450
- * @return {string} linear-gradient(x1 y1 x2 y2, color1 step, color2 step, ...); //radial-gradient(x1 y1 r1 x2 y2 r2, color1 step,color2 step, ...);
2451
- */
2452
- toString() {
2453
- let str = this.type + '-gradient(';
2454
- if(this.type == 'linear') {
2455
- str += this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2;
2456
- }
2457
- else {
2458
- str += this.x1 + ' ' + this.y1 + ' ' + this.r1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + this.r2;
2459
- }
2460
- //颜色渐变
2461
- this.stops.forEach(function(s) {
2462
- str += ',' + s.color + ' ' + s.offset;
2463
- });
2464
- return str + ')';
2465
- }
2466
2425
  }
2467
2426
 
2468
2427
  // 生成着色器
@@ -2787,8 +2746,8 @@ const pathVertexSource = `
2787
2746
  vec4 pos = translatePosition(a_position, a_center_point.x, a_center_point.y);
2788
2747
  gl_Position = pos;
2789
2748
  v_color = a_color;
2790
- if(a_type == 2) {
2791
- v_text_coord = a_text_coord;
2749
+ if(a_type == 2 || a_type == 5) {
2750
+ v_text_coord = a_position.xy;
2792
2751
  }
2793
2752
  }
2794
2753
  `;
@@ -2798,21 +2757,73 @@ const pathFragmentSource = `
2798
2757
  uniform sampler2D u_sample;
2799
2758
  uniform vec4 v_texture_bounds; // 纹理的左上坐标和大小 x,y,z,w
2800
2759
  uniform vec4 v_single_color;
2760
+ // GLSL 渐变 uniforms
2761
+ uniform int u_gradient_type; // 0=无 1=线性 2=径向
2762
+ uniform vec4 u_gradient_start; // 线性:{x1,y1,0,0} 径向:{cx,cy,r1,0}
2763
+ uniform vec4 u_gradient_end; // 线性:{x2,y2,0,0} 径向:{cx,cy,r2,0}
2764
+ uniform int u_gradient_stop_count;
2765
+ uniform float u_gradient_offsets[${MAX_STOPS}];
2766
+ uniform vec4 u_gradient_colors[${MAX_STOPS}]; // {r, g, b, a} 0~1 范围
2801
2767
  varying float v_type;
2802
2768
  varying vec4 v_color;
2803
2769
  varying vec2 v_text_coord;
2804
2770
 
2805
2771
  ${convertTexturePosition}
2806
2772
 
2773
+ // 在 sorted stops 中按 t 值采样颜色
2774
+ // 兼容 GLSL ES 1.0:循环仅与常量比较,无 break/continue
2775
+ vec4 sampleGradient(float t) {
2776
+ t = clamp(t, 0.0, 1.0);
2777
+ // 正向扫描:始终遍历 MAX_STOPS-1 次,找到 t 所在段并覆盖结果
2778
+ float localT = 0.0;
2779
+ vec4 c0 = u_gradient_colors[0];
2780
+ vec4 c1 = u_gradient_colors[0];
2781
+ for(int i = 0; i < ${MAX_STOPS - 1}; i++) {
2782
+ float s0 = u_gradient_offsets[i];
2783
+ float s1 = u_gradient_offsets[i + 1];
2784
+ if(t >= s0) {
2785
+ float range = s1 - s0;
2786
+ localT = range > 0.0001 ? clamp((t - s0) / range, 0.0, 1.0) : 0.0;
2787
+ c0 = u_gradient_colors[i];
2788
+ c1 = u_gradient_colors[i + 1];
2789
+ }
2790
+ }
2791
+ return mix(c0, c1, localT);
2792
+ }
2793
+
2807
2794
  void main() {
2808
2795
  // 如果是fill,则直接填充颜色
2809
2796
  if(v_type == 1.0) {
2810
2797
  gl_FragColor = v_single_color;
2811
2798
  }
2812
- // 渐变色
2799
+ // 渐变色 (旧方式,顶点颜色插值)
2813
2800
  else if(v_type == 3.0) {
2814
2801
  gl_FragColor = v_color;
2815
2802
  }
2803
+ // GLSL 渐变填充 (type=5)
2804
+ else if(v_type == 5.0) {
2805
+ float t;
2806
+ if(u_gradient_type == 2) {
2807
+ // 径向渐变
2808
+ vec2 d = v_text_coord - u_gradient_start.xy;
2809
+ float dist = length(d);
2810
+ float r1 = u_gradient_start.z;
2811
+ float r2 = u_gradient_end.z;
2812
+ float range = r2 - r1;
2813
+ t = range > 0.001 ? (dist - r1) / range : 0.0;
2814
+ } else {
2815
+ // 线性渐变
2816
+ vec2 dir = u_gradient_end.xy - u_gradient_start.xy;
2817
+ float lenSq = dot(dir, dir);
2818
+ if(lenSq > 0.001) {
2819
+ vec2 pos = v_text_coord - u_gradient_start.xy;
2820
+ t = dot(pos, dir) / lenSq;
2821
+ } else {
2822
+ t = 0.0;
2823
+ }
2824
+ }
2825
+ gl_FragColor = sampleGradient(t) * v_single_color.a;
2826
+ }
2816
2827
  else if(v_type == 2.0) {
2817
2828
  vec2 pos = translateTexturePosition(v_text_coord, v_texture_bounds);
2818
2829
  gl_FragColor = texture2D(u_sample, pos);
@@ -2883,7 +2894,7 @@ class WeblBase {
2883
2894
  rotate(angle) {
2884
2895
  const cos = Math.cos(angle);
2885
2896
  const sin = Math.sin(angle);
2886
- const [a, b, c, d, tx, ty] = this.transformMatrix;
2897
+ const [a, b, c, d] = this.transformMatrix;
2887
2898
 
2888
2899
  // 更新变换矩阵
2889
2900
  this.transformMatrix[0] = a * cos - b * sin;
@@ -2914,21 +2925,20 @@ class WeblBase {
2914
2925
  };
2915
2926
  }
2916
2927
 
2917
- // 纹理绘制canvas
2918
- get textureCanvas() {
2919
- let canvas = this.graph.textureCanvas;
2920
- if(!canvas) {
2921
- if(typeof document === 'undefined') return null;
2922
- canvas = this.graph.textureCanvas = document.createElement('canvas');
2928
+ // 文本测量用的离屏 canvas context(1x1 单例缓存,不依赖 textureCanvas)
2929
+ get _measureCtx() {
2930
+ if(!this.__measureCtx) {
2931
+ try {
2932
+ if(typeof document !== 'undefined') {
2933
+ const c = document.createElement('canvas');
2934
+ c.width = c.height = 1;
2935
+ this.__measureCtx = c.getContext('2d');
2936
+ }
2937
+ } catch(e) {
2938
+ this.__measureCtx = null;
2939
+ }
2923
2940
  }
2924
- return canvas;
2925
- }
2926
- // 纹理绘制canvas ctx
2927
- get textureContext() {
2928
- const ctx = this.textureCanvas.ctx || (this.textureCanvas.ctx = this.textureCanvas.getContext('2d', {
2929
- willReadFrequently: true
2930
- }));
2931
- return ctx;
2941
+ return this.__measureCtx;
2932
2942
  }
2933
2943
 
2934
2944
  // i当前程序
@@ -2974,23 +2984,37 @@ class WeblBase {
2974
2984
  // 把传统颜色转为webgl识别的
2975
2985
  convertColor(color) {
2976
2986
  if(this.isGradient(color)) return color;
2977
- if(typeof color === 'string') color = this.graph.utils.hexToRGBA(color);
2978
- return this.graph.utils.rgbToDecimal(color);
2979
- }
2980
-
2981
- setTextureStyle(style, value='') {
2982
-
2983
- if(typeof style === 'string') {
2984
- if(['fillStyle', 'strokeStyle', 'shadowColor'].indexOf(style) > -1) {
2985
- value = this.graph.utils.toColor(value);
2987
+ if(typeof color === 'string') {
2988
+ // 先尝试 hexToRGBA 解析
2989
+ color = this.graph.utils.hexToRGBA(color);
2990
+ // hexToRGBA 对无法识别的格式(如 hsl)会原样返回字符串
2991
+ // 利用离屏 canvas 将任意 CSS 颜色转为 rgba
2992
+ if(typeof color === 'string') {
2993
+ color = this.__parseCSSColor(color);
2986
2994
  }
2987
- this.textureContext[style] = value;
2988
2995
  }
2989
- else {
2990
- for(const name in style) {
2991
- if(name === 'constructor') continue;
2992
- this.setTextureStyle(name, style[name]);
2996
+ if(typeof color === 'object' && color.r !== undefined) {
2997
+ return this.graph.utils.rgbToDecimal(color);
2998
+ }
2999
+ return color;
3000
+ }
3001
+
3002
+ // 利用离屏 canvas 解析任意 CSS 颜色(hsl/hsla/命名颜色等)
3003
+ __parseCSSColor(colorStr) {
3004
+ const ctx = this._measureCtx;
3005
+ if(!ctx) return { r: 0, g: 0, b: 0, a: 0 };
3006
+ try {
3007
+ ctx.clearRect(0, 0, 1, 1);
3008
+ ctx.fillStyle = '#000000';
3009
+ ctx.fillStyle = colorStr;
3010
+ ctx.fillRect(0, 0, 1, 1);
3011
+ const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data;
3012
+ if(ctx.fillStyle === '#000000' && colorStr !== '#000000' && colorStr !== 'black') {
3013
+ return { r: 0, g: 0, b: 0, a: 0 };
2993
3014
  }
3015
+ return { r, g, b, a: a / 255 };
3016
+ } catch(e) {
3017
+ return { r: 0, g: 0, b: 0, a: 0 };
2994
3018
  }
2995
3019
  }
2996
3020
 
@@ -3165,70 +3189,23 @@ class WeblBase {
3165
3189
  return obj && obj instanceof WebglGradient;
3166
3190
  }
3167
3191
 
3168
- /**
3192
+ /**
3169
3193
  * 测试获取文本所占大小
3170
3194
  *
3171
3195
  * @method testSize
3172
3196
  * @return {object} 含文本大小的对象
3173
3197
  */
3174
3198
  testSize(text, style=this.style) {
3175
-
3176
- this.textureContext.save && this.textureContext.save();
3177
- // 修改字体,用来计算
3178
- if(style.font || style.fontSize) this.textureContext.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
3179
-
3180
- //计算宽度
3181
- const size = this.textureContext.measureText?
3182
- this.textureContext.measureText(text):
3183
- {width:15};
3184
- this.textureContext.restore &&this.textureContext.restore();
3185
- size.height = this.style.fontSize? this.style.fontSize: 15;
3199
+ const ctx = this._measureCtx;
3200
+ if(!ctx) return { width: 15, height: style.fontSize || 15 };
3201
+
3202
+ ctx.save && ctx.save();
3203
+ if(style.font || style.fontSize) ctx.font = style.font || (style.fontSize + 'px ' + style.fontFamily);
3204
+ const size = ctx.measureText ? ctx.measureText(text) : { width: 15 };
3205
+ ctx.restore && ctx.restore();
3206
+ size.height = style.fontSize ? parseInt(style.fontSize) : 15;
3186
3207
  return size;
3187
3208
  }
3188
-
3189
- // 使用纹理canvas生成图,
3190
- // 填充可以是颜色或渐变对象
3191
- // 如果指定了points,则表明要绘制不规则的图形
3192
- toFillTexture(fillStyle, bounds, points=null) {
3193
- const canvas = this.textureCanvas;
3194
- if(!canvas) {
3195
- return fillStyle;
3196
- }
3197
- canvas.width = bounds.width;
3198
- canvas.height = bounds.height;
3199
-
3200
- if(!canvas.width || !canvas.height) {
3201
- return fillStyle;
3202
- }
3203
-
3204
- this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
3205
-
3206
- this.textureContext.fillStyle = fillStyle;
3207
-
3208
- // 规则图形用 fillRect,比 beginPath/lineTo/fill 快
3209
- if(!points || !points.length) {
3210
- this.textureContext.fillRect(0, 0, bounds.width, bounds.height);
3211
- } else {
3212
- this.textureContext.beginPath();
3213
- for(const p of points) {
3214
- //移至当前坐标
3215
- if(p.m) {
3216
- this.textureContext.moveTo(p.x - bounds.left, p.y - bounds.top);
3217
- }
3218
- else {
3219
- this.textureContext.lineTo(p.x - bounds.left, p.y - bounds.top);
3220
- }
3221
- }
3222
- this.textureContext.closePath();
3223
- this.textureContext.fill();
3224
- }
3225
-
3226
- const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
3227
- return {
3228
- data,
3229
- points
3230
- };
3231
- }
3232
3209
  }
3233
3210
 
3234
3211
  // path 绘制类
@@ -3435,6 +3412,112 @@ class WebglPath extends WeblBase {
3435
3412
  equalPoint(p1, p2) {
3436
3413
  return p1.x === p2.x && p1.y === p2.y;
3437
3414
  }
3415
+
3416
+ // 将带 moveTo 标记的点集拆分为外轮廓和多个洞
3417
+ splitSubPaths(points) {
3418
+ const subPaths = [];
3419
+ let current = [];
3420
+ for(let i = 0; i < points.length; i++) {
3421
+ const p = points[i];
3422
+ if(p.m && current.length > 0) {
3423
+ subPaths.push(current);
3424
+ current = [];
3425
+ }
3426
+ current.push(p);
3427
+ }
3428
+ if(current.length > 0) subPaths.push(current);
3429
+
3430
+ // 面积最大的作为外轮廓,其余作为洞
3431
+ let maxArea = -1;
3432
+ let outerIdx = 0;
3433
+ for(let i = 0; i < subPaths.length; i++) {
3434
+ const area = Math.abs(this.polygonArea(subPaths[i]));
3435
+ if(area > maxArea) {
3436
+ maxArea = area;
3437
+ outerIdx = i;
3438
+ }
3439
+ }
3440
+
3441
+ const outerPoints = subPaths[outerIdx];
3442
+ const holes = [];
3443
+ for(let i = 0; i < subPaths.length; i++) {
3444
+ if(i !== outerIdx) holes.push(subPaths[i]);
3445
+ }
3446
+ return { outerPoints, holes };
3447
+ }
3448
+
3449
+ // 计算多边形面积(Shoelace 公式)
3450
+ polygonArea(points) {
3451
+ let area = 0;
3452
+ const n = points.length;
3453
+ for(let i = 0; i < n; i++) {
3454
+ const j = (i + 1) % n;
3455
+ area += points[i].x * points[j].y;
3456
+ area -= points[j].x * points[i].y;
3457
+ }
3458
+ return area / 2;
3459
+ }
3460
+
3461
+ // 使用 earcut 带 holes 填充多边形
3462
+ fillWithHoles(outerPoints, holes, isTexture = false) {
3463
+ // 将所有点合并:外轮廓 + 各个洞,并记录洞的起始索引
3464
+ const allPoints = [...outerPoints];
3465
+ const holeIndices = [];
3466
+ for(const hole of holes) {
3467
+ holeIndices.push(allPoints.length);
3468
+ allPoints.push(...hole);
3469
+ }
3470
+
3471
+ const dim = 2;
3472
+ const vertexData = [];
3473
+ for(const p of allPoints) {
3474
+ vertexData.push(p.x, p.y);
3475
+ }
3476
+
3477
+ // 用 earcut 进行带洞三角化
3478
+ const indices = earcut(vertexData, holeIndices, dim);
3479
+
3480
+ if(!indices || indices.length < 3) return;
3481
+
3482
+ // 构建 GPU 顶点数据
3483
+ const allVertices = [];
3484
+ const allTexCoords = [];
3485
+ for(let i = 0; i < indices.length; i++) {
3486
+ const p = allPoints[indices[i]];
3487
+ allVertices.push(p.x, p.y);
3488
+ if(isTexture) allTexCoords.push(p.x, p.y);
3489
+ }
3490
+
3491
+ const gl = this.context;
3492
+ const vertexArr = new Float32Array(allVertices);
3493
+
3494
+ let posBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_position);
3495
+ if(!posBuffer) {
3496
+ posBuffer = this.createFloat32Buffer(vertexArr, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3497
+ posBuffer.attr = this.program.attrs.a_position;
3498
+ this.__cachedBuffers.push(posBuffer);
3499
+ } else {
3500
+ gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer.buffer);
3501
+ gl.bufferData(gl.ARRAY_BUFFER, vertexArr, gl.DYNAMIC_DRAW);
3502
+ }
3503
+ this.writeVertexAttrib(posBuffer, this.program.attrs.a_position, 2, 0, 0);
3504
+
3505
+ if(isTexture && allTexCoords.length) {
3506
+ const texData = new Float32Array(allTexCoords);
3507
+ let texBuffer = this.__cachedBuffers.find(b => b.attr === this.program.attrs.a_text_coord);
3508
+ if(!texBuffer) {
3509
+ texBuffer = this.createFloat32Buffer(texData, gl.ARRAY_BUFFER, gl.DYNAMIC_DRAW);
3510
+ texBuffer.attr = this.program.attrs.a_text_coord;
3511
+ this.__cachedBuffers.push(texBuffer);
3512
+ } else {
3513
+ gl.bindBuffer(gl.ARRAY_BUFFER, texBuffer.buffer);
3514
+ gl.bufferData(gl.ARRAY_BUFFER, texData, gl.DYNAMIC_DRAW);
3515
+ }
3516
+ this.writeVertexAttrib(texBuffer, this.program.attrs.a_text_coord, 2, 0, 0);
3517
+ }
3518
+
3519
+ gl.drawArrays(gl.TRIANGLES, 0, allVertices.length / 2);
3520
+ }
3438
3521
  // 把path坐标集合转为线段集
3439
3522
  pathToLines(points) {
3440
3523
  let start = null;
@@ -3661,9 +3744,46 @@ class WebglPath extends WeblBase {
3661
3744
  }
3662
3745
  if(points && points.length) {
3663
3746
  const regular = lineWidth <= 1.2;
3664
- points = regular? points : this.pathToPoints(points);
3665
- this.writePoints(points);
3666
- this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
3747
+ const hasMoveTo = points.some && points.some(p => p.m);
3748
+ const isRing = !hasMoveTo && this.needCut; // 空心形状(jmHArc close=true 时无 m 标记)
3749
+ if(regular && (hasMoveTo || isRing)) {
3750
+ // 有 moveTo 标记或空心形状时,分段绘制每个子路径的 LINE_LOOP
3751
+ // 避免 LINE_LOOP 把不同子路径的点连起来产生拉扯线
3752
+ if(hasMoveTo) {
3753
+ let subPath = [];
3754
+ for(let i = 0; i < points.length; i++) {
3755
+ if(points[i].m && subPath.length > 0) {
3756
+ this.writePoints(subPath);
3757
+ this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
3758
+ subPath = [];
3759
+ }
3760
+ subPath.push(points[i]);
3761
+ }
3762
+ if(subPath.length > 1) {
3763
+ this.writePoints(subPath);
3764
+ this.context.drawArrays(this.context.LINE_LOOP, 0, subPath.length);
3765
+ }
3766
+ }
3767
+ else if(isRing) {
3768
+ // 空心形状:前半段为内弧,后半段为外弧(反向),各自 LINE_LOOP
3769
+ const mid = Math.floor(points.length / 2);
3770
+ const inner = points.slice(0, mid);
3771
+ const outer = points.slice(mid);
3772
+ if(inner.length > 1) {
3773
+ this.writePoints(inner);
3774
+ this.context.drawArrays(this.context.LINE_LOOP, 0, inner.length);
3775
+ }
3776
+ if(outer.length > 1) {
3777
+ this.writePoints(outer);
3778
+ this.context.drawArrays(this.context.LINE_LOOP, 0, outer.length);
3779
+ }
3780
+ }
3781
+ }
3782
+ else {
3783
+ points = regular? points : this.pathToPoints(points);
3784
+ this.writePoints(points);
3785
+ this.context.drawArrays(regular? this.context.LINE_LOOP: this.context.POINTS, 0, points.length);
3786
+ }
3667
3787
  // buffer 由 endDraw 统一清理
3668
3788
  }
3669
3789
  colorBuffer && this.disableVertexAttribArray(colorBuffer && colorBuffer.attr);
@@ -3685,10 +3805,9 @@ class WebglPath extends WeblBase {
3685
3805
 
3686
3806
  fillColor(color, points, bounds, type=1) {
3687
3807
 
3688
- // 如果是渐变色,则需要计算偏移量的颜色
3808
+ // 如果是渐变色,使用 GLSL 着色器直接计算
3689
3809
  if(this.isGradient(color)) {
3690
- const imgData = color.toImageData(this, bounds, points);
3691
- return this.fillImage(imgData.data, imgData.points, bounds);
3810
+ return this.fillGradient(color, points, bounds);
3692
3811
  }
3693
3812
 
3694
3813
  // 标注为fill
@@ -3701,6 +3820,83 @@ class WebglPath extends WeblBase {
3701
3820
 
3702
3821
  }
3703
3822
 
3823
+ /**
3824
+ * 使用 GLSL 着色器渲染渐变填充
3825
+ * 无需 textureCanvas,直接通过 uniform 传递渐变参数给 GPU
3826
+ */
3827
+ fillGradient(gradient, points, bounds) {
3828
+ const params = gradient.toUniformParams();
3829
+ if(!params) return;
3830
+
3831
+ // 标注为 GLSL 渐变 (type=5)
3832
+ this.context.uniform1i(this.program.uniforms.a_type.location, 5);
3833
+
3834
+ // 设置 globalAlpha(通过 v_single_color.a 传递给着色器)
3835
+ this.context.uniform4f(this.program.uniforms.v_single_color.location, 1.0, 1.0, 1.0, this.style.globalAlpha);
3836
+
3837
+ // 设置渐变类型
3838
+ if(this.program.uniforms.u_gradient_type) {
3839
+ this.context.uniform1i(this.program.uniforms.u_gradient_type.location, params.gradientType);
3840
+ }
3841
+
3842
+ // 设置渐变起点/终点
3843
+ if(this.program.uniforms.u_gradient_start) {
3844
+ this.context.uniform4fv(this.program.uniforms.u_gradient_start.location, params.gradientStart);
3845
+ }
3846
+ if(this.program.uniforms.u_gradient_end) {
3847
+ this.context.uniform4fv(this.program.uniforms.u_gradient_end.location, params.gradientEnd);
3848
+ }
3849
+
3850
+ // 设置颜色断点数量
3851
+ if(this.program.uniforms.u_gradient_stop_count) {
3852
+ this.context.uniform1i(this.program.uniforms.u_gradient_stop_count.location, params.stopCount);
3853
+ }
3854
+
3855
+ // 设置每个 stop 的 offset
3856
+ // 关键:必须填充完整的 MAX_STOPS 长度数组,否则未初始化元素默认为 0
3857
+ // 会导致着色器循环中 t >= 0 始终为 true,返回黑色
3858
+ if(this.program.uniforms.u_gradient_offsets) {
3859
+ const offsets = new Float32Array(MAX_STOPS);
3860
+ for(let i = 0; i < params.stopCount; i++) {
3861
+ offsets[i] = params.stops[i * 5];
3862
+ }
3863
+ // 用 2.0 填充剩余项,使 t(0~1) >= 2.0 为 false,不会被匹配
3864
+ for(let i = params.stopCount; i < MAX_STOPS; i++) {
3865
+ offsets[i] = 2.0;
3866
+ }
3867
+ this.context.uniform1fv(this.program.uniforms.u_gradient_offsets.location, offsets);
3868
+ }
3869
+
3870
+ // 设置每个 stop 的颜色 (rgba)
3871
+ if(this.program.uniforms.u_gradient_colors) {
3872
+ const colors = new Float32Array(MAX_STOPS * 4);
3873
+ for(let i = 0; i < params.stopCount; i++) {
3874
+ colors[i * 4 + 0] = params.stops[i * 5 + 1]; // r
3875
+ colors[i * 4 + 1] = params.stops[i * 5 + 2]; // g
3876
+ colors[i * 4 + 2] = params.stops[i * 5 + 3]; // b
3877
+ colors[i * 4 + 3] = params.stops[i * 5 + 4]; // a
3878
+ }
3879
+ // 用最后一个 stop 的颜色填充剩余项,确保不会返回黑色
3880
+ if(params.stopCount > 0) {
3881
+ const lastR = params.stops[(params.stopCount - 1) * 5 + 1];
3882
+ const lastG = params.stops[(params.stopCount - 1) * 5 + 2];
3883
+ const lastB = params.stops[(params.stopCount - 1) * 5 + 3];
3884
+ const lastA = params.stops[(params.stopCount - 1) * 5 + 4];
3885
+ for(let i = params.stopCount; i < MAX_STOPS; i++) {
3886
+ colors[i * 4 + 0] = lastR;
3887
+ colors[i * 4 + 1] = lastG;
3888
+ colors[i * 4 + 2] = lastB;
3889
+ colors[i * 4 + 3] = lastA;
3890
+ }
3891
+ }
3892
+ this.context.uniform4fv(this.program.uniforms.u_gradient_colors.location, colors);
3893
+ }
3894
+
3895
+ // 填充多边形(需要纹理坐标来计算渐变位置)
3896
+ this.fillPolygons(points, true);
3897
+ this.disableVertexAttribArray(this.program.attrs.a_text_coord);
3898
+ }
3899
+
3704
3900
  // 区域填充图片
3705
3901
  // points绘制的图形顶点
3706
3902
  // 图片整体绘制区域
@@ -3766,6 +3962,29 @@ class WebglPath extends WeblBase {
3766
3962
 
3767
3963
  // 规则图形(凸多边形,如圆):直接用 TRIANGLE_FAN 一次性绘制,无需 earcut
3768
3964
  if(this.isRegular) {
3965
+ // 检查是否有 moveTo 标记,如果有说明路径包含多个子路径(如空心圆弧 jmHArc)
3966
+ const hasMoveTo = points.some && points.some(p => p.m);
3967
+ if(hasMoveTo) {
3968
+ // 有 m 标记:按 m 标记拆分子路径
3969
+ const { outerPoints, holes } = this.splitSubPaths(points);
3970
+ this.fillWithHoles(outerPoints, holes, isTexture);
3971
+ return;
3972
+ }
3973
+ // 无 m 标记但 needCut=true 表示空心形状(如 jmHArc close=true)
3974
+ // 前半段为内弧,后半段为外弧(反向),按中点拆分
3975
+ if(this.needCut && points.length >= 6) {
3976
+ const mid = Math.floor(points.length / 2);
3977
+ const inner = points.slice(0, mid);
3978
+ const outer = points.slice(mid);
3979
+ const innerArea = Math.abs(this.polygonArea(inner));
3980
+ const outerArea = Math.abs(this.polygonArea(outer));
3981
+ if(outerArea >= innerArea) {
3982
+ this.fillWithHoles(outer, [inner], isTexture);
3983
+ } else {
3984
+ this.fillWithHoles(inner, [outer], isTexture);
3985
+ }
3986
+ return;
3987
+ }
3769
3988
  this.writePoints(points);
3770
3989
  isTexture? this.writePoints(points, this.program.attrs.a_text_coord): null;
3771
3990
  this.context.drawArrays(this.context.TRIANGLE_FAN, 0, points.length);
@@ -3833,46 +4052,73 @@ class WebglPath extends WeblBase {
3833
4052
  }
3834
4053
 
3835
4054
  drawText(text, x, y, bounds) {
3836
- let canvas = this.textureCanvas;
4055
+ // 文本渲染仍需要 2D canvas 绘制字形,然后作为纹理上传
4056
+ // 使用临时 canvas,不依赖共享的 textureCanvas
4057
+ if(!bounds.width || !bounds.height) return null;
4058
+ if(typeof document === 'undefined') return null;
4059
+
4060
+ let canvas = this.__textCanvas;
3837
4061
  if(!canvas) {
3838
- return null;
4062
+ canvas = document.createElement('canvas');
4063
+ this.__textCanvas = canvas;
3839
4064
  }
3840
4065
  canvas.width = bounds.width;
3841
4066
  canvas.height = bounds.height;
3842
4067
 
3843
- if(!canvas.width || !canvas.height) {
3844
- return null;
3845
- }
4068
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
4069
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
3846
4070
 
3847
- this.textureContext.clearRect(0, 0, canvas.width, canvas.height);
3848
4071
  // 修改字体
3849
- this.textureContext.font = this.style.font || (this.style.fontSize + 'px ' + this.style.fontFamily);
4072
+ ctx.font = this.style.font || (this.style.fontSize + 'px ' + this.style.fontFamily);
3850
4073
 
3851
4074
  x -= bounds.left;
3852
4075
  y -= bounds.top;
3853
4076
 
3854
- this.setTextureStyle(this.style);
3855
-
3856
- if(this.style.fillStyle && this.textureContext.fillText) {
4077
+ // 设置文本样式
4078
+ if(this.style.fillStyle) {
4079
+ ctx.fillStyle = this.graph.utils.toColor(this.style.fillStyle);
4080
+ }
4081
+ if(this.style.strokeStyle) {
4082
+ ctx.strokeStyle = this.graph.utils.toColor(this.style.strokeStyle);
4083
+ }
4084
+ if(this.style.shadowColor) {
4085
+ ctx.shadowColor = this.graph.utils.toColor(this.style.shadowColor);
4086
+ }
4087
+ if(this.style.shadowBlur) {
4088
+ ctx.shadowBlur = this.style.shadowBlur;
4089
+ }
4090
+ if(this.style.shadowOffsetX !== undefined) {
4091
+ ctx.shadowOffsetX = this.style.shadowOffsetX;
4092
+ }
4093
+ if(this.style.shadowOffsetY !== undefined) {
4094
+ ctx.shadowOffsetY = this.style.shadowOffsetY;
4095
+ }
4096
+ if(this.style.textAlign) {
4097
+ ctx.textAlign = this.style.textAlign;
4098
+ }
4099
+ if(this.style.textBaseline) {
4100
+ ctx.textBaseline = this.style.textBaseline;
4101
+ }
3857
4102
 
4103
+ if(this.style.fillStyle && ctx.fillText) {
3858
4104
  if(this.style.maxWidth) {
3859
- this.textureContext.fillText(text, x, y, this.style.maxWidth);
4105
+ ctx.fillText(text, x, y, this.style.maxWidth);
3860
4106
  }
3861
4107
  else {
3862
- this.textureContext.fillText(text, x, y);
4108
+ ctx.fillText(text, x, y);
3863
4109
  }
3864
4110
  }
3865
- if(this.textureContext.strokeText) {
3866
-
4111
+ if(this.style.strokeStyle && ctx.strokeText) {
3867
4112
  if(this.style.maxWidth) {
3868
- this.textureContext.strokeText(text, x, y, this.style.maxWidth);
4113
+ ctx.strokeText(text, x, y, this.style.maxWidth);
3869
4114
  }
3870
4115
  else {
3871
- this.textureContext.strokeText(text, x, y);
4116
+ ctx.strokeText(text, x, y);
3872
4117
  }
3873
4118
  }
4119
+
3874
4120
  // 用纹理图片代替文字
3875
- const data = this.textureContext.getImageData(0, 0, canvas.width, canvas.height);
4121
+ const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
3876
4122
  this.fillImage(data, this.points, bounds);
3877
4123
  }
3878
4124
  }
@@ -6105,8 +6351,8 @@ class jmHArc extends jmArc {
6105
6351
 
6106
6352
  maxps.reverse();//大圆逆序
6107
6353
  if(!this.style || !this.style.close) {
6108
- maxps[0].m = true;//开始画大圆时表示为移动
6109
- }
6354
+ maxps[0].m = true;//非闭合时标记 moveTo,分隔内外两个子路径
6355
+ }
6110
6356
  this.points = minps.concat(maxps);
6111
6357
  }
6112
6358
  }
@@ -8124,8 +8370,6 @@ class jmGraph extends jmControl {
8124
8370
  else {
8125
8371
  this.context = canvas.getContext(this.mode);
8126
8372
  }
8127
-
8128
- this.textureCanvas = option.textureCanvas || null;
8129
8373
 
8130
8374
  // webgl模式
8131
8375
  if(this.mode === 'webgl') {
@@ -8834,10 +9078,14 @@ class jmGraphImpl extends jmGraph {
8834
9078
  if(typeof option == 'function') {
8835
9079
  callback = option;
8836
9080
  option = {};
8837
- }
9081
+ }
8838
9082
 
8839
9083
  super(canvas, option, callback);
8840
9084
  }
9085
+
9086
+ static create(...args) {
9087
+ return new jmGraphImpl(...args);
9088
+ }
8841
9089
  }
8842
9090
 
8843
9091
  /**
@@ -9414,27 +9662,6 @@ var defaultStyle = {
9414
9662
  spiral: true,
9415
9663
  zIndex: 11,
9416
9664
  cursor: 'default'
9417
- },
9418
- sunburst: {
9419
- normal: {
9420
- zIndex: 11,
9421
- cursor: 'default'
9422
- },
9423
- hover: {
9424
- cursor: 'pointer'
9425
- },
9426
- innerRadius: 0,
9427
- startAngle: 0,
9428
- showLabels: true,
9429
- showCenter: true,
9430
- centerFill: '#fff',
9431
- centerStroke: '#e0e0e0',
9432
- stroke: '#fff',
9433
- lineWidth: 1,
9434
- labelColor: '#fff',
9435
- labelFont: '12px Arial',
9436
- zIndex: 11,
9437
- cursor: 'default'
9438
9665
  }
9439
9666
  };
9440
9667
 
@@ -11743,10 +11970,42 @@ class jmLineSeries extends jmSeries {
11743
11970
  this.graph.legend.append(this, shape);
11744
11971
  }
11745
11972
 
11746
- // 生成布效果
11747
- createArea(points, needClosePoint = true) {
11973
+ // 生成面积效果
11974
+ createArea(points) {
11748
11975
  // 有指定绘制区域效果才展示
11749
11976
  if (!this.style.area || points.length < 2) return;
11977
+
11978
+ // 基线:取X轴的实际Y位置(chartArea相对坐标),没有X轴时用下边界
11979
+ const axisY = this.graph.xAxis ? this.graph.xAxis.start.y - this.graph.chartArea.position.y : this.graph.chartArea.height;
11980
+
11981
+ // 过滤有效点
11982
+ const validPoints = points.filter(p => p.y != null && typeof p.y !== 'undefined');
11983
+ if (validPoints.length < 2) return;
11984
+
11985
+ // 检测所有点是否在基线的同一侧
11986
+ // y < axisY 表示在基线上方(数据值大于基线值),y > axisY 表示在基线下方
11987
+ let allAbove = true;
11988
+ let allBelow = true;
11989
+ for (const p of validPoints) {
11990
+ if (p.y <= axisY) allBelow = false;
11991
+ if (p.y >= axisY) allAbove = false;
11992
+ }
11993
+ if (allAbove || allBelow) {
11994
+ // 全部在基线同侧:创建单个封闭区域
11995
+ this._createAreaSegment(points, allAbove, axisY);
11996
+ } else {
11997
+ // 混合数据:在交叉点处分割,分别创建封闭区域
11998
+ this._createMixedAreas(points, axisY);
11999
+ }
12000
+ }
12001
+
12002
+ /**
12003
+ * 创建单个面积段
12004
+ * @param {Array} points 线条上的点
12005
+ * @param {boolean} isAbove 是否在基线上方
12006
+ * @param {number} axisY 基线Y坐标(chartArea相对坐标)
12007
+ */
12008
+ _createAreaSegment(points, isAbove, axisY) {
11750
12009
  const start = points[0];
11751
12010
  const end = points[points.length - 1];
11752
12011
  const style = this.graph.utils.clone(this.style.area, {}, true);
@@ -11754,12 +12013,21 @@ class jmLineSeries extends jmSeries {
11754
12013
  style.stroke = style.stroke || 'transparent';
11755
12014
  if (!style.fill) {
11756
12015
  const color = this.graph.utils.hexToRGBA(this.style.stroke);
11757
- style.fill = `linear-gradient(50% 0 50% 100%,
11758
- rgba(${color.r},${color.g},${color.b}, 0) 1,
11759
- rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
11760
- rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
12016
+ if (isAbove) {
12017
+ // 基线上方:渐变从线条处(顶部)不透明到基线处(底部)透明
12018
+ style.fill = `linear-gradient(50% 0 50% 100%,
12019
+ rgba(${color.r},${color.g},${color.b}, 0) 1,
12020
+ rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
12021
+ rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
12022
+ } else {
12023
+ // 基线下方:渐变从基线处(顶部)透明到线条处(底部)不透明
12024
+ style.fill = `linear-gradient(50% 0 50% 100%,
12025
+ rgba(${color.r},${color.g},${color.b}, 0.3) 1,
12026
+ rgba(${color.r},${color.g},${color.b}, 0.1) 0.3,
12027
+ rgba(${color.r},${color.g},${color.b}, 0) 0)`;
12028
+ }
11761
12029
  } else if (typeof style.fill === 'function') {
11762
- style.fill = style.fill.call(this, style);
12030
+ style.fill = style.fill.call(this, style, isAbove);
11763
12031
  }
11764
12032
  const area = this.graph.createShape('path', {
11765
12033
  points: this.graph.utils.clone(points, true),
@@ -11768,19 +12036,78 @@ class jmLineSeries extends jmSeries {
11768
12036
  height: this.graph.chartArea.height
11769
12037
  });
11770
12038
 
11771
- // 在点集合前后加上落地到X轴的点就可以组成一个封闭的图形area
11772
- if (needClosePoint) {
12039
+ // 在首尾加上基线上的点,组成封闭多边形
12040
+ // 如果首/尾点已经在基线上(交叉点),则不重复添加
12041
+ if (Math.abs(start.y - axisY) > 0.5) {
11773
12042
  area.points.unshift({
11774
12043
  x: start.x,
11775
- y: this.baseY
12044
+ y: axisY
11776
12045
  });
12046
+ }
12047
+ if (Math.abs(end.y - axisY) > 0.5) {
11777
12048
  area.points.push({
11778
12049
  x: end.x,
11779
- y: this.baseY
12050
+ y: axisY
11780
12051
  });
11781
12052
  }
11782
12053
  this.addShape(area);
11783
12054
  }
12055
+
12056
+ /**
12057
+ * 处理跨基线的混合数据,在交叉点处分割成多个独立区域
12058
+ * @param {Array} points 线条上的点
12059
+ * @param {number} axisY 基线Y坐标(chartArea相对坐标)
12060
+ */
12061
+ _createMixedAreas(points, axisY) {
12062
+ const segments = [];
12063
+ let currentSegment = [];
12064
+ let prevAbove = null;
12065
+ for (let i = 0; i < points.length; i++) {
12066
+ const p = points[i];
12067
+ if (p.y == null || typeof p.y === 'undefined') continue;
12068
+ const isAbove = p.y < axisY;
12069
+ if (currentSegment.length === 0) {
12070
+ currentSegment.push(p);
12071
+ prevAbove = isAbove;
12072
+ } else if (isAbove === prevAbove) {
12073
+ currentSegment.push(p);
12074
+ } else {
12075
+ // 检测到跨基线交叉:计算交叉点
12076
+ const prev = currentSegment[currentSegment.length - 1];
12077
+ const t = (axisY - prev.y) / (p.y - prev.y);
12078
+ const crossPoint = {
12079
+ x: prev.x + (p.x - prev.x) * t,
12080
+ y: axisY
12081
+ };
12082
+
12083
+ // 当前段在交叉点处结束
12084
+ currentSegment.push(crossPoint);
12085
+ if (currentSegment.length >= 2) {
12086
+ segments.push({
12087
+ points: [...currentSegment],
12088
+ above: prevAbove
12089
+ });
12090
+ }
12091
+
12092
+ // 新段从交叉点开始
12093
+ currentSegment = [crossPoint, p];
12094
+ prevAbove = isAbove;
12095
+ }
12096
+ }
12097
+
12098
+ // 最后一段
12099
+ if (currentSegment.length >= 2) {
12100
+ segments.push({
12101
+ points: currentSegment,
12102
+ above: prevAbove
12103
+ });
12104
+ }
12105
+
12106
+ // 为每段创建独立的面积
12107
+ for (const seg of segments) {
12108
+ this._createAreaSegment(seg.points, seg.above, axisY);
12109
+ }
12110
+ }
11784
12111
  }
11785
12112
 
11786
12113
  /**
@@ -12448,8 +12775,10 @@ class jmGaugeSeries extends jmSeries {
12448
12775
  const value = typeof p.yValue === 'number' ? p.yValue : 0;
12449
12776
  const min = this.style.min || 0;
12450
12777
  const max = this.style.max || 100;
12451
- const startAngle = this.style.startAngle || -150;
12452
- const endAngle = this.style.endAngle || 150;
12778
+ // 度数转弧度(jmArc 内部使用弧度)
12779
+ const DEG = Math.PI / 180;
12780
+ const startAngle = (this.style.startAngle || -150) * DEG;
12781
+ const endAngle = (this.style.endAngle || 150) * DEG;
12453
12782
  const angleRange = endAngle - startAngle;
12454
12783
  const normalizedValue = Math.max(min, Math.min(max, value));
12455
12784
  const ratio = (normalizedValue - min) / (max - min);
@@ -12530,10 +12859,10 @@ class jmGaugeSeries extends jmSeries {
12530
12859
  for (let i = 0; i < tickCount; i++) {
12531
12860
  const angle = startAngle + angleStep * i;
12532
12861
  const value = min + (max - min) / (tickCount - 1) * i;
12533
- const tickStartX = centerX + Math.cos(angle * Math.PI / 180) * (radius - 30);
12534
- const tickStartY = centerY + Math.sin(angle * Math.PI / 180) * (radius - 30);
12535
- const tickEndX = centerX + Math.cos(angle * Math.PI / 180) * (radius - 10);
12536
- const tickEndY = centerY + Math.sin(angle * Math.PI / 180) * (radius - 10);
12862
+ const tickStartX = centerX + Math.cos(angle) * (radius - 30);
12863
+ const tickStartY = centerY + Math.sin(angle) * (radius - 30);
12864
+ const tickEndX = centerX + Math.cos(angle) * (radius - 10);
12865
+ const tickEndY = centerY + Math.sin(angle) * (radius - 10);
12537
12866
  const tickLine = this.graph.createShape('line', {
12538
12867
  style: {
12539
12868
  stroke: this.style.tickColor || '#666',
@@ -12550,8 +12879,8 @@ class jmGaugeSeries extends jmSeries {
12550
12879
  }
12551
12880
  });
12552
12881
  this.addShape(tickLine);
12553
- const labelX = centerX + Math.cos(angle * Math.PI / 180) * (radius - 45);
12554
- const labelY = centerY + Math.sin(angle * Math.PI / 180) * (radius - 45);
12882
+ const labelX = centerX + Math.cos(angle) * (radius - 45);
12883
+ const labelY = centerY + Math.sin(angle) * (radius - 45);
12555
12884
  const tickLabel = this.graph.createShape('label', {
12556
12885
  style: {
12557
12886
  fill: this.style.tickLabelColor || '#333',
@@ -12575,8 +12904,8 @@ class jmGaugeSeries extends jmSeries {
12575
12904
  */
12576
12905
  createGaugePointer(centerX, centerY, radius, angle) {
12577
12906
  const pointerLength = radius * 0.7;
12578
- const pointerX = centerX + Math.cos(angle * Math.PI / 180) * pointerLength;
12579
- const pointerY = centerY + Math.sin(angle * Math.PI / 180) * pointerLength;
12907
+ const pointerX = centerX + Math.cos(angle) * pointerLength;
12908
+ const pointerY = centerY + Math.sin(angle) * pointerLength;
12580
12909
  const pointerLine = this.graph.createShape('line', {
12581
12910
  style: {
12582
12911
  stroke: this.style.pointerColor || '#333',
@@ -13115,7 +13444,9 @@ class jmRingProgressSeries extends jmSeries {
13115
13444
  const centerY = chartHeight / 2;
13116
13445
  const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
13117
13446
  const lineWidth = this.style.lineWidth || 20;
13118
- const startAngle = this.style.startAngle !== undefined ? this.style.startAngle : -90;
13447
+ // 角度转弧度(jmArc 内部使用弧度)
13448
+ const DEG = Math.PI / 180;
13449
+ const startAngle = (this.style.startAngle !== undefined ? this.style.startAngle : -90) * DEG;
13119
13450
  const maxValue = this.style.max || 100;
13120
13451
  const field = this.field || 'value';
13121
13452
  const len = data.length;
@@ -13153,7 +13484,7 @@ class jmRingProgressSeries extends jmSeries {
13153
13484
  },
13154
13485
  radius: radius,
13155
13486
  startAngle: 0,
13156
- endAngle: 360
13487
+ endAngle: Math.PI * 2
13157
13488
  });
13158
13489
  this.addShape(bgArc);
13159
13490
  }
@@ -13164,7 +13495,7 @@ class jmRingProgressSeries extends jmSeries {
13164
13495
  createRingProgress(centerX, centerY, radius, lineWidth, startAngle, ratio, index) {
13165
13496
  if (ratio <= 0) return;
13166
13497
  const color = this.getColor(null, index);
13167
- const endAngle = startAngle + ratio * 360;
13498
+ const endAngle = startAngle + ratio * Math.PI * 2;
13168
13499
  const progressArc = this.graph.createShape('arc', {
13169
13500
  style: {
13170
13501
  stroke: color,
@@ -13720,228 +14051,6 @@ class jmWordCloudSeries extends jmSeries {
13720
14051
  }
13721
14052
  }
13722
14053
 
13723
- /**
13724
- * 旭日图
13725
- *
13726
- * 旭日图用于展示层级数据的占比关系,由多个同心圆环组成,每个圆环代表一个层级。
13727
- * 内层圆环是外层圆环的父级,扇形角度代表数据占比。
13728
- *
13729
- * 数据格式要求:
13730
- * - 树形结构数据,每个节点包含 name、value 和 children
13731
- *
13732
- * 样式配置:
13733
- * - innerRadius: 内圆半径,默认 0
13734
- * - startAngle: 起始角度,默认 0
13735
- * - showLabels: 是否显示标签,默认 true
13736
- *
13737
- * @class jmSunburstSeries
13738
- * @module jmChart
13739
- * @extends jmSeries
13740
- *
13741
- * @example
13742
- * chart.createSeries('sunburst', {
13743
- * data: [{
13744
- * name: 'Root',
13745
- * children: [
13746
- * { name: 'A', value: 30 },
13747
- * { name: 'B', value: 70 }
13748
- * ]
13749
- * }]
13750
- * });
13751
- */
13752
- class jmSunburstSeries extends jmSeries {
13753
- constructor(options) {
13754
- options.style = options.style || options.graph.style.sunburst;
13755
- super(options);
13756
- this.maxDepth = 0;
13757
- }
13758
-
13759
- /**
13760
- * 初始化旭日图
13761
- */
13762
- init() {
13763
- const data = this.option.data || this.data;
13764
- if (!data || !data.length) return;
13765
- const chartWidth = this.graph.chartArea.width;
13766
- const chartHeight = this.graph.chartArea.height;
13767
- const centerX = chartWidth / 2;
13768
- const centerY = chartHeight / 2;
13769
- const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
13770
- this.maxDepth = this.calculateMaxDepth(data);
13771
- if (this.maxDepth === 0) return;
13772
- const ringWidth = maxRadius / this.maxDepth;
13773
- const innerRadius = this.style.innerRadius || 0;
13774
- this.drawLevel(data, centerX, centerY, innerRadius, ringWidth, 0, 360, 0);
13775
- if (this.style.showCenter !== false && innerRadius > 0) {
13776
- this.drawCenter(centerX, centerY, innerRadius);
13777
- }
13778
- }
13779
-
13780
- /**
13781
- * 计算最大深度
13782
- */
13783
- calculateMaxDepth(data, depth = 1) {
13784
- let maxDepth = depth;
13785
- for (const node of data) {
13786
- if (node.children && node.children.length > 0) {
13787
- const childDepth = this.calculateMaxDepth(node.children, depth + 1);
13788
- maxDepth = Math.max(maxDepth, childDepth);
13789
- }
13790
- }
13791
- return maxDepth;
13792
- }
13793
-
13794
- /**
13795
- * 计算节点总值
13796
- */
13797
- calculateTotalValue(data) {
13798
- let total = 0;
13799
- for (const node of data) {
13800
- if (node.children && node.children.length > 0) {
13801
- total += this.calculateTotalValue(node.children);
13802
- } else {
13803
- total += node.value || 0;
13804
- }
13805
- }
13806
- return total;
13807
- }
13808
-
13809
- /**
13810
- * 绘制层级
13811
- */
13812
- drawLevel(data, centerX, centerY, innerRadius, ringWidth, startAngle, totalAngle, level) {
13813
- const total = this.calculateTotalValue(data);
13814
- if (total === 0) return;
13815
- let currentAngle = startAngle;
13816
- const outerRadius = innerRadius + ringWidth;
13817
- for (const node of data) {
13818
- let nodeValue = 0;
13819
- if (node.children && node.children.length > 0) {
13820
- nodeValue = this.calculateTotalValue(node.children);
13821
- } else {
13822
- nodeValue = node.value || 0;
13823
- }
13824
- const angleRatio = nodeValue / total;
13825
- const nodeAngle = totalAngle * angleRatio;
13826
- const endAngle = currentAngle + nodeAngle;
13827
- if (nodeAngle > 0.1) {
13828
- const color = this.getColor(node, level);
13829
- this.drawArc(centerX, centerY, innerRadius, outerRadius, currentAngle, endAngle, color, node);
13830
- if (this.style.showLabels !== false && nodeAngle > 10) {
13831
- this.drawLabel(centerX, centerY, innerRadius, outerRadius, currentAngle, endAngle, node);
13832
- }
13833
- if (node.children && node.children.length > 0) {
13834
- this.drawLevel(node.children, centerX, centerY, outerRadius, ringWidth, currentAngle, nodeAngle, level + 1);
13835
- }
13836
- }
13837
- currentAngle = endAngle;
13838
- }
13839
- }
13840
-
13841
- /**
13842
- * 绘制扇形
13843
- */
13844
- drawArc(centerX, centerY, innerRadius, outerRadius, startAngle, endAngle, color, node) {
13845
- const arc = this.graph.createShape('arc', {
13846
- style: {
13847
- fill: color,
13848
- stroke: this.style.stroke || '#fff',
13849
- lineWidth: this.style.lineWidth || 1,
13850
- zIndex: 1
13851
- },
13852
- center: {
13853
- x: centerX,
13854
- y: centerY
13855
- },
13856
- radius: outerRadius,
13857
- innerRadius: innerRadius,
13858
- startAngle: startAngle - 90,
13859
- endAngle: endAngle - 90
13860
- });
13861
- this.addShape(arc);
13862
- node.shape = arc;
13863
- }
13864
-
13865
- /**
13866
- * 绘制标签
13867
- */
13868
- drawLabel(centerX, centerY, innerRadius, outerRadius, startAngle, endAngle, node) {
13869
- const midAngle = (startAngle + endAngle) / 2 - 90;
13870
- const midRadius = (innerRadius + outerRadius) / 2;
13871
- const x = centerX + midRadius * Math.cos(midAngle * Math.PI / 180);
13872
- const y = centerY + midRadius * Math.sin(midAngle * Math.PI / 180);
13873
- const text = node.name || '';
13874
- if (!text) return;
13875
- const label = this.graph.createShape('label', {
13876
- style: {
13877
- fill: this.style.labelColor || '#fff',
13878
- font: this.style.labelFont || '12px Arial',
13879
- textAlign: 'center',
13880
- textBaseline: 'middle',
13881
- zIndex: 10
13882
- },
13883
- text: text,
13884
- position: {
13885
- x,
13886
- y
13887
- }
13888
- });
13889
- this.addShape(label);
13890
- }
13891
-
13892
- /**
13893
- * 绘制中心
13894
- */
13895
- drawCenter(centerX, centerY, innerRadius) {
13896
- const centerCircle = this.graph.createShape('circle', {
13897
- style: {
13898
- fill: this.style.centerFill || '#fff',
13899
- stroke: this.style.centerStroke || '#e0e0e0',
13900
- lineWidth: 1,
13901
- zIndex: 0
13902
- },
13903
- center: {
13904
- x: centerX,
13905
- y: centerY
13906
- },
13907
- radius: innerRadius
13908
- });
13909
- this.addShape(centerCircle);
13910
- }
13911
-
13912
- /**
13913
- * 获取颜色
13914
- */
13915
- getColor(node, level) {
13916
- if (node.color) return node.color;
13917
- if (this.style.colors && this.style.colors.length > 0) {
13918
- return this.style.colors[level % this.style.colors.length];
13919
- }
13920
- if (typeof this.style.color === 'function') {
13921
- return this.style.color.call(this, node, level);
13922
- }
13923
- return this.graph.getColor(level);
13924
- }
13925
-
13926
- /**
13927
- * 生成图例
13928
- */
13929
- createLegend() {
13930
- const style = this.graph.utils.clone(this.style);
13931
- style.fill = this.style.color || this.graph.getColor(0);
13932
- style.stroke = style.fill;
13933
- const shape = this.graph.createShape('circle', {
13934
- style: style,
13935
- center: {
13936
- x: this.graph.style.legend.item.shape.width / 2,
13937
- y: this.graph.style.legend.item.shape.height / 2
13938
- },
13939
- radius: this.graph.style.legend.item.shape.height / 2
13940
- });
13941
- this.graph.legend.append(this, shape);
13942
- }
13943
- }
13944
-
13945
14054
  /**
13946
14055
  * 轴
13947
14056
  *
@@ -14661,8 +14770,7 @@ class jmChart extends jmGraphImpl {
14661
14770
  'funnel': jmFunnelSeries,
14662
14771
  'ringProgress': jmRingProgressSeries,
14663
14772
  'boxPlot': jmBoxPlotSeries,
14664
- 'wordCloud': jmWordCloudSeries,
14665
- 'sunburst': jmSunburstSeries
14773
+ 'wordCloud': jmWordCloudSeries
14666
14774
  };
14667
14775
  }
14668
14776