jmchart 1.2.12 → 1.2.15

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.
@@ -1,8 +1,23 @@
1
-
2
1
  import jmSeries from './series.js';
3
2
 
4
3
  /**
5
- * K线图
4
+ * K线图(蜡烛图)
5
+ *
6
+ * K线图是金融领域常用的图表类型,用于显示股票、期货等金融产品的价格走势。
7
+ * 每根K线包含四个价格:开盘价、收盘价、最高价、最低价。
8
+ *
9
+ * 数据格式要求:
10
+ * - fields: ['open', 'close', 'high', 'low']
11
+ * - open: 开盘价
12
+ * - close: 收盘价
13
+ * - high: 最高价
14
+ * - low: 最低价
15
+ *
16
+ * 样式配置:
17
+ * - masculineColor: 阳线颜色(收盘价 > 开盘价),默认红色
18
+ * - negativeColor: 阴线颜色(收盘价 < 开盘价),默认绿色
19
+ * - barWidth: K线宽度(像素)
20
+ * - perWidth: K线宽度占比(0-1),默认0.4
6
21
  *
7
22
  * @class jmCandlestickSeries
8
23
  * @module jmChart
@@ -11,44 +26,41 @@ import jmSeries from './series.js';
11
26
  * @param {style} style 样式
12
27
  */
13
28
 
14
- //构造函数
15
29
  export default class jmCandlestickSeries extends jmSeries {
16
30
  constructor(options) {
17
31
  options.style = options.style || options.graph.style.line;
18
32
  super(options);
19
-
20
- //this.on('beginDraw', this[PreDrawKey]);
21
33
  }
22
34
 
23
35
  /**
24
- * 绘制图形前 初始化线条
25
- *
26
- * @method preDraw
27
- * @for jmLineSeries
36
+ * 初始化K线图
37
+ *
38
+ * 绘制逻辑:
39
+ * 1. 遍历所有数据点
40
+ * 2. 判断阳线或阴线
41
+ * 3. 绘制K线实体和上下影线
28
42
  */
29
43
  init() {
30
- //生成描点位
31
- const {
32
- points
33
- } = this.initDataPoint();
44
+ const { points } = this.initDataPoint();
34
45
 
35
- //去除多余的线条
36
- //当数据源线条数比现有的少时,删除多余的线条
37
46
  const len = points.length;
47
+ if(!len) return;
48
+
38
49
  this.initWidth(len);
39
50
 
40
- const w = this.barWidth / 2; //实心处宽度的一半
51
+ const w = this.barWidth / 2;
41
52
 
42
- for(let i=0; i< len;i++) {
53
+ for(let i = 0; i < len; i++) {
43
54
  const p = points[i];
44
55
 
45
- //如果当前点无效,则跳致下一点
46
- if(typeof p.y === 'undefined' || p.y === null) {
47
- //prePoint = null;
56
+ if(typeof p.y === 'undefined' || p.y === null) {
48
57
  continue;
49
58
  }
50
59
 
51
- const sp = this.addShape(this.graph.createPath([], p.style));
60
+ if(!p.points || p.points.length < 4) {
61
+ console.warn('K线图数据不完整,需要4个字段(开盘、收盘、最高、最低)');
62
+ continue;
63
+ }
52
64
 
53
65
  const bl = {
54
66
  x: p.x - w,
@@ -67,14 +79,15 @@ export default class jmCandlestickSeries extends jmSeries {
67
79
  y: p.points[1].y
68
80
  };
69
81
 
70
- // 默认认为是阳线
71
82
  let tm = p.points[1];
72
83
  let bm = p.points[0];
73
- p.style.stroke = p.style.fill = p.style.masculineColor || 'red';
84
+
85
+ // 使用浅拷贝避免污染共享引用
86
+ const style = Object.assign({}, p.style);
87
+ style.stroke = style.fill = style.masculineColor || 'red';
74
88
 
75
- // 开盘大于收盘,则阴线
76
89
  if(p.points[0].yValue > p.points[1].yValue) {
77
- p.style.stroke = p.style.fill = p.style.negativeColor || 'green';
90
+ style.stroke = style.fill = style.negativeColor || 'green';
78
91
  bl.y = br.y = p.points[1].y;
79
92
  tl.y = tr.y = p.points[0].y;
80
93
 
@@ -82,17 +95,19 @@ export default class jmCandlestickSeries extends jmSeries {
82
95
  bm = p.points[1];
83
96
  }
84
97
 
98
+ const sp = this.addShape(this.graph.createPath([], style));
85
99
  sp.points.push(p.points[2], tm, tl, bl, bm, p.points[3], bm, br, tr, tm, p.points[2]);
86
100
 
87
- // 生成关健值标注
88
101
  this.emit('onPointCreated', p);
89
102
  }
90
103
  }
91
104
 
92
- // 计算实心体宽度
105
+ /**
106
+ * 计算K线宽度
107
+ *
108
+ * @param {number} count 数据点数量
109
+ */
93
110
  initWidth(count) {
94
- //计算每个柱子占宽
95
- //每项柱子占宽除以柱子个数,默认最大宽度为30
96
111
  const maxWidth = this.xAxis.width / count;
97
112
 
98
113
  if(this.style.barWidth > 0) {
@@ -0,0 +1,190 @@
1
+ import jmSeries from './series.js';
2
+
3
+ /**
4
+ * 漏斗图
5
+ *
6
+ * 漏斗图用于展示数据在不同阶段的转化情况,常用于分析业务流程中的转化率。
7
+ * 每个阶段用一个梯形表示,宽度代表该阶段的数值大小。
8
+ *
9
+ * 数据格式要求:
10
+ * - field: 'value' - 数值字段
11
+ * - xField: 'name' - 阶段名称字段
12
+ *
13
+ * 样式配置:
14
+ * - align: 'center'|'left'|'right' - 对齐方式,默认 center
15
+ * - gap: 每层之间的间隔,默认 2
16
+ * - colors: 颜色数组,默认使用图表配色
17
+ *
18
+ * @class jmFunnelSeries
19
+ * @module jmChart
20
+ * @extends jmSeries
21
+ *
22
+ * @example
23
+ * chart.createSeries('funnel', {
24
+ * field: 'value',
25
+ * xField: 'name',
26
+ * style: {
27
+ * align: 'center',
28
+ * gap: 2
29
+ * }
30
+ * });
31
+ */
32
+ export default class jmFunnelSeries extends jmSeries {
33
+ constructor(options) {
34
+ options.style = options.style || options.graph.style.funnel;
35
+ super(options);
36
+ }
37
+
38
+ /**
39
+ * 初始化漏斗图
40
+ */
41
+ init() {
42
+ const data = this.data;
43
+ if(!data || !data.length) return;
44
+
45
+ const chartWidth = this.graph.chartArea.width;
46
+ const chartHeight = this.graph.chartArea.height;
47
+ const centerX = chartWidth / 2;
48
+
49
+ const field = this.field || 'value';
50
+ const xField = this.option.xField || 'name';
51
+
52
+ const values = data.map(item => Math.abs(item[field] || 0));
53
+ const maxValue = Math.max(...values);
54
+
55
+ if(maxValue === 0) return;
56
+
57
+ const align = this.style.align || 'center';
58
+ const gap = this.style.gap || 2;
59
+ const len = data.length;
60
+ const layerHeight = (chartHeight - gap * (len - 1)) / len;
61
+
62
+ let currentY = 0;
63
+
64
+ for(let i = 0; i < len; i++) {
65
+ const item = data[i];
66
+ const value = values[i];
67
+ const ratio = value / maxValue;
68
+ const width = chartWidth * ratio * 0.8;
69
+
70
+ let topLeftX, topRightX, bottomLeftX, bottomRightX;
71
+
72
+ const nextValue = i < len - 1 ? values[i + 1] : 0;
73
+ const nextRatio = nextValue / maxValue;
74
+ const nextWidth = chartWidth * nextRatio * 0.8;
75
+
76
+ switch(align) {
77
+ case 'left':
78
+ topLeftX = 0;
79
+ topRightX = width;
80
+ bottomLeftX = 0;
81
+ bottomRightX = nextWidth;
82
+ break;
83
+ case 'right':
84
+ topLeftX = chartWidth - width;
85
+ topRightX = chartWidth;
86
+ bottomLeftX = chartWidth - nextWidth;
87
+ bottomRightX = chartWidth;
88
+ break;
89
+ case 'center':
90
+ default:
91
+ topLeftX = centerX - width / 2;
92
+ topRightX = centerX + width / 2;
93
+ bottomLeftX = centerX - nextWidth / 2;
94
+ bottomRightX = centerX + nextWidth / 2;
95
+ break;
96
+ }
97
+
98
+ const color = this.getColor(item, i);
99
+ const style = {
100
+ fill: color,
101
+ stroke: this.style.stroke || '#fff',
102
+ lineWidth: this.style.lineWidth || 1,
103
+ zIndex: 1
104
+ };
105
+
106
+ const trapezoid = this.graph.createShape('path', {
107
+ style: style,
108
+ points: [
109
+ { x: topLeftX, y: currentY },
110
+ { x: topRightX, y: currentY },
111
+ { x: bottomRightX, y: currentY + layerHeight },
112
+ { x: bottomLeftX, y: currentY + layerHeight }
113
+ ]
114
+ });
115
+
116
+ this.addShape(trapezoid);
117
+
118
+ if(this.style.label && this.style.label.show !== false) {
119
+ this.createFunnelLabel(item, topLeftX, topRightX, currentY, layerHeight, i, field, xField);
120
+ }
121
+
122
+ currentY += layerHeight + gap;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * 创建漏斗图标签
128
+ */
129
+ createFunnelLabel(item, leftX, rightX, y, height, index, field, xField) {
130
+ const centerX = (leftX + rightX) / 2;
131
+ const text = this.option.itemLabelFormat
132
+ ? this.option.itemLabelFormat.call(this, { data: item, xLabel: item[xField], yValue: item[field] })
133
+ : `${item[xField]}: ${item[field]}`;
134
+
135
+ if(!text) return;
136
+
137
+ const label = this.graph.createShape('label', {
138
+ style: {
139
+ fill: this.style.label.fill || '#fff',
140
+ font: this.style.label.font || '12px Arial',
141
+ textAlign: 'center',
142
+ textBaseline: 'middle',
143
+ zIndex: 10
144
+ },
145
+ text: text,
146
+ position: {
147
+ x: centerX,
148
+ y: y + height / 2
149
+ }
150
+ });
151
+
152
+ this.addShape(label);
153
+ }
154
+
155
+ /**
156
+ * 获取颜色
157
+ */
158
+ getColor(item, index) {
159
+ if(this.style.colors && this.style.colors.length > 0) {
160
+ return this.style.colors[index % this.style.colors.length];
161
+ }
162
+ if(typeof this.style.color === 'function') {
163
+ return this.style.color.call(this, item, index);
164
+ }
165
+ return this.graph.getColor(index);
166
+ }
167
+
168
+ /**
169
+ * 生成图例
170
+ */
171
+ createLegend() {
172
+ const style = this.graph.utils.clone(this.style);
173
+ style.fill = this.style.color || this.graph.getColor(0);
174
+ style.stroke = style.fill;
175
+
176
+ const shape = this.graph.createShape('path', {
177
+ style: style,
178
+ points: [
179
+ { x: 0, y: 0 },
180
+ { x: this.graph.style.legend.item.shape.width, y: 0 },
181
+ { x: this.graph.style.legend.item.shape.width * 0.6, y: this.graph.style.legend.item.shape.height },
182
+ { x: this.graph.style.legend.item.shape.width * 0.4, y: this.graph.style.legend.item.shape.height }
183
+ ]
184
+ });
185
+
186
+ this.graph.legend.append(this, shape);
187
+ }
188
+ }
189
+
190
+ export { jmFunnelSeries };
@@ -0,0 +1,255 @@
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
2
+
3
+ /**
4
+ * 仪表盘
5
+ *
6
+ * @class jmGaugeSeries
7
+ * @module jmChart
8
+ * @param {jmChart} chart 当前图表
9
+ * @param {array} mappings 图形字段映射
10
+ * @param {style} style 样式
11
+ */
12
+
13
+ export default class jmGaugeSeries extends jmSeries {
14
+ constructor(options) {
15
+ options.style = options.style || options.graph.style.gauge;
16
+ super(options);
17
+ }
18
+
19
+ /**
20
+ * 初始化仪表盘
21
+ */
22
+ init() {
23
+ const {
24
+ points,
25
+ dataChanged
26
+ } = this.initDataPoint();
27
+
28
+ const len = points.length;
29
+ if(!len) return;
30
+
31
+ const p = points[0];
32
+ const value = typeof p.yValue === 'number' ? p.yValue : 0;
33
+
34
+ const min = this.style.min || 0;
35
+ const max = this.style.max || 100;
36
+ const startAngle = this.style.startAngle || -150;
37
+ const endAngle = this.style.endAngle || 150;
38
+
39
+ const angleRange = endAngle - startAngle;
40
+
41
+ const normalizedValue = Math.max(min, Math.min(max, value));
42
+ const ratio = (normalizedValue - min) / (max - min);
43
+ const currentAngle = startAngle + (ratio * angleRange);
44
+
45
+ const centerX = this.graph.chartArea.width / 2;
46
+ const centerY = this.graph.chartArea.height / 2;
47
+ const radius = Math.min(centerX, centerY) * (this.style.radiusScale || 0.8);
48
+
49
+ this.createGaugeBackground(centerX, centerY, radius, startAngle, endAngle);
50
+ this.createGaugeTicks(centerX, centerY, radius, startAngle, endAngle, min, max);
51
+ this.createGaugePointer(centerX, centerY, radius, currentAngle);
52
+ this.createGaugeCenter(centerX, centerY, radius);
53
+ this.createGaugeValueLabel(centerX, centerY, radius, value);
54
+
55
+ this.emit('onPointCreated', p);
56
+ }
57
+
58
+ /**
59
+ * 创建仪表盘背景
60
+ */
61
+ createGaugeBackground(centerX, centerY, radius, startAngle, endAngle) {
62
+ const backgroundStyle = {
63
+ stroke: this.style.backgroundStroke || '#e0e0e0',
64
+ fill: this.style.backgroundFill || 'transparent',
65
+ lineWidth: this.style.backgroundLineWidth || 20,
66
+ zIndex: 1
67
+ };
68
+
69
+ const backgroundArc = this.graph.createShape('arc', {
70
+ style: backgroundStyle,
71
+ center: { x: centerX, y: centerY },
72
+ radius: radius,
73
+ startAngle: startAngle,
74
+ endAngle: endAngle
75
+ });
76
+
77
+ this.addShape(backgroundArc);
78
+
79
+ if(this.style.gradient) {
80
+ const gradientColors = this.style.gradientColors || [
81
+ { offset: 0, color: '#52c41a' },
82
+ { offset: 0.7, color: '#faad14' },
83
+ { offset: 1, color: '#f5222d' }
84
+ ];
85
+
86
+ const step = (endAngle - startAngle) / (gradientColors.length - 1);
87
+
88
+ for(let i = 0; i < gradientColors.length - 1; i++) {
89
+ const start = startAngle + (step * i);
90
+ const end = startAngle + (step * (i + 1));
91
+
92
+ const gradientArc = this.graph.createShape('arc', {
93
+ style: {
94
+ stroke: gradientColors[i].color,
95
+ fill: 'transparent',
96
+ lineWidth: this.style.lineWidth || 15,
97
+ zIndex: 2
98
+ },
99
+ center: { x: centerX, y: centerY },
100
+ radius: radius - (this.style.backgroundLineWidth || 20) / 2,
101
+ startAngle: start,
102
+ endAngle: end
103
+ });
104
+
105
+ this.addShape(gradientArc);
106
+ }
107
+ }
108
+ }
109
+
110
+ /**
111
+ * 创建仪表盘刻度
112
+ */
113
+ createGaugeTicks(centerX, centerY, radius, startAngle, endAngle, min, max) {
114
+ const tickCount = this.style.tickCount || 10;
115
+ const angleStep = (endAngle - startAngle) / (tickCount - 1);
116
+
117
+ for(let i = 0; i < tickCount; i++) {
118
+ const angle = startAngle + (angleStep * i);
119
+ const value = min + ((max - min) / (tickCount - 1)) * i;
120
+
121
+ const tickStartX = centerX + Math.cos(angle * Math.PI / 180) * (radius - 30);
122
+ const tickStartY = centerY + Math.sin(angle * Math.PI / 180) * (radius - 30);
123
+ const tickEndX = centerX + Math.cos(angle * Math.PI / 180) * (radius - 10);
124
+ const tickEndY = centerY + Math.sin(angle * Math.PI / 180) * (radius - 10);
125
+
126
+ const tickLine = this.graph.createShape('line', {
127
+ style: {
128
+ stroke: this.style.tickColor || '#666',
129
+ lineWidth: 2,
130
+ zIndex: 3
131
+ },
132
+ start: { x: tickStartX, y: tickStartY },
133
+ end: { x: tickEndX, y: tickEndY }
134
+ });
135
+
136
+ this.addShape(tickLine);
137
+
138
+ const labelX = centerX + Math.cos(angle * Math.PI / 180) * (radius - 45);
139
+ const labelY = centerY + Math.sin(angle * Math.PI / 180) * (radius - 45);
140
+
141
+ const tickLabel = this.graph.createShape('label', {
142
+ style: {
143
+ fill: this.style.tickLabelColor || '#333',
144
+ font: '12px Arial',
145
+ textAlign: 'center',
146
+ textBaseline: 'middle',
147
+ zIndex: 4
148
+ },
149
+ position: { x: labelX, y: labelY },
150
+ text: value.toString()
151
+ });
152
+
153
+ this.addShape(tickLabel);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * 创建仪表盘指针
159
+ */
160
+ createGaugePointer(centerX, centerY, radius, angle) {
161
+ const pointerLength = radius * 0.7;
162
+ const pointerX = centerX + Math.cos(angle * Math.PI / 180) * pointerLength;
163
+ const pointerY = centerY + Math.sin(angle * Math.PI / 180) * pointerLength;
164
+
165
+ const pointerLine = this.graph.createShape('line', {
166
+ style: {
167
+ stroke: this.style.pointerColor || '#333',
168
+ lineWidth: this.style.pointerWidth || 3,
169
+ zIndex: 5
170
+ },
171
+ start: { x: centerX, y: centerY },
172
+ end: { x: pointerX, y: pointerY }
173
+ });
174
+
175
+ this.addShape(pointerLine);
176
+ }
177
+
178
+ /**
179
+ * 创建仪表盘中心
180
+ */
181
+ createGaugeCenter(centerX, centerY, radius) {
182
+ const centerCircle = this.graph.createShape('circle', {
183
+ style: {
184
+ fill: this.style.centerColor || '#333',
185
+ stroke: 'transparent',
186
+ zIndex: 6
187
+ },
188
+ center: { x: centerX, y: centerY },
189
+ radius: this.style.centerRadius || 8
190
+ });
191
+
192
+ this.addShape(centerCircle);
193
+ }
194
+
195
+ /**
196
+ * 创建仪表盘值标签
197
+ */
198
+ createGaugeValueLabel(centerX, centerY, radius, value) {
199
+ const valueLabel = this.graph.createShape('label', {
200
+ style: {
201
+ fill: this.style.valueColor || '#333',
202
+ font: this.style.valueFont || '24px Arial',
203
+ textAlign: 'center',
204
+ textBaseline: 'middle',
205
+ zIndex: 7
206
+ },
207
+ position: { x: centerX, y: centerY + radius * 0.3 },
208
+ text: value.toString()
209
+ });
210
+
211
+ this.addShape(valueLabel);
212
+
213
+ if(this.style.unit) {
214
+ const unitLabel = this.graph.createShape('label', {
215
+ style: {
216
+ fill: this.style.unitColor || '#666',
217
+ font: this.style.unitFont || '14px Arial',
218
+ textAlign: 'center',
219
+ textBaseline: 'middle',
220
+ zIndex: 8
221
+ },
222
+ position: { x: centerX, y: centerY + radius * 0.5 },
223
+ text: this.style.unit
224
+ });
225
+
226
+ this.addShape(unitLabel);
227
+ }
228
+ }
229
+
230
+ /**
231
+ * 生成图例
232
+ */
233
+ createLegend() {
234
+ var style = this.graph.utils.clone(this.style);
235
+ style.stroke = style.color;
236
+ style.fill = style.color;
237
+
238
+ var shape = this.graph.createShape('arc', {
239
+ style: style,
240
+ center: {
241
+ x: this.graph.style.legend.item.shape.width / 2,
242
+ y: this.graph.style.legend.item.shape.height / 2
243
+ },
244
+ radius: this.graph.style.legend.item.shape.height / 2,
245
+ startAngle: -150,
246
+ endAngle: 150
247
+ });
248
+
249
+ this.graph.legend.append(this, shape);
250
+ }
251
+ }
252
+
253
+ export {
254
+ jmGaugeSeries
255
+ }