Skip to content

Commit

Permalink
Add React Project from Twitch Session
Browse files Browse the repository at this point in the history
  • Loading branch information
jexp committed Jun 17, 2020
1 parent 6c9ad0d commit 980d0b7
Show file tree
Hide file tree
Showing 22 changed files with 11,570 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
const session = driver.session({database:"gameofthrones"});
const start = new Date()
session
.run('MATCH (n)-->(m) RETURN id(n) as source, id(m) as target LIMIT $limit', {limit: neo4j.int(5000)})
.run('MATCH (n)-[:INTERACTS1]->(m) RETURN id(n) as source, id(m) as target LIMIT $limit', {limit: neo4j.int(5000)})
.then(function (result) {
const links = result.records.map(r => { return {source:r.get('source').toNumber(), target:r.get('target').toNumber()}});
session.close();
console.log(links.length+" links loaded in "+(new Date()-start)+" ms.")
const ids = new Set()
links.forEach(l => {ids.add(l.source);ids.add(l.target);});
const gData = { nodes: Array.from(ids).map(id => {return {id:id}}), links: links}
const gData = { nodes: Array.from(ids).map(id => {return {id}}), links: links}
const Graph = ForceGraph3D()(document.getElementById('3d-graph')).graphData(gData);
})
.catch(function (error) {
Expand Down
2 changes: 1 addition & 1 deletion particles.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const session = driver.session({database:"gameofthrones"});
const start = new Date()
session
.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)})
.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)})
.then(function (result) {
const nodes = {}
const links = result.records.map(r => {
Expand Down
23 changes: 23 additions & 0 deletions react-graph-viz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
27 changes: 27 additions & 0 deletions react-graph-viz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# React Graph Viz
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).

A Cypher query from a textarea is used to query the database.
The results are then rendered as graph visualization.

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `yarn 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.

### `yarn 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.
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
36 changes: 36 additions & 0 deletions react-graph-viz/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "graph-viz",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"neo4j-driver": "^4.0.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-force-graph-2d": "^1.16.3",
"react-scripts": "3.4.1"
},
"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"
]
}
}
Binary file added react-graph-viz/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions react-graph-viz/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!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>
Binary file added react-graph-viz/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added react-graph-viz/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions react-graph-viz/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"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"
}
3 changes: 3 additions & 0 deletions react-graph-viz/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions react-graph-viz/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
13 changes: 13 additions & 0 deletions react-graph-viz/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import './App.css';
import CypherViz from './CypherViz';

function App({driver}) {
return (
<div className="App">
<CypherViz driver={driver}></CypherViz>
</div>
);
}

export default App;
9 changes: 9 additions & 0 deletions react-graph-viz/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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();
});
52 changes: 52 additions & 0 deletions react-graph-viz/src/CypherViz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import './App.css';
import ForceGraph2D from 'react-force-graph-2d';

// Usage: <CypherViz driver={driver}/>

class CypherViz extends React.Component {
constructor({driver}) {
super();
this.driver = driver;
this.state = {
query: `
MATCH (n:Character)-[:INTERACTS1]->(m:Character)
RETURN n.name as source, m.name as target
`,
data : {nodes:[{name:"Joe"},{name:"Jane"}],links:[{source:"Joe",target:"Jane"}]} }
}

handleChange = (event) => {
this.setState({query:event.target.value})
}
loadData = async () => {
let session = await this.driver.session({database:"gameofthrones"});
let res = await session.run(this.state.query);
session.close();
console.log(res);
let nodes = new Set();
let links = res.records.map(r => {
let source = r.get("source");
let target = r.get("target");
nodes.add(source);
nodes.add(target);
return {source, target}});
nodes = Array.from(nodes).map(name => {return {name}});
this.setState({ data : {nodes, links}});
}

render() {
return (
<div>
<textarea style={{display:"block",width:"800px", height:"100px"}}
value={this.state.query}
onChange={this.handleChange}/>
<button onClick={this.loadData}>Reload</button>
<ForceGraph2D graphData={this.state.data} nodeId="name"
linkCurvature={0.2} linkDirectionalArrowRelPos={1} linkDirectionalArrowLength={10}/>
</div>
);
}
}

export default CypherViz
13 changes: 13 additions & 0 deletions react-graph-viz/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
29 changes: 29 additions & 0 deletions react-graph-viz/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import * as neo4j from 'neo4j-driver';

const driver = neo4j.driver(
process.env.NEO4J_URI || 'bolt://demo.neo4jlabs.com',
neo4j.auth.basic(
process.env.NEO4J_USER || 'gameofthrones',
process.env.NEO4J_PASSWORD || 'gameofthrones'
),
{
encrypted: process.env.NEO4J_ENCRYPTED ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF',
}
)

ReactDOM.render(
<React.StrictMode>
<App driver={driver}/>
</React.StrictMode>,
document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
7 changes: 7 additions & 0 deletions react-graph-viz/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 980d0b7

Please sign in to comment.