سؤال

I am unable to get this seemingly simple code to work. I have a csv file with 5 rows of data and trying to create a bubble chart out of it! Really appreciate if someone can help!

<body>
    <script type="text/javascript">


var diameter = 960,
format = d3.format(",d"),
color = d3.scale.category20c();

var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5);

        var svg = d3.select("#svgid")
                    .append("svg")
                    .attr("width", diameter)
                    .attr("height", diameter)
                    .attr("class","bubble");


        //Data
        //var dataset = [ 5, 10, 15, 20, 25 ];
        d3.text("http://bpgpuae.com/bil-rupeex.csv", function(csvData) {

            var dataset = d3.csv.parse(csvData);

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

  node.append("title")
  .text(function(d) { return d.scam + ": " + format(d.funds); });

  node.append("circle")
  .attr("r", function(d) { return d.funds; })
  .style("fill", function(d) { return color(d.decade); });
});


    </script>
</body>

and here's the csv file:

decade,scam,funds
2010s,NSEL  scam,55
2010s,Railway Iron-Ore freight scam,170
2010s,Vodafone tax scam,110
2010s,Odisha Mining scam,600
2010s,DIAL Scam,1670
2010s,Tamil Nadu Granite scam,160
هل كانت مفيدة؟

المحلول

The pack layout is expecting a hierarchical data structure. So, you have to prepare the flat CSV data accordingly.

var data = { name: "decade", children: csvData };

var node = vis.data([data]).selectAll("circle")
    .data(pack.nodes)
    ...

Here is a working PLUNK with your data and most of your original logic.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top