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.
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/15ultraChart1.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;
Thanks for the reply. Unfortunately I don't think this addresses my particular issue, since this code is plotting finite data points. How would I create a point for each data table record in a range of rows.
Ie.,
Public void btnGraph_Click(object sender, EventArgs e)
DataSet ds = new DataSet( );
DataTable dt = ds.Tables.Add("Table");
DateTime userSpecifiedBeginDate;
DateTime userSpecifiedEndDate;
foreach(DataRow dr in dt)
{
dt.Rows.Add(new object[] {DateTime.Parse(dr[0]),dr[1]});
//dr[0] would be the DateTime colume in the datarow of dt, dr[1] would be the y-axis value from the //datatable in the second column
}
ultraChart1.ChartType = ChartType.LineChart;
ultraChart1.Data.DataSource = dt;
ultraChart1.Data.DataBind( );
ultraChart1.Data.SwapRowsAndColumns = true;
ultraChart1.Axis.X.RangeMin = userSpecifiedBeginDate.Ticks;
ultraChart1.Axis.X.RangeMax = userSpecifiedEndDate.Ticks;
ultraChart1.Axis.X.RangeType = AxisRangeType.Custom;
Does that make sense what I'm looking for? Essentially the UltraChart is trying to graph up a variable series of points, depending on how many records within the DataTable. I have run through example where you are plotting finite data points, like (1/1/2010, 5), (2/1/2010, 10) etc. but what if I need to graph whatever is in the table, but arbitrary DataPoints? Thanks in advance for any advice.
~Ruben
Here is the correct code for this case:
Infragistics.UltraChart.Resources.Appearance.NumericTimeSeries gwElevSeries = new Infragistics.UltraChart.Resources.Appearance.NumericTimeSeries();
foreach (DataRow gwElevDr in qrySelectGwMonRecords)
{ gwElevSeries.Points.Add(new Infragistics.UltraChart.Resources.Appearance.NumericTimeDataPoint(System.DateTime.Parse(gwElevDr.ItemArray[2].ToString()),System.Double.Parse(gwElevDr.ItemArray[8].ToString()),"C",false)) }
the qrySelectGwMonRecords
is an EnumerableRowCollection created through Linq that accesses that creates the collection based on the user's specified date range:
EnumerableRowCollection<DataRow> qrySelectGwMonRecords =
(from g in gwMonDataTable.AsEnumerable()
where g.Field<DateTime>("readingDate") >= clndrGwMonStart.Value && g.Field<DateTime>("readingDate")<= clndrGwMonEnd.Value
select g);
Can you give me a VB sample about how to use datatable as line chart source? Thanks