Trying to fill rect colours on a bar chart by a scale,
var x1 = d3.scaleTime()
.domain([parseTime('00:00'), d3.max(data, function(d) {
return d.value
})])
.range([2, 256]);
like this,
.style('fill', function(d) {
return "'rgb(0,0," + x1(d.value) + ")'"
})
Trying to range over the colour blue on the scale d.value
I'm getting black at the moment, presumable a default colour.
Thanks
You can simplify this, d3 scales can interpolate between colors, so you could use code such as :
var x1 = d3.scaleLinear()
.domain([0,100]])
.range(["#000000","#0000ff"]);
You could also use:
var x1 = d3.scaleLinear()
.domain([0,100]])
.range(["black","blue"]);
And then use the scale to color to the rectangles directly:
.style('fill', function(d) {
return x1(d.value);
})
Also, yes, black is the default color. For sake of demonstration in the snippet, I'm using a linear rather than date scale:
var svg = d3.select("body")
.append("svg")
.attr("width",500)
.attr("height",200);
var x1 = d3.scaleLinear()
.domain([0,100])
.range(["#000000","#0000ff"]);
var x2 = d3.scaleLinear()
.domain([0,100])
.range(["orange","steelblue"]);
var rects = svg.selectAll(null)
.data(d3.range(100))
.enter()
.append("rect")
.attr("fill",function(d) { return x1(d); })
.attr("width",10)
.attr("height",10)
.attr("y",function(d) { return Math.floor(d/10) * 12; })
.attr("x",function(d) { return d % 10 * 12; })
var rects = svg.selectAll("null")
.data(d3.range(100))
.enter()
.append("rect")
.attr("fill",function(d) { return x2(d); })
.attr("width",10)
.attr("height",10)
.attr("y",function(d) { return Math.floor(d/10) * 12; })
.attr("x",function(d) { return d % 10 * 12 + 130 ; })
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>