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