@fc-plot/ts-graph 0.22.15 → 0.22.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": "@fc-plot/ts-graph",
3
- "version": "0.22.15",
3
+ "version": "0.22.16",
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();
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
+ }