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