I am trying to display the X and Y coordinates of the chart data in a small display. Everything works well but the data shown isn't accurate.
Here is the code below:
var results = chart1.HitTest(e.X, e.Y, false, ChartElementType.PlottingArea);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.PlottingArea)
{
yValue = chart1.ChartAreas[0].AxisY2.PixelPositionToValue(e.Y);
xValue = chart1.ChartAreas[0].AxisX2.PixelPositionToValue(e.X);
}
}
if (OverlapcheckBox1.Checked)
{
int val = Convert.ToInt16(yValue / 24);
yValue = yValue - 24 * val;
}
if (Cursor1checkBox.Checked && ClickMouse)
{
V1textBox1.Text = string.Concat(string.Concat(yValue).ToString());
}
if (Cursor2checkBox.Checked && ClickMouse)
{
V2textBox2.Text = string.Concat(string.Concat(yValue).ToString());
}
The image shows cursor at 10 but the value in V1 is 9.88 And an image:
Unless your mouse has amazing accuracy you would never see an exact 10.000. You could round it off:
private void Chart1_MouseClick(object sender, MouseEventArgs e) {
double yValue = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
yValue = Math.Round(yValue, 0);
}
Or perhaps you wish to find DataPoints near the cursor click position?
private void Chart1_MouseClick(object sender, MouseEventArgs e) {
HitTestResult result = chart1.HitTest(e.X, e.Y);
if (result.ChartElementType == ChartElementType.DataPoint) {
DataPoint point = (DataPoint)result.Object;
double yValue = point.YValues[0];
}
}