@@ -4,18 +4,17 @@ import { SeriesPoint } from "@visx/shape/lib/types";
4
4
import { Group } from "@visx/group" ;
5
5
import { Grid } from "@visx/grid" ;
6
6
import { AxisBottom } from "@visx/axis" ;
7
- // import cityTemperature from "./cityTemperature";
8
- import snapshots from "./snapshots" ;
9
7
import { scaleBand , scaleLinear , scaleOrdinal } from "@visx/scale" ;
10
- import { timeParse , timeFormat } from "d3-time-format" ;
11
8
import { useTooltip , useTooltipInPortal , defaultStyles } from "@visx/tooltip" ;
12
9
import { LegendOrdinal } from "@visx/legend" ;
10
+ import snapshots from "./snapshots" ;
11
+
13
12
14
13
/* TYPESCRIPT */
15
14
type CityName = "New York" | "San Francisco" | "Austin" ;
16
-
15
+ type snapshot = any ;
17
16
type TooltipData = {
18
- bar : SeriesPoint < snapshots > ;
17
+ bar : SeriesPoint < snapshot > ;
19
18
key : CityName ;
20
19
index : number ;
21
20
height : number ;
@@ -48,12 +47,9 @@ const tooltipStyles = {
48
47
49
48
/* DATA PREP */
50
49
const data = [ ...snapshots ] ;
51
- console . log ( 'data' , data )
52
50
53
51
// array of all object keys
54
52
const keys = Object . keys ( data [ 0 ] ) . filter ( ( d ) => d !== "snapshot" ) as CityName [ ] ;
55
- console . log ( 'keys' , keys )
56
-
57
53
58
54
// ARRAY OF TOTAL VALUES PER SNAPSHOT
59
55
const temperatureTotals = data . reduce ( ( allTotals , currentDate ) => {
@@ -65,18 +61,13 @@ const temperatureTotals = data.reduce((allTotals, currentDate) => {
65
61
return allTotals ;
66
62
} , [ ] as number [ ] ) ;
67
63
68
- // console.log(temperatureTotals)
69
-
70
- const parseDate = timeParse ( "%Y-%m-%d" ) ;
71
- const format = timeFormat ( "%b %d" ) ;
72
- const formatDate = ( date : string ) => format ( parseDate ( date ) as Date ) ;
73
64
74
65
/* ACCESSORS */
75
- const getDate = ( d : CityTemperature ) => d . snapshot ;
66
+ const getSnapshot = ( d : snapshot ) => d . snapshot ;
76
67
77
68
/* SCALE */
78
69
const dateScale = scaleBand < string > ( {
79
- domain : data . map ( getDate ) ,
70
+ domain : data . map ( getSnapshot ) ,
80
71
padding : 0.2
81
72
} ) ;
82
73
const temperatureScale = scaleLinear < number > ( {
@@ -88,7 +79,6 @@ const colorScale = scaleOrdinal<CityName, string>({
88
79
range : [ purple1 , purple2 , purple3 , purple4 ]
89
80
} ) ;
90
81
91
-
92
82
let tooltipTimeout : number ;
93
83
94
84
/* EXPORT COMPONENT */
@@ -112,16 +102,15 @@ export default function PerformanceVisx({
112
102
if ( width < 10 ) return null ;
113
103
// bounds
114
104
115
- // width, height
105
+ // width, height
116
106
const xMax = width ;
117
107
const yMax = height - margin . top - 100 ;
118
108
119
109
dateScale . rangeRound ( [ 0 , xMax ] ) ;
120
110
temperatureScale . range ( [ yMax , 0 ] ) ;
121
111
122
112
return width < 10 ? null : (
123
- // relative position is needed for correct tooltip positioning
124
-
113
+ // relative position is needed for correct tooltip positioning
125
114
126
115
< div style = { { position : "relative" } } >
127
116
< svg ref = { containerRef } width = { width } height = { height } >
@@ -145,17 +134,15 @@ export default function PerformanceVisx({
145
134
xOffset = { dateScale . bandwidth ( ) / 2 }
146
135
/>
147
136
< Group top = { margin . top } >
148
- < BarStack < CityTemperature , CityName >
137
+ < BarStack < snapshot , CityName >
149
138
data = { data }
150
139
keys = { keys }
151
- x = { getDate }
140
+ x = { getSnapshot }
152
141
xScale = { dateScale }
153
142
yScale = { temperatureScale }
154
143
color = { colorScale }
155
144
>
156
- { ( barStacks ) =>
157
- barStacks . map ( ( barStack ) =>
158
- barStack . bars . map ( ( bar ) => (
145
+ { ( barStacks ) => barStacks . map ( barStack => barStack . bars . map ( ( bar => (
159
146
< rect
160
147
key = { `bar-stack-${ barStack . index } -${ bar . index } ` }
161
148
x = { bar . x }
@@ -171,18 +158,18 @@ export default function PerformanceVisx({
171
158
hideTooltip ( ) ;
172
159
} , 300 ) ;
173
160
} }
174
- onMouseMove = { ( event ) => {
161
+ onMouseMove = { event => {
175
162
if ( tooltipTimeout ) clearTimeout ( tooltipTimeout ) ;
176
163
const top = event . clientY - margin . top - bar . height ;
177
164
const left = bar . x + bar . width / 2 ;
178
165
showTooltip ( {
179
166
tooltipData : bar ,
180
167
tooltipTop : top ,
181
- tooltipLeft : left
168
+ tooltipLeft : left ,
182
169
} ) ;
183
170
} }
184
171
/>
185
- ) )
172
+ ) ) ,
186
173
)
187
174
}
188
175
</ BarStack >
@@ -196,7 +183,7 @@ export default function PerformanceVisx({
196
183
tickLabelProps = { ( ) => ( {
197
184
fill : purple1 ,
198
185
fontSize : 11 ,
199
- textAnchor : " middle"
186
+ textAnchor : ' middle' ,
200
187
} ) }
201
188
/>
202
189
</ svg >
@@ -211,14 +198,12 @@ export default function PerformanceVisx({
211
198
fontSize: "14px"
212
199
}}
213
200
>
214
-
215
201
<LegendOrdinal
216
202
scale={colorScale}
217
203
direction="row"
218
204
labelMargin="0 15px 0 0"
219
205
/>
220
206
</div> */ }
221
-
222
207
{ /* FOR HOVER OVER DISPLAY */ }
223
208
{ tooltipOpen && tooltipData && (
224
209
< TooltipInPortal
@@ -230,9 +215,11 @@ export default function PerformanceVisx({
230
215
< div style = { { color : colorScale ( tooltipData . key ) } } >
231
216
< strong > { tooltipData . key } </ strong >
232
217
</ div >
233
- < div > { tooltipData . bar . data [ tooltipData . key ] } ℉</ div >
234
218
< div >
235
- < small > { formatDate ( getDate ( tooltipData . bar . data ) ) } </ small >
219
+ { tooltipData . bar . data [ tooltipData . key ] }
220
+ </ div >
221
+ < div >
222
+ < small > { getSnapshot ( tooltipData . bar . data ) } </ small >
236
223
</ div >
237
224
</ TooltipInPortal >
238
225
) }
0 commit comments