Scaling Algorithm for Linear Graph Axis

Quote:

> I'm looking for algorithms to determine the optimum scale for a

> linear graph axis in VB3.0. Specifically, I'm reading in an array of

> data that can have any possible numeric value. After determining the

> max and min values, I need to plot the data on a linear graph axis.

> What I'm looking for is an alogrithm that will help me set the graph

> scale to an aesthetic range based on the max and min values of the

> data.

[SNIP!]

Here's what I use - I hope it's of some use...

(Note that this routine allows you to use a reversed scale

where ScaleMax < ScaleMin)

'-------------------------------------------------------

' Compute scale and number of ticks for graph to display

' values ranging between DataMin and DataMax.

'

Sub Ticks_Compute (ScaleMin As Single, ScaleMax As Single, ScaleTicks

As Integer, ByVal DataMin As Single, ByVal DataMax As Single)

Dim DataMul As Single

Dim DataExt As Single

Dim GuessMul As Single

DataExt = Abs(DataMax - DataMin)

If DataExt > 0 Then

GuessMul = 10 ^ Int(Log(DataExt) / Log(10))

Select Case DataExt / GuessMul

Case Is >= 8: ScaleMul = GuessMul * 2

Case Is >= 4: ScaleMul = GuessMul * 1

Case Is >= 2: ScaleMul = GuessMul * .5

Case Is >= 1.25: ScaleMul = GuessMul * .25

Case Is >= 1: ScaleMul = GuessMul * .2

End Select

ScaleMin = Int(DataMin / ScaleMul + .2) * ScaleMul

ScaleMax = Int(DataMax / ScaleMul + .8) * ScaleMul

ScaleTicks = Int(Abs(ScaleMax - ScaleMin) / ScaleMul)

Else

ScaleMin = DataMin

ScaleMax = DataMax

ScaleTicks = 1

End If

End Sub

--

Alasdair McIntyre