score:1

Accepted answer

That result is expected: you have undefined values in the scale's domain.

The issue is that Array.prototype.map cannot skip elements. For instance:

const arr = [70, 20, 50, 80, 10, 90, 40];
console.log(arr.map(d => {
  if (d > 40) return d;
}))

As you can see this does not filter the array removing elements whose value is less than 40: the array's length keeps the same, but now it has several undefined.

Therefore, the solution is filtering the data, just like you did in the data() method:

data = data.filter(function(d){if(d.sales>40) {return d;}})

And use that filtered array for the scale's domain.

By the way, you can simplify the filter to just this:

data = data.filter(function(d){return d.sales>40})

Related Query

More Query from same tag