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.
@@ -188,11 +188,45 @@ export default class jmLineSeries extends jmSeries {
188
188
  this.graph.legend.append(this, shape);
189
189
  }
190
190
 
191
- // 生成布效果
192
- createArea(points, needClosePoint = true) {
191
+ // 生成面积效果
192
+ createArea(points) {
193
193
  // 有指定绘制区域效果才展示
194
194
  if(!this.style.area || points.length < 2) return;
195
195
 
196
+ // 基线:取X轴的实际Y位置(chartArea相对坐标),没有X轴时用下边界
197
+ const axisY = this.graph.xAxis
198
+ ? this.graph.xAxis.start.y - this.graph.chartArea.position.y
199
+ : this.graph.chartArea.height;
200
+
201
+ // 过滤有效点
202
+ const validPoints = points.filter(p => p.y != null && typeof p.y !== 'undefined');
203
+ if(validPoints.length < 2) return;
204
+
205
+ // 检测所有点是否在基线的同一侧
206
+ // y < axisY 表示在基线上方(数据值大于基线值),y > axisY 表示在基线下方
207
+ let allAbove = true;
208
+ let allBelow = true;
209
+ for(const p of validPoints) {
210
+ if(p.y <= axisY) allBelow = false;
211
+ if(p.y >= axisY) allAbove = false;
212
+ }
213
+
214
+ if(allAbove || allBelow) {
215
+ // 全部在基线同侧:创建单个封闭区域
216
+ this._createAreaSegment(points, allAbove, axisY);
217
+ } else {
218
+ // 混合数据:在交叉点处分割,分别创建封闭区域
219
+ this._createMixedAreas(points, axisY);
220
+ }
221
+ }
222
+
223
+ /**
224
+ * 创建单个面积段
225
+ * @param {Array} points 线条上的点
226
+ * @param {boolean} isAbove 是否在基线上方
227
+ * @param {number} axisY 基线Y坐标(chartArea相对坐标)
228
+ */
229
+ _createAreaSegment(points, isAbove, axisY) {
196
230
  const start = points[0];
197
231
  const end = points[points.length - 1];
198
232
 
@@ -202,14 +236,24 @@ export default class jmLineSeries extends jmSeries {
202
236
 
203
237
  if(!style.fill) {
204
238
  const color = this.graph.utils.hexToRGBA(this.style.stroke);
205
- style.fill = `linear-gradient(50% 0 50% 100%,
206
- rgba(${color.r},${color.g},${color.b}, 0) 1,
207
- rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
208
- rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
239
+ if(isAbove) {
240
+ // 基线上方:渐变从线条处(顶部)不透明到基线处(底部)透明
241
+ style.fill = `linear-gradient(50% 0 50% 100%,
242
+ rgba(${color.r},${color.g},${color.b}, 0) 1,
243
+ rgba(${color.r},${color.g},${color.b}, 0.1) 0.7,
244
+ rgba(${color.r},${color.g},${color.b}, 0.3) 0)`;
245
+ } else {
246
+ // 基线下方:渐变从基线处(顶部)透明到线条处(底部)不透明
247
+ style.fill = `linear-gradient(50% 0 50% 100%,
248
+ rgba(${color.r},${color.g},${color.b}, 0.3) 1,
249
+ rgba(${color.r},${color.g},${color.b}, 0.1) 0.3,
250
+ rgba(${color.r},${color.g},${color.b}, 0) 0)`;
251
+ }
209
252
  }
210
253
  else if(typeof style.fill === 'function') {
211
- style.fill = style.fill.call(this, style);
254
+ style.fill = style.fill.call(this, style, isAbove);
212
255
  }
256
+
213
257
  const area = this.graph.createShape('path', {
214
258
  points: this.graph.utils.clone(points, true),
215
259
  style,
@@ -217,20 +261,76 @@ export default class jmLineSeries extends jmSeries {
217
261
  height: this.graph.chartArea.height
218
262
  });
219
263
 
220
- // 在点集合前后加上落地到X轴的点就可以组成一个封闭的图形area
221
- if(needClosePoint) {
264
+ // 在首尾加上基线上的点,组成封闭多边形
265
+ // 如果首/尾点已经在基线上(交叉点),则不重复添加
266
+ if(Math.abs(start.y - axisY) > 0.5) {
222
267
  area.points.unshift({
223
268
  x: start.x,
224
- y: this.baseY
269
+ y: axisY
225
270
  });
271
+ }
272
+ if(Math.abs(end.y - axisY) > 0.5) {
226
273
  area.points.push({
227
274
  x: end.x,
228
- y: this.baseY
275
+ y: axisY
229
276
  });
230
277
  }
231
278
 
232
279
  this.addShape(area);
233
280
  }
281
+
282
+ /**
283
+ * 处理跨基线的混合数据,在交叉点处分割成多个独立区域
284
+ * @param {Array} points 线条上的点
285
+ * @param {number} axisY 基线Y坐标(chartArea相对坐标)
286
+ */
287
+ _createMixedAreas(points, axisY) {
288
+ const segments = [];
289
+ let currentSegment = [];
290
+ let prevAbove = null;
291
+
292
+ for(let i = 0; i < points.length; i++) {
293
+ const p = points[i];
294
+ if(p.y == null || typeof p.y === 'undefined') continue;
295
+
296
+ const isAbove = p.y < axisY;
297
+
298
+ if(currentSegment.length === 0) {
299
+ currentSegment.push(p);
300
+ prevAbove = isAbove;
301
+ } else if(isAbove === prevAbove) {
302
+ currentSegment.push(p);
303
+ } else {
304
+ // 检测到跨基线交叉:计算交叉点
305
+ const prev = currentSegment[currentSegment.length - 1];
306
+ const t = (axisY - prev.y) / (p.y - prev.y);
307
+ const crossPoint = {
308
+ x: prev.x + (p.x - prev.x) * t,
309
+ y: axisY
310
+ };
311
+
312
+ // 当前段在交叉点处结束
313
+ currentSegment.push(crossPoint);
314
+ if(currentSegment.length >= 2) {
315
+ segments.push({ points: [...currentSegment], above: prevAbove });
316
+ }
317
+
318
+ // 新段从交叉点开始
319
+ currentSegment = [crossPoint, p];
320
+ prevAbove = isAbove;
321
+ }
322
+ }
323
+
324
+ // 最后一段
325
+ if(currentSegment.length >= 2) {
326
+ segments.push({ points: currentSegment, above: prevAbove });
327
+ }
328
+
329
+ // 为每段创建独立的面积
330
+ for(const seg of segments) {
331
+ this._createAreaSegment(seg.points, seg.above, axisY);
332
+ }
333
+ }
234
334
  }
235
335
 
236
336
  export {
@@ -51,7 +51,9 @@ export default class jmRingProgressSeries extends jmSeries {
51
51
 
52
52
  const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
53
53
  const lineWidth = this.style.lineWidth || 20;
54
- const startAngle = this.style.startAngle !== undefined ? this.style.startAngle : -90;
54
+ // 角度转弧度(jmArc 内部使用弧度)
55
+ const DEG = Math.PI / 180;
56
+ const startAngle = (this.style.startAngle !== undefined ? this.style.startAngle : -90) * DEG;
55
57
  const maxValue = this.style.max || 100;
56
58
  const field = this.field || 'value';
57
59
 
@@ -92,7 +94,7 @@ export default class jmRingProgressSeries extends jmSeries {
92
94
  center: { x: centerX, y: centerY },
93
95
  radius: radius,
94
96
  startAngle: 0,
95
- endAngle: 360
97
+ endAngle: Math.PI * 2
96
98
  });
97
99
 
98
100
  this.addShape(bgArc);
@@ -105,7 +107,7 @@ export default class jmRingProgressSeries extends jmSeries {
105
107
  if(ratio <= 0) return;
106
108
 
107
109
  const color = this.getColor(null, index);
108
- const endAngle = startAngle + ratio * 360;
110
+ const endAngle = startAngle + ratio * Math.PI * 2;
109
111
 
110
112
  const progressArc = this.graph.createShape('arc', {
111
113
  style: {
@@ -1,240 +0,0 @@
1
- import jmSeries from './series.js';
2
-
3
- /**
4
- * 旭日图
5
- *
6
- * 旭日图用于展示层级数据的占比关系,由多个同心圆环组成,每个圆环代表一个层级。
7
- * 内层圆环是外层圆环的父级,扇形角度代表数据占比。
8
- *
9
- * 数据格式要求:
10
- * - 树形结构数据,每个节点包含 name、value 和 children
11
- *
12
- * 样式配置:
13
- * - innerRadius: 内圆半径,默认 0
14
- * - startAngle: 起始角度,默认 0
15
- * - showLabels: 是否显示标签,默认 true
16
- *
17
- * @class jmSunburstSeries
18
- * @module jmChart
19
- * @extends jmSeries
20
- *
21
- * @example
22
- * chart.createSeries('sunburst', {
23
- * data: [{
24
- * name: 'Root',
25
- * children: [
26
- * { name: 'A', value: 30 },
27
- * { name: 'B', value: 70 }
28
- * ]
29
- * }]
30
- * });
31
- */
32
- export default class jmSunburstSeries extends jmSeries {
33
- constructor(options) {
34
- options.style = options.style || options.graph.style.sunburst;
35
- super(options);
36
- this.maxDepth = 0;
37
- }
38
-
39
- /**
40
- * 初始化旭日图
41
- */
42
- init() {
43
- const data = this.option.data || this.data;
44
- if(!data || !data.length) return;
45
-
46
- const chartWidth = this.graph.chartArea.width;
47
- const chartHeight = this.graph.chartArea.height;
48
- const centerX = chartWidth / 2;
49
- const centerY = chartHeight / 2;
50
- const maxRadius = Math.min(chartWidth, chartHeight) / 2 - 20;
51
-
52
- this.maxDepth = this.calculateMaxDepth(data);
53
- if(this.maxDepth === 0) return;
54
-
55
- const ringWidth = maxRadius / this.maxDepth;
56
- const innerRadius = this.style.innerRadius || 0;
57
-
58
- this.drawLevel(data, centerX, centerY, innerRadius, ringWidth, 0, 360, 0);
59
-
60
- if(this.style.showCenter !== false && innerRadius > 0) {
61
- this.drawCenter(centerX, centerY, innerRadius);
62
- }
63
- }
64
-
65
- /**
66
- * 计算最大深度
67
- */
68
- calculateMaxDepth(data, depth = 1) {
69
- let maxDepth = depth;
70
- for(const node of data) {
71
- if(node.children && node.children.length > 0) {
72
- const childDepth = this.calculateMaxDepth(node.children, depth + 1);
73
- maxDepth = Math.max(maxDepth, childDepth);
74
- }
75
- }
76
- return maxDepth;
77
- }
78
-
79
- /**
80
- * 计算节点总值
81
- */
82
- calculateTotalValue(data) {
83
- let total = 0;
84
- for(const node of data) {
85
- if(node.children && node.children.length > 0) {
86
- total += this.calculateTotalValue(node.children);
87
- } else {
88
- total += node.value || 0;
89
- }
90
- }
91
- return total;
92
- }
93
-
94
- /**
95
- * 绘制层级
96
- */
97
- drawLevel(data, centerX, centerY, innerRadius, ringWidth, startAngle, totalAngle, level) {
98
- const total = this.calculateTotalValue(data);
99
- if(total === 0) return;
100
-
101
- let currentAngle = startAngle;
102
- const outerRadius = innerRadius + ringWidth;
103
-
104
- for(const node of data) {
105
- let nodeValue = 0;
106
- if(node.children && node.children.length > 0) {
107
- nodeValue = this.calculateTotalValue(node.children);
108
- } else {
109
- nodeValue = node.value || 0;
110
- }
111
-
112
- const angleRatio = nodeValue / total;
113
- const nodeAngle = totalAngle * angleRatio;
114
- const endAngle = currentAngle + nodeAngle;
115
-
116
- if(nodeAngle > 0.1) {
117
- const color = this.getColor(node, level);
118
-
119
- this.drawArc(centerX, centerY, innerRadius, outerRadius, currentAngle, endAngle, color, node);
120
-
121
- if(this.style.showLabels !== false && nodeAngle > 10) {
122
- this.drawLabel(centerX, centerY, innerRadius, outerRadius, currentAngle, endAngle, node);
123
- }
124
-
125
- if(node.children && node.children.length > 0) {
126
- this.drawLevel(node.children, centerX, centerY, outerRadius, ringWidth, currentAngle, nodeAngle, level + 1);
127
- }
128
- }
129
-
130
- currentAngle = endAngle;
131
- }
132
- }
133
-
134
- /**
135
- * 绘制扇形
136
- */
137
- drawArc(centerX, centerY, innerRadius, outerRadius, startAngle, endAngle, color, node) {
138
- const arc = this.graph.createShape('arc', {
139
- style: {
140
- fill: color,
141
- stroke: this.style.stroke || '#fff',
142
- lineWidth: this.style.lineWidth || 1,
143
- zIndex: 1
144
- },
145
- center: { x: centerX, y: centerY },
146
- radius: outerRadius,
147
- innerRadius: innerRadius,
148
- startAngle: startAngle - 90,
149
- endAngle: endAngle - 90
150
- });
151
-
152
- this.addShape(arc);
153
- node.shape = arc;
154
- }
155
-
156
- /**
157
- * 绘制标签
158
- */
159
- drawLabel(centerX, centerY, innerRadius, outerRadius, startAngle, endAngle, node) {
160
- const midAngle = (startAngle + endAngle) / 2 - 90;
161
- const midRadius = (innerRadius + outerRadius) / 2;
162
-
163
- const x = centerX + midRadius * Math.cos(midAngle * Math.PI / 180);
164
- const y = centerY + midRadius * Math.sin(midAngle * Math.PI / 180);
165
-
166
- const text = node.name || '';
167
- if(!text) return;
168
-
169
- const label = this.graph.createShape('label', {
170
- style: {
171
- fill: this.style.labelColor || '#fff',
172
- font: this.style.labelFont || '12px Arial',
173
- textAlign: 'center',
174
- textBaseline: 'middle',
175
- zIndex: 10
176
- },
177
- text: text,
178
- position: { x, y }
179
- });
180
-
181
- this.addShape(label);
182
- }
183
-
184
- /**
185
- * 绘制中心
186
- */
187
- drawCenter(centerX, centerY, innerRadius) {
188
- const centerCircle = this.graph.createShape('circle', {
189
- style: {
190
- fill: this.style.centerFill || '#fff',
191
- stroke: this.style.centerStroke || '#e0e0e0',
192
- lineWidth: 1,
193
- zIndex: 0
194
- },
195
- center: { x: centerX, y: centerY },
196
- radius: innerRadius
197
- });
198
-
199
- this.addShape(centerCircle);
200
- }
201
-
202
- /**
203
- * 获取颜色
204
- */
205
- getColor(node, level) {
206
- if(node.color) return node.color;
207
-
208
- if(this.style.colors && this.style.colors.length > 0) {
209
- return this.style.colors[level % this.style.colors.length];
210
- }
211
-
212
- if(typeof this.style.color === 'function') {
213
- return this.style.color.call(this, node, level);
214
- }
215
-
216
- return this.graph.getColor(level);
217
- }
218
-
219
- /**
220
- * 生成图例
221
- */
222
- createLegend() {
223
- const style = this.graph.utils.clone(this.style);
224
- style.fill = this.style.color || this.graph.getColor(0);
225
- style.stroke = style.fill;
226
-
227
- const shape = this.graph.createShape('circle', {
228
- style: style,
229
- center: {
230
- x: this.graph.style.legend.item.shape.width / 2,
231
- y: this.graph.style.legend.item.shape.height / 2
232
- },
233
- radius: this.graph.style.legend.item.shape.height / 2
234
- });
235
-
236
- this.graph.legend.append(this, shape);
237
- }
238
- }
239
-
240
- export { jmSunburstSeries };