in

How to get a date from a DateTime field from dataset on X Axis in WPF Chart

Last post 05-13-2008 5:00 AM by C1_AlexT. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-07-2008 8:32 PM

    How to get a date from a DateTime field from dataset on X Axis in WPF Chart

    I'm filling a XYDataSeries with the double values that contain DateTimes from a DataTable. After reading the previous post on time question, I've converted the DateTime to OADate. No matter what I've tried, the X item names come out as numbers like "39544".

    Here is a snippet of some test code: 

    XYDataSeries xyds = new XYDataSeries();
    Double[] X = new Double[custCount];
    String[] Y = new String[custCount];

    int i = 0;
    foreach (DataRow row in dt.Rows)
    {
        X[i] = (Convert.ToDateTime(row["MIN"].ToString())).ToOADate();
        Y[i] = (i*i).ToString();
        i++;
    }

    xyds.Label = "Test Series";
    xyds.XValuesSource = X;
    xyds.ValuesSource = Y;
    c1Chart1.Data.Children.Clear();
    c1Chart1.Data.Children.Add(xyds);
     

    This is such a basic and typical thing to do, it is frustrating to find nothing on it!  Besides the Date question, any advice on filling a WPF chart from a DateSet or DateTable would be greatly appreciated!
     

  • 05-13-2008 5:00 AM In reply to

    Re: How to get a date from a DateTime field from dataset on X Axis in WPF Chart

    Please set AxisX.IsTime = true and appropriate date/time Axis.AnnoFormat if necessary.

    You don't need to convert DateTime values to double's. Chart should directly handle the array of DateTime.

    Here is sample code that create chart from the data table.  

    CS: 

    DataSet _dataSet;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      // create connection and fill data set
      string mdbFile = @"c:\db\nwind.mdb";
      string connString = string.Format(
        "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}",
        mdbFile);
      OleDbConnection conn = new OleDbConnection(connString);
      OleDbDataAdapter adapter = new OleDbDataAdapter(
         @"SELECT TOP 10 ProductName, UnitPrice FROM Products
           ORDER BY UnitPrice;", conn);

      _dataSet = new DataSet();
      adapter.Fill(_dataSet, "Products");

      // set data table rows as the source for chart data
      c1Chart1.Data.ItemsSource = _dataSet.Tables["Products"].Rows;
    }
    ...

    XAML:
    ...
    <my:C1Chart.Data>
      <my:ChartData ItemNameBinding="{Binding Path=[ProductName]}">
        <my:DataSeries ValueBinding="{Binding Path=[UnitPrice]}"/>
      </my:ChartData>
    </my:C1Chart.Data>
    ...

Page 1 of 1 (2 items)
Contact ComponentOne: 1.800.858.2739 ©1987-2007 ComponentOne LLC All Rights Reserved.