3

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!

John
  • 15,990
  • 10
  • 70
  • 110

2 Answers2

4

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

DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
2

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.

lukiffer
  • 11,025
  • 8
  • 46
  • 70
  • Thanks (+1). OK, changing XValueType and YValueType to "Auto" and sending in the x and y as doubles let me plot the graph, but why am I able to specify the types in the designer if I can't actually pass the data in like that? – John Apr 22 '11 at 00:40