Commit 82923abc authored by ETretyakov's avatar ETretyakov
Browse files

Affiliation map demo init

parent 5a7a8f76
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.<br />
You will also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
### Analyzing the Bundle Size
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
### Making a Progressive Web App
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
### Advanced Configuration
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
### Deployment
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
### `npm run build` fails to minify
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
This diff is collapsed.
{
"name": "affiliation-map-demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"@deck.gl/aggregation-layers": "latest",
"@deck.gl/layers": "latest",
"@deck.gl/react": "latest",
"@luma.gl/core": "latest",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.3",
"@testing-library/user-event": "^7.1.2",
"d3-scale": "^3.2.1",
"deck.gl": "^7.3.8",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-map-gl": "^5.1.3",
"react-scripts": "3.3.0",
"react-scrollbar": "^0.5.6"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
# https://www.robotstxt.org/robotstxt.html
User-agent: *
body {
background-color: #191a1a;
}
.column-tooltip {
position: absolute;
padding: 5px;
background-color: #191a1a;
box-shadow: 0 0 5px #0b0c0c;
z-index: 100;
color: #f2f2f2;
border-radius: 5px;
}
.column-tooltip h3 {
font-size: 1.2rem;
margin: 0;
padding: 0;
}
.column-tooltip span {
font-size: 1rem;
}
.filters-container {
position: absolute;
display: grid;
}
import React, {Component} from 'react';
import {StaticMap} from 'react-map-gl';
import {PhongMaterial} from '@luma.gl/core';
import AffiliationsFilter from './filters/affiliations'
import CountriesFilter from './filters/countries'
import {FlyToInterpolator} from 'deck.gl';
import DeckGL from '@deck.gl/react';
import {ArcLayer, ScatterplotLayer, ColumnLayer} from '@deck.gl/layers';
import {scaleQuantile} from 'd3-scale';
import './App.css'
const MAPBOX_TOKEN = 'pk.eyJ1IjoiZXRyZXR5YWtvdiIsImEiOiJjazJ3NGN5czgwYjByM25wanNmNmg1ZTl6In0.7t0bhGldnBbWJYLh3hy7FA';
const colorRange = [
[1, 152, 189],
[73, 227, 206],
[216, 254, 181],
[254, 237, 177],
[254, 173, 84],
[209, 55, 78]
];
const material = new PhongMaterial({
ambient: 0.64,
diffuse: 0.6,
shininess: 32,
specularColor: [51, 51, 51]
});
const INITIAL_VIEW_STATE = {
longitude: 37.5166667,
latitude: 55.7,
zoom: 8,
maxZoom: 15,
pitch: 40.5,
bearing: 0
};
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
arcs: [],
countries: [],
selectedAffiliation: null,
selectedCountry: "",
hoveredColumn: null,
hoveredArc: null,
tooltipX: null,
tooltipY: null,
viewState: INITIAL_VIEW_STATE
};
this.hoverColumn = this.hoverColumn.bind(this);
this.hoverArc = this.hoverArc.bind(this);
this.clickColumn = this.clickColumn.bind(this);
this.flyToAffiliation = this.flyToAffiliation.bind(this);
this.renderColumnTooltip = this.renderColumnTooltip.bind(this);
this.onViewStateChange = this.onViewStateChange.bind(this);
}
recalculateArcs(data, selectedAffiliation, country) {
if (!data || selectedAffiliation === null) {
return;
}
let {flows, centroid} = selectedAffiliation.properties;
let filteredKeys = Object.keys(flows)
.filter(key => data[key].properties.country.indexOf(country) > -1);
let cFlows = {};
filteredKeys.forEach(key => {
cFlows[key] = flows[key]
});
const arcs = Object.keys(cFlows).map(toId => {
const f = data[toId];
return {
source: centroid,
target: f.properties.centroid,
value: flows[toId]
};
});
const scale = scaleQuantile()
.domain(arcs.map(a => Math.abs(a.value)))
.range(colorRange.map((c, i) => {
return i
}));
arcs.forEach(a => {
// Mad fix
let quantile = scale(Math.abs(a.value));
if (quantile <= 0) {
quantile = 1
}
a.quantile = quantile;
});
this.setState({arcs: arcs})
}
recalculateCountries(data, selectedAffiliation) {
let {flows} = selectedAffiliation.properties;
let buffer = Object.keys(flows).map(toId => {
return data[toId].properties.country;
});
let countries = {};
for (let i = 0; i < buffer.length; i++) {
let country = buffer[i];
countries[country] = countries[country] ? countries[country] + 1 : 1
}
countries = Object.keys(countries).map(country => {
return {name: country, counter: countries[country]}
}).sort((a, b) => (a.counter > b.counter) ? -1 : 1);
this.setState({countries})
}
affiliationsSelectCallback = (object) => {
if (object === null) {
this.setState({
selectedAffiliation: null,
countries: [],
selectedCountry: ""
});
return
}
const affiliation = this.props.data[object.itemCounter];
this.setState({
selectedAffiliation: affiliation,
selectedCountry: ""
});
this.flyToAffiliation(affiliation);
this.recalculateArcs(this.props.data, affiliation, "");
this.recalculateCountries(this.props.data, affiliation);
};
countriesCallback = (country) => {
this.setState({
selectedCountry: country
});
const affiliation = this.state.selectedAffiliation;
this.recalculateArcs(this.props.data, affiliation, country);
};
prepareDatasetItems() {
// Hard code for demo here
}
hoverColumn({x, y, object}) {
this.setState({tooltipX: x, tooltipY: y, hoveredColumn: object});
}
hoverArc({object, x, y}) {
console.log(object)
}
clickColumn({object}) {
this.setState({
selectedAffiliation: object,
selectedCountry: "",
viewState: {
...this.state.viewState,
longitude: object.geometry.coordinates[0],
latitude: object.geometry.coordinates[1],
zoom: 8,
transitionDuration: 1000,
transitionInterpolator: new FlyToInterpolator()
}
});
this.recalculateArcs(this.props.data, object, "");
this.recalculateCountries(this.props.data, object);
}
renderColumnTooltip() {
const {tooltipX, tooltipY, hoveredColumn} = this.state;
if (!hoveredColumn) {
return
}
return (
<div className="column-tooltip" style={{left: tooltipX, top: tooltipY}}>
<h3>{hoveredColumn.properties.name}</h3>
<span>Total publications: {hoveredColumn.properties.publications_total}</span>
</div>
)
}
onViewStateChange({viewState}) {
this.setState({viewState});
}
flyToAffiliation(affiliation) {
const coordinates = affiliation.geometry.coordinates;
this.setState({
viewState: {
...this.state.viewState,
longitude: coordinates[0],
latitude: coordinates[1],
zoom: 10,
transitionDuration: 4000,
transitionInterpolator: new FlyToInterpolator()
}
});
}
renderLayers(dataFromProps = this.props.data) {
const scale = scaleQuantile()
.domain(dataFromProps.map(a => Math.abs(a.properties.publications_total)))
.range(colorRange.map((c, i) => {
return i
}));
let layers = [];
if (this.state.selectedAffiliation !== null) {
layers.push(
new ScatterplotLayer({
id: 'scatterLayer',
data: this.props.data.map(feature => {
return {...feature, position: feature.properties.centroid, name: feature.properties.name}
}),
opacity: 1,
pickable: true,
onHover: this.hoverColumn,
onClick: this.clickColumn,
stroked: true,
filled: false,
lineWidthMinPixels: 5,
getFillColor: d => colorRange[scale(d.properties.publications_total)],
getLineWidth: d => scale(d.properties.publications_total) * 100,
getLineColor: d => colorRange[scale(d.properties.publications_total)],
getRadius: d => scale(d.properties.publications_total) * 1000,
transitions: {
getRadius: {
type: 'spring',
stiffness: 0.01,
damping: 0.15,
enter: value => [0] // grow from size 0
}
}
})
);
layers.push(
new ArcLayer({
id: 'arcLayer',
data: this.state.arcs,
// getHeight: d => 1/d.quantile,
getSourcePosition: d => d.source,
getTargetPosition: d => d.target,
getSourceColor: d => colorRange[d.quantile],
getTargetColor: d => colorRange[d.quantile],
getWidth: d => d.quantile
})
)
} else {
layers.push(
new ColumnLayer({
id: 'columnLayer',
coverage: 1,
diskResolution: 6,
data: dataFromProps,
elevationScale: 2,
extruded: true,
getPosition: d => d.properties.centroid,
getElevation: d => d.properties.publications_total,
getFillColor: d => colorRange[scale(d.properties.publications_total)],
onClick: this.clickColumn,
onHover: this.hoverColumn,
pickable: true,
opacity: 1,
radius: 500,
upperPercentile: 100,
material: material,
transitions: {
getElevation: {
type: 'spring',
stiffness: 0.01,
damping: 0.15,
enter: value => [0] // grow from size 0
}
}
})
)
}
return layers
}
render() {
const {mapStyle = 'mapbox://styles/mapbox/dark-v10'} = this.props;
if (this.props.data !== undefined) {
return (
<div>
<div className={"filters-container"}>
<AffiliationsFilter
parentCallback={this.affiliationsSelectCallback}
filterId={"affiliation"}
filterName={"Affiliations"}
items={this.props.data}
selectedAffiliation={this.state.selectedAffiliation}
/>
<CountriesFilter
parentCallback={this.countriesCallback}
filterId={"country"}
filterName={"Countries"}
countries={this.state.countries}
selectedCountry={this.state.selectedCountry}
/>
</div>
<DeckGL
layers={this.renderLayers()}
initialViewState={this.state.viewState}
viewState={this.state.viewState}
controller={true}
onViewStateChange={this.onViewStateChange}
>
<StaticMap
reuseMaps
mapStyle={mapStyle}
preventStyleDiffing={true}
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
{this.renderColumnTooltip}
{document.addEventListener('contextmenu', event => event.preventDefault())}
</DeckGL>
</div>
)
} else {
return (<span>Loading ...</span>)
}
}
}
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
This diff is collapsed.
import React, {Component} from 'react';
import './css/affiliations.css'
import '../icons/css/iconmonstr-iconic-font.min.css'
function paginate(array, page_size, page_number) {
--page_number;