Charts determine interval for Major Grid lines

MadIce
MadIce used Ask the Experts™
on
What is the calculation used when charts determined the interval for the y axix major grid lines? I'm creating SSRS report which includes a chart. I want more Major grid lines than what is displayed by the auto generated.  So I want to write an expression in the Major grid line interval. I can't use a set value because my data ranges will change. Example: Base on user selection values can range from -3000 to 4000 or 0 to 600. Wont know til run time. But with those two examples, I should have interval of 1000 for the first example and 100 for the other. What the auto generated lines are at 2000 for the first example.
So If I knew the calculation used, I figure I could write an expression.  Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
MadIce,
In tha attached file is the macro shown below. It's an idea of the sort of thing that can be done. Adapt it to meet your needs and then Alt+F8 to run the macro.
Hope it helps
Patrick

Sub chart_scales()
Dim minx As Double
Dim maxx As Double
Dim maju As Double
 
minx = WorksheetFunction.Min(Range(Sheets("Sheet1").[B1], Sheets("Sheet1").[B13]))
maxx = WorksheetFunction.Max(Range(Sheets("Sheet1").[B1], Sheets("Sheet1").[B13]))
Select Case maxx
    Case Is <= 1000 'change to suit your needs
        maju = 100 'change to suit your needs
    Case Is >= 1000 'change to suit your needs
        maju = 2000 'change to suit your needs
End Select
 
ActiveSheet.ChartObjects("Chart 1").Activate
 
With ActiveChart.Axes(xlValue)
    .MinimumScale = minx - maju
    .MaximumScale = maxx + maju
    .MinorUnitIsAuto = True
    .MajorUnit = maju
    .Crosses = xlAutomatic
    .ReversePlotOrder = False
    .ScaleType = xlLinear
    .DisplayUnit = xlNone
End With
 
Sheets("Sheet1").[A1].Select
 
End Sub

Open in new window

chart-scales-01.xls
byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013
Commented:
After thinking about Patrick's approach, I thought you might want to generalize it by expressing the range (max value - min value) in scientific notation. In so doing, you get a radix (a number between 1 and 9.999) times a power of 10. By using the radix in Patrick's Select Case block, you make the logic applicable to any arbitrary range of numbers.

The macro in the snippet below, was based on Patrick's code. It applies to a chart that you select before running the macro. It determines the min and max values from the actual data used by that chart. It then determines the major unit scale that is most appropriate for your data. Finally, it sets the minimum and maximum values on the Value axis according to the major unit, going above and below your data by at most one major unit.

As Patrick suggested, you can add as many cases as you like to the Select Case block of code. As written, you will get between 6 and 12 major grid lines running through your actual data.

Brad
Sub chart_scales2()
Dim minx As Double, maxx As Double, maju As Double
Dim pwr10 As Double, radix As Double, d As Double
Dim sRange As String
Dim cht As Chart
Dim ser1 As Series
 
Set cht = ActiveChart
Set ser1 = cht.SeriesCollection(1)
minx = Application.Min(ser1.Values)
maxx = Application.Max(ser1.Values)
sRange = Format(maxx - minx, "0.00E+00")
radix = Val(Left(sRange, InStr(1, sRange, "E") - 1))
pwr10 = 10 ^ Val(Mid(sRange, 6))
Select Case radix          'Code stops at the first match
Case Is <= 1.2
    maju = 0.1 * pwr10
Case Is <= 1.8
    maju = 0.2 * pwr10
Case Is <= 2.5
    maju = 0.25 * pwr10
Case Is <= 3.5
    maju = 0.4 * pwr10
Case Is <= 6
    maju = 0.5 * pwr10
Case Else
    maju = 1 * pwr10
End Select
 
With cht.Axes(xlValue)
    .MinimumScale = Int(minx / maju) * maju
    .MaximumScale = (Int(maxx / maju) + 1) * maju
    .MinorUnitIsAuto = True
    .MajorUnit = maju
    .Crosses = xlAutomatic
    .ReversePlotOrder = False
    .ScaleType = xlLinear
    .DisplayUnit = xlNone
End With
 
End Sub

Open in new window

Author

Commented:
Thanks for the replys. I'm using BIDs to create these reports which I'm new at using and just found where I can use code on my reports. I thought I could only use an expression. I need to read up on using the code tab and then I will try the suggestions. It will be a few days...Thanks

Author

Commented:
I think I can use both of your suggestions to come up with what I'm looking for

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial