【问题标题】:How to generate D3.js circular dendrogram code from Python如何从 Python 生成 D3.js 圆形树状图代码
【发布时间】:2014-05-08 17:59:10
【问题描述】:

下图是用D3.js生成的。

基于代码here

<!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>

有没有一种方法可以使用 Python 生成上述精确数字 (HTML)?

【问题讨论】:

  • 当然有一种方法——你只需要用python重写d3.js库和DOM的所有相关部分——但这可能不是你想听到的。你能更清楚地解释为什么你想使用python而不是Javascript吗?也许有一种替代方法可以满足您的目标,而无需重新发明轮子。

标签: javascript python d3.js plot


【解决方案1】:

有没有一种方法可以使用 Python 生成上述精确数字 (HTML)?

pandita's suggestion 不太相似,您可以使用Brython,它允许您在网络浏览器中运行 Python。一些示例骨架代码(使用 D3.js;我不知道任何 Python 库可以为您提供与 D3.js 一样好的树状图结果。):

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

<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="brython.js"></script>
<script type="text/python">
# 
# Python code to generate dendogram here 
# (equivalent to the sample JavaScript code provided) 
#
</script>


<body onload="brython()">
</body>
</html>

但这似乎不是一个很好的解决方案。

您可能还会发现此 StackOverflow 页面很有帮助:How do I create a radial cluster like the following code-example in Python?

【讨论】:

    【解决方案2】:

    假设您想避免编写 javascript 并改用 python,您可以看看 pyjs。来自他们的website

    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.
    

    我没有使用过它,我不确定你将如何包含 d3 库。但是他们的 wiki 有以下页面:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2018-02-03
      相关资源
      最近更新 更多