如何从Python生成D3.js循环树形图代码

nev*_*int 5 javascript python plot d3.js

使用D3.js生成下图.

在此输入图像描述

根据这里的代码:

<!DOCTYPE html>
<meta charset="utf-8">
<title>Flare Dendrogram</title>
<style>

.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var radius = 960 / 2;

var cluster = d3.layout.cluster()
    .size([360, radius - 120]);

var diagonal = d3.svg.diagonal.radial()
    .projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });

var svg = d3.select("body").append("svg")
    .attr("width", radius * 2)
    .attr("height", radius * 2)
  .append("g")
    .attr("transform", "translate(" + radius + "," + radius + ")");

d3.json("/d/4063550/flare.json", function(error, root) {
  var nodes = cluster.nodes(root);

  var link = svg.selectAll("path.link")
      .data(cluster.links(nodes))
    .enter().append("path")
      .attr("class", "link")
      .attr("d", diagonal);

  var node = svg.selectAll("g.node")
      .data(nodes)
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })

  node.append("circle")
      .attr("r", 4.5);

  node.append("text")
      .attr("dy", ".31em")
      .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
      .attr("transform", function(d) { return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)"; })
      .text(function(d) { return d.name; });
});

d3.select(self.frameElement).style("height", radius * 2 + "px");

</script>
Run Code Online (Sandbox Code Playgroud)

有没有办法可以使用Python生成上面的确切数字(HTML)?

pan*_*ita 4

假设你想避免编写 javascript 并使用 python 来代替,你可以看看 pyjs。从他们的网站

pyjs contains a Python-to-JavaScript compiler, an AJAX framework and a 
Widget Set API. pyjs started life as a Python port of Google Web Toolkit, 
the Java-to-JavaScript compiler.
Run Code Online (Sandbox Code Playgroud)

我还没有使用过它,并且不确定您将如何包含 d3 库。然而他们的维基有以下页面:

https://github.com/pyjs/pyjs/wiki/Calling-a-jQuery-component-from-Pyjs

那篇文章可能会让您了解 pyjs 对您是否有用。