Search code examples
c++.netvisual-studiomschart

How to extract date on X axis from a point on chart [mschart]


X axis type is DateTime. I want to know how to extract original DateTime value from the double value on the x axis corresponding to the point in my series?

I can add point with

point->SetValueXY(xdateTime,yvalue);
chart1->Series[0]-Add(point);

Solution

  • Try this C# code, it should give you a start. I use it to add text to a Label thats shows the x value as date and the yvalue as a number. Hope this helps

    Chart Click GIF

    public void Cht_Click(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    //Call HitTest()
    HitTestResult result = sender.HitTest(e.X, e.Y);
    
    //If the mouse if over a data point
    if (result.ChartElementType == ChartElementType.DataPoint) {
        //Reset Data Point Attributes
        DataPoint point = default(DataPoint);
    
        //Find selected data point
        point = result.Series.Points(result.PointIndex);
    
        //extract x value
        System.DateTime _date = System.DateTime.FromOADate(point.XValue);
        Label24.Text = "Date: " + Strings.Format(_date, "dd/MM/yy") + "    Value: " + point.YValues(0);
    
    }
    
    }