最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Center Vis.js timeline on current date on open - Stack Overflow

programmeradmin1浏览0评论

I have a VisJS timeline ranging from January 2017-2018. The timeline opens centered on a three month range mid-year, but I would like it to open every time centered at the current time.

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

I have a VisJS timeline ranging from January 2017-2018. The timeline opens centered on a three month range mid-year, but I would like it to open every time centered at the current time.

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
Share Improve this question edited Aug 10, 2018 at 15:25 YakovL 8,37513 gold badges73 silver badges112 bronze badges asked Oct 6, 2017 at 18:07 user7631026user7631026 811 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You may try with something like this (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 });
});

or better with the 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);
  });

you can use start and end on Configuration Options to achieve what you want.

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
};

I created a CodePen implementation of this specific solution.

发布评论

评论列表(0)

  1. 暂无评论