Search code examples
.netvbavisual-studiochartslogarithm

.Net Chart Logarithmic Axis Scaling, unexpected scaling issues


I have an example chart with logarithmic scaling x axis and y axis as such,

enter image description here

Ok now if I set the min X value to say 48 I get

enter image description here

This is super unreadable, what I would prefer is to keep the 1, 10, 100, base 10 log scale, but essentially shift the starting point over if that make sense, so the graph above would look more like....

enter image description here

(Pretend there is a Y axis in that photo)

A few examples I have Tried,

  • playing around with Crossing property but that only affects where the x and y axis cross each other
  • Playing around with scale view, but this runs into the save x-Axis scaling issue
  • Trying to play with the interval-offset **I'm not sure if this won't work, but it's proving hard to work with in a logarithmic scale

The end result of this will be a chart like this.... enter image description here

As Values update the Vertical line on the left side will update in position, however I want it to stay at a constant position and the image to essentially shift to accommodate that

I have looked through every property with regards to the chart Area, and Axis, but I cannot seem to find a way to do this.


Solution

  • I found that Math.Log10 is required to calculate the IntervalOffset property. Rather than hard-coding a whole set of numbers which all depend on the starting number, you can calculate these (including an offset for the MinorGrid, which will need to be different if you want the minor grid-lines between chartMin and the next order of 10). So, assuming a Chart object called chart, you need (in C#):

    int chartMin = 48;
    double logMin = Math.Log10 (chartMin);
    chart.ChartAreas[0].AxisX.Minimum = chartMin;
    chart.ChartAreas[0].AxisX.IntervalOffset = Math.Ceiling (logMin) - logMin;
    chart.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
    chart.ChartAreas[0].AxisX.MinorGrid.IntervalOffset = Math.Floor (logMin) - logMin;
    

    And the resulting X-axis looks like this