Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Chart Tooltip with the Date (DateTime Format)

I can't get a hint in the right format

foreach (RootObject o in myRootObjects)
{ 
    seriesTemperatur.Points.AddXY(DateTime.Parse(o.datum), o.temp);               
    seriesPressure.Points.AddXY(DateTime.Parse(o.datum),  o.pressure);
}

__

private void chart2_GetToolTipText(object sender, ToolTipEventArgs e)
{
    switch (e.HitTestResult.ChartElementType)
    {
        case ChartElementType.DataPoint:
            var dataPoint = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex];
            e.Text = string.Format("Temperatur C:\t{1}\nData: {0}", dataPoint.XValue, dataPoint.YValues[0]);
            break;
    }
}

As a Date in dataPoint.XValue i get

dataPoint {{X=43459,6591203704, Y=3,19}}, 

But i need X="2018-11-22 HH:mm".

var dataPoint = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex];
e.Text = string.Format("Temperatur C:\t{1}\nData: {0}", dataPoint.XValue, dataPoint.YValues[0]);

i get:

dataPoint {{X=43459,6591203704, Y=3,19}} System.Windows.Forms.DataVisualization.Charting.DataPoint

AxisLabel is empty

    AxisLabel   ""  string
like image 718
Thorsten Avatar asked Jan 26 '23 23:01

Thorsten


2 Answers

If your values were added as DateTime and you want to format them for some other purpose than a Label (which ought to be able to use its Format string) you will need to convert them. The same applies if you want to do other processing like comparisons or filters etc..

In fact all the chart values you add, always get converted to double. So you need to use the conversion function FromOADate:

var dataPoint = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex];
DateTime dtx = DateTime.FromOADate(dataPoint.XValue);
e.Text = string.Format("Temperatur C:\t{1}\nData: {0}", dtx, dataPoint.YValues[0]);
like image 63
TaW Avatar answered Feb 02 '23 10:02

TaW


You should specify format of your X axis like this

chart.seriesTemperatur.XValueType = ChartValueType.DateTime;
chart.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd HH:mm";
like image 22
Александр Морковин Avatar answered Feb 02 '23 10:02

Александр Морковин