Skip to content

Commit fbe1d79

Browse files
committed
Added onClick handler to individual vertices. Better example
1 parent c58dd0e commit fbe1d79

File tree

5 files changed

+104
-58
lines changed

5 files changed

+104
-58
lines changed

README.md

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,59 @@ A simple react component to draw branched graphs consisting of vertices and edge
88
3. Use directly in render function. Here's an example explaining all possible props you can pass:
99

1010
```jsx
11-
<Graph
12-
vertices={[ // all possible edges
13-
"HTML",
14-
"CSS",
15-
"jQuery",
16-
"JavaScript",
17-
"PHP",
18-
"Python",
19-
"Perl",
20-
"Ruby"
21-
]}
22-
edges={[ // edges between nodes. Please draw a graph on paper first and see for yourself how the edges flow to place a suitable array value here
23-
["HTML","CSS"],
24-
["CSS", "jQuery"],
25-
["jQuery","JavaScript"],
26-
["jQuery", "PHP"],
27-
["PHP", "Python"],
28-
["Python", "Perl"],
29-
["Python", "Ruby"]
30-
]}
31-
orientation="horizontal" // orientation of graph (whether horizontal or vertical)
32-
width={window.innerWidth} // width of graph
33-
height={window.innerHeight} // height of graph
34-
vertexStroke="#df6766" // color of border of vertices
35-
edgeStroke="#ebb2b2" // color of edge
36-
edgeWidth={2} // thickness of edge
37-
vertexRadius={10} // radius of vertex -> bigger the radius, larger the vertex size
38-
/>
11+
import React from 'react'
12+
import ReactDOM from 'react-dom'
13+
import Graph from 'reactjs-graphs'
14+
15+
const onClick = (label, index, extras) => {
16+
console.log(label, index, extras)
17+
}
18+
19+
const vertices = [
20+
{ label: "Here it begins", onClick, extras: "Some helper data" },
21+
{ label: "HTML", onClick },
22+
{ label: "CSS", onClick },
23+
{ label: "Sass", onClick },
24+
{ label: "JavaScript", onClick },
25+
{ label: "Bootstrap", onClick },
26+
{ label: "jQuery", onClick },
27+
{ label: "ReactJS", onClick, extras: "Could be anything, literally" },
28+
{ label: "Jest", onClick },
29+
{ label: "Angular", onClick },
30+
{ label: "Vue", onClick },
31+
{ label: "Redux", onClick },
32+
{ label: "React Material", onClick },
33+
{ label: "Vuetify", onClick },
34+
]
35+
36+
const edges = [
37+
["Here it begins", "HTML"],
38+
["HTML", "CSS"],
39+
["CSS", "JavaScript"],
40+
["CSS", "Sass"],
41+
["JavaScript", "jQuery"],
42+
["jQuery", "Bootstrap"],
43+
["jQuery", "ReactJS"],
44+
["jQuery", "Angular"],
45+
["jQuery", "Vue"],
46+
["ReactJS", "Redux"],
47+
["Redux", "React Material"],
48+
["React Material", "Jest"],
49+
["Vue", "Vuetify"],
50+
]
51+
52+
53+
ReactDOM.render(<Graph
54+
vertices={vertices}
55+
edges={edges}
56+
orientation="horizontal"
57+
width={window.innerWidth}
58+
height={window.innerHeight}
59+
vertexStroke="#df6766"
60+
edgeStroke="#ebb2b2"
61+
edgeWidth={2}
62+
vertexRadius={10}
63+
/>, document.getElementById('root'))
3964
```
4065

4166
4. The example above would render the following graph:

graph.png

100755100644
34.3 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reactjs-graphs",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Create branched graphs in ReactJS",
55
"main": "build/index.js",
66
"scripts": {

source/Graph.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class Graph extends React.Component {
1515
}
1616

1717
props.vertices.forEach((vertex, index) => {
18-
this.state.list.push({ name: vertex, next: [] })
18+
this.state.list.push({ name: vertex.label, next: [] })
1919
})
2020

2121
props.edges.map(edge => {
@@ -94,19 +94,19 @@ export default class Graph extends React.Component {
9494
let flag = false
9595

9696
props.vertices.forEach(vertex => {
97-
if(vertex in this.state.vertexCoordinates) {
97+
if(vertex.label in this.state.vertexCoordinates) {
9898
// ok
9999
} else {
100100
if(flag) throw new Error("Initializing the graph with multiple vertices? Only 1 vertex is supported for now")
101101
// this is the first vertex which was not registered in the for-loop above
102102
flag = true
103103
if(this.props.orientation === 'horizontal') {
104-
this.state.vertexCoordinates[vertex] = {
104+
this.state.vertexCoordinates[vertex.label] = {
105105
x: 0,
106106
y: totalYAllowed/2
107107
}
108108
} else {
109-
this.state.vertexCoordinates[vertex] = {
109+
this.state.vertexCoordinates[vertex.label] = {
110110
x: totalYAllowed/2,
111111
y: 0
112112
}
@@ -167,10 +167,10 @@ export default class Graph extends React.Component {
167167
{vertices.map((vertex, index) => {
168168
return <Vertex
169169
key={index}
170-
x={vertexCoordinates[vertex].x}
171-
y={vertexCoordinates[vertex].y}
172-
label={vertex}
173-
onClick={_ => alert(vertex)}
170+
x={vertexCoordinates[vertex.label].x}
171+
y={vertexCoordinates[vertex.label].y}
172+
label={vertex.label}
173+
onClick={_ => vertex.onClick(vertex.label, index, vertex.extras)}
174174
{...vertexProps}
175175
/>
176176
})

source/index.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom'
3-
import Graph from '../build/index'
3+
import Graph from './Graph'
4+
5+
const onClick = (label, index, extras) => {
6+
console.log(label, index, extras)
7+
}
8+
9+
const vertices = [
10+
{ label: "Here it begins", onClick, extras: "Some helper data" },
11+
{ label: "HTML", onClick },
12+
{ label: "CSS", onClick },
13+
{ label: "Sass", onClick },
14+
{ label: "JavaScript", onClick },
15+
{ label: "Bootstrap", onClick },
16+
{ label: "jQuery", onClick },
17+
{ label: "ReactJS", onClick },
18+
{ label: "Jest", onClick },
19+
{ label: "Angular", onClick },
20+
{ label: "Vue", onClick },
21+
{ label: "Redux", onClick },
22+
{ label: "React Material", onClick },
23+
{ label: "Vuetify", onClick },
24+
]
25+
26+
const edges = [
27+
["Here it begins", "HTML"],
28+
["HTML", "CSS"],
29+
["CSS", "JavaScript"],
30+
["CSS", "Sass"],
31+
["JavaScript", "jQuery"],
32+
["jQuery", "Bootstrap"],
33+
["jQuery", "ReactJS"],
34+
["jQuery", "Angular"],
35+
["jQuery", "Vue"],
36+
["ReactJS", "Redux"],
37+
["Redux", "React Material"],
38+
["React Material", "Jest"],
39+
["Vue", "Vuetify"],
40+
]
41+
442

543
ReactDOM.render(<Graph
6-
vertices={[
7-
"HTML",
8-
"CSS",
9-
"jQuery",
10-
"JavaScript",
11-
"PHP",
12-
"Python",
13-
"Perl",
14-
"Ruby"
15-
]}
16-
edges={[
17-
["HTML","CSS"],
18-
["CSS", "jQuery"],
19-
["jQuery","JavaScript"],
20-
["jQuery", "PHP"],
21-
["PHP", "Python"],
22-
["Python", "Perl"],
23-
["Python", "Ruby"]
24-
]}
44+
vertices={vertices}
45+
edges={edges}
2546
orientation="horizontal"
2647
width={window.innerWidth}
27-
height={167}
48+
height={window.innerHeight}
2849
vertexStroke="#df6766"
2950
edgeStroke="#ebb2b2"
3051
edgeWidth={2}

0 commit comments

Comments
 (0)