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

Custom ticks on the X-axis

4 Answers 203 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Jesper
Top achievements
Rank 1
Jesper asked on 01 Jun 2011, 07:48 AM
Hi there
The situation is the following:
I am in need of having custom ticks on the X-axis in the form of DateTimes, jeg AutoRange doesnt seem to be working for me when i have ticks with same date but different time of day.
I have tried setting AutoRange to false, and creating ticks my self but this gives me no graphs at all, can someone tell me what I am missing here?

Following is from my code-behind:
private void SetupGraph()
{
    radChart.ItemsSource = dataContext.Points;
    var seriesMappings = CreateSeriesMappings();
    var axisX = new AxisX
        {
            DefaultLabelFormat = "dd-MM-yy",
            LabelRotationAngle = 45,
            Step = 1,
            LabelStep = 1,
            AutoRange = false,
            IsDateTime = true                                  
        };
  
        var tickPoints = dataContext.Points.SelectMany(gp => gp).Select(gp => gp.Date).Distinct().Select(dt => new TickPoint(){IsDateTime = true, Label = dt.ToShortDateString(), Value = dt.ToOADate()});
    axisX.TickPoints.AddRange(tickPoints);
  
    var axisY = new AxisY { DefaultLabelFormat = "N3", };
    var chartArea = new ChartArea { AxisX = axisX, AxisY = axisY, LegendName = "legend" };
    var chartLegend = new ChartLegend { Name = "legend", UseAutoGeneratedItems = true, Width = 145 };
    var chartTitle = new ChartTitle { Content = "Graph" };
    var chartDefaultView = new ChartDefaultView
        {
            ChartArea = chartArea,
            ChartTitle = chartTitle,
            ChartLegend = chartLegend,
        };
  
        radChart.SeriesMappings.AddRange(seriesMappings);
        radChart.DefaultView = chartDefaultView;
        radChart.AnimationSettings = new AnimationSettings() { TotalSeriesAnimationDuration = new TimeSpan(0, 0, 0, 00), ItemAnimationDuration = new TimeSpan(0,0,0,0), ItemDelay = new TimeSpan(0,0,0,0)};
        radChart.Rebind();
}

4 Answers, 1 is accepted

Sort by
0
Missing User
answered on 03 Jun 2011, 01:35 PM
Hi Jesper,

When you want to set custom TickPoints collection, the step between the Points should be equal. In case that you want to set the range of the XAxis by yourself, you need to calculate the Min, Max and Step values of the axis. 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();
radChart.DefaultView.ChartArea.AxisX.Step = 5;

You can also try with Category axis, by which the Chart treats data as a sequence of non-numerical text labels. Thus the number of the ticks will be equal to the number of the values that you have for the XAxis. To create categorical charts, you have to map the category values (in your case the Date value) to the XCategory property of the ItemMapping:
itemMapping.DataPointMember = DataPointMember.XCategory;
itemMapping.FieldName = "Date";

You can review our online help topics about the XAxis here and for Categorical Charts here. I hope that this helps.

Regards,
Polina
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
KADI
Top achievements
Rank 1
answered on 07 Jul 2011, 02:00 PM
Hello,

I have a similar issue, how did you do?

thx, my best regards.
          K.Houssem
0
Yavor
Telerik team
answered on 12 Jul 2011, 08:28 AM
Hi Kadi,

Can you please supply some additional information on the exact issue, which you are facing? Also, was the reply posted previously helpful, and if not, where/how does it fall short.
Any additional information on these, as well as other relevant points will be appreciated.

Greetings,
Yavor
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
KADI
Top achievements
Rank 1
answered on 12 Jul 2011, 11:26 AM
Hi,

Thanks for your reply,
Thankfully i find a turn around to my problem,
I have a series of data categorized by date, my problem was that the ticks in the x-Axis are not ordered correctly.
i putted AutoRange to false and added the ticks manually and all worked just fine.
i hope in the future to have the ability to order the ticks without relying to do it manually, also when i was putting my code together, in some point i got my ticks correctly orderred, but no data in my Chart Area, after some debugging i find that i should put the mapping to XValue and not XCategorie, here is the code:

mapping1.ItemMappings.Add(new ItemMapping(dataMemberName, DataPointMember.XValue));//If I was to put the XValue to XCategorie nothing will show in the chart Area
RadChart1.DefaultView.ChartArea.AxisX.IsDateTime = true;
RadChart1.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "dd/MM/yyyy";
RadChart1.DefaultView.ChartArea.AxisX.AutoRange = false;
//RadChart1.DefaultView.ChartArea.AxisX.DefaultLabelFormat = }";
//RadChart1.DefaultView.ChartArea.AxisX.TickPoints.CollectionChanging += new EventHandler<CollectionChangingEventArgs>(TickPoints_CollectionChanging);
var dates = new List<DateTime>();
foreach (var dynamicObject in ChartData)
{
var datetime = dynamicObject.GetValue<DateTime>(dataMemberName);
if (!dates.Contains(datetime))
dates.Add(datetime);
}
RadChart1.DefaultView.ChartArea.AxisX.MinValue = dates.Min(e=>e).ToOADate();
RadChart1.DefaultView.ChartArea.AxisX.MaxValue = dates.Max(e => e).ToOADate();
var orderedDates = dates.OrderBy(e => e).ToList();
for (int index = 0; index < orderedDates.Count; index++)
{
var dateTime = orderedDates[index];
TickPoint tickPoint = new TickPoint() { };
tickPoint.IsDateTime = true;
  
tickPoint.LabelFormat = "dd/MM/yyyy";
tickPoint.Value = dateTime.ToOADate();
RadChart1.DefaultView.ChartArea.AxisX.TickPoints.Add(tickPoint);
}

I hope this will help.

My best regards
K.Houssem


Tags
Chart
Asked by
Jesper
Top achievements
Rank 1
Answers by
Missing User
KADI
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or