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

Strange DateTime behavior

2 Answers 83 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Pelle
Top achievements
Rank 1
Pelle asked on 28 Dec 2010, 11:52 AM
I followed the example regarding DateTime-support on the help site. I more or less copied the code but still I got a real strange behavior that can be seen on attached image (sorry for attaching one directly from Word but it seems it's not possible to delete attached items).

The first picture shows a continuous trendline for five (different) days but the graph places two points on the same day. At first I thought that the "oldest" data point was not showing correctly but after a more thorough look I realized that it's actually all data points except the first that is placed incorrectly.

The second picture shows the result after I changed a date. Some items are now shown on the correct place while some are placed way off. 

Am I missing something obvious? 

Here's the chart setup:
SeriesMapping seriesMapping = new SeriesMapping();<br>
seriesMapping.LegendLabel = "MSFT";<br>
seriesMapping.SeriesDefinition = new LineSeriesDefinition();<br>
seriesMapping.ItemMappings.Add(new ItemMapping("DateTime", DataPointMember.XValue));<br>
seriesMapping.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));<br>
chart.SeriesMappings.Add(seriesMapping);<br>
chart.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd-MMM-yyyy";<br>
chart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;

2 Answers, 1 is accepted

Sort by
0
Accepted
Evgenia
Telerik team
answered on 28 Dec 2010, 03:07 PM
Hi Pelle,

RadChart does not sort the user-defined data internally and that is why you are getting the strange behavior.

You need to either pass the TradeData instances in the correct DateTime order (December 10th through December 28th), or you can explicitly declare the sorting order with SortDescriptor like this:
List<TradeData> data = new List<TradeData>();
data.Add(new TradeData(new DateTime(2010, 12, 28), 0));
data.Add(new TradeData(new DateTime(2010, 12, 27), 1));
data.Add(new TradeData(new DateTime(2010, 12, 10), 2));
data.Add(new TradeData(new DateTime(2010, 12, 25), 3));
data.Add(new TradeData(new DateTime(2010, 12, 24), 4));
  
SeriesMapping sm = new SeriesMapping();
sm.SeriesDefinition = new LineSeriesDefinition();
sm.ItemMappings.Add(new ItemMapping("DateTime", DataPointMember.XValue));
sm.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));
sm.SortDescriptors.Add(new ChartSortDescriptor("DateTime", System.ComponentModel.ListSortDirection.Ascending));
  
RadChart1.SeriesMappings.Add(sm);
RadChart1.ItemsSource = data;
  
RadChart1.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd-MMM-yyyy";
RadChart1.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;

Hope this helps.


Regards,
Evgenia
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Pelle
Top achievements
Rank 1
answered on 28 Dec 2010, 04:41 PM
That did the trick! :)

Maybe that code should be added to "RadChart/Getting Started/Create Data Bound Chart".

Thanks for the help!
Tags
Chart
Asked by
Pelle
Top achievements
Rank 1
Answers by
Evgenia
Telerik team
Pelle
Top achievements
Rank 1
Share this question
or