Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
125
Center content in datetime timeline using both ScrollPosition and ScrollScale does'nt work correctley
posted

Hi

I have a timeline that shows events with a start date and a duration during a year.

I would like the timeline to center its events and zoom in to them when the user selects a year in an externa control.

The scale and scroll gets set and the timeline updates, but it is not correctley showing all the entries, the scroll is slightly of center from the events on the timeline.

I have made a sample project that shows the algorithm used and the scenario that I have attached to this post and also added a image showing how the scroll gets offset.

Once i have set found the earliest and latest (starttime + duration) datetime of the events in the timeline I use the following method to center the ScrollPosition and set the ScrollScale to cover all events.

public void UpdateTimeLineScroll(DateTime minItemDate, DateTime maxItemDate)
{
     if (minItemDate == DateTime.MinValue || maxItemDate == DateTime.MinValue)
     {
          return;
     }

     // Check so that we are not out of scope
     if (minItemDate < igDateTimeAxis.ActualMinimum) minItemDate = igDateTimeAxis.ActualMinimum;
     if (maxItemDate > igDateTimeAxis.ActualMaximum) maxItemDate = igDateTimeAxis.ActualMaximum;

     // Calculate the relative sizes
     double span = (maxItemDate - minItemDate).Ticks;
     double minSpan = (minItemDate - igDateTimeAxis.ActualMinimum).Ticks;
     double range = (igDateTimeAxis.ActualMaximum - igDateTimeAxis.ActualMinimum).Ticks;
     double middle = minSpan + (span / 2);

     // Just to be save, don't divide by zero
     if (range == 0) return;

     double newScrollPosition = middle / range;

     // Add 0.05 to make it just a little larger then the content
     double newScrollScale = span / range + 0.05;

     // Update scroll position and scale to zoom in to content
     igDateTimeAxis.ScrollScale = newScrollScale;
     igDateTimeAxis.ScrollPosition = newScrollPosition;
}

TimeLineScrollScaleBinding.zip
Parents
  • 12875
    posted

    Hi Hannes, 

    Looking thru your sample I see that in your CenterContent function your reference to FirstOrDefault appears to be assuming that your first record is the earliest record based on DueDate.  I think you will need to sort the records first.

    var sortData = Data.OrderBy(x => x.DueDate);

    Then I’m not sure if you want to reference the duration of the first sorted item because you are trying to center on one item or you might need to also identify the last record in the sorted values so that you have the minimum and maximum values across all records.

Reply Children