简体   繁体   English

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

[英]How to generate D3.js circular dendrogram code from Python

The following figure is generated with D3.js. 使用D3.js生成下图。

在此输入图像描述

based on the code 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>

Is there a way I can use Python to generate the above exact figure (HTML)? 有没有办法可以使用Python生成上面的确切数字(HTML)?

Assuming that you want to avoid having to write javascript and using python instead, you could have a look at pyjs. 假设您想要避免编写javascript并使用python,您可以查看pyjs。 From their 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. 

I haven't used it and am unsure how you would go about including the d3 library. 我没有使用它,我不确定你将如何包括d3库。 However their wiki has the following page: 但是他们的wiki有以下页面:

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

That article might give you an idea of whether pyjs is useful to you. 那篇文章可能会让你知道pyjs是否对你有用。

Is there a way I can use Python to generate the above exact figure (HTML)? 有没有办法可以使用Python生成上面的确切数字(HTML)?

Not too dissimilar to pandita's suggestion , you could use Brython , which allows you to run Python in a web browser. pandita的建议没什么不同,您可以使用Brython ,它允许您在Web浏览器中运行Python。 Some sample skeleton code (using D3.js; I don't know of any Python libraries that give you a dendogram result as good as D3.js.): 一些示例框架代码(使用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>

But this doesn't seem like a great solution. 但这似乎不是一个很好的解决方案。

You may also find this StackOverflow page helpful: How do I create a radial cluster like the following code-example in Python? 您可能还会发现此StackOverflow页面很有用: 如何在Python中创建径向集群,如下面的代码示例?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM