0

I am implementing two crosshair cursors using labels for winforms chart after following tutorials on youtube. I am using the cursors to find the values on my graph but I need to find the max and minimum positions of the x-yaxis in order to prevent overrange exception

My approach was to find the crossing of the axis inorder to find the minimum and maximum as following:

Minimum: crossing of primary x- and yaxis Max: crossing of primary x- and yaxis + width and height.

Here is a picture of the point I am trying to find the pixel value for

Points I am trying to find the pixel values for

Fatz
  • 5
  • 6

2 Answers2

0

You can use the Axis functions to convert between the chart's 3 coordinate systems (pixels, values and positions).

The values to look for are probably the Minimum and Maximum values:

Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;

var x0 = (int)ax.ValueToPixelPosition(ax.Minimum);
var y0 = (int)ay.ValueToPixelPosition(ay.Minimum);
var x1 = (int)ax.ValueToPixelPosition(ax.Maximum);
var y1 = (int)ay.ValueToPixelPosition(ay.Maximum);

You may want to study this and this for more on finding pixel positions in a chart..

Note that the actual crossing can be shifted by setting the Axis.Crossing values!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • The code throws the following error : System.NullReferenceException: 'Object reference not set to an instance of an object.' – Fatz Sep 27 '19 at 10:07
  • I found the answer. Thanks for your help though! – Fatz Sep 27 '19 at 13:02
0

x0 = Convert.ToInt32(chart1.Width * decimal.Divide(Convert.ToInt32(chart1.ChartAreas[0].Position.X),100) + Convert.ToInt32(Decimal.Divide(Convert.ToInt32(chart1.Width*chart1.ChartAreas[0].Position.Width chart1.ChartAreas[0].InnerPlotPosition.Widthchart1.ChartAreas[0].InnerPlotPosition.X),1000000)))-10; x1 = Convert.ToInt32(x0 + Convert.ToInt32(Decimal.Divide(Convert.ToInt32(chart1.Width * chart1.ChartAreas[0].Position.Width * chart1.ChartAreas[0].InnerPlotPosition.Width),10000)))+10; y0 = Convert.ToInt32(chart1.Heightdecimal.Divide(Convert.ToInt32( chart1.ChartAreas[0].Position.Y),100)+ Convert.ToInt32(Decimal.Divide(Convert.ToInt32(chart1.Heightchart1.ChartAreas[0].Position.Height * chart1.ChartAreas[0].InnerPlotPosition.Height * chart1.ChartAreas[0].InnerPlotPosition.Y),1000000)))-15; y1 = Convert.ToInt32(y0 + chart1.Height *decimal.Divide(Convert.ToInt32(chart1.ChartAreas[0].Position.Height * chart1.ChartAreas[0].InnerPlotPosition.Height),10000))+25;

Fatz
  • 5
  • 6