Skip to content

Commit 044532b

Browse files
committed
Downloading and formatting data on load.
1 parent 4b38337 commit 044532b

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This project is a workshop for teaching basics of using D3 (version 4.x) and Rea
44
## Credits
55
The data used in this project comes from [Five Thirty Eight](https://github.com/fivethirtyeight/data/tree/master/comic-characters).
66

7-
Big thanks to United Income for letting my try the talk out on my co-workers, and letting me get started prepping it during our Innovation Sprint.
7+
Big thanks to [United Income](https://unitedincome.com) for letting my try the talk out on my co-workers, and letting me get started prepping it during our Innovation Sprint.
88

99
# Bootstrapping
1010
This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app).

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"d3-dsv": "=1.0.7",
7+
"moment": "=2.18.1",
68
"react": "^16.0.0",
79
"react-dom": "^16.0.0",
810
"react-scripts": "1.0.14"
@@ -13,4 +15,4 @@
1315
"test": "react-scripts test --env=jsdom",
1416
"eject": "react-scripts eject"
1517
}
16-
}
18+
}

src/App.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
import React, { Component } from 'react';
22
import logo from './logo.svg';
33
import './App.css';
4+
import { csvParse } from 'd3-dsv';
5+
import { combineCsvs } from './dataParsers';
46

57
class App extends Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {};
11+
}
12+
13+
componentWillMount() {
14+
const dcUrl = 'https://raw.githubusercontent.com/fivethirtyeight/data/master/comic-characters/dc-wikia-data.csv';
15+
const marvelUrl = 'https://raw.githubusercontent.com/fivethirtyeight/data/master/comic-characters/marvel-wikia-data.csv'
16+
return Promise.all([
17+
dcUrl,
18+
marvelUrl,
19+
].map((url) => {
20+
return fetch(url)
21+
.then((response) => response.ok ? response.text() : Promise.reject(`Failed to fetch ${url}`))
22+
.then((text) => csvParse(text));
23+
}))
24+
.then((parsedCsvs) => this.setState({ data: combineCsvs([
25+
{
26+
data: parsedCsvs[0],
27+
dateFormat: 'YYYY, MMMM',
28+
}, {
29+
data: parsedCsvs[1],
30+
dateFormat: 'MMM-YY',
31+
},
32+
]) }));
33+
}
34+
635
render() {
736
return (
837
<div className="App">
@@ -11,7 +40,7 @@ class App extends Component {
1140
<h1 className="App-title">Welcome to React</h1>
1241
</header>
1342
<p className="App-intro">
14-
To get started, edit <code>src/App.js</code> and save to reload.
43+
{ this.state.data ? 'I loaded all the data!' : 'Loading...' }
1544
</p>
1645
</div>
1746
);

src/dataParsers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import moment from 'moment';
2+
3+
export const combineCsvs = (csvArray) => {
4+
return csvArray.reduce((acc, current) => {
5+
const dateFormatted = current.data.map((value) => {
6+
value['FIRST APPEARANCE'] = moment(value['FIRST APPEARANCE'], current.dateFormat).format('MM/YYYY');
7+
return value;
8+
});
9+
return acc.concat(dateFormatted);
10+
}, []);
11+
};

yarn.lock

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ combined-stream@^1.0.5, combined-stream@~1.0.5:
14391439
dependencies:
14401440
delayed-stream "~1.0.0"
14411441

1442-
commander@2.11.x, commander@~2.11.0:
1442+
commander@2, commander@2.11.x, commander@~2.11.0:
14431443
version "2.11.0"
14441444
resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563"
14451445

@@ -1731,6 +1731,14 @@ currently-unhandled@^0.4.1:
17311731
dependencies:
17321732
array-find-index "^1.0.1"
17331733

1734+
d3-dsv@=1.0.7:
1735+
version "1.0.7"
1736+
resolved "https://registry.yarnpkg.com/d3-dsv/-/d3-dsv-1.0.7.tgz#137076663f398428fc3d031ae65370522492b78f"
1737+
dependencies:
1738+
commander "2"
1739+
iconv-lite "0.4"
1740+
rw "1"
1741+
17341742
d@1:
17351743
version "1.0.0"
17361744
resolved "https://registry.yarnpkg.com/d/-/d-1.0.0.tgz#754bb5bfe55451da69a58b94d45f4c5b0462d58f"
@@ -3029,14 +3037,14 @@ https-browserify@0.0.1:
30293037
version "0.0.1"
30303038
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-0.0.1.tgz#3f91365cabe60b77ed0ebba24b454e3e09d95a82"
30313039

3040+
iconv-lite@0.4, iconv-lite@0.4.19, iconv-lite@^0.4.17, iconv-lite@~0.4.13:
3041+
version "0.4.19"
3042+
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
3043+
30323044
iconv-lite@0.4.13:
30333045
version "0.4.13"
30343046
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"
30353047

3036-
iconv-lite@0.4.19, iconv-lite@^0.4.17, iconv-lite@~0.4.13:
3037-
version "0.4.19"
3038-
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
3039-
30403048
icss-replace-symbols@^1.1.0:
30413049
version "1.1.0"
30423050
resolved "https://registry.yarnpkg.com/icss-replace-symbols/-/icss-replace-symbols-1.1.0.tgz#06ea6f83679a7749e386cfe1fe812ae5db223ded"
@@ -4121,6 +4129,10 @@ mkdirp@0.5.1, mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdi
41214129
dependencies:
41224130
minimist "0.0.8"
41234131

4132+
moment@=2.18.1:
4133+
version "2.18.1"
4134+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
4135+
41244136
ms@2.0.0:
41254137
version "2.0.0"
41264138
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
@@ -5075,7 +5087,7 @@ react-dev-utils@^4.1.0:
50755087
strip-ansi "3.0.1"
50765088
text-table "0.2.0"
50775089

5078-
react-dom@16.0.0, "react-dom@^15 || ^16":
5090+
"react-dom@^15 || ^16", react-dom@^16.0.0:
50795091
version "16.0.0"
50805092
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.0.0.tgz#9cc3079c3dcd70d4c6e01b84aab2a7e34c303f58"
50815093
dependencies:
@@ -5139,7 +5151,7 @@ react-scripts@1.0.14:
51395151
optionalDependencies:
51405152
fsevents "1.1.2"
51415153

5142-
react@16.0.0, "react@^15 || ^16":
5154+
"react@^15 || ^16", react@^16.0.0:
51435155
version "16.0.0"
51445156
resolved "https://registry.yarnpkg.com/react/-/react-16.0.0.tgz#ce7df8f1941b036f02b2cca9dbd0cb1f0e855e2d"
51455157
dependencies:
@@ -5438,6 +5450,10 @@ run-async@^2.2.0:
54385450
dependencies:
54395451
is-promise "^2.1.0"
54405452

5453+
rw@1:
5454+
version "1.3.3"
5455+
resolved "https://registry.yarnpkg.com/rw/-/rw-1.3.3.tgz#3f862dfa91ab766b14885ef4d01124bfda074fb4"
5456+
54415457
rx-lite-aggregates@^4.0.8:
54425458
version "4.0.8"
54435459
resolved "https://registry.yarnpkg.com/rx-lite-aggregates/-/rx-lite-aggregates-4.0.8.tgz#753b87a89a11c95467c4ac1626c4efc4e05c67be"

0 commit comments

Comments
 (0)