Natasha R.
Natasha R.

Reputation: 531

How to add XY coordinate data to a four quadrant scatter-plot using D3?

This is likely a very basic question, but I'm a newbie to D3/JS and would appreciate any help.

I'm trying to add coordinate data to a four quadrant scatter-plot, but I'm not sure how one adds data using D3.

Here is my code so far. It produces an empty four quadrant scatter-plot:

// graph dimensions
    var width = 750,
        height = 750,
        padding = 50;

    // svg container
    var vis = d3.select("#graph")
        .append("svg:svg")
        .attr("width", width)
        .attr("height", height);

    var xScale = d3.scale.linear().domain([1, -1]).range([width - padding, padding]);
    var yScale = d3.scale.linear().domain([-1, 1]).range([height - padding, padding]);

    // y axis
    var yAxis = d3.svg.axis()
            .orient("left")
            .scale(yScale);

    // x axis
    var xAxis = d3.svg.axis()
            .orient("bottom")
            .scale(xScale);

    var xAxisPlot = vis.append("g")
            .attr("class", "axis axis--x")
            .attr("transform", "translate(0," + (height/2) + ")")
            .call(xAxis.tickSize(-height, 0, 0));

    var yAxisPlot = vis.append("g")
            .attr("class", "axis axis--y")
            .attr("transform", "translate("+ (width/2) +",0)")
            .call(yAxis.tickSize(-width, 0, 0));

Any suggestions for how I could add a single XY coordinate to this plane? I really appreciate it.

Thank you in advance!

Upvotes: 0

Views: 1070

Answers (1)

Mark
Mark

Reputation: 108567

With d3, you generally use data-binding. Say we have data in the following array where each member of the array is an object with properties x and y:

var data = [
  {
    x: -0.454,
    y: 0.786
  },{
    x: -0.454,
    y: 0.786
  }
];

We would bind this data and generate a circle for each point like so:

// get all elements with class point
vis.selectAll(".point")
  // bind my data to them
  .data(data)
  // for all those points that are new
  .enter()
  // add a circle
  .append("circle")
  // class them appropriately
  .attr("class", "point")
  // radius
  .attr("r", 5)
  // make em blue
  .style("fill", "steelblue")
  // position them
  .attr("cx", function(d){
    return xScale(d.x);
  })
  .attr("cy", function(d){
    return yScale(d.y);
  });

Running code:

<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="3.5.17" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
  <style>
    body {
      font: 12px Arial;
    }
    
    path {
      stroke: steelblue;
      stroke-width: 2;
      fill: none;
    }
    
    .axis path,
    .axis line {
      fill: none;
      stroke: grey;
      stroke-width: 1;
      shape-rendering: crispEdges;
    }
  </style>
</head>

<body>
  <div id="graph"></div>
  <script>
    // graph dimensions
    var width = 750,
      height = 750,
      padding = 50;

    // svg container
    var vis = d3.select("#graph")
      .append("svg:svg")
      .attr("width", width)
      .attr("height", height);

    var xScale = d3.scale.linear().domain([1, -1]).range([width - padding, padding]);
    var yScale = d3.scale.linear().domain([-1, 1]).range([height - padding, padding]);

    // y axis
    var yAxis = d3.svg.axis()
      .orient("left")
      .scale(yScale);

    // x axis
    var xAxis = d3.svg.axis()
      .orient("bottom")
      .scale(xScale);

    var xAxisPlot = vis.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + (height / 2) + ")")
      .call(xAxis) //.tickSize(-height, 0));

    var yAxisPlot = vis.append("g")
      .attr("class", "axis axis--y")
      .attr("transform", "translate(" + (width / 2) + ",0)")
      .call(yAxis) //.tickSize(-width, 0));
      
    var data = d3.range(100).map(function(d){
      return {
       x: Math.random() > 0.5 ? Math.random() * -1 : Math.random(),
       y: Math.random() > 0.5 ? Math.random() * -1 : Math.random()
      };
    });
    
    vis.selectAll(".point")
      .data(data)
      .enter()
      .append("circle")
      .attr("class", "point")
      .attr("r", 5)
      .style("fill", "steelblue")
      .attr("cx", function(d){
        return xScale(d.x);
      })
      .attr("cy", function(d){
        return yScale(d.y);
      });
    
    
  </script>
</body>

</html>

Upvotes: 1

Related Questions