Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 color scale - linear with multiple colors?

Tags:

I'm trying to create something a little like a quantize scale, but act like a linear color scale?

When I try to put multiple colors into a linear scale, it seems to only scale between the first two colors.

I'd like multiple colors like a quantize scale, but fade between those colors. I'm unsure if this is possible.

//red and green works ok var color = d3.scale.linear()             .range(['red','green']);  //doesn't work - only red and green show var color = d3.scale.linear()             .range(['red','green', 'blue']);   //red green and blue show, however it doesn't fade between the colors var color = d3.scale.quantize()             .range(['red','green', 'blue']); 
like image 306
Alex KeySmith Avatar asked Apr 06 '14 12:04

Alex KeySmith


People also ask

What is D3 scale chromatic?

An array of eight categorical colors represented as RGB hexadecimal strings. An array of eight categorical colors represented as RGB hexadecimal strings.

What is D3 scale linear?

The d3. scaleLinear() method is used to create a visual scale point. This method is used to transform data values into visual variables.

What does range in linear scale describe?

A scale's range, on the other hand, describes the set of possible output values that the input values can be converted to. If we're building a bar chart to display our test scores, we'd want our range to describe the minimum and maximum bar sizes.

What is scaleBand D3?

scaleBand() function in D3. js is used to construct a new band scale with the domain specified as an array of values and the range as the minimum and maximum extents of the bands. This function splits the range into n bands where n is the number of values in the domain array.


1 Answers

You have to use domain 'pivot' value like:

d3.scale.linear()     .domain([0, pivot, max])     .range(['red', 'green', 'blue']); 

From the documentation for continuous scale domains:

Although continuous scales typically have two values each in their domain and range, specifying more than two values produces a piecewise scale. For example, to create a diverging color scale that interpolates between white and red for negative values, and white and green for positive values, say:

var color = d3.scaleLinear()     .domain([-1, 0, 1])     .range(["red", "white", "green"]);  color(-0.5); // "rgb(255, 128, 128)" color(+0.5); // "rgb(128, 192, 128)" 
like image 170
Anto Jurković Avatar answered Sep 19 '22 15:09

Anto Jurković