jmchart 1.1.95 → 1.1.98

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.
@@ -73,26 +73,8 @@ jmLegend.prototype.append = function(series, shape, options = {}) {
73
73
  panel.height = shape.height;
74
74
 
75
75
  //执行进入事件
76
- //触动图例后加粗显示图
77
- /*const hover = options.hover || function() {
78
- //应用图的动态样式
79
- //Object.assign(series.style, series.style.hover);
80
-
81
- //Object.assign(this.style, this.style.hover || {});
82
-
83
- //series.graph.refresh();
84
- };
85
- panel.bind('mouseover', hover);
86
- //执行离开
87
- const leave = options.leave || function() {
88
- //应用图的普通样式
89
- //Object.assign(series.style, series.style.normal);
90
-
91
- //Object.assign(this.style, this.style.normal || {});
92
- //jmUtils.apply(this.series.style.normal,this.series.style);
93
- //series.graph.refresh();
94
- };
95
- panel.bind('mouseleave', leave);*/
76
+ options.hover && panel.bind('mouseover touchover', options.hover);
77
+ options.leave && panel.bind('mouseleave touchleave', options.leave);
96
78
 
97
79
  const legendPosition = this.legendPosition || this.style.legendPosition || 'right';
98
80
  if(legendPosition == 'top' || legendPosition == 'bottom') {
@@ -70,6 +70,7 @@ export default class jmMarkLine extends jmLine {
70
70
  this.start.x = this.end.x = isTocuhGraph? (point.x + graph.chartArea.position.x): point.x;
71
71
 
72
72
  for(const p of point.points) {
73
+ if(!p || typeof p.y === 'undefined') continue;
73
74
  this.markArc = graph.createShape('circle', {
74
75
  style: this.style,
75
76
  radius: (this.style.radius || 5) * this.graph.devicePixelRatio
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
  };
@@ -414,4 +415,10 @@ export default class jmChart extends jmGraph {
414
415
  }
415
416
  return serie;
416
417
  }
418
+
419
+ // 销毁
420
+ destroy() {
421
+ super.destroy();
422
+ this.touchGraph && this.touchGraph.destroy();
423
+ }
417
424
  }
@@ -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位的点。顺时针和逆时针二个点大小不一样,这里只取,大小计算时处理
@@ -315,7 +314,7 @@ jmPieSeries.prototype.createLegend = function() {
315
314
  const points = this.createPoints();
316
315
  if(!points || !points.length) return;
317
316
 
318
- for(let k in points) {
317
+ for(let k=0; k < points.length; k++) {
319
318
  const p = points[k];
320
319
  if(!p) continue;
321
320
 
@@ -331,18 +330,6 @@ jmPieSeries.prototype.createLegend = function() {
331
330
  //此处重写图例事件
332
331
  this.graph.legend.append(this, shape, {
333
332
  name: this.legendLabel,
334
- hover: function() {
335
- //var sp = this.children.get(0);
336
- //应用图的动态样式
337
- Object.assign(this.targetShape.style, this.targetShape.style.hover);
338
- Object.assign(this.style, this.style.hover);
339
- },
340
- leave: function() {
341
- //var sp = this.children.get(0);
342
- //应用图的普通样式
343
- Object.assign(this.targetShape.style, this.targetShape.style.normal);
344
- Object.assign(this.style, this.style.normal);
345
- },
346
333
  data: this.data[k]
347
334
  });
348
335
  }
@@ -0,0 +1,313 @@
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
+ // 是否正在动画中
112
+ const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0 );
113
+
114
+ const aniCount = (this.style.aniCount || 20);
115
+ let aniIsEnd = true;// 当次是否结束动画
116
+ const len = points.length;
117
+ const shapeMap = {};
118
+ const self = this;
119
+
120
+ for(let i=0; i<len; i++) {
121
+ const p = points[i];
122
+ let shape = shapeMap[p.index];
123
+ if(!shape) {
124
+ shape = shapeMap[p.index] = this.graph.createShape('path', {
125
+ ...p,
126
+ style: p.style,
127
+ points: [],
128
+ });
129
+ this.addShape(shape);
130
+
131
+ // 如果有点击事件
132
+ if(this.option.onClick) {
133
+ shape.on('click', function(e) {
134
+ return self.option.onClick.call(this, e);
135
+ });
136
+ }
137
+ if(this.option.onOver) {
138
+ shape.on('mouseover touchover', function(e) {
139
+ return self.option.onOver.call(this, e);
140
+ });
141
+ }
142
+ if(this.option.onLeave) {
143
+ shape.on('mouseleave touchleave', function(e) {
144
+ return self.option.onLeave.call(this, e);
145
+ });
146
+ }
147
+ this.createLegend(p);
148
+ }
149
+
150
+ shape.zIndex += p.radius / this.radius;// 用每个轴占比做为排序号,这样占面积最大的排最底层
151
+ let point = null;
152
+ // 在动画中,则一直刷新
153
+ if(isRunningAni) {
154
+ let step = p.radius / aniCount * this.___animateCounter;
155
+ if(step >= p.radius) {
156
+ step = p.radius;
157
+ }
158
+ else {
159
+ aniIsEnd = false;
160
+ }
161
+ point = {
162
+ ...p,
163
+ x: this.center.x + p.axis.radarOption.cos * step,
164
+ y: this.center.y - p.axis.radarOption.sin * step,
165
+ };
166
+ }
167
+ else {
168
+ point = p;
169
+ }
170
+ shape.points.push(point);
171
+ this.createLabel(point);// 生成标签
172
+ }
173
+ // 所有动画都完成,则清空计数器
174
+ if(aniIsEnd) {
175
+ this.___animateCounter = 0;
176
+ }
177
+ else {
178
+ this.___animateCounter ++;
179
+ // next tick 再次刷新
180
+ this.graph.utils.requestAnimationFrame(()=>{
181
+ this.needUpdate = true;//需要刷新
182
+ });
183
+ }
184
+ }
185
+
186
+ /**
187
+ * 生成序列图描点
188
+ *
189
+ * @method createPoints
190
+ */
191
+ createPoints(center, radius) {
192
+ if(!this.data || !this.axises) return [];
193
+ center = center || this.center;
194
+ const points = [];
195
+
196
+ for(var i=0;i< this.data.length;i++) {
197
+ const s = this.data[i];
198
+ const style = this.graph.utils.clone(this.style);
199
+ if(style.color && typeof style.color === 'function') {
200
+ style.stroke = style.color.call(this, {
201
+ data: s,
202
+ index: i
203
+ });
204
+ }
205
+ else {
206
+ style.stroke = this.graph.getColor(i);
207
+ }
208
+ if(typeof style.fill === 'function') {
209
+ style.fill = style.fill.call(this, style);
210
+ }
211
+ else {
212
+ const color = this.graph.utils.hexToRGBA(style.stroke);
213
+ style.fill = `rgba(${color.r},${color.g},${color.b}, 0.2)`;
214
+ }
215
+
216
+ const shapePoints = [];
217
+ for(const axis of this.axises) {
218
+ if(!axis || !axis.field) continue;
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(point) {
297
+ if(!point) return;
298
+
299
+ //生成图例前的图标
300
+ const style = this.graph.utils.clone(point.style);
301
+
302
+ //delete style.stroke;
303
+ const shape = this.graph.createShape('rect',{
304
+ style: style,
305
+ position : {x: 0, y: 0}
306
+ });
307
+ //此处重写图例事件
308
+ this.graph.legend.append(this, shape, {
309
+ name: this.legendLabel,
310
+ data: point.data
311
+ });
312
+ }
313
+
@@ -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
@@ -256,7 +260,9 @@ export default class jmSeries extends jmPath {
256
260
  // 这里的点应相对于chartArea
257
261
  p.x = xstep * i + this.xAxis.labelStart;
258
262
 
259
- for(const f of fields) {
263
+ for(let j=0; j<fields.length; j++) {
264
+ const f = fields[j];
265
+
260
266
  const yv = s[f];
261
267
  p.yLabel = p.yValue = yv;
262
268
  // 高度
@@ -316,6 +322,7 @@ export default class jmSeries extends jmPath {
316
322
  style
317
323
  });
318
324
  this.graph.legend.append(this, shape);
325
+ return shape;
319
326
  }
320
327
 
321
328
  // 生成柱图的标注
@@ -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
 
@@ -116,7 +120,8 @@ export default class jmStackBarSeries extends jmBarSeries {
116
120
 
117
121
  for(const row of this.data) {
118
122
  let max, min;
119
- for(const f of fields) {
123
+ for(let i=0; i<fields.length; i++) {
124
+ const f = fields[i];
120
125
  const v = Number(row[f]);
121
126
  if(typeof max === 'undefined') max = v;
122
127
  else {