简体   繁体   中英

Attribute cy returning expected length error in D3 V4

I am trying to create a scatter plot with the following data set:

cyclist-data.json

Here is the code for the circles I am trying to create for the scatter plot:

var x = d3.scaleTime()
          .domain([minTime, maxTime]).range([0, width]);
var y = d3.scaleLinear().range([height, 0])
          .domain([0, d3.max(function(d){
            return d.Place
          })]);

chart.selectAll('.dot')
       .data(data)
       .enter().append('circle')
       .attr('class', 'dot')
       .attr('r', '3.5')
       .attr('cx', function(d){
          return x(parseInt(d.Time))
       })
       .attr('cy', function(d){
          return y(d.Place)
       })

However, I keep getting this error

d3.min.js:4 Error: attribute cy: Expected length, "NaN".

Not really sure what's going wrong, because the value I am passing into my y function is a number ( d.Place ).

Your call to d3.max() is missing the first parameter, ie the array to get the maximum value of. Accoding to the docs , the correct usage will be:

d3.max( array [, accessor ]

Thus, you need to change your y-scale's definition to:

var y = d3.scaleLinear().range([height, 0])
          .domain([0, d3.max(data, function(d){
            return d.Place
          })]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM