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