문제

I'm trying to use this sample code.

My question is for this line : d3.json("miserables.json", function(error, graph){}

Where should I put the miserables.json file?

My d3.js is in static/js/d3.py

Should I just put the file under static/js?

I have tried this, but all I got is a blank page.

I'm not familiar with how path works for d3.json()

My html file:

{{extend 'layout.html'}}
 <div id="vi"></div>
<style>

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

.link {
  stroke: #999;
  stroke-opacity: .6;
}

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

var width = 960,
    height = 500;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.select("#vi").json("miserables.json", function(error, graph) {
  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", 5)
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

  node.append("title")
      .text(function(d) { return d.name; });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

</script>
<p>k<p>

This response.files.append(URL('static', 'plugin_d3/d3/d3.js')) is in my function code already. And there's an copy of the json code under static/plugin_d3/d3 also!

Please bear me for this simple question.

Thanks!!

도움이 되었습니까?

해결책

Yes, you can put miserable.json in the /static folder (perhaps in a subfolder within /static). You then have to use the correct URL to refer to it. Given that you code is in a web2py template, you can use the URL() function to generate the URL:

d3.select("#vi").json("{{=URL('static', 'miserables.json')}}", ...)

In your original code:

d3.select("#vi").json("miserables.json", ...)

"miserable.json" is a relative URL, so the browser will just append that to the URL of the current page, which would end up being something like /yourapp/default/index/miserables.json. Instead, you need to specify the full URL, such as /yourapp/static/miserables.json (which the URL() function will generate).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top