1

I have a problem with getting value of AxisX.

I add new points form dataTable:

chart1.Series.Add(dataTable.Columns[x].Caption);
chart1.Series[dataTable.Columns[x].Caption].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series[dataTable.Columns[x].Caption].BorderWidth = 1;

for (int i = 0; i < dataTable.Rows.Count - 1; i++)
    chart1.Series[dataTable.Columns[x].Caption].Points
           .AddXY(dataTable.Rows[i][0], dataTable.Rows[i][x]);

and I use mouse_move event to get the values and show them in the labels.

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
        Point mousePoint = new Point(e.X, e.Y);

        var pos = e.Location;
        if (prevPosition.HasValue && pos == prevPosition.Value)
            return;
        tooltip.RemoveAll();
        prevPosition = pos;
        var results = chart1.HitTest(pos.X, pos.Y, false,
                                        ChartElementType.DataPoint);
        foreach (var result in results)
        {
            if (result.ChartElementType == ChartElementType.DataPoint)
            {
                var prop = result.Object as DataPoint;
                if (prop != null)
                {
                        tooltip.Show("X=" + DateTime.FromOADate(prop.XValue) + "\n Y=" + prop.YValues[0], this.chart1,
                                        pos.X, pos.Y - 15);
                }
            }
        }
}

It's strange that prop.XValue is empty. Does anyone have any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is it empty or null or is the conversion `DateTime.FromOADate(prop.XValue)` failing? Can you show `prop.XValue.ToString()` instead? – TaW Jul 03 '14 at 10:55
  • [link](https://dl.dropboxusercontent.com/u/25937396/fgh3.jpg) Here is breakpoints of two ways of taking value and showed as it looks. – user3447900 Jul 03 '14 at 11:11
  • Hm. Strange. Can you show me how the X-axis label is set? Is there more than one series? What is the original datatype for X? – TaW Jul 03 '14 at 11:40
  • ok, I take data from database. `tooltip.Show("X=" + dataTable.Rows[result.PointIndex][0].ToString() + "\n Y=" + prop.YValues[0], this.chart1, pos.X, pos.Y - 15);` – user3447900 Jul 03 '14 at 12:23
  • I have another question. Do you know function `var results = chart1.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint);` I want to take consider only `pos.X`. `Pos.Y` can be whatever. – user3447900 Jul 03 '14 at 12:24
  • What is the question? It seems to work for me. Copying your code shows my XValues fine. Could it be that the values are very small? – TaW Jul 03 '14 at 12:32
  • I handle with it. Now it's working. Thanks :) – user3447900 Jul 03 '14 at 13:06
  • You can use the built-in label and tooltip keywords, to show x and y values automatically on each `DataPoint`. When adding the points to the series, do `point.Label = "#VALX";`, or `point.Tooltip = "#VALX;"`, for instance. – mmathis Jul 07 '14 at 15:00

0 Answers0