Skip to content

Commit

Permalink
Expose time-screen conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
FranCarrascosaMS committed Jun 24, 2023
1 parent 52a9f4a commit a9190f2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/timeline/Graph2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,45 @@ Graph2d.prototype.getEventProperties = function (event) {
}
};

/**
* Convert a datetime (Date object) into a position on the screen
* @param {Date} time A date
* @return {int} x The position on the screen in pixels which corresponds
* with the given date.
*/
Graph2d.prototype.toScreen = function (time) {
return this.body.util.toScreen(time)
};

/**
* Convert a datetime (Date object) into a position on the root
* This is used to get the pixel density estimate for the screen, not the center panel
* @param {Date} time A date
* @return {int} x The position on root in pixels which corresponds
* with the given date.
*/
Graph2d.prototype.toGlobalScreen = function (time) {
return this.body.util.toGlobalScreen(time)
};

/**
* Convert a position on screen (pixels) to a datetime
* @param {int} x Position on the screen in pixels
* @return {Date} time The datetime the corresponds with given position x
*/
Graph2d.prototype.toTime = function (x) {
return this.body.util.toTime(x)
};

/**
* Convert a position on the global screen (pixels) to a datetime
* @param {int} x Position on the screen in pixels
* @return {Date} time The datetime the corresponds with given position x
*/
Graph2d.prototype.toGlobalTime = function (x) {
return this.body.util.toGlobalTime(x)
};

/**
* Load a configurator
* @return {Object}
Expand Down
39 changes: 39 additions & 0 deletions lib/timeline/Timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,45 @@ export default class Timeline extends Core {
}
}

/**
* Convert a datetime (Date object) into a position on the screen
* @param {Date} time A date
* @return {int} x The position on the screen in pixels which corresponds
* with the given date.
*/
toScreen(time) {
return this.body.util.toScreen(time)
}

/**
* Convert a datetime (Date object) into a position on the root
* This is used to get the pixel density estimate for the screen, not the center panel
* @param {Date} time A date
* @return {int} x The position on root in pixels which corresponds
* with the given date.
*/
toGlobalScreen(time) {
return this.body.util.toGlobalScreen(time)
}

/**
* Convert a position on screen (pixels) to a datetime
* @param {int} x Position on the screen in pixels
* @return {Date} time The datetime the corresponds with given position x
*/
toTime(x) {
return this.body.util.toTime(x)
}

/**
* Convert a position on the global screen (pixels) to a datetime
* @param {int} x Position on the screen in pixels
* @return {Date} time The datetime the corresponds with given position x
*/
toGlobalTime(x) {
return this.body.util.toGlobalTime(x)
}

/**
* Toggle Timeline rolling mode
*/
Expand Down
35 changes: 35 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@ export class Graph2d {
setOptions(options: TimelineOptions): void;
setSelection(ids: IdType | IdType[]): void;
setWindow(start: DateType, end: DateType, options?: TimelineAnimationOptions): void;
toScreen(time: Date): number;
toGlobalScreen(time: Date): number;
toTime(x: number): Date;
toGlobalTime(x: number): Date;
}

export interface Graph2d {
Expand Down Expand Up @@ -706,6 +710,37 @@ export class Timeline {
*/
setWindow(start: DateType, end: DateType, options?: TimelineAnimationOptions, callback?: () => void): void;

/**
* Convert a datetime (Date object) into a position on the screen
* @param {Date} time A date
* @return {int} x The position on the screen in pixels which corresponds
* with the given date.
*/
toScreen(time: Date): number;

/**
* Convert a datetime (Date object) into a position on the root
* This is used to get the pixel density estimate for the screen, not the center panel
* @param {Date} time A date
* @return {int} x The position on root in pixels which corresponds
* with the given date.
*/
toGlobalScreen(time: Date): number;

/**
* Convert a position on screen (pixels) to a datetime
* @param {int} x Position on the screen in pixels
* @return {Date} time The datetime the corresponds with given position x
*/
toTime(x: number): Date;

/**
* Convert a position on the global screen (pixels) to a datetime
* @param {int} x Position on the screen in pixels
* @return {Date} time The datetime the corresponds with given position x
*/
toGlobalTime(x: number): Date;

/**
* Toggle rollingMode.
*/
Expand Down

0 comments on commit a9190f2

Please sign in to comment.