Use d3 log scale instead of linear scale

luisfarzati picture luisfarzati · Mar 1, 2013 · Viewed 21.7k times · Source

I'm trying to do a chart based on http://mbostock.github.com/d3/talk/20111116/bar-hierarchy.html, the only difference being that I'd like to use a log scale for the x-axis.

Here's my fiddle: http://jsfiddle.net/JhDVC/5/

As you can see, the x-axis is defined at line 4:

x = d3.scale.linear().range([0, w]),

If I change it for

x = d3.scale.log().range([0, w]),

Then it doesn't work (nothing is rendered), throwing these error messages:

Error: Invalid value for <rect> attribute width="NaN" 

Changing the domain setting from

x.domain([0, root.value]).nice();

to

x.domain([1, root.value]).nice();

shows me the z axis (names) but still no bars or values.

Answer

Lars Kotthoff picture Lars Kotthoff · Mar 1, 2013

There are a few other places where the domain for the scale is set. You need to update those as well.

Working jsfiddle here.

And here's some code so that it allows me to post this:

x.domain([1, root.value]).nice();