If you have a domain that has one of the boundaries of the domain, since a scale of 0 will not work for you. In addition, the log scale provides you with a tick () function with an undetectable number of ticks.
My current purpose is to display data with arbitrary domains and ranges on a linear scaled scatterplot. BUT, perhaps the user can go to the logarithmic scale. This includes problematic [0, n] and [n, 0] domains.
Here is the solution I came up with to handle these cases: This problem can be avoided by using a linear scale to design our domain to an arbitrary range with positive boundaries. I choose [1,10], but it can accept any positive numbers. After that, we can use the usual log scale.
d3.scale.genericLog = function() { return GenericLog(); }; function GenericLog() { var PROJECTION=[1,10]; var linearScale, logScale; linearScale=d3.scale.linear(); linearScale.range(PROJECTION); logScale=d3.scale.log(); logScale.domain(PROJECTION); function scale(x) { return logScale(linearScale(x)); } scale.domain = function(x) { if (!arguments.length) return linearScale.domain(); linearScale.domain(x); return scale; }; scale.range = function(x) { if (!arguments.length) return logScale.range(); logScale.range(x); return scale; }; scale.ticks = function(m) { return linearScale.ticks(m); }; return scale; }
Using:
var scale1 = d3.scale.genericLog().domain([0,1]).range([500,1000]); var scale2 = d3.scale.genericLog().domain([-10,0]).range([500,1000]); scale1(0)
I need the range (), scale (), ticks () functions, so I turned them on, but it takes no more than 5 minutes to implement all the others. Also: note that I used the linear scale values ββ() because I had to limit the number of ticks, and this is easier with a linear scale.
EDIT: LIMIT Depending on what you select as PROJECTION, it distorts the log scale. The wider spacing you use will reduce the scale of your bottom.
sanya
source share