Skip to content

Instantly share code, notes, and snippets.

@jason-s
Last active August 29, 2015 14:23
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://getspringy.com/springy.js"></script>
<script src="http://getspringy.com/springyui.js"></script>
<script>
var graph = new Springy.Graph();
var recipes =
["Martian Stew:thingy,pig,cheese danish,chicken",
"cheese danish:flour,butter,eggs,vanilla,cream cheese,sugar",
"butter:milk,salt",
"thingy:iron,apple,vanilla",
"cream cheese:milk,salt",
"eggs:chicken",
"chicken:worm",
"milk:cow",
"cow:grass",
"pig:apple,worm",
"worm:apple"];
function createGraphFromRecipes(graph, recipes)
{
var nodes = {};
function addNode(name)
{
if (!(name in nodes))
{
nodes[name] = graph.newNode({label: name});
}
return nodes[name];
}
for (var i = 0; i < recipes.length; ++i)
{
var recipe = recipes[i];
var info = recipe.split(':');
var result = info[0];
var ingredients = info[1];
ingredients = ingredients.split(',');
var dstnode = addNode(result);
for (var j = 0; j < ingredients.length; ++j)
{
var srcnode = addNode(ingredients[j]);
graph.newEdge(srcnode, dstnode, {color: '#E0E0E0'});
}
}
}
createGraphFromRecipes(graph,recipes);
jQuery(function(){
var springy = window.springy = jQuery('#springydemo').springy({
graph: graph,
nodeSelected: function(node){
console.log('Node selected: ' + JSON.stringify(node.data));
}
});
});
</script>
<canvas id="springydemo" width="800" height="600" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment