@fc-plot/ts-graph 0.22.15 → 0.22.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fc-plot/ts-graph",
3
- "version": "0.22.15",
3
+ "version": "0.22.17",
4
4
  "scripts": {
5
5
  "commit": "npx git-cz",
6
6
  "compile": "fetk run compile",
package/src/index.ts CHANGED
@@ -35,7 +35,6 @@ import {
35
35
  getYkeyValue,
36
36
  getNearestPointsByEvent,
37
37
  } from "./utils";
38
- import getNearestPoints from "./getNearestPoints";
39
38
  import "../assets/style.less";
40
39
 
41
40
  const eventEmitter = new EventEmitter();
@@ -280,6 +279,7 @@ export default class TsGraph {
280
279
  const plotleaveType = cascadeScope
281
280
  ? `plotleave:${cascadeScope}`
282
281
  : "plotleave";
282
+ const zoomEventType = cascadeScope ? `zoom:${cascadeScope}` : "zoom";
283
283
  let mousedownStatus = false;
284
284
  let mousedownPos: EventPosition = {} as EventPosition;
285
285
  let mouseupPos: EventPosition = {} as EventPosition;
@@ -344,14 +344,7 @@ export default class TsGraph {
344
344
  d3.event.stopPropagation();
345
345
  const eventPosition = d3.event as EventPosition;
346
346
  if (mousedownPos && mousedownPos.layerX !== eventPosition.layerX) {
347
- _this.zoom.clearMarker();
348
- _this.zoom.onZoom(
349
- mousedownPos,
350
- eventPosition,
351
- (transform: Transform) => {
352
- _this.handleZoom(transform, [mousedownPos, eventPosition]);
353
- }
354
- );
347
+ eventEmitter.emit(zoomEventType, mousedownPos, eventPosition);
355
348
  } else {
356
349
  handleClick();
357
350
  }
@@ -400,14 +393,7 @@ export default class TsGraph {
400
393
 
401
394
  // 触发 zoom
402
395
  if (mousedownPos && mousedownPos.layerX !== mouseupPos.layerX) {
403
- _this.zoom.clearMarker();
404
- _this.zoom.onZoom(
405
- mousedownPos,
406
- mouseupPos,
407
- (transform: Transform) => {
408
- _this.handleZoom(transform, [mousedownPos, mouseupPos]);
409
- }
410
- );
396
+ eventEmitter.emit(zoomEventType, mousedownPos, mouseupPos);
411
397
  } else {
412
398
  handleClick();
413
399
  }
@@ -419,6 +405,7 @@ export default class TsGraph {
419
405
  if (cascade) {
420
406
  eventEmitter.on(plotmoveType, this.showTooltip);
421
407
  eventEmitter.on(plotleaveType, this.hideTooltip);
408
+ eventEmitter.on(zoomEventType, this.handleZoomEvent);
422
409
  }
423
410
 
424
411
  // TODO: removeListener
@@ -426,14 +413,7 @@ export default class TsGraph {
426
413
  if (!isEmpty(mousedownPos)) {
427
414
  const eventPosition = mouseleavePos;
428
415
  mousedownStatus = false;
429
- _this.zoom.clearMarker();
430
- _this.zoom.onZoom(
431
- mousedownPos,
432
- eventPosition,
433
- (transform: Transform) => {
434
- _this.handleZoom(transform, [mousedownPos, eventPosition]);
435
- }
436
- );
416
+ eventEmitter.emit(zoomEventType, mousedownPos, eventPosition);
437
417
  mousedownPos = {} as EventPosition;
438
418
  }
439
419
  });
@@ -484,6 +464,16 @@ export default class TsGraph {
484
464
  this.draw(false);
485
465
  };
486
466
 
467
+ handleZoomEvent = (
468
+ mousedownPos: EventPosition,
469
+ eventPosition: EventPosition
470
+ ) => {
471
+ this.zoom.clearMarker();
472
+ this.zoom.onZoom(mousedownPos, eventPosition, (transform: Transform) => {
473
+ this.handleZoom(transform, [mousedownPos, eventPosition]);
474
+ });
475
+ };
476
+
487
477
  handleZoom = (transform?: Transform, eventPositions?: EventPosition[]) => {
488
478
  if (this.options.onZoomWithoutDefult) {
489
479
  let times;
@@ -783,7 +773,8 @@ export default class TsGraph {
783
773
  this.options,
784
774
  this.handleZoom,
785
775
  this.container,
786
- this.eventCanvas
776
+ this.eventCanvas,
777
+ eventEmitter
787
778
  );
788
779
  }
789
780
 
package/src/tooltip.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  import { Options, EventPosition, Point, XScales, YScales } from "./interface";
10
10
  import TsGraph from "./index";
11
11
  import getNearestPoints from "./getNearestPoints";
12
+ import { getPointsByTime } from "./utils";
12
13
 
13
14
  export default class Tooltip {
14
15
  options: Options;
@@ -67,7 +68,8 @@ export default class Tooltip {
67
68
  eventPosition: EventPosition,
68
69
  xScales: XScales,
69
70
  yScales: YScales,
70
- cbk: (nearestPoints: Point[]) => void
71
+ cbk: (nearestPoints: Point[]) => void,
72
+ forceTimestamp?: number
71
73
  ) {
72
74
  const {
73
75
  series = [],
@@ -80,23 +82,28 @@ export default class Tooltip {
80
82
  timestamp,
81
83
  fillNull,
82
84
  } = this.options;
83
- // const containerRect = this.container.getBoundingClientRect();
84
85
  const offsetX = eventPosition.offsetX || eventPosition.layerX;
85
86
  const x = xScales.invert(offsetX);
86
87
  let nearestPoints: Point[] = [];
87
88
 
88
89
  if (this.isMouserover === false) return;
90
+ let tempNearestPoints: any[] = [];
91
+ if (forceTimestamp) {
92
+ tempNearestPoints = getPointsByTime(forceTimestamp, this.options);
93
+ } else {
94
+ tempNearestPoints = getNearestPoints({
95
+ x,
96
+ xkey,
97
+ ykey,
98
+ ykey2,
99
+ oykey,
100
+ timestamp,
101
+ series,
102
+ fillNull,
103
+ });
104
+ }
89
105
 
90
- nearestPoints = getNearestPoints({
91
- x,
92
- xkey,
93
- ykey,
94
- ykey2,
95
- oykey,
96
- timestamp,
97
- series,
98
- fillNull,
99
- }).map((item: any) => {
106
+ nearestPoints = tempNearestPoints.map((item: any) => {
100
107
  return {
101
108
  ...item,
102
109
  x: xScales(item.timestamp),
@@ -149,26 +156,25 @@ export default class Tooltip {
149
156
  draw(
150
157
  eventPosition: EventPosition,
151
158
  instance: TsGraph,
152
- forceTimestamp?: number,
153
- cbk?: (nearestPoints: Point[]) => void
159
+ forceTimestamp?: number
154
160
  ) {
155
161
  const { xScales, yScales, yAxis } = instance;
156
162
  this.isMouserover = true;
157
- this.getNearestPoints(eventPosition, xScales, yScales, (nearestPoints) => {
158
- this.clear();
159
- if (nearestPoints.length) {
160
- if (nearestPoints[0].x < yAxis.tickMaxWidth) return; // 判断是否在 y 轴上
161
- if (forceTimestamp && nearestPoints[0].timestamp !== forceTimestamp) {
162
- return;
163
+ this.getNearestPoints(
164
+ eventPosition,
165
+ xScales,
166
+ yScales,
167
+ (nearestPoints) => {
168
+ this.clear();
169
+ if (nearestPoints.length) {
170
+ if (nearestPoints[0].x < yAxis.tickMaxWidth) return; // 判断是否在 y 轴上
171
+ this.drawCrosshair(nearestPoints[0].x);
172
+ this.drawSymbol(nearestPoints);
173
+ this.drawModal(nearestPoints, eventPosition);
163
174
  }
164
- this.drawCrosshair(nearestPoints[0].x);
165
- this.drawSymbol(nearestPoints);
166
- this.drawModal(nearestPoints, eventPosition);
167
- }
168
- if (cbk && Object.prototype.toString.call(cbk) === "[object Function]") {
169
- cbk(nearestPoints);
170
- }
171
- });
175
+ },
176
+ forceTimestamp
177
+ );
172
178
  }
173
179
 
174
180
  drawModal(nearestPoints: Point[], eventPosition: EventPosition) {
package/src/utils.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  import * as d3 from "d3";
2
- import { max } from "lodash";
3
- import { Timestamp, Options, XScales, YScales } from "./interface";
2
+ import { max, pickBy } from "lodash";
3
+ import {
4
+ Timestamp,
5
+ Options,
6
+ XScales,
7
+ YScales,
8
+ NearestPoint,
9
+ } from "./interface";
4
10
  import getNearestPoints from "./getNearestPoints";
5
11
 
6
12
  type Point = {
@@ -148,3 +154,33 @@ export function getNearestPointsByEvent(
148
154
  nearestPoints,
149
155
  };
150
156
  }
157
+
158
+ export function getPointsByTime(time: number, options: Options) {
159
+ const { series = [], xkey, ykey, oykey, timestamp } = options;
160
+ const points: NearestPoint[] = [];
161
+ series.forEach((serie, i) => {
162
+ if (serie.visible === false) return;
163
+ const { name, color } = serie;
164
+ const serieWithEffective = pickBy(serie, (_val: string, key: string) => {
165
+ return key !== "data";
166
+ });
167
+ let { data = [] } = serie;
168
+ if (Object.prototype.toString.call(data) !== "[object Array]") return;
169
+ data.forEach((item) => {
170
+ const x = getMsTs(item[xkey], timestamp);
171
+ if (x === time) {
172
+ points.push({
173
+ ...item,
174
+ name,
175
+ color,
176
+ timestamp: x,
177
+ value: item[ykey],
178
+ origin: item[oykey],
179
+ serieIndex: i,
180
+ serieOptions: serieWithEffective,
181
+ });
182
+ }
183
+ });
184
+ });
185
+ return points;
186
+ }
package/src/zoom.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as d3 from "d3";
2
+ import EventEmitter from "events";
2
3
  import { Options, EventPosition, Transform } from "./interface";
3
4
 
4
5
  type ResetFuncType = (
@@ -10,14 +11,17 @@ export default class Zoom {
10
11
  options: Options;
11
12
 
12
13
  reset: ResetFuncType;
14
+ eventEmitter: EventEmitter;
13
15
 
14
16
  constructor(
15
17
  userOptions: Options,
16
18
  reset: ResetFuncType,
17
19
  container: HTMLElement,
18
- canvas: HTMLCanvasElement
20
+ canvas: HTMLCanvasElement,
21
+ eventEmitter: EventEmitter
19
22
  ) {
20
23
  this.options = userOptions;
24
+ this.eventEmitter = eventEmitter;
21
25
  this.init(container, canvas);
22
26
  this.reset = reset;
23
27
  }
@@ -54,15 +58,32 @@ export default class Zoom {
54
58
  onReset() {
55
59
  const {
56
60
  chart: { id },
61
+ tooltip,
57
62
  } = this.options;
58
63
  const resetEle = document.getElementById(
59
64
  `${id}-zoom-resetBtn`
60
65
  ) as HTMLElement;
61
66
 
67
+ const { cascade, cascadeScope } = tooltip;
68
+ const zoomResetEventType = cascadeScope
69
+ ? `zoomReset:${cascadeScope}`
70
+ : "zoomReset";
71
+
72
+ if (cascade) {
73
+ this.eventEmitter.on(zoomResetEventType, () => {
74
+ this.reset();
75
+ this.clearResetBtn();
76
+ });
77
+ }
78
+
62
79
  resetEle?.addEventListener("click", (e) => {
63
80
  e.stopPropagation();
64
- this.reset();
65
- this.clearResetBtn();
81
+ if (cascade) {
82
+ this.eventEmitter.emit(zoomResetEventType, e);
83
+ } else {
84
+ this.reset();
85
+ this.clearResetBtn();
86
+ }
66
87
  });
67
88
  }
68
89