score:1

there's many possible implementations of this; here's an example.

given a csv of:

x,y,r,l
5,26,10,2
43,62,22,2
45,21,15,0
10,10,12,0

where x and y are position, r is radius and l is the index of another point to draw a line to.

<!doctype html>
<meta charset="utf-8">
<style>

.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}

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

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispedges;
}

</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>

// standard d3 plot setup
var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 500 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var xaxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yaxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
var color = d3.scale.category10();

// commenting out to play nice with stack overflow code editor
/*
d3.csv("data.csv", function(d){
  return {
    x: +d.x,
    y: +d.y,
    r: +d.r,
    l: +d.l
  }
}, function(error, data) {
*/

  // this would be the data return from d3.csv
  var data = [{"x":5,"y":26,"r":10,"l":2},{"x":43,"y":62,"r":22,"l":2},{"x":45,"y":21,"r":15,"l":0},{"x":10,"y":10,"r":12,"l":0}];
  
  // standard axis stuff
  x.domain([0, d3.max(data, function(d) { return d.x; })]);
  y.domain([0, d3.max(data, function(d) { return d.y; })]);
  
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xaxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yaxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end");

  // draw a point g
  var point = svg.selectall(".point")
    .data(data)
    .enter()
    .append("g")
    .attr("class", "point");
    
  // add circle
  point.append("circle")
    .attr("cx", function(d) { return x(d.x); })
    .attr("r", function(d) { return d.r; })
    .attr("cy", function(d) { return y(d.y); })
    .style("fill", function(d,i){ return color(i); })
    // handle mouse events
    // in this case we are going to show 
    // lines drawn to our mouse point
    .on("mouseover",function(d, i){
        d3.selectall(".lineto" + i)
          .style("opacity", 1);
    })
    .on("mouseout",function(d, i){
        d3.selectall(".lineto" + i)
          .style("opacity", 0);
    })
  
  // add line and set opacity to invisible
  point
    .append("line")
    .attr("x1", function(d) { return x(d.x); })
    .attr("y1", function(d) { return y(d.y); })
    .attr("x2", function(d) { return x(data[d.l].x); })
    .attr("y2", function(d) { return y(data[d.l].y); })
    .attr("class", function(d) { return "lineto" + d.l; })
    .style("stroke", function(d,i){ return color(i); })
    .style("stroke-width", 2)
    .style("fill","none")
    .style("opacity", 0);
    
//});

</script>
</body>
</html>


Related Query

More Query from same tag