# Algorithm for calculating regular divisions of lengths in easy-to-read form

I am creating some charts for elevation v distance data.

The original source of the data is GPX files and from them I have extracted...

The total route distance (km) and each point along the route as [lat, long, elevation, distFromStart]

The chart plots distanceFromStart (X-axis) v elevation (Y-axis).

The routes can be anything of length between 0.5km and 450km.

The issue I have is with calculating the divisions (the tick values) for the x-axis of the graph. The tick values should always be easy to read, rounded values. The number of ticks is flexible from 5 - 12 (or thereabouts).

So given

1. A distance (in km)

2. A list of acceptable tick values (0.1km, 0.2km, 0.5km, 1km, 2km, 5km, 10km, 20km, 50km)

How can I calculate which acceptable tick value I should use and the number of ticks on the x-axis?

• could you give example of your data and expected result? Oct 10, 2018 at 20:11

I would shoot for having about the same number of ticks each time, given that the tick values are acceptable. I am interpreting tick values as the constant difference between two tick values. Say you wanted to have about $$n$$ ticks and you are given a distance $$d$$. Then your tick values would be about $$\frac{d}{n}$$. Round this up or down to the nearest acceptable tick value.