graph axis calibration in c++

混江龙づ霸主 提交于 2019-12-06 05:46:30

ACM Algorithm 463 provides three simple functions to produce good axis scales with outputs xminp, xmaxp and dist for the minimum and maximum values on the scale and the distance between tick marks on the scale, given a request for n intervals that include the data points xmin and xmax:

  1. Scale1() gives a linear scale with approximately n intervals and dist being an integer power of 10 times 1, 2 or 5.
  2. Scale2() gives a linear scale with exactly n intervals (the gap between xminp and xmaxp tends to be larger than the gap produced by Scale1()).
  3. Scale3() gives a logarithmic scale.

The code is in Fortran but it is very straightforward to interpret and convert into other languages. There are more complicated functions that give prettier scales (e.g. the ones in gnuplot), but Scale1 would likely do the job for you with minimal code.

(EDIT)

I found the text of the original 1973 paper online here, which provides more explanation than the code linked to above.

One way to calculate a good step would be to find the value of the most significant digit of the range's length (i.e. the diff = maxVlaue - minValue), and use that as your step. To calculate the value of the most significant digit use this simple formula:

pow(10, floor(log10(diff)))

This takes a decimal logarithm of the difference, discards the fractional part, if any, and raises ten to the power of that logarithm. For a difference of 7.2165, the calculation would return 1; for 721.65, it would return 100, and so on.

One shortcoming of this calculation is that the grid step for the diff of 9.99 and the diff of 1.001 would be the same. One way to address this would be to calculate the number of grid lines you'd get for the step, and decrease the step ten times if the number of lines is insufficient (say, less than three).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!