3

I'm drawing a chart with logarithmic scale that I can zoom and pan. In the process of zooming and panning I need to be prepared for drawing nice tick labels. My general problem is: given two points, x0 and x1, create a subdivision of n elements in log scale that 'looks nice'. I have gone through the math gems algorithms about nice grid line but it doesn't work that well for logarithmic scale. Any pointers?

Ricardo Marimon
  • 10,339
  • 9
  • 52
  • 59

1 Answers1

1

One option would be to use the standard algorithms for nicely subdividing up tick marks for a normal range of values by simply computing the logarithm of the range you want ti divide and applying the linear algorithm directly. That is, if you already have an algorithm that solves this problem in the case where you want linearly-distributed values, transforming your data by applying the logarithm to each value will then let you compute the logarithm if the tick marks you want. You can then raise these tick marks to the appropriate power to invert the transformation, ending up with the tick marks for the log plot.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • By rising it to the power I no longer get nice values and if I maintain the linear scale the labels get squished to one side. Say, for example, that the linear nice division gives 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. log(1) is 0, log(2) is 0.3, but log(8) = 0.9, log(9) = 0.95 and log(10) = 1. Extremely close together. The right scale should be something like 1, 2, 5, 10. – Ricardo Marimon Sep 05 '11 at 23:38
  • @rmarimon- Let me clarify what I meant here. You are absolutely right that you don't want to just use the linear tick algorithm and then just take the logarithm. What I'm suggesting is instead first taking the logarithm of the data points, **then** using the linear tick function on the transformed points. This gives back evenly-spaced points in the log scale. You then exponentiate the values you get to get back well-distributed points that are exponentially spaced apart. Does that make sense? – templatetypedef Sep 06 '11 at 00:21
  • 1
    Got it. The problem is that the tick marks will be evenly spaced out thus obscuring the log scale. One approach I've been working is to use the nice linear scale and try some heuristics to drop some of the labels that get close together. – Ricardo Marimon Sep 06 '11 at 04:49