@@ -32,6 +32,12 @@ interface ISeqrProportionalMapGraphProps {
32
32
start : string
33
33
end : string
34
34
}
35
+
36
+ interface IPropMapData {
37
+ date : string
38
+ [ project : string ] : number
39
+ }
40
+
35
41
const SeqrProportionalMapGraph : React . FunctionComponent < ISeqrProportionalMapGraphProps > = ( {
36
42
start,
37
43
end,
@@ -43,57 +49,57 @@ const SeqrProportionalMapGraph: React.FunctionComponent<ISeqrProportionalMapGrap
43
49
const [ projectSelections , setProjectSelections ] = React . useState <
44
50
{ [ key : string ] : boolean } | undefined
45
51
> ( )
46
- const [ data , setData ] = React . useState < any > ( )
52
+ const [ data , setData ] = React . useState < IPropMapData [ ] > ( )
47
53
// const [propMap, setPropMap] = React.useState<>()
48
54
49
- // function updateProjects(projects: { [key: string]: boolean }) {
50
- // const updatedProjects = { ...(projectSelections || {}), ...projects }
51
- // setProjectSelections(updatedProjects)
52
- // loadPropMap(updatedProjects)
53
- // }
55
+ function updateProjects ( projects : { [ key : string ] : boolean } ) {
56
+ const updatedProjects = { ...( projectSelections || { } ) , ...projects }
57
+ setProjectSelections ( updatedProjects )
58
+ loadPropMap ( updatedProjects )
59
+ }
54
60
55
- // function loadPropMap(projects: { [key: string]: boolean } = {}) {
56
- // const projectsToSearch = Object.keys(projects).filter((project) => projects[project])
61
+ function loadPropMap ( projects : { [ key : string ] : boolean } = { } ) {
62
+ const projectsToSearch = Object . keys ( projects ) . filter ( ( project ) => projects [ project ] )
57
63
58
- // const api = new AnalysisApi()
59
- // const defaultPropMap = projectsToSearch.reduce((prev, p) => ({ ...prev, [p]: 0 }), {})
60
- // api.getProportionateMap(sequencingType, projectsToSearch, start, end)
61
- // .then((summary) => {
62
- // setIsLoading(false)
64
+ const api = new AnalysisApi ( )
65
+ const defaultPropMap = projectsToSearch . reduce ( ( prev , p ) => ( { ...prev , [ p ] : 0 } ) , { } )
66
+ api . getProportionateMap ( sequencingType , projectsToSearch , start , end )
67
+ . then ( ( summary ) => {
68
+ setIsLoading ( false )
63
69
64
- // const graphData = summary.data.map((obj: IProportionalDateModel) => ({
65
- // date: obj.date,
66
- // // ...defaultPropMap,
67
- // ...obj.projects.reduce(
68
- // (prev: { [project: string]: number }, projectObj) => ({
69
- // ...prev,
70
- // // in percentage, rounded to 2 decimal places
71
- // [projectObj.project]: _.round( projectObj.percentage * 100, 2) ,
72
- // }),
73
- // {}
74
- // ),
75
- // }))
76
- // const projectsToSee = new Set(projectsToSearch)
77
- // for (let index = 1; index < graphData.length; index++) {
78
- // const graphObj = graphData[index]
79
- // if (projectsToSearch.length == 0) continue
80
- // // make sure the entry BEFORE a project is visible,
81
- // // it's set to 0 to make the graph prettier
82
- // for (const project of projectsToSee) {
83
- // if (project in graphObj) {
84
- // projectsToSee.delete(project)
85
- // graphData[index - 1][project] = 0
86
- // }
87
- // if (projectsToSee.size == 0) break
88
- // }
89
- // }
90
- // setPropMap (graphData)
91
- // })
92
- // .catch((er) => {
93
- // setError(er.message)
94
- // setIsLoading(false)
95
- // })
96
- // }
70
+ const graphData : IPropMapData [ ] = summary . data . map ( ( obj : IProportionalDateModel ) => ( {
71
+ date : obj . date ,
72
+ ...defaultPropMap ,
73
+ ...obj . projects . reduce (
74
+ ( prev : { [ project : string ] : number } , projectObj ) => ( {
75
+ ...prev ,
76
+ // in percentage, rounded to 2 decimal places
77
+ [ projectObj . project ] : projectObj . percentage ,
78
+ } ) ,
79
+ { }
80
+ ) ,
81
+ } ) )
82
+ const projectsToSee = new Set ( projectsToSearch )
83
+ for ( let index = 1 ; index < graphData . length ; index ++ ) {
84
+ const graphObj = graphData [ index ]
85
+ if ( projectsToSearch . length == 0 ) continue
86
+ // make sure the entry BEFORE a project is visible,
87
+ // it's set to 0 to make the graph prettier
88
+ for ( const project of projectsToSee ) {
89
+ if ( project in graphObj ) {
90
+ projectsToSee . delete ( project )
91
+ graphData [ index - 1 ] [ project ] = 0
92
+ }
93
+ if ( projectsToSee . size == 0 ) break
94
+ }
95
+ }
96
+ setData ( graphData )
97
+ } )
98
+ . catch ( ( er ) => {
99
+ setError ( er . message )
100
+ setIsLoading ( false )
101
+ } )
102
+ }
97
103
98
104
function getSeqrProjects ( ) {
99
105
const api = new ProjectApi ( )
@@ -114,20 +120,36 @@ const SeqrProportionalMapGraph: React.FunctionComponent<ISeqrProportionalMapGrap
114
120
} )
115
121
}
116
122
117
- React . useEffect ( ( ) => {
118
- getSeqrProjects ( )
119
- } , [ ] )
123
+ // TODO, uncomment later
124
+ // React.useEffect(() => {
125
+ // getSeqrProjects()
126
+ // }, [])
127
+
120
128
121
- // This is where I'm getting the mock data - delete this and replace it with whatever you had above to pull in the real data
122
- // You can check the link to see the data format but basically each data point is a date and the values for all the different categories at that timepoint
123
129
React . useEffect ( ( ) => {
124
- const getData = async ( ) => {
125
- const d = await csv (
126
- 'https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/5_OneCatSevNumOrdered_wide.csv'
127
- )
128
- setData ( d )
129
- }
130
- getData ( )
130
+ setProjectSelections ( [ 'project1' , 'project2' , 'project3' ] . reduce (
131
+ ( prev : { [ project : string ] : boolean } , project : string ) => ( {
132
+ ...prev ,
133
+ [ project ] : true ,
134
+ } ) , { }
135
+ ) )
136
+
137
+ setData ( [ {
138
+ date : "2021-01-01" ,
139
+ project1 : 0.1 ,
140
+ project2 : 0.2 ,
141
+ project3 : 0.7 ,
142
+ } , {
143
+ date : "2021-01-02" ,
144
+ project1 : 0.2 ,
145
+ project2 : 0.3 ,
146
+ project3 : 0.5 ,
147
+ } , {
148
+ date : "2021-01-03" ,
149
+ project1 : 0.3 ,
150
+ project2 : 0.4 ,
151
+ project3 : 0.3 ,
152
+ } ] )
131
153
} , [ ] )
132
154
133
155
if ( ! data ) {
@@ -138,24 +160,22 @@ const SeqrProportionalMapGraph: React.FunctionComponent<ISeqrProportionalMapGrap
138
160
? _ . sortBy ( Object . keys ( projectSelections ) . filter ( ( project ) => projectSelections [ project ] ) )
139
161
: [ ]
140
162
163
+
141
164
// svg sizing info
142
165
const margin = { top : 10 , right : 30 , bottom : 50 , left : 60 }
143
166
const width = 1000 - margin . left - margin . right
144
167
const height = 1000 - margin . top - margin . bottom
145
168
const id = '1'
146
169
147
- // get heading of data (column names)
148
- const sumstat = data . columns . slice ( 1 )
149
-
150
170
// d3 function that turns the data into stacked proportions
151
- const stackedData = stack ( ) . offset ( stackOffsetExpand ) . keys ( sumstat ) ( data )
171
+ const stackedData = stack ( ) . offset ( stackOffsetExpand ) . keys ( selectedProjects ) ( data )
152
172
153
173
// function for generating the x Axis
154
174
// domain refers to the min and max of the data (in this case earliest and latest dates)
155
175
// range refers to the min and max pixel positions on the screen
156
176
// basically it is a mapping of pixel positions to data values
157
177
const xScale = scaleLinear ( )
158
- . domain ( extent ( data , ( d ) => d . year ) )
178
+ . domain ( extent ( data , ( d ) => d . date ) ) // date is a string, will this take a date object?
159
179
. range ( [ 0 , width - margin . left - margin . right ] )
160
180
161
181
// function for generating the y Axis
@@ -164,11 +184,11 @@ const SeqrProportionalMapGraph: React.FunctionComponent<ISeqrProportionalMapGrap
164
184
165
185
// function that assigns each category a colour
166
186
// can fiddle with the schemeAccent parameter for different colour scales - see https://d3js.org/d3-scale-chromatic/categorical#schemeAccent
167
- const color = scaleOrdinal ( schemeAccent ) . domain ( sumstat )
187
+ const color = scaleOrdinal ( schemeAccent ) . domain ( selectedProjects )
168
188
169
189
// function that takes the various stacked data info and generates an svg path element (magically)
170
190
const areaGenerator = area ( )
171
- . x ( ( d ) => xScale ( d . data . year ) )
191
+ . x ( ( d ) => xScale ( d . data . date ) )
172
192
. y0 ( ( d ) => yScale ( d [ 0 ] ) )
173
193
. y1 ( ( d ) => yScale ( d [ 1 ] ) )
174
194
@@ -196,9 +216,8 @@ const SeqrProportionalMapGraph: React.FunctionComponent<ISeqrProportionalMapGrap
196
216
{ xScale . ticks ( ) . map ( ( tick ) => (
197
217
< g
198
218
key = { tick }
199
- transform = { `translate(${ xScale ( tick ) } , ${
200
- height - margin . top - margin . bottom
201
- } )`}
219
+ transform = { `translate(${ xScale ( tick ) } , ${ height - margin . top - margin . bottom
220
+ } )`}
202
221
>
203
222
< text
204
223
y = { 8 }
@@ -248,7 +267,7 @@ const SeqrProportionalMapGraph: React.FunctionComponent<ISeqrProportionalMapGrap
248
267
< path
249
268
key = { i }
250
269
d = { areaGenerator ( area ) }
251
- style = { { fill : color ( sumstat [ i ] ) } }
270
+ style = { { fill : color ( selectedProjects [ i ] ) } }
252
271
/>
253
272
) ) }
254
273
</ g >
0 commit comments