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