Skip to content

Commit 980d0b7

Browse files
committed
Add React Project from Twitch Session
1 parent 6c9ad0d commit 980d0b7

22 files changed

+11570
-3
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
const session = driver.session({database:"gameofthrones"});
1515
const start = new Date()
1616
session
17-
.run('MATCH (n)-->(m) RETURN id(n) as source, id(m) as target LIMIT $limit', {limit: neo4j.int(5000)})
17+
.run('MATCH (n)-[:INTERACTS1]->(m) RETURN id(n) as source, id(m) as target LIMIT $limit', {limit: neo4j.int(5000)})
1818
.then(function (result) {
1919
const links = result.records.map(r => { return {source:r.get('source').toNumber(), target:r.get('target').toNumber()}});
2020
session.close();
2121
console.log(links.length+" links loaded in "+(new Date()-start)+" ms.")
2222
const ids = new Set()
2323
links.forEach(l => {ids.add(l.source);ids.add(l.target);});
24-
const gData = { nodes: Array.from(ids).map(id => {return {id:id}}), links: links}
24+
const gData = { nodes: Array.from(ids).map(id => {return {id}}), links: links}
2525
const Graph = ForceGraph3D()(document.getElementById('3d-graph')).graphData(gData);
2626
})
2727
.catch(function (error) {

particles.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
const session = driver.session({database:"gameofthrones"});
1616
const start = new Date()
1717
session
18-
.run('MATCH (n)-[r]->(m) RETURN { id: id(n), label:head(labels(n)), community:n.louvain, caption:n.name, size:n.pagerank } as source, { id: id(m), label:head(labels(m)), community:n.louvain, caption:m.name, size:m.pagerank } as target, {weight:r.weight, type:type(r), community:case when n.community < m.community then n.community else m.community end} as rel LIMIT $limit', {limit: neo4j.int(5000)})
18+
.run('MATCH (n)-[r:INTERACTS1]->(m) RETURN { id: id(n), label:head(labels(n)), community:n.louvain, caption:n.name, size:n.pagerank } as source, { id: id(m), label:head(labels(m)), community:n.louvain, caption:m.name, size:m.pagerank } as target, {weight:r.weight, type:type(r), community:case when n.community < m.community then n.community else m.community end} as rel LIMIT $limit', {limit: neo4j.int(5000)})
1919
.then(function (result) {
2020
const nodes = {}
2121
const links = result.records.map(r => {

react-graph-viz/.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

react-graph-viz/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# React Graph Viz
2+
This project shows how to use [2d-force-graph react components](https://github.com/vasturiano/react-force-graph) with a [Neo4j Database](https://neo4j.com/developer).
3+
4+
A Cypher query from a textarea is used to query the database.
5+
The results are then rendered as graph visualization.
6+
7+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
8+
9+
## Available Scripts
10+
11+
In the project directory, you can run:
12+
13+
### `yarn start`
14+
15+
Runs the app in the development mode.<br />
16+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
17+
18+
The page will reload if you make edits.<br />
19+
You will also see any lint errors in the console.
20+
21+
### `yarn build`
22+
23+
Builds the app for production to the `build` folder.<br />
24+
It correctly bundles React in production mode and optimizes the build for the best performance.
25+
Your app is ready to be deployed!
26+
27+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

react-graph-viz/package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "graph-viz",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^4.2.4",
7+
"@testing-library/react": "^9.3.2",
8+
"@testing-library/user-event": "^7.1.2",
9+
"neo4j-driver": "^4.0.2",
10+
"react": "^16.13.1",
11+
"react-dom": "^16.13.1",
12+
"react-force-graph-2d": "^1.16.3",
13+
"react-scripts": "3.4.1"
14+
},
15+
"scripts": {
16+
"start": "react-scripts start",
17+
"build": "react-scripts build",
18+
"test": "react-scripts test",
19+
"eject": "react-scripts eject"
20+
},
21+
"eslintConfig": {
22+
"extends": "react-app"
23+
},
24+
"browserslist": {
25+
"production": [
26+
">0.2%",
27+
"not dead",
28+
"not op_mini all"
29+
],
30+
"development": [
31+
"last 1 chrome version",
32+
"last 1 firefox version",
33+
"last 1 safari version"
34+
]
35+
}
36+
}

react-graph-viz/public/favicon.ico

3.08 KB
Binary file not shown.

react-graph-viz/public/index.html

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

react-graph-viz/public/logo192.png

5.22 KB
Loading

react-graph-viz/public/logo512.png

9.44 KB
Loading

react-graph-viz/public/manifest.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

react-graph-viz/public/robots.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:

react-graph-viz/src/App.css

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.App {
2+
text-align: center;
3+
}
4+
5+
.App-logo {
6+
height: 40vmin;
7+
pointer-events: none;
8+
}
9+
10+
@media (prefers-reduced-motion: no-preference) {
11+
.App-logo {
12+
animation: App-logo-spin infinite 20s linear;
13+
}
14+
}
15+
16+
.App-header {
17+
background-color: #282c34;
18+
min-height: 100vh;
19+
display: flex;
20+
flex-direction: column;
21+
align-items: center;
22+
justify-content: center;
23+
font-size: calc(10px + 2vmin);
24+
color: white;
25+
}
26+
27+
.App-link {
28+
color: #61dafb;
29+
}
30+
31+
@keyframes App-logo-spin {
32+
from {
33+
transform: rotate(0deg);
34+
}
35+
to {
36+
transform: rotate(360deg);
37+
}
38+
}

react-graph-viz/src/App.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import './App.css';
3+
import CypherViz from './CypherViz';
4+
5+
function App({driver}) {
6+
return (
7+
<div className="App">
8+
<CypherViz driver={driver}></CypherViz>
9+
</div>
10+
);
11+
}
12+
13+
export default App;

react-graph-viz/src/App.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import App from './App';
4+
5+
test('renders learn react link', () => {
6+
const { getByText } = render(<App />);
7+
const linkElement = getByText(/learn react/i);
8+
expect(linkElement).toBeInTheDocument();
9+
});

react-graph-viz/src/CypherViz.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import './App.css';
3+
import ForceGraph2D from 'react-force-graph-2d';
4+
5+
// Usage: <CypherViz driver={driver}/>
6+
7+
class CypherViz extends React.Component {
8+
constructor({driver}) {
9+
super();
10+
this.driver = driver;
11+
this.state = {
12+
query: `
13+
MATCH (n:Character)-[:INTERACTS1]->(m:Character)
14+
RETURN n.name as source, m.name as target
15+
`,
16+
data : {nodes:[{name:"Joe"},{name:"Jane"}],links:[{source:"Joe",target:"Jane"}]} }
17+
}
18+
19+
handleChange = (event) => {
20+
this.setState({query:event.target.value})
21+
}
22+
loadData = async () => {
23+
let session = await this.driver.session({database:"gameofthrones"});
24+
let res = await session.run(this.state.query);
25+
session.close();
26+
console.log(res);
27+
let nodes = new Set();
28+
let links = res.records.map(r => {
29+
let source = r.get("source");
30+
let target = r.get("target");
31+
nodes.add(source);
32+
nodes.add(target);
33+
return {source, target}});
34+
nodes = Array.from(nodes).map(name => {return {name}});
35+
this.setState({ data : {nodes, links}});
36+
}
37+
38+
render() {
39+
return (
40+
<div>
41+
<textarea style={{display:"block",width:"800px", height:"100px"}}
42+
value={this.state.query}
43+
onChange={this.handleChange}/>
44+
<button onClick={this.loadData}>Reload</button>
45+
<ForceGraph2D graphData={this.state.data} nodeId="name"
46+
linkCurvature={0.2} linkDirectionalArrowRelPos={1} linkDirectionalArrowLength={10}/>
47+
</div>
48+
);
49+
}
50+
}
51+
52+
export default CypherViz

react-graph-viz/src/index.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}

react-graph-viz/src/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './index.css';
4+
import App from './App';
5+
import * as serviceWorker from './serviceWorker';
6+
import * as neo4j from 'neo4j-driver';
7+
8+
const driver = neo4j.driver(
9+
process.env.NEO4J_URI || 'bolt://demo.neo4jlabs.com',
10+
neo4j.auth.basic(
11+
process.env.NEO4J_USER || 'gameofthrones',
12+
process.env.NEO4J_PASSWORD || 'gameofthrones'
13+
),
14+
{
15+
encrypted: process.env.NEO4J_ENCRYPTED ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF',
16+
}
17+
)
18+
19+
ReactDOM.render(
20+
<React.StrictMode>
21+
<App driver={driver}/>
22+
</React.StrictMode>,
23+
document.getElementById('root')
24+
);
25+
26+
// If you want your app to work offline and load faster, you can change
27+
// unregister() to register() below. Note this comes with some pitfalls.
28+
// Learn more about service workers: https://bit.ly/CRA-PWA
29+
serviceWorker.unregister();

react-graph-viz/src/logo.svg

+7
Loading

0 commit comments

Comments
 (0)