From bbfe7d3535854fe4b423ec25862bd9e7456a7a3e Mon Sep 17 00:00:00 2001 From: jos Date: Thu, 28 May 2015 16:48:49 +0200 Subject: [PATCH] Halfway improving Timeline.fit() --- lib/timeline/Core.js | 6 ++--- lib/timeline/Timeline.js | 47 +++++++++++++++++++++++----------------- test/timeline.html | 6 ++--- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/timeline/Core.js b/lib/timeline/Core.js index ae4cc452..4c0886e0 100644 --- a/lib/timeline/Core.js +++ b/lib/timeline/Core.js @@ -477,7 +477,7 @@ Core.prototype._getDataRange = function() { // apply the data range as range var dataRange = this.getItemRange(); - // add 5% space on both sides + // add 1% space on both sides var start = dataRange.min; var end = dataRange.max; if (start != null && end != null) { @@ -486,8 +486,8 @@ Core.prototype._getDataRange = function() { // prevent an empty interval interval = 24 * 60 * 60 * 1000; // 1 day } - start = new Date(start.valueOf() - interval * 0.05); - end = new Date(end.valueOf() + interval * 0.05); + start = new Date(start.valueOf() - interval * 0.01); + end = new Date(end.valueOf() + interval * 0.01); } return { diff --git a/lib/timeline/Timeline.js b/lib/timeline/Timeline.js index b59ff2da..288cde09 100644 --- a/lib/timeline/Timeline.js +++ b/lib/timeline/Timeline.js @@ -218,6 +218,8 @@ Timeline.prototype.setItems = function(items) { this.setWindow(start, end, {animation: false}); } else { + // call fit twice as + this.fit({animation: false}); this.fit({animation: false}); } } @@ -350,32 +352,37 @@ Timeline.prototype.focus = function(id, options) { * When no maximum is found, max==null */ Timeline.prototype.getItemRange = function() { - // calculate min from start filed - var dataset = this.itemsData && this.itemsData.getDataSet(); var min = null; var max = null; + var minId = null; + var maxId = null; + var dataset = this.itemsData && this.itemsData.getDataSet(); if (dataset) { - // calculate the minimum value of the field 'start' - var minItem = dataset.min('start'); - min = minItem ? util.convert(minItem.start, 'Date').valueOf() : null; - // Note: we convert first to Date and then to number because else - // a conversion from ISODate to Number will fail - - // calculate maximum value of fields 'start' and 'end' - var maxStartItem = dataset.max('start'); - if (maxStartItem) { - max = util.convert(maxStartItem.start, 'Date').valueOf(); - } - var maxEndItem = dataset.max('end'); - if (maxEndItem) { - if (max == null) { - max = util.convert(maxEndItem.end, 'Date').valueOf(); + var fieldId = dataset._fieldId; + + dataset.forEach(function (item) { + var start = util.convert(item.start, 'Date').valueOf(); + var end = util.convert(item.end != undefined ? item.end : item.start, 'Date').valueOf(); + if (min === null || start < min) { + min = start; + minId = item[fieldId]; } - else { - max = Math.max(max, util.convert(maxEndItem.end, 'Date').valueOf()); + if (max === null || end > max) { + max = start; + maxId = item[fieldId]; } - } + }); + } + + var minItem = this.itemSet.items[minId]; + var maxItem = this.itemSet.items[maxId]; + if (minItem && minItem.width && maxItem && maxItem.width) { + var left = minItem.left; + var right = maxItem.left + maxItem.width; + + min = this._toTime(left - 10); + max = this._toTime(right + 10); } return { diff --git a/test/timeline.html b/test/timeline.html index ed2e5dbb..5464de8f 100644 --- a/test/timeline.html +++ b/test/timeline.html @@ -137,7 +137,7 @@ title: 'hello title!' }, {_id: 5, content: 'item 5', start: now.clone().add(9, 'days').toDate(), type:'point', title: 'hello title!', className: 'special'}, - {_id: 6, content: 'item 6', start: now.clone().add(11, 'days').toDate()} + {_id: 6, content: 'item 6 very long test bla bla bla', start: now.clone().add(11, 'days').toDate()} ]); var container = document.getElementById('visualization'); @@ -147,8 +147,8 @@ editable: true, //orientation: 'top', orientation: 'both', - start: now.clone().add(-7, 'days'), - end: now.clone().add(7, 'days'), +// start: now.clone().add(-7, 'days'), +// end: now.clone().add(7, 'days'), //maxHeight: 200, //height: 200, showCurrentTime: true,