Auto-Interval precision in MS Chart

Viewed 15102

I'm currently using the charting within .NET using System.Windows.Forms.DataVisualization.Charting.Chart. Thus far it seems very powerful, and works great. However, there is a huge problem in terms of how it is auto-calculating intervals. I use a lot of double values, and in libraries like ZedGraph, it handles this perfectly. It selects min/max/interval just fine. However, in MS Chart, it may select 206.3334539832 as a minimum, and intervals of a similar decimal precision. Obviously this looks quite ugly.

So, I tried simply making the axis format {0.00} and it works great when it loads the chart. Except when you zoom in, you need greater precision, maybe at 4 decimal places instead of 2. It seems I'm either stuck with 9 decimal places all the time, or else a constant fixed number that may break when someone requires greater precision. I'd rather it pick up the precision based on the level of zoom currently applied. Libraries like ZedGraph and Dundas (which I believe MS is even using!) tend to pick good values that change as you zoom in and out.

Is there any way to have the intervals change precision as the zoom frame changes? It's probably some simple property I have set wrong, but it's hard to tell with the millions of properties this thing has (especially when there's about 14 places that represent the concept of Interval).

7 Answers

Why not modify number format string.

Create format string

string formatString = "{0.00";

Identify zoom level, say zoomLevel = 2;

formatString = formatString.PadRight(5+zoomLevel, '0');
formatString += "}";

Now use this format on axis legend. Use string builder or some better way to modify the format string.

To provide the result with minimal cost you can use exponential scientific format

You can attach to customize event. From there, you can modify the labels on x-axis:

var xAxisLabels = chart1.ChartAreas[0].AxisX.CustomLabels;
...
xAxisLabels[0].Text = ...

set min. and max. values:

 chart1.ChartAreas[0].AxisX.Maximum = ...;

etc.

Related