NullReference exception from DataVisualization.Charting.Axis.GetLinearPosition ()

I created a form that displays a chart using the Microsoft DataVisualization.Charting.Chart (I am using version 4 of the .NET framework). I also draw some annotations on the chart, and to find them I need to know about the axes of the chart.

Code

 myChart.ChartAreas[0].AxisX.ValueToPixelPosition(location) 

gives a NullReferenceException . The graph is definitely created, and I can set the AxisX properties - for example, myChart.ChartAreas[0].AxisX.Maximum = 1 works fine.

Having folded the exception message, it seems that the problem is with the GetLinearPosition method, which is something internal to the Chart control:

 at System.Windows.Forms.DataVisualization.Charting.Axis.GetLinearPosition(Double axisValue) at System.Windows.Forms.DataVisualization.Charting.Axis.GetPosition(Double axisValue) at System.Windows.Forms.DataVisualization.Charting.Axis.ValueToPixelPosition(Double axisValue) 

Does anyone have the insight to get me to start this fix? Thanks in advance!

+4
source share
1 answer

This answer came to a comment on a question from Hans Passant:

This is ringing. I think the problem is that this cannot work until the management has figured out the data mapping to display. What does not happen until you need to paint yourself, in a typically lazy way. Call Upgrade () first, something like that.

Who got me far enough to make this discovery:

You get it, Hans. The chart is on the TabControl tab, and I have to move this tab forward (using the TabControl.SelectedTab property) before calling ValueToPixelPosition .

+3
source

Source: https://habr.com/ru/post/1381989/


All Articles