This is a migrated thread and some comments may be shown as answers.

Use date as x-axis on line chart

3 Answers 123 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Antony
Top achievements
Rank 1
Antony asked on 20 Aug 2010, 09:43 PM
I want to set a date as the x-axis, either declaratively or programmatically.

For example: I have a datatable with two colums:

Column 1 - ID="ScoreDate" DataType="DateTime"
Column 2 - ID="Score" DataType="int"

The data in ScoreDate is not incremental, eg, the dates can be 12/08/2010, 15/08/2010, 22/08/2010.

I want to set the x-axis values to be the dates but i am having all sorts of trouble.

Any help would be appreciated.

3 Answers, 1 is accepted

Sort by
0
Tony Ser
Top achievements
Rank 1
answered on 23 Aug 2010, 07:30 PM
I had to do something like this:
_RadChart.DefaultView.ChartArea.AxisX.IsDateTime = true; // Dates must be converted with DateTime.ToOADate.

Hope this helps

0
Accepted
Velin
Telerik team
answered on 25 Aug 2010, 04:51 PM
Hello AnTony,

Please, find attached a small example that should help to properly set up the RadChart control for this scenario.

Best wishes,
Ryan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Antony
Top achievements
Rank 1
answered on 27 Aug 2010, 06:47 PM
Many thanks for your code.
string scoreQuery = "SELECT [ScoreDate], [ScoreValue] FROM [Scores] WHERE ([PlayerCode] = @PlayerCode) AND ([ScoreValue] > 0) ORDER BY [ScoreDate] ASC";
Connections scCon = new Connections(scoreQuery);
scCon.AddParam("PlayerCode", gridItem);
DataTable resTable = new DataTable();
resTable = scCon.ReturnTable();
ChartSeries chSeries = new ChartSeries();
chSeries.Type = ChartSeriesType.Line;
ChartAxisItem xAxis = new ChartAxisItem();
RadChart1.PlotArea.XAxis.IsZeroBased = false;
RadChart1.PlotArea.XAxis.AutoScale = false;
RadChart1.PlotArea.XAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortDate;
 
foreach (DataRow dr in resTable.Rows)
{
    chSeries.AddItem(Convert.ToDouble(dr["ScoreValue"]));
    DateTime scDate = new DateTime();
    scDate = Convert.ToDateTime(dr["ScoreDate"]);
    ChartAxisItem item = new ChartAxisItem();
    item.Value = (decimal)scDate.ToOADate();
    RadChart1.PlotArea.XAxis.AddItem(item);
}
RadChart1.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Gradient;
RadChart1.PlotArea.Appearance.FillStyle.MainColor = Color.FromArgb(78, 198, 19);
RadChart1.PlotArea.Appearance.FillStyle.SecondColor = Color.FromArgb(123, 230, 108);
RadChart1.Series.Add(chSeries);


I have managed to get it working. Here is the code i have used.

Tags
Chart (Obsolete)
Asked by
Antony
Top achievements
Rank 1
Answers by
Tony Ser
Top achievements
Rank 1
Velin
Telerik team
Antony
Top achievements
Rank 1
Share this question
or