How can I make the line var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);
from this example work in d3 v4 using d3.scaleBand?
It means scaling factor is 0.5 and the data will be represented in pixels as: data value * 0.5. So, now if our input value is 300, the output value would be 150.
The d3. scaleLinear() method is used to create a visual scale point. This method is used to transform data values into visual variables.
There is one particular function of d3. scale. ordinal that is particularly useful. That is rangeBands . It will evenly divide a space up for you into bands across the range.
The domain is the complete set of values, so in this case that is all of your temperatures, from 33 to 64. The range is the set of resulting values of a function, in this case the resulting values of scaling your temperatures from 0 to 600.
in D3 4.x, ordinal.rangeRoundBands has been replaced with band.rangeRound (thus, there is no more rangeRoundBands
). Besides that...
The new band.padding, band.paddingInner and band.paddingOuter methods replace the optional arguments to ordinal.rangeBands.
So, that 0.05
goes to paddingInner
. This is how the line looks in D3 v4.x:
d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.05);
I have rewritten the code in your example, updated to D3 v4.x: https://plnkr.co/edit/HQz1BL9SECFIsQ5bG8qb?p=preview
Necessary changes:
var parseDate = d3.timeParse("%Y-%m");
var x = d3.scaleBand().rangeRound([0, width]).paddingInner(0.05);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom(x).tickFormat(d3.timeFormat("%Y-%m"));
var yAxis = d3.axisLeft(y).ticks(10);
.attr("width", x.bandwidth())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With