.NET Chart: How to add DataPoint with different member types than double?

7.1k Views Asked by At

The namespace I'm looking at is System.Windows.Forms.DataVisualization.Charting in .NET Framework 3.5.

I've constructed a Chart with name chart1 and added a Series called mySeries to that chart.

Within the Data section of that series in the designer, I have:

IsXValueIndexed --> False
Name --> mySeries
Points --> (Collection)
XValueType --> DateTime
YValuesPerPoint --> 1
YValueType --> Int32

When I try to add DataPoints to the series like this, it seems like I cannot add them with the x and y values of the appropriate type:

public void InitializeChart()
{
    Series theSeries = chart1.Series["mySeries"];

    // MyData is of type List<MyDatum> and MyDatum has two members --
    //    one of type DateTime and another of type int

    foreach (MyDatum datum in MyData)
    {
        // I'd like to construct a DataPoint with the appropriate types,
        //    but the compiler wants me to supply doubles to dp

        DataPoint dp = new DataPoint(mySeries);
        dp.XValue = datum.TimeStamp;
        dp.YValue = datum.MyInteger;

        radiosConnectedSeries.Points.Add(dp);         
    }
}

What am I missing? Thanks as always!

2

There are 2 best solutions below

0
On BEST ANSWER

DateTime.ToOADate() will return the timestamp as a double that can be used as x value

1
On

DataPoint doesn't accept other types for x and y. You'll need to use datum.TimeStamp.Ticks for ordering/sorting, and then set the AxisLabel property (string) to display the DateTime.