gty1996
gty1996

Reputation: 333

How to convert from v4 to v3 in d3

I have this scatterplot which is not working with d3 v3. I don't get any error in the console but it's not showing the axis as it should.

Here is the js file:

    var data = [
      {
    "xaxis": 0.2,
    "yaxis": 0.8,
    "color": 0
      },
      {
    "xaxis": 0.3,
    "yaxis": 0.7,
    "color": 1
      },
 ]

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

//Read the data
function draw(data) {

  // Add X axis
  var x = d3.scale.linear()
    .domain([0, 1])
    .range([ 0, width ]);
  svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.svg.axis(y));
    // text label for the x axis
  svg.append("text")             
      .attr("transform",
            "translate(" + (width/2) + " ," + 
                           (height + margin.top + 20) + ")")
      .style("text-anchor", "middle")
      .text("Date");  

  // Add Y axis
  var y = d3.scale.linear()
    .domain([0, 1])
    .range([ height, 0]);
  svg.append("g")
    .call(d3.svg.axis(y));
      // text label for the y axis
  svg.append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 0 - margin.left)
      .attr("x",0 - (height / 2))
      .attr("dy", "1em")
      .style("text-anchor", "middle")
      .text("Value");   


  // Color scale: give me a specie name, I return a color
  var color = d3.scale.ordinal()
    .domain(["0", "1", "2" ])
    .range([ "#440154ff", "#21908dff", "#fde725ff"])

  // Add dots
  svg.append('g')
    .selectAll("dot")
    .data(data)
    .enter()
    .append("circle")
      .attr("cx", function (d) { return x(d.xaxis); } )
      .attr("cy", function (d) { return y(d.yaxis); } )
      .attr("r", 5)
      .style("fill", function (d) { return color(d.color) } )

}

draw(data);

and the html:

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v3.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

I have adapted this scatterplot from v4 to work for v3 but seems like something is missing and I can't manage to find it. Any help would be appreciated.

Upvotes: 1

Views: 380

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102194

This is one of the few times I've seen a question asking about downgrading a version. Why you're doing this is unclear. Nevertheless, the issue is clear, in D3 v3 you don't pass the scale to the axis generator like you did:

d3.svg.axis(y)

It has to be:

d3.svg.axis()
    .scale(y)//pass the scale here
    .orient("left")//or "right", depending on what position you want

Upvotes: 2

Related Questions