Rafa
Rafa

Reputation: 3349

d3.extent() gets skipped in d3js execution

I'm trying to assign a value to my xDomain variable below, but when I use the chrome debugger, the value is not defined, and the console does not print the console.log("DOMAIN" + element.created) line. What could be causing d3 to look over this line?

d3.json("tweets.json", function(error, data){

  if (error) return console.warn(error);

  console.log(data);

  //THIS PART GETS SKIPPED OVER
  var xDomain = d3.extent(data, function(element){
      console.log("DOMAIN" + element.created);
      time = parseTweetTime(element.created);
      return parseTime.parse(time);
  });

  xScale.domain(xDomain);



  var dots = svg.selectAll("circle")
      .data(data.tweet)
      .enter()
      .append("circle");

 dots.attr("r", function(d, i){ 
  return 5;

})
  .attr("cx", function(d){ 
    console.log(d.created);
    date = parseTime.parse(d.created);
    return xScale(date);
  })
  .attr("cy", function(d){ return yScale(d.text.length)})
  .style("fill", "black");

});

EDIT: This is a sample of the tweets.json file

{
   "tweet":[
           {"text": "hello"}
           {"text": "goodbye"}
    ]
}

Upvotes: 1

Views: 127

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

The bug is in the way you calculating extent. As you dataset is like this:

{
   "tweet":[
           {"text": "hello"}
           {"text": "goodbye"}
    ]
}

So instead of doing this:

d3.extent(data, function(element){ ...}

Do it this way:

d3.extent(data.tweet, function(element){ ... });

Upvotes: 1

Related Questions