Repository navigation
Expand file tree
/
Copy pathPointSeries.tsx
More file actions
142 lines (124 loc) · 2.84 KB
/
Copy pathPointSeries.tsx
File metadata and controls
142 lines (124 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { FC, ReactElement, useCallback } from 'react';
import { ChartInternalShallowDataShape } from '@/common/data';
import { CloneElement } from 'reablocks';
import { ScatterPoint, ScatterSeries, ScatterPointProps } from '@/ScatterPlot';
import css from './PointSeries.module.css';
import isEqual from 'react-fast-compare';
import { mergeDefaultProps } from '@/common';
export interface PointSeriesProps {
/**
* Determines if the points should be animated or not.
*/
animated: boolean;
/**
* The color of the points.
*/
color: any;
/**
* The active values for the points.
*/
activeValues?: any;
/**
* The data for the points.
*/
data: ChartInternalShallowDataShape[];
/**
* The y-scale for the points.
*/
yScale: any;
/**
* The x-scale for the points.
*/
xScale: any;
/**
* The unique identifier for the points.
*/
id: string;
/**
* The height of the points.
*/
height: number;
/**
* The width of the points.
*/
width: number;
/**
* Determines when the points should be shown. Can be a boolean or one of the following strings: 'hover', 'first', 'last'.
*
* @default 'hover'
*/
show: boolean | 'hover' | 'first' | 'last';
/**
* The point element.
*
* @default `<ScatterPoint />`
*/
point: ReactElement<ScatterPointProps, typeof ScatterPoint>;
/**
* The index of the points.
*/
index: number;
}
export const PointSeries: FC<Partial<PointSeriesProps>> = (props) => {
const {
data,
xScale,
yScale,
animated,
point,
color,
height,
width,
id,
activeValues,
show
} = mergeDefaultProps(POINT_SERIES_DEFAULT_PROPS, props);
const getIsVisible = useCallback(
(point: ChartInternalShallowDataShape, index: number): boolean => {
const isActive =
activeValues && point && isEqual(activeValues.x, point.x);
if (show === 'hover') {
return isActive;
} else if (show === 'first') {
if (activeValues) {
return isActive;
} else {
return index === 0;
}
} else if (show === 'last') {
if (activeValues) {
return isActive;
} else {
return index === data.length - 1;
}
}
return Boolean(show);
},
[activeValues, data.length, show]
);
return (
<ScatterSeries
height={height}
width={width}
id={id}
animated={animated}
data={data}
xScale={xScale}
yScale={yScale}
point={
<CloneElement<ScatterPointProps>
element={point}
color={color}
className={css.point}
size={4}
tooltip={null}
visible={getIsVisible}
/>
}
/>
);
};
export const POINT_SERIES_DEFAULT_PROPS: Partial<PointSeriesProps> = {
show: 'hover',
point: <ScatterPoint />
};