0

I have a chart that the points was added from a datagridview using C#. I would like to include a tooltip on the chart that shows the x and y values of the chart. I have adopted the code from see values of chart points when the mouse is on points.

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
chartIC.Series["IC"].Points.AddXY(Convert.ToDateTime(dataGridView1[4,i].Value).ToString("yyyy-MM-dd"), 
dataGridView1[1, i].Value);
}
      
Point? prevPosition = null;
ToolTip tooltip = new ToolTip();

private void chartIC_MouseMove(object sender, MouseEventArgs e)
{
  var pos = e.Location;

  if (prevPosition.HasValue && pos == prevPosition.Value)
                return;
            tooltip.RemoveAll();
            prevPosition = pos;
            var results = chartIC.HitTest(pos.X, pos.Y, false,
                                         ChartElementType.PlottingArea);
            foreach (var result in results)
            {
                if (result.ChartElementType == ChartElementType.PlottingArea)
                {
                    
                    var xVal = chartIC.Series["IC"].Points[result.PointIndex].XValue;
                    var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);


                    tooltip.Show("X Value=" + xVal + ", Y Value=" + yVal.ToString("0.0"), this.chartIC,
                                 pos.X, pos.Y - 15);
                }
            }
        }

However I got an exception (System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index') when I 'mouse over' the chartIC. Would appreciate any help provided.

XCY
  • 1
  • 2
  • from my initial thought error is thrown from `chartIC.Series["IC"].Points[result.PointIndex].XValue` probably series does not contain the index value, you can verify the series has all expected items – coder_b Dec 13 '20 at 14:22
  • I the chart did successfully generated with data points. Is there anything I can change to the code to walk around this? – XCY Dec 13 '20 at 14:32
  • well you could check before calculating x and see whether series (points) has that index? – coder_b Dec 13 '20 at 14:35
  • Thanks for your comment. I am still a newbie so could you advice me how to proceed with the checking? – XCY Dec 13 '20 at 14:49
  • 1
    before calculation x and y have condtion chartIC.Series["IC"].Points.Length <= result.PointIndex so it ignores but I would advise to see why index value not found in point series. – coder_b Dec 13 '20 at 15:14
  • Do you want just values from actual datapoints of of the mouse position? – TaW Dec 13 '20 at 18:39
  • Yes. I would want the tooltip to display the data point at that mouse position. – XCY Dec 14 '20 at 08:44
  • @coder_b, the Points.Length is not an accessible method. What would be the correct member of the class? – XCY Dec 14 '20 at 15:18
  • try with Count? @XCY – coder_b Dec 14 '20 at 15:21
  • The tooltip does not show any label which means the series does not contain index value. Some additional information, I have updated the code above to show how do I populate the series from a datagridview. – XCY Dec 14 '20 at 15:54

0 Answers0