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

Problem with Axis-X label datetime

4 Answers 151 Views
Chart
This is a migrated thread and some comments may be shown as answers.
luke
Top achievements
Rank 1
luke asked on 27 Jun 2011, 10:53 AM
Hi, i have problem with making label for axis at my radchart.

This code show how im trying to create labels on x - axis. The problem is that instead of current date- the labels start sine 1899-12-31 then 1900-1-1.

rchFactory.DefaultView.ChartArea.AxisX.IsDateTime = true;
rchFactory.DefaultView.ChartArea.AxisX.AddRange(DateTime.Now.Date.Ticks, DateTime.Now.AddDays(6).Date.Ticks, (new TimeSpan(1, 0, 0, 0, 0)).Ticks);
rchFactory.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd MMM yyyy";
What is problem in this solution?
Thanks for help.

4 Answers, 1 is accepted

Sort by
0
Peshito
Telerik team
answered on 30 Jun 2011, 12:02 PM
Hi Luke,

You need to set manually your DateTime MinValue and MaxValue. For this the DateTime values have to be converted to OLE Automation dates. This can be achieved with the ToOADate method.

For example:
radChart.DefaultView.ChartArea.AxisX.AutoRange = false;
radChart.DefaultView.ChartArea.AxisX.MinValue = new DateTime( 2009, 1, 1 ).ToOADate();
radChart.DefaultView.ChartArea.AxisX.MaxValue = new DateTime( 2009, 3, 31 ).ToOADate();


You can also read more about Date Time support and X-Axis here and here.

Kind regards,
Peshito
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
luke
Top achievements
Rank 1
answered on 04 Jul 2011, 02:40 PM
Thanks for respond

Its partially solved my problem(the date are now correct), but when i set axisx like in your code, chart area is empty(all chart bars not appearing). Any idea what's wrong?

The itemssource of my chart is List<List<double>>


                   List<List<double>> chartSource = fillByData();
           for (int i = 1; i <= 5; i++)
            {
                HorizontalStackedBarSeriesDefinition definition = new HorizontalStackedBarSeriesDefinition();
                SeriesMapping sm = new SeriesMapping() { SeriesDefinition = definition };
                sm.ItemMappings.Add(new ItemMapping(string.Empty, DataPointMember.YValue));
                sm.CollectionIndex = i - 1;
                radchart1.SeriesMappings.Add(sm);

            }
            radchart1.ItemsSource = chartSource;

Thanks for help.
0
Peshito
Telerik team
answered on 07 Jul 2011, 07:29 AM
Hi Luke,

Your issue is caused because your bars are not bound to any values. In order to fix it, you need to find a way to bind the bar values. Right now they are drawn too early in the chart, this is why you cannot see them. In the first place you were able to see the bars because the AutoRange was enabled which automatically displayed the bars.

What you could try is using Data Binding with Manual Series Mapping as it is explained here.

Also checkout the following demo examples using DateTime. You may find them useful:
http://demos.telerik.com/silverlight/#Chart/DateTimeGrouping
http://demos.telerik.com/silverlight/#Chart/Sampling
http://demos.telerik.com/silverlight/#Chart/FlexibleAPI


Kind regards,
Peshito
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Pragati Dukale
Top achievements
Rank 1
answered on 05 Dec 2011, 12:27 PM

Hello,

Project platform details - VS 10, .net famework 4.0, SL 4, dll- Telerik.Windows.Controls.Charting - 2010.1.712.1040.

For some reasons, I cannot change the dll with new version.

The problem I'm facing is - The min value of X-axis with the data bounded is different than that of I need. So, I'm setting it through code. But, its not getting the expected result. I'm using the single / multiple Y-axes. Setting its Autorange true/false through code. When I'm setting the AutoRange of X-axis to false, its not giving any chart at all & not any exception or error as well.

Please, check the code & help me.

private void DrawLineChart(RadChart ValChart, List<tbModelData> lstChart, string ChartName)
       {
           try
           {
               strCurrentChartName = ChartName;
               List<List<IntChartData>> chartData = new List<List<IntChartData>>();
               ValChart.DefaultView.ChartTitle.Visibility = Visibility.Collapsed;
               ValChart.SeriesMappings.Clear();
               ValChart.DefaultView.ChartArea.AdditionalYAxes.Clear();
               ValChart.ItemsSource = null;
               int jctr = 0;
               double dPrice;
               long iUncheckedSpecElementId = -1;
               bool IsOnlyPercentageData = false;
               dblMaxlist = new List<double>();
               dblMinlist = new List<double>();
               List<double> dblPercentMaxlist = new List<double>();
               List<double> dblPercentMinlist = new List<double>();
               double MinPercentValue;
               double MaxPercentValue = 0;
               double dStartYr = 0;
               double dEndYr = 0;
               if (lstChart != null)
               {
                   ValChart.DefaultView.ChartArea.AxisX.LabelStep = 1;
                   ValChart.DefaultView.ChartArea.AxisX.Step = 6;
                   ValChart.DefaultView.ChartArea.AxisY.Step = 6;
                   ValChart.DefaultView.ChartArea.AxisY.Title = "";
                   ValChart.DefaultView.ChartArea.ZoomScrollSettingsX.ScrollMode = ScrollMode.None;
                   ValChart.DefaultView.ChartArea.ZoomScrollSettingsY.ScrollMode = ScrollMode.None;
                   ValChart.DefaultView.ChartArea.LabelFormatBehavior = LabelFormatBehavior.None;
               }
               for (int rowcnt = 0; rowcnt < lstChart.Count; rowcnt++)
               {
                   for (int iComboItemInd = 1; iComboItemInd < lstComboItems.Count; iComboItemInd++)
                   {
                       if (lstComboItems[iComboItemInd].IsChecked == false && lstComboItems[iComboItemInd].SpecElementId == lstChart[rowcnt].SpecElementId)
                       {
                           iUncheckedSpecElementId = lstComboItems[iComboItemInd].SpecElementId;
                           break;
                       }
                   }
                   if (lstChart.Count > 0 && lstChart[rowcnt] != null && iUncheckedSpecElementId != lstChart[rowcnt].SpecElementId)
                   {
                       SeriesMapping ValMapping = new SeriesMapping();
                       LineSeriesDefinition lineDefinition = new LineSeriesDefinition();
                       lineDefinition.ShowPointMarks = true;
                       lineDefinition.ShowItemLabels = false;
                       lineDefinition.ShowItemToolTips = true;
                       lineDefinition.Appearance.StrokeThickness = 1.5;
                       ValMapping.SeriesDefinition = lineDefinition;
                       ValMapping.ItemMappings.Add(new ItemMapping("XValue", DataPointMember.XValue));
                       ValMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                       //ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim();
                       ValMapping.CollectionIndex = rowcnt;
                       if ((Convert.ToString(lstChart[rowcnt].DataTypeId).Trim()) == "3")
                       {
                           ValMapping.SeriesDefinition.ItemToolTipFormat = "(Value: #DATAITEM.YValue{#,###.##}%, Period: #DATAITEM.XValue)";
                       }
                       else
                       {
                           ValMapping.SeriesDefinition.ItemToolTipFormat = "(Value: #DATAITEM.YValue{#,###.##}, Period: #DATAITEM.XValue)";
                       }
                       ValChart.SeriesMappings.Add(ValMapping);
                       var lstDistinctType = from p in lstChart
                                             where p != null
                                             select p.DataTypeId;
                       int?[] DistinctTypeId = lstDistinctType.Distinct().ToArray();
                       //if(two distinct type of eleemtns  
                       //then 
                       if (lstChart[rowcnt] != null && lstChart[rowcnt].DataTypeId != null && lstChart[rowcnt].DataTypeId != 11)   //Added condition to remove abstract elements from list display on X axis as per ticket #2922
                       {
                           if (DistinctTypeId.Length > 1)
                           {
                               if ((Convert.ToString(lstChart[rowcnt].DataTypeId).Trim()) == "3")
                               {
                                   if (ValChart.DefaultView.ChartArea.AdditionalYAxes.Count == 0)
                                   {
                                       ValChart.DefaultView.ChartArea.AdditionalYAxes.Add(new AxisY());
                                       ValChart.DefaultView.ChartArea.AdditionalYAxes[0].AxisName = "Secondary";
                                       ValChart.DefaultView.ChartArea.AdditionalYAxes[0].Title = "%";
                                   }
                                   lineDefinition.AxisName = "Secondary";
                                   if (Convert.ToString(lstChart[rowcnt].Description).Trim().Length > 18)
                                   {
                                       ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim().Substring(0, 18) + "..."; //[Y2]
                                   }
                                   else
                                       ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim();// +" [Y2]";
                               }
                               else if (Convert.ToString(lstChart[rowcnt].Description).Trim().Length > 0)
                               {
                                   if (lstChart[rowcnt].Description.Trim().Length > 18)
                                   {
                                       ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim().Substring(0, 18) + "..."; // [Y1]                                        
                                   }
                                   else
                                       ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim(); // +" [Y1]";
                               }
                           }
                           else if (Convert.ToString(lstChart[rowcnt].Description).Trim().Length > 0)
                           {
                               if ((Convert.ToString(lstChart[rowcnt].DataTypeId).Trim()) == "3")
                               {
                                   if (ValChart.DefaultView.ChartArea.AdditionalYAxes.Count == 0)
                                   {
                                       IsOnlyPercentageData = true;
                                       ValChart.DefaultView.ChartArea.AxisY.AxisName = "Secondary";
                                       ValChart.DefaultView.ChartArea.AxisY.Title = "%";
                                       lineDefinition.AxisName = "Secondary";
                                   }
                                   else
                                   {
                                       IsOnlyPercentageData = false;
                                       ValChart.DefaultView.ChartArea.AxisY.Title = "";
                                   }
                               }
                               else
                               {
                                   IsOnlyPercentageData = false;
                                   ValChart.DefaultView.ChartArea.AxisY.Title = "";
                               }
                               if (lstChart[rowcnt].Description.Trim().Length > 18)
                               {
                                   ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim().Substring(0, 18) + "..."; // [Y1]
                               }
                               else
                                   ValMapping.LegendLabel = lstChart[rowcnt].Description.Trim(); // +" [Y1]";
                           }
                       }
                   }
               }
               for (int cnt = 0; cnt < lstChart.Count; cnt++)
               {
                   var Element = lstChart[cnt];
                   List<IntChartData> lstData = new List<IntChartData>();
                   //Added condition to remove abstract elements from list display on X axis as per ticket #2922
                   if (Element != null && Element.DataTypeId != 11 && iUncheckedSpecElementId != lstChart[cnt].SpecElementId)
                   {
                       jctr = GlobalVariables.intDiff;
                       for (int icrt = 0; icrt < GlobalVariables.ArrPeriodID.Length; icrt++)
                       {
                           if (icrt > Array.IndexOf(GlobalVariables.ArrPeriodID, GlobalVariables.ForecastMax))
                           {
                               break;
                           }
                           if (icrt >= Array.IndexOf(GlobalVariables.ArrPeriodID, (GlobalVariables.intScenarioHistMin + GlobalVariables.intDiff)))
                           {
                               string strColumnFld = objComMethods.GetColumnField(0, jctr);
                               dPrice = objComMethods.GetKeyMetricsValueByProperty(Element, strColumnFld);
                               if (GlobalVariables.ListPeriodCont.Where(A => A.PeriodContextRefId == GlobalVariables.ArrPeriodID[icrt]).Count() != 0)
                               {
                                   int result = Convert.ToInt32(GlobalVariables.ListPeriodCont.Where(A => A.PeriodContextRefId == GlobalVariables.ArrPeriodID[icrt]).ElementAt(0).PeriodContextRefYear);
                                   if (dStartYr == 0)
                                   {
                                       // To get the Historic min as a starting point for X-axis everytime, though the data for Historic is not avalaible.
                                       dStartYr = result;
                                   }
                                   if (double.IsNaN(dPrice) == false)
                                   {
                                       if (dEndYr < result)
                                           dEndYr = result;
                                       if (GlobalVariables.CompanyUnit.ToUpper() == "Actual".ToUpper())
                                       {
                                           lstData.Add(new IntChartData(result, dPrice));
                                       }
                                       else
                                       {
                                           dPrice = (dPrice * GlobalVariables.unitValue) / GlobalVariables.unitValue;
                                           lstData.Add(new IntChartData(result, dPrice));
                                       }
                                   }
                               }
                               //}
                               jctr++;
                           }
                       }
                       if (lstData.Count == 0)
                       {
                           continue;
                       }
                       chartData.Add(lstData);
                       if (Element.DataTypeId == 3 && IsOnlyPercentageData == false)
                       {
                           MaxPercentValue = lstData.Select(A => A.YValue).Max();
                           MinPercentValue = lstData.Select(A => A.YValue).Min();
                           dblPercentMaxlist.Add(MaxPercentValue);
                           dblPercentMinlist.Add(MinPercentValue);
                       }
                       else
                       {
                           MaxValue = lstData.Select(A => A.YValue).Max();
                           MinValue = lstData.Select(A => A.YValue).Min();
                           dblMaxlist.Add(MaxValue);
                           dblMinlist.Add(MinValue);
                       }
                   }
               }
               ValChart.Height = 300;
               //ValChart.Width = 750;
               ValChart.DefaultView.ChartArea.Margin = new Thickness(1, 5, 5, 0);
               ValChart.DefaultView.ChartArea.FontFamily = new FontFamily("Arial");
               ValChart.DefaultView.ChartArea.FontSize = 11;
               ValChart.DefaultView.ChartTitle.OuterBorderThickness = new Thickness(0, 0, 0, 0);
               ValChart.DefaultView.ChartTitle.Foreground = new SolidColorBrush(Colors.Black);
               ValChart.DefaultView.ChartTitle.Height = 20;
               ValChart.DefaultView.ChartTitle.FontWeight = FontWeights.Bold;
               ValChart.DefaultView.ChartTitle.VerticalAlignment = VerticalAlignment.Center;
               ValChart.DefaultView.ChartTitle.Padding = new Thickness(0, 3, 0, 0);
               LinearGradientBrush lgb = new LinearGradientBrush();
               lgb.EndPoint = new Point(0.5, 1);
               lgb.StartPoint = new Point(0.5, 0);
               GradientStop gs1 = new GradientStop();
               gs1.Offset = 0;
               gs1.Color = Color.FromArgb(255, 146, 186, 231);
               GradientStop gs2 = new GradientStop();
               gs2.Offset = 1;
               gs2.Color = Color.FromArgb(255, 108, 162, 223);
               lgb.GradientStops.Add(gs1);
               lgb.GradientStops.Add(gs2);
               ValChart.DefaultView.ChartTitle.Background = lgb;
               ValChart.DefaultView.ChartTitle.BorderThickness = new Thickness(0, 0, 0, 0);
               ValChart.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = Visibility.Visible;
               ValChart.DefaultView.ChartArea.AxisY.MinorGridLinesVisibility = Visibility.Collapsed;
               ValChart.DefaultView.ChartArea.AxisY.StripLinesVisibility = Visibility.Collapsed;
               //ValChart.DefaultView.ChartArea.AxisX.AutoRange = false;
               ////ValChart.DefaultView.ChartArea.AxisX.IsZeroBased = false;
               ValChart.DefaultView.ChartArea.ZoomScrollSettingsX.ScrollMode = ScrollMode.ScrollAndZoom;
               ValChart.DefaultView.ChartArea.ZoomScrollSettingsY.ScrollMode = ScrollMode.ScrollAndZoom;
               ValChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Inside;
               ValChart.DefaultView.ChartLegend.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
               ValChart.DefaultView.ChartLegend.MaxHeight = 100;
               ValChart.DefaultView.ChartLegend.FontFamily = new FontFamily("Arial");
               ValChart.DefaultView.ChartLegend.FontSize = 11;
               ValChart.DefaultView.ChartLegendPosition = Telerik.Windows.Controls.Dock.Bottom;
               ValChart.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.Circle;
               ValChart.DefaultView.ChartLegend.BorderThickness = new Thickness(0, 0, 0, 0);
               ValChart.DefaultView.ChartLegend.Header = "";
               ValChart.DefaultView.ChartLegend.Padding = new Thickness(10, 10, 0, 2);
               ValChart.DefaultView.ChartLegend.Margin = new Thickness(55, 0, 0, 0);
               ValChart.DefaultView.ChartLegend.Background = new SolidColorBrush(Colors.White);
               ValChart.DefaultView.ChartArea.AxisY.AutoRange = false;
               double dblstep = 0;
               if (Math.Round(dblMinlist.Min()) == Math.Round(dblMaxlist.Max()))
               {
                   MinValue = Math.Round(dblMinlist.Min() - 1);
                   MaxValue = Math.Round(dblMaxlist.Max() + 1);
                   dblstep = Math.Round(dblMaxlist.Max());
               }
               else
               {
                   MinValue = Math.Round(dblMinlist.Min());
                   MaxValue = Math.Round(dblMaxlist.Max());
                   dblstep = Math.Round(dblMaxlist.Max()) / 5;
               }
               ValChart.DefaultView.ChartArea.AxisY.MinValue = Math.Round(MinValue);
               ValChart.DefaultView.ChartArea.AxisY.MaxValue = Math.Round(MaxValue + dblstep);
               ////if (ValChart.DefaultView.ChartArea.AxisX.MinValue != dStartYr)
               ////{
               ValChart.DefaultView.ChartArea.AxisX.MinValue = dStartYr - 1;
               ////ValChart.DefaultView.ChartArea.AxisX.AutoRange = false;
               ////}
               if ((Math.Abs(MaxValue) + Math.Abs(MinValue)) / dblstep < 10)
               {
                   ValChart.DefaultView.ChartArea.AxisY.Step = Math.Round(dblstep);
               }
               else
               {
                   ValChart.DefaultView.ChartArea.AxisY.AutoRange = true;
               }
               if (MaxValue > 9999)
               {
                   ValChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = "#VAL{##,###}";
               }
               else if (MaxValue > 999)
                   ValChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = "#VAL{#,###}";
               else
                   ValChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = "#VAL{#.##}";
               if (IsOnlyPercentageData == false)
               {
                   if (dblPercentMinlist != null && dblPercentMinlist.Count > 0)
                   {
                       if (Math.Round(dblPercentMinlist.Min()) == Math.Round(dblPercentMaxlist.Max()))
                       {
                           MinPercentValue = Math.Round(dblPercentMinlist.Min() - 1);
                           MaxPercentValue = Math.Round(dblPercentMaxlist.Max() + 1);
                           dblstep = Math.Round(dblPercentMaxlist.Max());
                       }
                       else
                       {
                           MinPercentValue = Math.Round(dblPercentMinlist.Min());
                           MaxPercentValue = Math.Round(dblPercentMaxlist.Max());
                           dblstep = Math.Round(dblPercentMaxlist.Max()) / 5;
                       }
                       ValChart.DefaultView.ChartArea.AdditionalYAxes[0].MinValue = Math.Round(MinPercentValue);
                       ValChart.DefaultView.ChartArea.AdditionalYAxes[0].MaxValue = Math.Round(MaxPercentValue + dblstep);
                       ValChart.DefaultView.ChartArea.AdditionalYAxes[0].DefaultLabelFormat = "#VAL{#.##}";
                       if ((Math.Abs(MaxPercentValue) + Math.Abs(MinPercentValue)) / dblstep < 10)
                       {
                           ValChart.DefaultView.ChartArea.AdditionalYAxes[0].Step = Math.Round(dblstep);
                       }
                       else
                       {
                           ValChart.DefaultView.ChartArea.AdditionalYAxes[0].AutoRange = true;
                       }
                   }
               }
               ValChart.Background = (new SolidColorBrush(Color.FromArgb(255, 255, 255, 255))) as Brush;
               ValChart.BorderBrush = new SolidColorBrush(Colors.Transparent);
               ValChart.DefaultView.ChartArea.VerticalAlignment = VerticalAlignment.Top;
               ValChart.ItemsSource = chartData;
               CurrentChart = ValChart;
           }
           catch (Exception)
           {
               //ShowNoDataMsg(true, "Unable to load the chart.");
           }
       }


Thanks,
Pragati.

Tags
Chart
Asked by
luke
Top achievements
Rank 1
Answers by
Peshito
Telerik team
luke
Top achievements
Rank 1
Pragati Dukale
Top achievements
Rank 1
Share this question
or