打开当前日期的中心Vis.js时间轴

时间:2017-10-06 18:07:48

标签: javascript vis.js vis.js-timeline

我的VisJS时间表从2017年1月到2018年不等。时间轴以年中三个月为中心开放,但我希望每次都以当前时间为中心开放。

min: new Date(2017, 1, 5),           // lower limit of visible range
max: new Date(2018, 1, 11),          // upper limit of visible range
zoomMin: 1000 * 60 * 60 * 24,        // one day in milliseconds
zoomMax: 1000 * 60 * 60 * 24*31*3,   // three months in milliseconds

2 个答案:

答案 0 :(得分:2)

你可以试试这样的事情(timeline.setWindow()):

const todayStart = new Date();
todayStart.setHours(8, 0, 0, 0);
const todayEnd = new Date();
todayEnd.setHours(18, 0, 0, 0);

console.log(todayStart, ':', todayEnd);
setTimeout(_ => {
  this.timeline.setWindow(todayStart, todayEnd, { animation: true });
});
使用moveTo

或更好

  this.timeline.moveTo(new Date());//or
  this.timeline.moveTo(new Date(), { animation: true });//or
  this.timeline.moveTo(new Date(), { animation: true }, (props) => {
    console.log("movedTo", props);
  });

答案 1 :(得分:0)

您可以使用Configuration Options上的开始和结束来实现所需的目标。

var today = new Date(new Date().setHours(0,0,0,0));
var tomorrow = new Date(new Date().setHours(23,59,59,999)+1);

var options = {
    in: new Date(2018, 1, 5), 
    max: new Date(2019, 1, 11),    
    zoomMax: 1000 * 60 * 60 * 24*31*3, 
    start:today,
    end:tomorrow
};

我为此特定解决方案创建了CodePen实现。