-
Notifications
You must be signed in to change notification settings - Fork 46
/
graph.jsx
78 lines (66 loc) · 2.19 KB
/
graph.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* global React,ReactDOM,d3,dagreD3,App*/
var update = function(props) {
return function(me) {
var g = new dagreD3.graphlib.Graph()
.setGraph({})
.setDefaultEdgeLabel(function() { return {}; });
// Here we"re setting nodeclass, which is used by our custom drawNodes function
// below.
var i = 0;
var dict = {};
var visit = function (recipe, k) {
dict[recipe.name] = k;
g.setNode(k, { label: recipe.name, class: "type-TOP"});
if (recipe.ingredients && recipe.ingredients.length) {
for (var j = 0; j < recipe.ingredients.length; j++) {
var ingredient = recipe.ingredients[j];
if(!(ingredient.name in dict)){
var n = ++i;
visit(ingredient.recipe, n);
}
if(dict[ingredient.name] != dict[recipe.name]) {
g.setEdge(dict[ingredient.name], dict[recipe.name],
{}); //lineInterpolate: 'linear'
}
}
}
};
if (props.req) {
props.req.recipes.forEach(function(recipe) {
visit(recipe, i);
i++;
});
}
// Create the renderer
// var renderer = new dagreD3.Renderer().edgeInterpolate("linear");
var render = new dagreD3.render();
// Run the renderer. This is what draws the final graph.
render(me, g);
// Center the graph. Skip if graph is empty.
if (i > 0) {
var svgGroup = me;
var svg = d3.select("svg");
svgGroup.attr("transform", "translate(50, 5)");
svg.attr("width", g.graph().width + 100);
svg.attr("height", g.graph().height + 10);
}
};
};
App.Graph = class Graph extends React.Component {
render() {
return <div className="graph"><svg width="1" height="1"></svg></div>;
}
componentDidMount() {
d3.select(ReactDOM.findDOMNode(this))
.select("svg")
.append("g")
.call(update(this.props));
}
shouldComponentUpdate(props) {
d3.select(ReactDOM.findDOMNode(this))
.select("g")
.call(update(props));
// always skip React's render step
return false;
}
};