#!/usr/bin/env python # coding: utf-8 # # Autotranslation: Python to JavaScript and D3 # # Generate a random graph with Python, then visualize it with a [D3](http://d3js.org/) interactive, force-directed graph. # # The first cell imports the BeakerX package and initializes the runtime. # # Then we generates the graph (one made of nodes and edges, like a social network graph) # and store it in the BeakerX object. # # Then we load D3 and set its styles. # # Finally, a JavaScript cell gets the data from the BeakerX object and renders it with D3. # # This final cell was # copied almost verbatim from the [D3 documentation](http://bl.ocks.org/mbostock/4062045). Other D3 examples # should be similarly easy to get working in BeakerX. # In[ ]: from beakerx.object import beakerx # In[ ]: from random import randrange import math nnodes = 100 nodes = [] links = [] for i in range(0, nnodes): nodes.append({"name": str(i), "group": int(i*7/nnodes)}) for i in range(0, int(nnodes*1.15)): source = i % nnodes target = int(math.log(1 + randrange(nnodes), 1.3)) value = 10.0 / (1 + abs(source - target)) links.append({"source": source, "target": target, "value": value * value}) beakerx.graph = {"nodes":nodes, "links":links} # In[ ]: get_ipython().run_cell_magic('javascript', '', "require.config({\n paths: {\n d3: '//cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min'\n }});\n") # In[ ]: get_ipython().run_cell_magic('html', '', '\n') # In[ ]: get_ipython().run_cell_magic('javascript', '', '\nbeakerx.displayHTML(this, \'
\');\n\nvar graph = beakerx.graph\n\nvar d3 = require([\'d3\'], function (d3) {\n \n var width = 600,\n height = 500;\n\n var color = d3.scaleOrdinal(d3.schemeCategory20);\n\n var simulation = d3.forceSimulation()\n .force("link", d3.forceLink().distance(30))\n .force("charge", d3.forceManyBody().strength(-200))\n .force("center", d3.forceCenter(width / 2, height / 2))\n .force("y", d3.forceY(width / 2).strength(0.3))\n .force("x", d3.forceX(height / 2).strength(0.3));\n\n var svg = d3.select("#fdg")\n .append("svg")\n .attr("width", width)\n .attr("height", height)\n .attr("transform", "translate("+[100, 0]+")");\n\n simulation\n .nodes(graph.nodes)\n .force("link")\n .links(graph.links);\n\n var link = svg.selectAll(".link")\n .data(graph.links)\n .enter().append("line")\n .attr("class", "link")\n .style("stroke-width", function(d) { return Math.sqrt(d.value); });\n\n var node = svg.selectAll(".node")\n .data(graph.nodes)\n .enter().append("circle")\n .attr("class", "node")\n .attr("r", 10)\n .style("fill", function(d) { return color(d.group); });\n\n node.append("title")\n .text(function(d) { return d.name; });\n\n simulation.on("tick", function() {\n link.attr("x1", function(d) { return d.source.x; })\n .attr("y1", function(d) { return d.source.y; })\n .attr("x2", function(d) { return d.target.x; })\n .attr("y2", function(d) { return d.target.y; });\n\n node.attr("cx", function(d) { return d.x; })\n .attr("cy", function(d) { return d.y; });\n });\n \n node.call(d3.drag()\n .on("start", dragstarted)\n .on("drag", dragged)\n .on("end", dragended)\n );\n \n function dragstarted(d) {\n if (!d3.event.active) simulation.alphaTarget(0.3).restart();\n d.fx = d.x;\n d.fy = d.y;\n }\n\n function dragged(d) {\n d.fx = d3.event.x;\n d.fy = d3.event.y;\n }\n\n function dragended(d) {\n if (!d3.event.active) simulation.alphaTarget(0);\n d.fx = null;\n d.fy = null;\n }\n});\n') # In[ ]: