Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
410
UltraChart line chart: plot data point from database table
posted

This seems like a simple problem, but in all my searching I have not found a clear answer.

I am trying to plot an x-y data series as a line chart. the series comes from a table, gwMonDataTable, which is linked to a data source set up in the c# project.

The x-series data needs to be read in as DateTime format from a field called 'dateTime' in the gwMonDataTable, also in DateTime format.  The y-series data is elevation data which is numeric (double) format from the 'gwElevation' field in the gwMonDataTable.

I need to plot the data for a specified date range.  So if the user specifies Jan 1st, 2010-Nov. 1st, 2010, the LineChart will display the data series from those dates.

Any help is greatly appreciated, even the most elementary advice such as how to simply read and plot data from a DataTable whose source is a DataSet.  Thanks in advance for any assistance. Ruben.

Parents
  • 26458
    Offline posted

    This should be fairly simple, and while there are several ways of binding date data to the chart, this is usually the most straight forward. Check out this code snippet:

    DataTable dt = new DataTable();
    dt.Columns.Add("DateTime", typeof(DateTime));
    dt.Columns.Add("Value", typeof(double));

    dt.Rows.Add(new object[] { DateTime.Parse("1/1/2010"), 5 });
    dt.Rows.Add(new object[] { DateTime.Parse("3/1/2010"), 10 });
    dt.Rows.Add(new object[] { DateTime.Parse("6/1/2010"), 15 });
    dt.Rows.Add(new object[] { DateTime.Parse("8/1/2010"), 20 });
    dt.Rows.Add(new object[] { DateTime.Parse("10/1/2010"), 25 });

    ultraChart1.ChartType = ChartType.LineChart;
    ultraChart1.LineChart.TreatDateTimeAsString = false;
    ultraChart1.Data.DataSource = dt;
    ultraChart1.Data.DataBind();
    ultraChart1.Data.SwapRowsAndColumns = true;

    //set up the range from 2/15 to 9/15
    ultraChart1.Axis.X.RangeMin = DateTime.Parse("2/15/2010").Ticks;
    ultraChart1.Axis.X.RangeMax = DateTime.Parse("9/15/2010").Ticks;
    ultraChart1.Axis.X.RangeType = AxisRangeType.Custom;

Reply Children