jmchart 1.1.94 → 1.1.96

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.
@@ -48,13 +48,15 @@ export default class jmMarkLineManager {
48
48
  area.children.add(this.yMarkLine);
49
49
  }
50
50
 
51
- let longtap = 0;// 是否有长按, 0 未开始,1已按下,2识别为长按
51
+ let longtap = 0; // 是否有长按, 0 未开始,1已按下,2识别为长按
52
+ let lineTouching = 0; // 1=启用标线状态,2=非标线,则可以触发系统行为
52
53
  let longtapHandler = 0;
53
54
  let touchStartPos = {
54
55
  x: 0,
55
56
  y: 0
56
57
  };
57
58
  graph.on('mousedown touchstart', (args) => {
59
+ lineTouching = 0;
58
60
  // 如果长按才启用
59
61
  if(chart.style.markLine.longtap) {
60
62
  longtap = 1;
@@ -102,14 +104,33 @@ export default class jmMarkLineManager {
102
104
  // 如果是长按启用,但手指又滑动了。则取消标线
103
105
  if(longtap === 1) {
104
106
  if(args.offsetInfo.offset > 15) longtap = 0; // 如果移动了,则取消长按
107
+ else lineTouching = 1;
105
108
  }
109
+
110
+ args.event.stopPropagation();
111
+ // 如果指定了锁定图表标线操作值,则触发后当次滑动不再响应系统默认行为
112
+ if(chart.style.markLine.lock){
113
+ // 标线状态一直禁用系统能力
114
+ // 如果指定了锁定值,只需要一项符合要求就进行锁定
115
+ if((lineTouching === 0 &&
116
+ ((chart.style.markLine.lock.y && Math.abs(args.offsetInfo.y) < chart.style.markLine.lock.y) ||
117
+ (chart.style.markLine.lock.x && Math.abs(args.offsetInfo.x) < chart.style.markLine.lock.x)))
118
+ || lineTouching === 1) {
119
+
120
+ lineTouching = 1;
121
+ args.event.preventDefault(); // 阻止默认行为
122
+ }
123
+ }
124
+
125
+ if(lineTouching === 0) lineTouching = 2;
126
+
106
127
  args.longtap = longtap;
107
128
  this.move(args);
108
129
  });
109
130
  // 取消移动
110
131
  graph.on('mouseup touchend touchcancel touchleave', (args) => {
111
132
  longtap = 0;
112
-
133
+ lineTouching = 0;
113
134
  this.endMove(args);
114
135
  });
115
136
  }
package/src/jmChart.js CHANGED
@@ -10,7 +10,7 @@ import jmLegend from './core/legend/legend.js';
10
10
  import jmBarSeries from './series/barSeries.js';
11
11
  import jmStackBarSeries from './series/stackBarSeries.js';
12
12
  import jmPieSeries from './series/pieSeries.js';
13
-
13
+ import jmRadarSeries from './series/radarSeries.js';
14
14
  import jmLineSeries from './series/lineSeries.js';
15
15
  import jmStackLineSeries from './series/stackLineSeries.js';
16
16
  import jmCandlestickSeries from './series/candlestickSeries.js';
@@ -333,7 +333,7 @@ export default class jmChart extends jmGraph {
333
333
  type: 'x',
334
334
  visible: this.style.axis.x === false? false: true,
335
335
  format: this.option.xLabelFormat,
336
- ...this.option.yAxisOption
336
+ ...this.option.xAxisOption
337
337
  }, options || {});
338
338
 
339
339
  if(typeof this.option.minXValue !== 'undefined') {
@@ -366,7 +366,7 @@ export default class jmChart extends jmGraph {
366
366
  visible: this.style.axis.y === false? false: true,
367
367
  format: this.option.yLabelFormat,
368
368
  zeroBase: this.baseY === 0,
369
- ...this.option.xAxisOption,
369
+ ...this.option.yAxisOption,
370
370
  }, options || {});
371
371
  if(typeof this.option.minYValue !== 'undefined') {
372
372
  options.minYValue = typeof options.minYValue === 'undefined'?this.option.minYValue:Math.min(this.option.minYValue, options.minYValue);
@@ -395,6 +395,7 @@ export default class jmChart extends jmGraph {
395
395
  'bar' : jmBarSeries,
396
396
  'stackBar' : jmStackBarSeries,
397
397
  'pie' : jmPieSeries,
398
+ 'radar' : jmRadarSeries,
398
399
  'stackLine' : jmStackLineSeries,
399
400
  'candlestick': jmCandlestickSeries
400
401
  };
@@ -43,7 +43,7 @@ export default class jmPieSeries extends jmSeries {
43
43
  y: this.graph.chartArea.height / 2
44
44
  };
45
45
 
46
- const radius = Math.min(center.x - this.style.margin.left -
46
+ const radius = Math.min(center.x - this.style.margin.left * this.graph.devicePixelRatio -
47
47
  this.style.margin.right * this.graph.devicePixelRatio,
48
48
  center.y - this.style.margin.top * this.graph.devicePixelRatio - this.style.margin.bottom * this.graph.devicePixelRatio);
49
49
 
@@ -141,6 +141,7 @@ export default class jmPieSeries extends jmSeries {
141
141
  style: this.graph.utils.clone(this.style),
142
142
  anticlockwise
143
143
  };
144
+ points.push(p);
144
145
  //p.style.color = this.graph.getColor(index);
145
146
  if(p.style.color && typeof p.style.color === 'function') {
146
147
  p.style.fill = p.style.color.call(this, p);
@@ -218,34 +219,32 @@ export default class jmPieSeries extends jmSeries {
218
219
  // 如果有点击事件
219
220
  if(this.option.onClick) {
220
221
  p.shape.on('click', (e) => {
221
- this.option.onClick.call(this, p, e);
222
+ return this.option.onClick.call(this, p, e);
222
223
  });
223
224
  }
224
225
  if(this.option.onOver) {
225
226
  p.shape.on('mouseover touchover', (e) => {
226
- this.option.onOver.call(this, p, e);
227
+ return this.option.onOver.call(this, p, e);
227
228
  });
228
229
  }
229
230
  if(this.option.onLeave) {
230
231
  p.shape.on('mouseleave touchleave', (e) => {
231
- this.option.onLeave.call(this, p, e);
232
+ return this.option.onLeave.call(this, p, e);
232
233
  });
233
234
  }
234
235
 
235
236
  this.createLabel(p);// 生成标签
237
+ // 生成标点的回调
238
+ this.emit('onPointCreated', p);
236
239
  }
237
- points.push(p);
238
- index++;
239
-
240
- // 生成标点的回调
241
- this.emit('onPointCreated', p);
240
+ index++;
242
241
  }
243
242
  }
244
243
 
245
244
  return points;
246
245
  }
247
246
 
248
- // 生成柱图的标注
247
+ // 生成图的标注
249
248
  createLabel(point) {
250
249
  if(this.style.label && this.style.label.show === false) return;
251
250
 
@@ -269,7 +268,7 @@ export default class jmPieSeries extends jmSeries {
269
268
  const parentRect = this.parent.getBounds();
270
269
  //const rect = this.getBounds.call(this.parent);
271
270
 
272
- // 圆弧的中间位,离圆心最近和最完的二个点
271
+ // 圆弧的中间位,离圆心最近和最远的二个点
273
272
  let centerMaxPoint = this.parent.points[Math.floor(this.parent.points.length / 2)];
274
273
  let centerMinPoint = this.parent.center;
275
274
  // 如果是空心圆,则要计算 1/4 和 3/4位的点。顺时针和逆时针二个点大小不一样,这里只取,大小计算时处理
@@ -0,0 +1,332 @@
1
+ import {
2
+ jmControl
3
+ } from 'jmgraph';
4
+ import jmSeries from './series.js';
5
+
6
+ /**
7
+ * 雷达图
8
+ *
9
+ * @class jmRadarSeries
10
+ * @module jmChart
11
+ * @param {jmChart} chart 当前图表
12
+ * @param {array} mappings 图形字段映射
13
+ * @param {style} style 样式
14
+ */
15
+
16
+ //构造函数
17
+ export default class jmRadarSeries extends jmSeries {
18
+ constructor(options) {
19
+ super(options);
20
+ }
21
+
22
+ // 重新生成轴,雷达图只需要Y轴即可
23
+ createAxises(center, radius) {
24
+
25
+ this.axises = [
26
+ this.yAxis
27
+ ];
28
+
29
+ const yCount = this.field.length;
30
+ if(!yCount) return;
31
+
32
+ //每个维度点的角度
33
+ const rotateStep = Math.PI * 2 / yCount;
34
+
35
+ // 清空除了一个默认外的所有Y轴
36
+ for(let index in this.graph.yAxises) {
37
+ const axis = this.graph.yAxises[index];
38
+ if(!axis || axis === this.yAxis) continue;
39
+ axis.remove();
40
+ delete this.graph.yAxises[index];
41
+ }
42
+
43
+ for(let index=0; index < yCount; index++) {
44
+ if(!this.field[index]) continue;
45
+ let axis = this.yAxis;
46
+ // 除了默认的y轴外,其它都重新生成
47
+ if(index > 0) {
48
+ axis = this.graph.createYAxis({
49
+ index: index + 1,
50
+ format: this.option.yLabelFormat || this.graph.option.yLabelFormat
51
+ });
52
+ this.axises.push(axis);
53
+ }
54
+ const rotate = Math.PI/2 + rotateStep * index; //从向上90度开始
55
+ axis.init({
56
+ field: this.field[index],
57
+ radarOption: {
58
+ center,
59
+ radius,
60
+ yCount,
61
+ rotate: rotate,
62
+ cos: Math.cos(rotate),
63
+ sin: Math.sin(rotate)
64
+ }
65
+ });
66
+
67
+ }
68
+
69
+ // x轴初始化
70
+ this.xAxis.init({
71
+ radarOption: {
72
+ center,
73
+ radius,
74
+ yCount,
75
+ yAxises: this.axises
76
+ }
77
+ });
78
+
79
+ return this.axises;
80
+ }
81
+
82
+ // 计算最大值和最小值,一般图形直接采用最大最小值即可,有些需要多值叠加
83
+ initAxisValue() {
84
+ this.center = {
85
+ x: this.graph.chartArea.width / 2,
86
+ y: this.graph.chartArea.height / 2
87
+ };
88
+
89
+ this.radius = Math.min(this.center.x - this.style.margin.left * this.graph.devicePixelRatio -
90
+ this.style.margin.right * this.graph.devicePixelRatio,
91
+ this.center.y - this.style.margin.top * this.graph.devicePixelRatio - this.style.margin.bottom * this.graph.devicePixelRatio);
92
+
93
+ const axises = this.createAxises(this.center, this.radius);// 重置所有轴
94
+ // 计算最大最小值
95
+ // 当前需要先更新axis的边界值,轴好画图
96
+ for(let i=0; i< this.data.length;i++) {
97
+ axises.forEach((axis)=> {
98
+ const v = this.data[i][axis.field];
99
+ axis.max(v);
100
+ axis.min(v);
101
+ });
102
+ }
103
+ }
104
+
105
+ // 重新初始化图形
106
+ init() {
107
+ //生成描点位
108
+ // super.init会把参数透传给 createPoints
109
+ const { points, dataChanged } = this.initDataPoint(this.center, this.radius);
110
+
111
+ this.createLegend(points);
112
+
113
+ // 是否正在动画中
114
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 );
115
+
116
+ const aniCount = (this.style.aniCount || 20);
117
+ let aniIsEnd = true;// 当次是否结束动画
118
+ const len = points.length;
119
+ const shapeMap = {};
120
+ const self = this;
121
+
122
+ for(let i=0; i<len; i++) {
123
+ const p = points[i];
124
+ let shape = shapeMap[p.index];
125
+ if(!shape) {
126
+ shape = shapeMap[p.index] = this.graph.createShape('path', {
127
+ ...p,
128
+ style: p.style,
129
+ points: [],
130
+ });
131
+ this.addShape(shape);
132
+
133
+ // 如果有点击事件
134
+ if(this.option.onClick) {
135
+ shape.on('click', function(e) {
136
+ return self.option.onClick.call(this, e);
137
+ });
138
+ }
139
+ if(this.option.onOver) {
140
+ shape.on('mouseover touchover', function(e) {
141
+ return self.option.onOver.call(this, e);
142
+ });
143
+ }
144
+ if(this.option.onLeave) {
145
+ shape.on('mouseleave touchleave', function(e) {
146
+ return self.option.onLeave.call(this, e);
147
+ });
148
+ }
149
+ }
150
+
151
+ shape.zIndex += p.radius / this.radius;// 用每个轴占比做为排序号,这样占面积最大的排最底层
152
+ let point = null;
153
+ // 在动画中,则一直刷新
154
+ if(isRunningAni) {
155
+ let step = p.radius / aniCount * this.___animateCounter;
156
+ if(step >= p.radius) {
157
+ step = p.radius;
158
+ }
159
+ else {
160
+ aniIsEnd = false;
161
+ }
162
+ point = {
163
+ ...p,
164
+ x: this.center.x + p.axis.radarOption.cos * step,
165
+ y: this.center.y - p.axis.radarOption.sin * step,
166
+ };
167
+ }
168
+ else {
169
+ point = p;
170
+ }
171
+ shape.points.push(point);
172
+ this.createLabel(point);// 生成标签
173
+ }
174
+ // 所有动画都完成,则清空计数器
175
+ if(aniIsEnd) {
176
+ this.___animateCounter = 0;
177
+ }
178
+ else {
179
+ this.___animateCounter ++;
180
+ // next tick 再次刷新
181
+ this.graph.utils.requestAnimationFrame(()=>{
182
+ this.needUpdate = true;//需要刷新
183
+ });
184
+ }
185
+ }
186
+
187
+ /**
188
+ * 生成序列图描点
189
+ *
190
+ * @method createPoints
191
+ */
192
+ createPoints(center, radius) {
193
+ if(!this.data || !this.axises) return [];
194
+ center = center || this.center;
195
+ const points = [];
196
+
197
+ for(var i=0;i< this.data.length;i++) {
198
+ const s = this.data[i];
199
+ const style = this.graph.utils.clone(this.style);
200
+ if(style.color && typeof style.color === 'function') {
201
+ style.stroke = style.color.call(this, {
202
+ data: s,
203
+ index: i
204
+ });
205
+ }
206
+ else {
207
+ style.stroke = this.graph.getColor(i);
208
+ }
209
+ if(typeof style.fill === 'function') {
210
+ style.fill = style.fill.call(this, style);
211
+ }
212
+ else {
213
+ const color = this.graph.utils.hexToRGBA(style.stroke);
214
+ style.fill = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
215
+ }
216
+
217
+ const shapePoints = [];
218
+ for(const axis of this.axises) {
219
+ const yv = s[axis.field];
220
+
221
+ const p = {
222
+ x: center.x,
223
+ y: center.y,
224
+ index: i,
225
+ radius: 0,
226
+ data: s,
227
+ yValue: yv,
228
+ yLabel: yv,
229
+ style,
230
+ axis
231
+ };
232
+ shapePoints.push(p);
233
+
234
+ //如果Y值不存在。则此点无效,不画图
235
+ if(yv == null || typeof yv == 'undefined') {
236
+ continue;
237
+ }
238
+
239
+ p.radius = Math.abs(yv - axis.min()) * axis.step();
240
+ p.x = center.x + axis.radarOption.cos * p.radius;
241
+ p.y = center.y - axis.radarOption.sin * p.radius;
242
+
243
+ // 生成标点的回调
244
+ this.emit('onPointCreated', p);
245
+ }
246
+ points.push(...shapePoints);
247
+ }
248
+
249
+ return points;
250
+ }
251
+
252
+ // 生成图的标注
253
+ createLabel(point) {
254
+ if(this.style.label && this.style.label.show === false) return;
255
+
256
+ const text = this.option.itemLabelFormat?this.option.itemLabelFormat.call(this, point): point.yValue;
257
+ if(!text) return;
258
+
259
+ // v如果指定了为控件,则直接加入
260
+ if(text instanceof jmControl) {
261
+ this.addShape(text);
262
+ return text;
263
+ }
264
+ const self = this;
265
+ const label = this.graph.createShape('label', {
266
+ style: this.style.label,
267
+ text: text,
268
+ point,
269
+ position: function() {
270
+ const p = {
271
+ x: this.option.point.x,
272
+ y: this.option.point.y
273
+ }
274
+ if(p.x < self.center.x) {
275
+ p.x -= this.width;
276
+ }
277
+
278
+ if(p.y < self.center.y) {
279
+ p.y -= this.height;
280
+ }
281
+ return p;
282
+ }
283
+ });
284
+
285
+ this.addShape(label);
286
+ }
287
+ }
288
+
289
+
290
+
291
+ /**
292
+ * 生成图例
293
+ *
294
+ * @method createLegend
295
+ */
296
+ jmRadarSeries.prototype.createLegend = function(points) {
297
+ if(!points || !points.length) return;
298
+
299
+ const legendMap = {};
300
+ for(let k in points) {
301
+ const p = points[k];
302
+ if(!p) continue;
303
+ if(legendMap[p.index]) continue;
304
+ //生成图例前的图标
305
+ const style = this.graph.utils.clone(p.style);
306
+ style.fill = style.fill;
307
+ //delete style.stroke;
308
+ const shape = legendMap[p.index] = this.graph.createShape('rect',{
309
+ style: style,
310
+ position : {x: 0, y: 0}
311
+ });
312
+ //shape.targetShape = p.shape;
313
+ //此处重写图例事件
314
+ this.graph.legend.append(this, shape, {
315
+ name: this.legendLabel,
316
+ hover: function() {
317
+ //var sp = this.children.get(0);
318
+ //应用图的动态样式
319
+ Object.assign(this.targetShape.style, this.targetShape.style.hover);
320
+ Object.assign(this.style, this.style.hover);
321
+ },
322
+ leave: function() {
323
+ //var sp = this.children.get(0);
324
+ //应用图的普通样式
325
+ Object.assign(this.targetShape.style, this.targetShape.style.normal);
326
+ Object.assign(this.style, this.style.normal);
327
+ },
328
+ data: p.data
329
+ });
330
+ }
331
+ }
332
+
@@ -34,7 +34,7 @@ export default class jmSeries extends jmPath {
34
34
 
35
35
  // 初始化一些参数, 因为这里有多个Y轴的可能,所以每次都需要重调一次init
36
36
  this.yAxis.init({
37
- field: this.field,
37
+ field: Array.isArray(this.field)? this.field[0]: this.field,
38
38
  minYValue: options.minYValue,
39
39
  maxYValue: options.maxYValue
40
40
  });
@@ -121,6 +121,10 @@ export default class jmSeries extends jmPath {
121
121
  else {
122
122
  this.dataPoints = this.createPoints(...args);
123
123
  }
124
+ // 执行初始化函数回调
125
+ if(this.option && this.option.onInit) {
126
+ this.option.onInit.apply(this, args);
127
+ }
124
128
  return {
125
129
  dataChanged,
126
130
  points: this.dataPoints
@@ -183,12 +187,12 @@ export default class jmSeries extends jmPath {
183
187
  while(shape = this.shapes.shift()) {
184
188
  shape && shape.remove();
185
189
  }
190
+
191
+ this.initAxisValue();// 处理最大值最小值
186
192
 
187
193
  //生成图例 这里要放到shape清理后面
188
194
  this.createLegend();
189
195
 
190
- this.initAxisValue();// 处理最大值最小值
191
-
192
196
  return this.chartInfo = {
193
197
  xAxis: this.xAxis,
194
198
  yAxis: this.yAxis
@@ -316,6 +320,7 @@ export default class jmSeries extends jmPath {
316
320
  style
317
321
  });
318
322
  this.graph.legend.append(this, shape);
323
+ return shape;
319
324
  }
320
325
 
321
326
  // 生成柱图的标注
@@ -43,15 +43,19 @@ export default class jmStackBarSeries extends jmBarSeries {
43
43
  let bottomStartY = this.baseY;
44
44
  for(let index=0; index < point.points.length; index ++) {
45
45
  const style = this.graph.utils.clone(this.style);
46
+ const p = point.points[index];
47
+
46
48
  if(style.color && typeof style.color === 'function') {
47
- style.fill = style.color.call(this, this, index);
49
+ style.fill = style.color.call(this, {
50
+ index,
51
+ point: p
52
+ });
48
53
  }
49
54
  else {
50
55
  style.fill = this.graph.getColor(index);
51
56
  }
52
- const sp = this.addShape(this.graph.createPath(null, style));
57
+ const sp = this.addShape(this.graph.createPath(null, style));
53
58
 
54
- const p = point.points[index];
55
59
  let startY = topStartY;
56
60
  if(p.yValue < this.baseYValue) startY = bottomStartY;
57
61