jmchart 1.2.13 → 1.2.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jmchart",
3
- "version": "1.2.13",
3
+ "version": "1.2.16",
4
4
  "description": "chart",
5
5
  "main": "./index.js",
6
6
  "directories": {
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "homepage": "https://surl.fit/tools/tools/jmchart",
30
30
  "dependencies": {
31
- "jmgraph": "^3.2.20"
31
+ "jmgraph": "^3.2.24"
32
32
  },
33
33
  "publishConfig": {
34
34
  "registry": "https://registry.npmjs.org/"
@@ -36,7 +36,7 @@ export default {
36
36
  },
37
37
 
38
38
  updated() {
39
- this.initChart();
39
+ this.refresh();
40
40
  },
41
41
 
42
42
  destroyed() {
@@ -44,8 +44,29 @@ export default {
44
44
  },
45
45
 
46
46
  watch: {
47
- chartData: function(newData, oldData) {
48
- this.refresh();
47
+ chartData: {
48
+ handler: function(newData, oldData) {
49
+ this.refresh();
50
+ },
51
+ deep: false // 仅监听引用变化,由使用者显式替换数组时触发
52
+ },
53
+ chartOptions: {
54
+ handler: function(newOptions, oldOptions) {
55
+ // 配置变化时需要重建图表
56
+ if(this.chartInstance) {
57
+ this.chartInstance.destroy();
58
+ this.chartInstance = null;
59
+ }
60
+ this.option = Object.assign({
61
+ enableAnimate: false,
62
+ legendPosition: 'top',
63
+ legendVisible: true,
64
+ width: this.width,
65
+ height: this.height
66
+ }, newOptions);
67
+ this.$nextTick(() => this.initChart());
68
+ },
69
+ deep: false
49
70
  },
50
71
  width: function(newWidth, oldWidth) {
51
72
  if(!this.chartInstance) return;
@@ -662,13 +662,16 @@ export default class jmAxis extends jmArrowLine {
662
662
  clear() {
663
663
  this._min = null;
664
664
  this._max = null;
665
+ this._width = null; // 清除宽度缓存
665
666
  this.children.each((i, c) => {
666
667
  c.remove();
667
668
  }, true);
668
669
  // 清空栅格线
669
- this.gridLines && this.gridLines.forEach((line)=>{
670
- line.remove();
671
- });
670
+ if(this.gridLines && this.gridLines.length) {
671
+ for(let i = 0; i < this.gridLines.length; i++) {
672
+ this.gridLines[i].remove();
673
+ }
674
+ }
672
675
  this.labels && this.labels.forEach((label)=>{
673
676
  label.remove();
674
677
  });
@@ -64,12 +64,11 @@ export default class jmMarkLineManager {
64
64
  if(chart.style.markLine.longtap) {
65
65
  longtap = 1;
66
66
  longtapHandler && graph.utils.cancelAnimationFrame(longtapHandler);
67
- let tapStartTime = Date.now();
67
+ const tapStartTime = Date.now();
68
68
  const reqFun = ()=>{
69
- const elapsed = Date.now() - tapStartTime;
70
69
  if(longtap === 1 || longtap === 2) {
71
70
  // 如果还未过一定时间,则继续等待
72
- if(elapsed < 500) {
71
+ if(Date.now() - tapStartTime < 500) {
73
72
  longtapHandler = graph.utils.requestAnimationFrame(reqFun);
74
73
  return;
75
74
  }
@@ -80,9 +79,6 @@ export default class jmMarkLineManager {
80
79
  };
81
80
  // 如果一定时间后还没有取消,则表示长按了
82
81
  longtapHandler = graph.utils.requestAnimationFrame(reqFun);
83
-
84
- //args.event.stopPropagation();
85
- //args.event.preventDefault();// 阻止默认行为
86
82
  }
87
83
  else {
88
84
  this.startMove(args);
package/src/jmChart.js CHANGED
@@ -142,29 +142,28 @@ export default class jmChart extends jmGraph {
142
142
 
143
143
  // 绑定点击事件
144
144
  this.on('click', (args) => {
145
- if(this.option.onClick) {
146
- // 找到点击位置的数据点
147
- let closestPoint = null;
148
- let minDistance = Infinity;
149
-
150
- this.series.each((i, serie) => {
151
- if(serie.dataPoints) {
152
- serie.dataPoints.forEach(point => {
153
- const distance = Math.sqrt(
154
- Math.pow(args.position.x - point.x, 2) +
155
- Math.pow(args.position.y - point.y, 2)
156
- );
157
- if(distance < minDistance && distance < 20) { // 20px 范围内的点
158
- minDistance = distance;
159
- closestPoint = point;
160
- }
161
- });
145
+ if(!this.option.onClick) return;
146
+ // 找到点击位置的数据点
147
+ let closestPoint = null;
148
+ let minDistance = 20; // 20px 范围,同时作为阈值,无需再比较小于阈值
149
+
150
+ this.series.each((i, serie) => {
151
+ if(!serie.dataPoints) return;
152
+ const points = serie.dataPoints;
153
+ for(let j = 0, len = points.length; j < len; j++) {
154
+ const point = points[j];
155
+ const dx = args.position.x - point.x;
156
+ const dy = args.position.y - point.y;
157
+ const distance = dx * dx + dy * dy; // 避免开方,直接比较平方
158
+ if(distance < minDistance * minDistance) {
159
+ minDistance = distance;
160
+ closestPoint = point;
162
161
  }
163
- });
164
-
165
- if(closestPoint) {
166
- this.option.onClick(closestPoint);
167
162
  }
163
+ });
164
+
165
+ if(closestPoint) {
166
+ this.option.onClick(closestPoint);
168
167
  }
169
168
  });
170
169
  }
@@ -238,8 +237,9 @@ export default class jmChart extends jmGraph {
238
237
  delete this.xAxis;
239
238
  }
240
239
  if(this.yAxises) {
241
- for(let i in this.yAxises) {
242
- this.yAxises[i].remove();
240
+ const keys = Object.keys(this.yAxises);
241
+ for(let k = 0; k < keys.length; k++) {
242
+ this.yAxises[keys[k]].remove();
243
243
  }
244
244
  delete this.yAxises;
245
245
  }
@@ -251,20 +251,9 @@ export default class jmChart extends jmGraph {
251
251
  * @param {int} index 颜色索引
252
252
  */
253
253
  getColor(index) {
254
- const cacheKey = `color_${index}`;
255
- if(this._cache.has(cacheKey)) {
256
- return this._cache.get(cacheKey);
257
- }
258
-
259
- let color;
260
- if(index >= this.style.chartColors.length) {
261
- color = this.style.chartColors[index % this.style.chartColors.length];
262
- } else {
263
- color = this.style.chartColors[index];
264
- }
265
-
266
- this._cache.set(cacheKey, color);
267
- return color;
254
+ const colors = this.style.chartColors;
255
+ const colorIndex = index % colors.length;
256
+ return colors[colorIndex];
268
257
  }
269
258
 
270
259
  /**
@@ -285,28 +274,29 @@ export default class jmChart extends jmGraph {
285
274
  }
286
275
 
287
276
  if(this.yAxises) {
288
- for(let i in this.yAxises) {
289
- this.yAxises[i].clear();
277
+ const keys = Object.keys(this.yAxises);
278
+ for(let k = 0; k < keys.length; k++) {
279
+ this.yAxises[keys[k]].clear();
290
280
  }
291
281
  }
292
282
 
293
283
  this.barSeriesCount = 0;
284
+ // 合并两次 series 遍历为一次,减少遍历开销
285
+ const seriesInited = [];
294
286
  this.series.each((i, serie) => {
295
287
  if(!serie.style.color) {
296
288
  serie.style.color = serie.graph.getColor(i);
297
289
  }
298
290
 
299
- if(serie.graph.style.layout != 'inside') {
300
- if(serie instanceof jmBarSeries) {
291
+ if(serie instanceof jmBarSeries) {
292
+ if(serie.graph.style.layout != 'inside') {
301
293
  serie.graph.style.layout = 'inside';
302
294
  }
303
- }
304
-
305
- if(serie instanceof jmBarSeries) {
306
295
  serie.barIndex = serie.graph.barSeriesCount;
307
296
  serie.graph.barSeriesCount++;
308
297
  }
309
298
  serie.reset();
299
+ seriesInited.push(serie);
310
300
  });
311
301
 
312
302
  if(this.legend) {
@@ -314,8 +304,9 @@ export default class jmChart extends jmGraph {
314
304
  }
315
305
 
316
306
  if(this.yAxises) {
317
- for(let i in this.yAxises) {
318
- this.yAxises[i].reset();
307
+ const keys = Object.keys(this.yAxises);
308
+ for(let k = 0; k < keys.length; k++) {
309
+ this.yAxises[keys[k]].reset();
319
310
  }
320
311
  }
321
312
 
@@ -323,9 +314,9 @@ export default class jmChart extends jmGraph {
323
314
  this.xAxis.reset();
324
315
  }
325
316
 
326
- this.series.each((i, serie) => {
327
- serie.init && serie.init();
328
- });
317
+ for(let i = 0; i < seriesInited.length; i++) {
318
+ seriesInited[i].init && seriesInited[i].init();
319
+ }
329
320
  }
330
321
 
331
322
  /**
@@ -1,19 +1,14 @@
1
- import jmSeries from './series.js';
2
-
3
- const ANIMATION_DATA_THRESHOLD = 100;
4
- const DEFAULT_ANIMATION_COUNT = 10;
1
+ import jmLineSeries from './lineSeries.js';
5
2
 
6
3
  /**
7
4
  * 面积图
5
+ * 继承自折线图,默认启用区域填充
8
6
  *
9
7
  * @class jmAreaSeries
10
8
  * @module jmChart
11
- * @param {jmChart} chart 当前图表
12
- * @param {array} mappings 图形字段映射
13
- * @param {style} style 样式
9
+ * @extends jmLineSeries
14
10
  */
15
-
16
- export default class jmAreaSeries extends jmSeries {
11
+ export default class jmAreaSeries extends jmLineSeries {
17
12
  constructor(options) {
18
13
  options.style = options.style || options.graph.style.area;
19
14
  super(options);
@@ -21,229 +16,15 @@ export default class jmAreaSeries extends jmSeries {
21
16
  this.style.area = true;
22
17
  }
23
18
 
24
- /**
25
- * 初始化面积图
26
- *
27
- * @method init
28
- * @for jmAreaSeries
29
- */
30
- init() {
31
- const {
32
- points,
33
- dataChanged
34
- } = this.initDataPoint();
35
-
36
- const len = points.length;
37
- if(!len) return;
38
-
39
- this.style.stroke = this.style.color;
40
- this.style.item.stroke = this.style.color;
41
-
42
- const isRunningAni = this.enableAnimate && (dataChanged || this.___animateCounter > 0) && len < ANIMATION_DATA_THRESHOLD;
43
-
44
- let shapePoints = [];
45
- const aniCount = (this.style.aniCount || DEFAULT_ANIMATION_COUNT);
46
- const aniStep = Math.floor(len / aniCount) || 1;
47
-
48
- for(let i = 0; i < len; i++) {
49
- const p = points[i];
50
-
51
- if(typeof p.y === 'undefined' || p.y === null) {
52
- continue;
53
- }
54
-
55
- if(isRunningAni && i > this.___animateCounter) {
56
- break;
57
- }
58
-
59
- if(this.style.showItem) {
60
- this.createPointItem(p);
61
- }
62
-
63
- if(this.style.curve) {
64
- shapePoints = this.createCurePoints(shapePoints, p);
65
- } else if(this.style.lineType === 'dotted') {
66
- shapePoints = this.createDotLine(shapePoints, p);
67
- }
68
-
69
- shapePoints.push(p);
70
-
71
- this.createItemLabel(p);
72
-
73
- this.emit('onPointCreated', p);
74
- }
75
-
76
- if(this.___animateCounter >= len - 1) {
77
- this.___animateCounter = 0;
78
- } else if(isRunningAni) {
79
- this.___animateCounter += aniStep;
80
- this.graph.utils.requestAnimationFrame(() => {
81
- this.needUpdate = true;
82
- });
83
- }
84
-
85
- this.points = shapePoints;
86
- this.createArea(shapePoints);
87
- }
88
-
89
- /**
90
- * 生成点的小圆圈
91
- *
92
- * @method createPointItem
93
- * @for jmAreaSeries
94
- * @param {object} p 数据点
95
- */
96
- createPointItem(p) {
97
- const pointShape = this.graph.createShape('circle', {
98
- style: this.style.item,
99
- center: p,
100
- radius: this.style.radius || 3
101
- });
102
-
103
- pointShape.zIndex = (pointShape.style.zIndex || 1) + 1;
104
- return this.addShape(pointShape);
105
- }
106
-
107
- /**
108
- * 创建曲线点
109
- *
110
- * @method createCurePoints
111
- * @for jmAreaSeries
112
- * @param {array} shapePoints 形状点数组
113
- * @param {object} p 当前点
114
- * @return {array} 新的形状点数组
115
- */
116
- createCurePoints(shapePoints, p) {
117
- const startPoint = shapePoints[shapePoints.length - 1];
118
-
119
- if(!startPoint || !p) return shapePoints;
120
-
121
- if(startPoint.x === undefined || startPoint.x === null ||
122
- startPoint.y === undefined || startPoint.y === null ||
123
- p.x === undefined || p.x === null ||
124
- p.y === undefined || p.y === null) {
125
- return shapePoints;
126
- }
127
-
128
- const p1 = {x: startPoint.x + (p.x - startPoint.x) / 5, y: startPoint.y};
129
- const p2 = {x: startPoint.x + (p.x - startPoint.x) / 2, y: p.y - (p.y - startPoint.y) / 2};
130
- const p3 = {x: p.x - (p.x - startPoint.x) / 5, y: p.y};
131
-
132
- this.__bezier = this.__bezier || this.graph.createShape('bezier');
133
- this.__bezier.cpoints = [
134
- startPoint,
135
- p1,
136
- p2,
137
- p3,
138
- p
139
- ];
140
-
141
- const bzpoints = this.__bezier.initPoints();
142
- if(bzpoints && bzpoints.length) {
143
- shapePoints = shapePoints.concat(bzpoints);
144
- }
145
-
146
- return shapePoints;
147
- }
148
-
149
- /**
150
- * 创建虚线点
151
- *
152
- * @method createDotLine
153
- * @for jmAreaSeries
154
- * @param {array} shapePoints 形状点数组
155
- * @param {object} p 当前点
156
- * @return {array} 新的形状点数组
157
- */
158
- createDotLine(shapePoints, p) {
159
- const startPoint = shapePoints[shapePoints.length - 1];
160
-
161
- if(!startPoint || !p) return shapePoints;
162
-
163
- if(startPoint.x === undefined || startPoint.x === null ||
164
- startPoint.y === undefined || startPoint.y === null ||
165
- p.x === undefined || p.x === null ||
166
- p.y === undefined || p.y === null) {
167
- return shapePoints;
168
- }
169
-
170
- this.__line = this.__line || this.graph.createShape('line', {
171
- style: this.style,
172
- });
173
- this.__line.start = startPoint;
174
- this.__line.end = p;
175
-
176
- const dots = this.__line.initPoints();
177
- if(dots && dots.length) {
178
- shapePoints = shapePoints.concat(dots);
179
- }
180
-
181
- return shapePoints;
182
- }
183
-
184
- /**
185
- * 生成面积效果
186
- *
187
- * @method createArea
188
- * @for jmAreaSeries
189
- * @param {array} points 数据点数组
190
- * @param {boolean} needClosePoint 是否需要闭合点
191
- */
192
- createArea(points, needClosePoint = true) {
193
- // 有指定绘制区域效果才展示
194
- if(!this.style.area || points.length < 2) return;
195
-
196
- const start = points[0];
197
- const end = points[points.length - 1];
198
-
199
- const style = this.graph.utils.clone(this.style.area, {}, true);
200
- // 连框颜色如果没指定,就透明
201
- style.stroke = style.stroke || 'transparent';
202
-
203
- if(!style.fill) {
204
- 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)`;
209
- }
210
- else if(typeof style.fill === 'function') {
211
- style.fill = style.fill.call(this, style);
212
- }
213
- const area = this.graph.createShape('path', {
214
- points: this.graph.utils.clone(points, true),
215
- style,
216
- width: this.graph.chartArea.width,
217
- height: this.graph.chartArea.height
218
- });
219
-
220
- // 在点集合前后加上落地到X轴的点就可以组成一个封闭的图形area
221
- if(needClosePoint) {
222
- area.points.unshift({
223
- x: start.x,
224
- y: this.baseY
225
- });
226
- area.points.push({
227
- x: end.x,
228
- y: this.baseY
229
- });
230
- }
231
-
232
- this.addShape(area);
233
- }
234
-
235
19
  /**
236
20
  * 生成图例
237
- *
238
- * @method createLegend
239
- * @for jmAreaSeries
240
21
  */
241
22
  createLegend() {
242
- // 生成图例前的图标
243
23
  var style = this.graph.utils.clone(this.style);
244
24
  style.stroke = style.color;
25
+ style.fill = style.color;
245
26
  var shape = this.graph.createShape('path',{style:style});
246
-
27
+
247
28
  if(this.curve || this.style.curve) {
248
29
  var p1 = {x:0,y: this.graph.style.legend.item.shape.height};
249
30
  var p2 = {x:this.graph.style.legend.item.shape.width / 3,y:this.graph.style.legend.item.shape.height/3};
@@ -253,7 +34,7 @@ export default class jmAreaSeries extends jmSeries {
253
34
  this.__bezier = this.__bezier || this.graph.createShape('bezier');
254
35
  this.__bezier.cpoints = [
255
36
  p1,p2,p3,p4
256
- ];//设置控制点
37
+ ];
257
38
 
258
39
  shape.points = this.__bezier.initPoints();
259
40
  }
@@ -267,7 +48,3 @@ export default class jmAreaSeries extends jmSeries {
267
48
  this.graph.legend.append(this, shape);
268
49
  }
269
50
  }
270
-
271
- export {
272
- jmAreaSeries
273
- }
@@ -1,7 +1,4 @@
1
- import jmSeries from './series.js';
2
-
3
- const ANIMATION_DATA_THRESHOLD = 100;
4
- const DEFAULT_ANIMATION_COUNT = 10;
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
5
2
 
6
3
  /**
7
4
  * 柱图
@@ -43,8 +40,8 @@ export default class jmBarSeries extends jmSeries {
43
40
  continue;
44
41
  }
45
42
 
46
- point.style.fill = this.getColor(point);
47
- const sp = this.addShape(this.graph.createPath(null, point.style));
43
+ const style = Object.assign({}, point.style, { fill: this.getColor(point) });
44
+ const sp = this.addShape(this.graph.createPath(null, style));
48
45
 
49
46
  const p1 = {x: point.x - this.barTotalWidth / 2 + this.barWidth * this.barIndex, y: this.baseY };
50
47
  const p4 = {x: p1.x + this.barWidth, y: p1.y };
@@ -1,7 +1,4 @@
1
- import jmSeries from './series.js';
2
-
3
- const ANIMATION_DATA_THRESHOLD = 100;
4
- const DEFAULT_ANIMATION_COUNT = 10;
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
5
2
 
6
3
  /**
7
4
  * 气泡图
@@ -62,8 +62,6 @@ export default class jmCandlestickSeries extends jmSeries {
62
62
  continue;
63
63
  }
64
64
 
65
- const sp = this.addShape(this.graph.createPath([], p.style));
66
-
67
65
  const bl = {
68
66
  x: p.x - w,
69
67
  y: p.points[0].y
@@ -84,10 +82,12 @@ export default class jmCandlestickSeries extends jmSeries {
84
82
  let tm = p.points[1];
85
83
  let bm = p.points[0];
86
84
 
87
- p.style.stroke = p.style.fill = p.style.masculineColor || 'red';
85
+ // 使用浅拷贝避免污染共享引用
86
+ const style = Object.assign({}, p.style);
87
+ style.stroke = style.fill = style.masculineColor || 'red';
88
88
 
89
89
  if(p.points[0].yValue > p.points[1].yValue) {
90
- p.style.stroke = p.style.fill = p.style.negativeColor || 'green';
90
+ style.stroke = style.fill = style.negativeColor || 'green';
91
91
  bl.y = br.y = p.points[1].y;
92
92
  tl.y = tr.y = p.points[0].y;
93
93
 
@@ -95,6 +95,7 @@ export default class jmCandlestickSeries extends jmSeries {
95
95
  bm = p.points[1];
96
96
  }
97
97
 
98
+ const sp = this.addShape(this.graph.createPath([], style));
98
99
  sp.points.push(p.points[2], tm, tl, bl, bm, p.points[3], bm, br, tr, tm, p.points[2]);
99
100
 
100
101
  this.emit('onPointCreated', p);
@@ -1,7 +1,4 @@
1
- import jmSeries from './series.js';
2
-
3
- const ANIMATION_DATA_THRESHOLD = 100;
4
- const DEFAULT_ANIMATION_COUNT = 10;
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
5
2
 
6
3
  /**
7
4
  * 仪表盘
@@ -1,7 +1,4 @@
1
- import jmSeries from './series.js';
2
-
3
- const ANIMATION_DATA_THRESHOLD = 100;
4
- const DEFAULT_ANIMATION_COUNT = 10;
1
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
5
2
 
6
3
  /**
7
4
  * 热力图
@@ -1,8 +1,5 @@
1
1
 
2
- import jmSeries from './series.js';
3
-
4
- const ANIMATION_DATA_THRESHOLD = 100;
5
- const DEFAULT_ANIMATION_COUNT = 10;
2
+ import jmSeries, { ANIMATION_DATA_THRESHOLD, DEFAULT_ANIMATION_COUNT } from './series.js';
6
3
 
7
4
  /**
8
5
  * 线图
@@ -29,7 +29,7 @@ export default class jmPieSeries extends jmSeries {
29
29
  this.totalValue = 0;
30
30
  //计算最大值和最小值
31
31
  if(this.data) {
32
- for(const i in this.data) {
32
+ for(let i = 0; i < this.data.length; i++) {
33
33
  const s = this.data[i];
34
34
  const vy = s[this.field];
35
35
  if(vy) {
@@ -138,16 +138,18 @@ export default class jmPieSeries extends jmSeries {
138
138
  yValue: yv,
139
139
  yLabel: yv,
140
140
  step: Math.abs(yv / this.totalValue),// 每个数值点比
141
- style: this.graph.utils.clone(this.style),
141
+ style: this.style, // 共享引用,仅在需要修改时才拷贝
142
142
  anticlockwise
143
143
  };
144
144
  points.push(p);
145
145
  //p.style.color = this.graph.getColor(index);
146
146
  if(p.style.color && typeof p.style.color === 'function') {
147
+ p.style = this.graph.utils.clone(this.style);
147
148
  p.style.fill = p.style.color.call(this, p);
148
149
  }
149
150
  else {
150
- p.style.fill = this.graph.getColor(index);
151
+ // fill 需要独立,创建轻量样式覆盖
152
+ p.style = Object.assign({}, this.style, { fill: this.graph.getColor(index) });
151
153
  }
152
154
 
153
155
  const start = startAni;// 上一个扇形的结束角度为当前的起始角度