Skip to content

Commit f5eb94e

Browse files
committed
fix: allow seeing values of points on hover
1 parent f660464 commit f5eb94e

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

src/components/ControllableWave/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const ControllableWave = ({
1919
timestamps,
2020
gridLineMetrics,
2121
timeFormat,
22+
showValues,
2223
}) => {
2324
let [matrix, setMatrix] = useRafState(() => {
2425
const mat = new Matrix()
@@ -61,6 +62,7 @@ export const ControllableWave = ({
6162
gridLineMetrics={gridLineMetrics}
6263
timestamps={timestamps}
6364
durationGroups={durationGroups}
65+
showValues={showValues}
6466
/>
6567
</MouseTransformHandler>
6668
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React, { Fragment, useState, useCallback } from "react"
2+
import useThemeColors from "../../hooks/use-colors"
3+
4+
const HOV_SIZE = 50
5+
export const Point = ({ x, y, value, t, color, width }) => {
6+
const [showPoint, setShowPoint] = useState(false)
7+
const onShow = useCallback(() => setShowPoint(true), [setShowPoint])
8+
const onHide = useCallback(() => setShowPoint(false), [setShowPoint])
9+
return (
10+
<g onMouseEnter={onShow} onMouseLeave={onHide}>
11+
{showPoint && (
12+
<Fragment>
13+
<text x={x + 10} y={y - 10} fill={color}>
14+
{value}
15+
</text>
16+
<circle cx={x} cy={y} r={5} fill={color} />
17+
</Fragment>
18+
)}
19+
<rect
20+
x={x - width / 2}
21+
y={y - HOV_SIZE / 2}
22+
width={width}
23+
height={HOV_SIZE}
24+
fill="transparent"
25+
// If you're debugging the hover region, uncomment the following
26+
// fill="rgba(0,0,0,0.5)"
27+
></rect>
28+
</g>
29+
)
30+
}
31+
32+
export const HighlightValueLabels = ({
33+
visibleTransformedPointsOnCurves = [],
34+
curveColors = [],
35+
}) => {
36+
return (
37+
<g>
38+
{visibleTransformedPointsOnCurves.flatMap((points, curveIndex) =>
39+
points.map((p, i) => (
40+
<Point
41+
key={`${curveIndex},${i}`}
42+
color={curveColors[curveIndex]}
43+
width={
44+
i === 0 || i === points.length - 1
45+
? HOV_SIZE
46+
: (points[i + 1].x - points[i - 1].x) / 2
47+
}
48+
{...p}
49+
/>
50+
))
51+
)}
52+
</g>
53+
)
54+
}
55+
56+
export default HighlightValueLabels

src/components/MainLayout/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const MainLayout = ({
4141
graphHeight = 300,
4242
onChangeTimestamps,
4343
enabledTools = defaultEnabledTools,
44+
showValues = false,
4445
}) => {
4546
const themeColors = useColors()
4647
const [activeDurationGroup, setActiveDurationGroup] = useState(null)
@@ -207,6 +208,7 @@ export const MainLayout = ({
207208
{curveGroups.map((curves, i) => (
208209
<ControllableWave
209210
key={i}
211+
showValues={showValues}
210212
durations={durationGroups[activeDurationGroup]}
211213
onDragDuration={onDragDuration}
212214
onDragDurationStart={onDragDurationStart}

src/components/ReactTimeSeries/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ export const ReactTimeSeriesWithoutContext = ({
3535
if (!iface) throw new Error(`"interface" is a required prop`)
3636
if (!sample) throw new Error(`"sample" is a required prop`)
3737
const getRandomColorUsingHash = useGetRandomColorUsingHash()
38-
const {
38+
let {
3939
timeFormat,
4040
enabledTools = defaultEnabledTools,
4141
durationLabels = emptyAr,
4242
timestampLabels = emptyAr,
4343
graphs = defaultGraphs,
4444
allowCustomLabels,
45+
showValues,
4546
} = iface
4647
let { timeData: sampleTimeData, audioUrl, csvUrl, annotation } = sample
48+
if (showValues === undefined && !audioUrl) showValues = true
4749

4850
const timeDataAvailable = [sampleTimeData, audioUrl, csvUrl].some(Boolean)
4951

@@ -210,6 +212,7 @@ export const ReactTimeSeriesWithoutContext = ({
210212
durationLabels={durationLabels}
211213
allowCustomLabels={allowCustomLabels}
212214
enabledTools={enabledTools}
215+
showValues={showValues}
213216
/>
214217
</div>
215218
)}

src/components/Wave/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { styled } from "@material-ui/core/styles"
44
import colorAlpha from "color-alpha"
55
import useColors from "../../hooks/use-colors"
66
import { formatTime } from "../../utils/format-time"
7+
import HighlightValueLabels from "../HighlightValueLabels"
78

89
const userSelectOffStyle = {
910
userSelect: "none",
@@ -62,6 +63,7 @@ export const Wave = ({
6263
durationGroups = [],
6364
timestamps = [],
6465
gridLineMetrics,
66+
showValues = false,
6567
}) => {
6668
const colors = useColors()
6769

@@ -86,9 +88,11 @@ export const Wave = ({
8688
startTimeOnGraph,
8789
visibleDuration,
8890
width
89-
).map(([t, y]) => {
90-
return transformMatrix.applyToPoint(t, y)
91-
})
91+
).map(([t, y]) => ({
92+
...transformMatrix.applyToPoint(t, y),
93+
t,
94+
value: y,
95+
}))
9296
)
9397
}
9498
return visibleTransformedPointsOnCurves
@@ -200,6 +204,12 @@ export const Wave = ({
200204
/>
201205
)
202206
})}
207+
{showValues && (
208+
<HighlightValueLabels
209+
visibleTransformedPointsOnCurves={visibleTransformedPointsOnCurves}
210+
curveColors={curves.map((c) => c.color)}
211+
/>
212+
)}
203213
</svg>
204214
)
205215
}

0 commit comments

Comments
 (0)