2014-09-25

Align Secondary Axis in VBA, C#, and VB.NET

Recently, I was asked to align two axes in a particular chart as part of a process that generated 100+ files, each with a chart of this type, ran regularly. The code was prototyped in VBA, but I am also working with a team that will need to replicate the process in server-side .NET, so converted the code to a C# class, and then ran it through Telerik's code convertor to get VB.

The general algorithm used for aligning a chart's secondary axis with the primary axis:
  1. Get primary and secondary divisors
  2. Get upper and lower bounds of secondary axis
  3. Get larger of the larger absolute value of the secondary axis
  4. Multiply (max/min) divisor by each of an array of numbers to find the first multiplier larger than absolute maximum
  5. Apply multiplier to major unit
  6. Apply min and max (multiplier x divisors)
The three versions are here: