d3.js - ds3.js Horizontal bar - How to centralise y axis? -
i'm following mike bostock's example produce horizontal bar chart positive , negative values.
i'm trying centralise y-axis, there same length of x-axis each side. can't find examples out there. want scale of x-axis remain sensitive data, don't want specify range.
i can't example gives work. :
for quantitative scale, compute data domain (the minimum , maximum value) using d3.extent:
var x = d3.scale.linear() .domain(d3.extent(data, function(d) { return d.value; })) .range([0, width]);
nicing scale extend extent nearest round numbers. if want zero-value centered in middle of canvas, take greater of minimum , maximum value magnitude, or hard-code desired domain.
any examples of how correctly calculate domain or welcome!
first, let's discover 1 greater, minimum or maximum value:
var greater; if (math.abs(d3.min(data, function(d){ return d.value})) > math.abs(d3.max(data, function(d){ return d.value}))) { greater = math.abs(d3.min(data, function(d){ return d.value})); } else { greater = math.abs(d3.max(data, function(d){ return d.value})); };
and set scale:
var x = d3.scale.linear() .domain([greater * -1, greater]) .range([0, width]);
Comments
Post a Comment