Skip to content

Commit 89f67bf

Browse files
committed
fix: add formatting for small numbers in nonetype, width layout
1 parent 070ea30 commit 89f67bf

File tree

9 files changed

+120
-39
lines changed

9 files changed

+120
-39
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"moment": "^2.29.1",
2020
"react": "^16.13.1",
2121
"react-dom": "^16.13.1",
22+
"react-measure": "^2.5.2",
2223
"react-scripts": "3.4.3",
2324
"react-select": "^3.1.0",
2425
"react-use": "^15.3.4",

src/components/MainLayout/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const Container = styled("div")(({ themeColors, width }) => ({
1717
display: "flex",
1818
flexDirection: "column",
1919
backgroundColor: themeColors.bg,
20-
padding: 16,
2120
}))
2221

2322
export const MainLayout = ({
@@ -26,10 +25,11 @@ export const MainLayout = ({
2625
durationGroups,
2726
onChangeDurationGroups,
2827
timestamps,
28+
width = 500,
29+
graphHeight = 300,
2930
onChangeTimestamps,
3031
}) => {
3132
const themeColors = useColors()
32-
const width = 500
3333
const [activeDurationGroup, setActiveDurationGroup] = useState(null)
3434
const [draggedDurationIndex, setDraggedDurationIndex] = useState(null)
3535
const [selectedDurationIndex, setSelectedDurationIndex] = useState(null)
@@ -38,7 +38,10 @@ export const MainLayout = ({
3838
const [topLevelMatrix, setTopLevelMatrix] = useState(() =>
3939
initTopLevelMatrix({ curveGroups, width })
4040
)
41-
const { visibleTimeStart, visibleTimeEnd } = useTimeRange(topLevelMatrix, 500)
41+
const { visibleTimeStart, visibleTimeEnd } = useTimeRange(
42+
topLevelMatrix,
43+
width
44+
)
4245

4346
const onDragDuration = useEventCallback((startTime, endTime) => {
4447
if (activeDurationGroup === null) return
@@ -137,7 +140,7 @@ export const MainLayout = ({
137140
])
138141
})
139142

140-
const gridLineMetrics = getMinorMajorDurationLines(topLevelMatrix, 500)
143+
const gridLineMetrics = getMinorMajorDurationLines(topLevelMatrix, width)
141144

142145
return (
143146
<Container width={width} themeColors={themeColors}>
@@ -191,7 +194,7 @@ export const MainLayout = ({
191194
timestamps={timestamps}
192195
curves={curves}
193196
width={width}
194-
height={200}
197+
height={graphHeight}
195198
gridLineMetrics={gridLineMetrics}
196199
topLevelMatrix={topLevelMatrix}
197200
setTopLevelMatrix={setTopLevelMatrix}

src/components/MouseTransformHandler/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import useEventCallback from "use-event-callback"
44
import useToolMode from "../../hooks/use-tool-mode"
55

66
const Container = styled("div")({
7-
width: 500,
8-
height: 200,
7+
// width: 500,
8+
// height: 200,
99
})
1010

1111
export const MouseTransformHandler = ({

src/components/ReactTimeSeries/ReactTimeSeries.stories.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,37 @@ export const WithAudioURL = () => {
4444
/>
4545
)
4646
}
47+
48+
export const ReallySimple = () => {
49+
return (
50+
<ReactTimeSeries
51+
interface={{}}
52+
sample={{
53+
timeData: [
54+
{ time: 0, value: 0 },
55+
{ time: 500, value: 500 },
56+
{ time: 1000, value: 1000 },
57+
],
58+
}}
59+
onModifySample={() => null}
60+
/>
61+
)
62+
}
63+
64+
export const SmallValues = () => {
65+
return (
66+
<ReactTimeSeries
67+
interface={{
68+
timeFormat: "none",
69+
}}
70+
sample={{
71+
timeData: [
72+
{ time: 0, value: 0 },
73+
{ time: 0.02, value: 500 },
74+
{ time: 0.05, value: 1000 },
75+
],
76+
}}
77+
onModifySample={() => null}
78+
/>
79+
)
80+
}

src/components/ReactTimeSeries/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useMemo } from "react"
1+
import React, { useMemo, useState } from "react"
22
import { setIn } from "seamless-immutable"
33
import useEventCallback from "use-event-callback"
44
import { useAsyncMemo } from "use-async-memo"
55
import { RecoilRoot } from "recoil"
66
import useGetRandomColorUsingHash from "../../hooks/use-get-random-color-using-hash"
7+
import Measure from "react-measure"
78

89
import MainLayout from "../MainLayout"
910

@@ -28,6 +29,7 @@ export const ReactTimeSeriesWithoutContext = ({
2829
corsProxy = defaultCorsProxy,
2930
interface: iface,
3031
sample,
32+
width: widthProp,
3133
onModifySample,
3234
}) => {
3335
if (!iface) throw new Error(`"interface" is a required prop`)
@@ -174,6 +176,10 @@ export const ReactTimeSeriesWithoutContext = ({
174176
const onChangeTimestamps = useEventCallback((newTimestamps) => {
175177
onModifySample(setIn(sample, ["annotation", "timestamps"], newTimestamps))
176178
})
179+
const [width, setWidth] = useState(widthProp)
180+
const onResize = useEventCallback(({ bounds }) => {
181+
if (!widthProp) setWidth(bounds.width)
182+
})
177183

178184
if (timeDataLoading) return "loading" // TODO real loader
179185

@@ -188,14 +194,21 @@ export const ReactTimeSeriesWithoutContext = ({
188194
}
189195

190196
return (
191-
<MainLayout
192-
curveGroups={curveGroups}
193-
timeFormat={timeFormat}
194-
durationGroups={durationGroups}
195-
onChangeDurationGroups={onChangeDurationGroups}
196-
timestamps={timestamps}
197-
onChangeTimestamps={onChangeTimestamps}
198-
/>
197+
<Measure bounds onResize={onResize}>
198+
{({ measureRef }) => (
199+
<div ref={measureRef}>
200+
<MainLayout
201+
width={width}
202+
curveGroups={curveGroups}
203+
timeFormat={timeFormat}
204+
durationGroups={durationGroups}
205+
onChangeDurationGroups={onChangeDurationGroups}
206+
timestamps={timestamps}
207+
onChangeTimestamps={onChangeTimestamps}
208+
/>
209+
</div>
210+
)}
211+
</Measure>
199212
)
200213
}
201214

src/components/Timeline/index.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from "react"
22
import range from "lodash/range"
33
import { styled } from "@material-ui/core/styles"
4-
import moment from "moment"
54
import useColors from "../../hooks/use-colors"
65
import TimeStamp from "../TimeStamp"
76

7+
import { formatTime } from "../../utils/format-time"
8+
89
const Container = styled("div")(({ width, themeColors }) => ({
910
width,
1011
overflow: "hidden",
@@ -14,7 +15,7 @@ const Container = styled("div")(({ width, themeColors }) => ({
1415
color: themeColors.fg,
1516
}))
1617

17-
const TimeText = styled("div")(({ x }) => ({
18+
const TimeText = styled("div")(({ x, faded }) => ({
1819
display: "inline-block",
1920
width: 80,
2021
fontSize: 12,
@@ -24,6 +25,7 @@ const TimeText = styled("div")(({ x }) => ({
2425
borderLeft: "1px solid rgba(255,255,255,0.5)",
2526
paddingLeft: 4,
2627
whiteSpace: "wrap",
28+
opacity: faded ? 0.5 : 1,
2729
}))
2830

2931
const Svg = styled("svg")({
@@ -32,25 +34,6 @@ const Svg = styled("svg")({
3234
bottom: 0,
3335
})
3436

35-
export const formatTime = (time, format, visibleDuration) => {
36-
const lessThan3DaysShown = visibleDuration < 1000 * 60 * 60 * 24 * 3
37-
if (format === "none") return time
38-
if (format === "dates") {
39-
return (
40-
moment(time).format("L") +
41-
(!lessThan3DaysShown ? "" : "\n" + moment(time).format("h:mm:ss a"))
42-
)
43-
}
44-
if (time < 0) return ""
45-
const deciSecs = Math.floor((time % 1000) / 10)
46-
const secs = Math.floor((time / 1000) % 60)
47-
const mins = Math.floor((time / 60000) % 60)
48-
const hours = Math.floor(time / (60000 * 60))
49-
return [hours, mins, secs, deciSecs]
50-
.map((t) => t.toString().padStart(2, "0"))
51-
.join(":")
52-
}
53-
5437
export const Timeline = ({
5538
timeFormat,
5639
visibleTimeStart,
@@ -81,6 +64,7 @@ export const Timeline = ({
8164
<TimeText
8265
key={timeTextIndex}
8366
x={(timeTextIndex / timeTextCount) * width}
67+
faded={timeTextTimes[timeTextIndex] < 0}
8468
>
8569
{formatTime(
8670
timeTextTimes[timeTextIndex],

src/components/Wave/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import range from "lodash/range"
33
import { styled } from "@material-ui/core/styles"
44
import colorAlpha from "color-alpha"
55
import useColors from "../../hooks/use-colors"
6-
import { formatTime } from "../Timeline"
6+
import { formatTime } from "../../utils/format-time"
77

88
const userSelectOffStyle = { userSelect: "none" }
99

src/utils/format-time.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import moment from "moment"
2+
3+
export const formatTime = (time, format, visibleDuration) => {
4+
const lessThan3DaysShown = visibleDuration < 1000 * 60 * 60 * 24 * 3
5+
if (format === "none")
6+
return visibleDuration > 10
7+
? Math.round(time)
8+
: time.toFixed(2 - Math.log(visibleDuration) / Math.log(10))
9+
if (format === "dates") {
10+
return (
11+
moment(time).format("L") +
12+
(!lessThan3DaysShown ? "" : "\n" + moment(time).format("h:mm:ss a"))
13+
)
14+
}
15+
if (time < 0) return "< 00:00:00"
16+
const deciSecs = Math.floor((time % 1000) / 10)
17+
const secs = Math.floor((time / 1000) % 60)
18+
const mins = Math.floor((time / 60000) % 60)
19+
const hours = Math.floor(time / (60000 * 60))
20+
return [hours, mins, secs, deciSecs]
21+
.map((t) => t.toString().padStart(2, "0"))
22+
.join(":")
23+
}
24+
export default formatTime

yarn.lock

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,13 @@
19901990
dependencies:
19911991
regenerator-runtime "^0.13.4"
19921992

1993+
"@babel/runtime@^7.2.0":
1994+
version "7.12.1"
1995+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.1.tgz#b4116a6b6711d010b2dad3b7b6e43bf1b9954740"
1996+
integrity sha512-J5AIf3vPj3UwXaAzb5j1xM4WAQDX3EMgemF8rjCP3SoW09LfRKAXQKt6CoVYl230P6iWdRcBbnLDDdnqWxZSCA==
1997+
dependencies:
1998+
regenerator-runtime "^0.13.4"
1999+
19932000
"@babel/template@^7.10.4":
19942001
version "7.10.4"
19952002
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.4.tgz#3251996c4200ebc71d1a8fc405fba940f36ba278"
@@ -8372,6 +8379,11 @@ get-caller-file@^2.0.1:
83728379
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
83738380
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
83748381

8382+
get-node-dimensions@^1.2.1:
8383+
version "1.2.1"
8384+
resolved "https://registry.yarnpkg.com/get-node-dimensions/-/get-node-dimensions-1.2.1.tgz#fb7b4bb57060fb4247dd51c9d690dfbec56b0823"
8385+
integrity sha512-2MSPMu7S1iOTL+BOa6K1S62hB2zUAYNF/lV0gSVlOaacd087lc6nR1H1r0e3B1CerTo+RceOmi1iJW+vp21xcQ==
8386+
83758387
get-own-enumerable-property-symbols@^3.0.0:
83768388
version "3.0.2"
83778389
resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-3.0.2.tgz#b5fde77f22cbe35f390b4e089922c50bce6ef664"
@@ -13379,6 +13391,16 @@ react-lifecycles-compat@^3.0.4:
1337913391
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
1338013392
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==
1338113393

13394+
react-measure@^2.5.2:
13395+
version "2.5.2"
13396+
resolved "https://registry.yarnpkg.com/react-measure/-/react-measure-2.5.2.tgz#4ffc410e8b9cb836d9455a9ff18fc1f0fca67f89"
13397+
integrity sha512-M+rpbTLWJ3FD6FXvYV6YEGvQ5tMayQ3fGrZhRPHrE9bVlBYfDCLuDcgNttYfk8IqfOI03jz6cbpqMRTUclQnaA==
13398+
dependencies:
13399+
"@babel/runtime" "^7.2.0"
13400+
get-node-dimensions "^1.2.1"
13401+
prop-types "^15.6.2"
13402+
resize-observer-polyfill "^1.5.0"
13403+
1338213404
react-popper-tooltip@^2.11.0:
1338313405
version "2.11.1"
1338413406
resolved "https://registry.yarnpkg.com/react-popper-tooltip/-/react-popper-tooltip-2.11.1.tgz#3c4bdfd8bc10d1c2b9a162e859bab8958f5b2644"
@@ -13945,7 +13967,7 @@ requires-port@^1.0.0:
1394513967
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
1394613968
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
1394713969

13948-
resize-observer-polyfill@^1.5.1:
13970+
resize-observer-polyfill@^1.5.0, resize-observer-polyfill@^1.5.1:
1394913971
version "1.5.1"
1395013972
resolved "https://registry.yarnpkg.com/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz#0e9020dd3d21024458d4ebd27e23e40269810464"
1395113973
integrity sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==

0 commit comments

Comments
 (0)