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