betontalpfa
betontalpfa

Reputation: 3752

How can I check if a value is inside of a d3 scale range

I use d3's scaleLinear. I want to filter values that are outside of the scale's domain.

var arr = [0, 1, 15, 60, 700];

var scaleX = d3.scaleLinear()
    .domain([10, 100])
    .range([0, 200]);

// I need something like this:
scaleX.filter(arr)  // I want to get: [1, 15, 60]

My best solution is foreach all the values and check that the domain's min/max is less/greater than the given value. Like this.

Is there any more compact solution?

Upvotes: 0

Views: 775

Answers (2)

Zsolt Molnar
Zsolt Molnar

Reputation: 81

Apply the filter method to the arr variable:

const filteredArray = arr.filter(function (value) { return ((value >= 10) && (value <= 100)) });

The filteredArray will contain only the values between 10 and 100 (inclusive).

You can read more about the .filter method here.

Upvotes: 1

SmokeyShakers
SmokeyShakers

Reputation: 3412

var filteredArr = arr.filter(function(d) { 
    return d >= scaleX.domain()[0] && d <= scaleX.domain()[1]
})

Upvotes: 1

Related Questions