|
| 1 | +--- |
| 2 | +title: Add a Custom Time Marker in Gantt |
| 3 | +description: An example on how to add a vertical line similar to Current Time Marker in Kendo UI Gantt. |
| 4 | +type: how-to |
| 5 | +page_title: Add Custom Time Marker | Kendo UI Gantt |
| 6 | +slug: gantt-custom-time-marker |
| 7 | +tags: kendo, kendoui, gantt, custom, time, marker, primavera, status |
| 8 | +ticketid: 1343465 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Progress Kendo UI Gantt</td> |
| 18 | + </tr> |
| 19 | +</table> |
| 20 | + |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +How can I add a custom Time Marker at a specific time similar to the built-in Current Time Marker of my Gantt? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +1. Add the `renderLine` function |
| 29 | +1. In the dataBound event of the Gantt call the function with the desired options. |
| 30 | +1. Add CSS styles using the class for the line or style the line element returned by the function. |
| 31 | + |
| 32 | +```html |
| 33 | +<style> |
| 34 | + .k-gantt .k-status-time { |
| 35 | + background-color: blue; |
| 36 | + position: absolute; |
| 37 | + } |
| 38 | +</style> |
| 39 | +<div id="gantt"></div> |
| 40 | +<script> |
| 41 | + //https://stackoverflow.com/a/563442 |
| 42 | + // implement addDays method for demonstration purposes |
| 43 | + Date.prototype.addDays = function (days) { |
| 44 | + var date = new Date(this.valueOf()); |
| 45 | + date.setDate(date.getDate() + days); |
| 46 | + return date; |
| 47 | + } |
| 48 | +
|
| 49 | + Date.prototype.addHours = function (hours) { |
| 50 | + var date = new Date(this.valueOf()); |
| 51 | + date.setHours(date.getHours() + hours); |
| 52 | + return date; |
| 53 | + } |
| 54 | +
|
| 55 | + $("#gantt").kendoGantt({ |
| 56 | + dataSource: [ |
| 57 | + { |
| 58 | + id: 1, |
| 59 | + orderId: 0, |
| 60 | + title: "Task1", |
| 61 | + start: new Date().addHours(2), |
| 62 | + end: new Date().addDays(1) |
| 63 | + }, |
| 64 | + { |
| 65 | + id: 2, |
| 66 | + orderId: 1, |
| 67 | + title: "Task2", |
| 68 | + start: new Date().addHours(2), |
| 69 | + end: new Date().addDays(1) |
| 70 | + } |
| 71 | + ], |
| 72 | + dataBound: function (e) { |
| 73 | + var statusLineDate = new Date().addHours(1); |
| 74 | + var $lineElement = renderLine(e.sender, statusLineDate); |
| 75 | + }, |
| 76 | + dependencies: [ |
| 77 | + { |
| 78 | + id: 1, |
| 79 | + predecessorId: 1, |
| 80 | + successorId: 2, |
| 81 | + type: 1 |
| 82 | + } |
| 83 | + ] |
| 84 | + }); |
| 85 | +
|
| 86 | + function renderLine(gantt, date, lineClass) { |
| 87 | + var ganttView = gantt.view(); |
| 88 | + lineClass = lineClass || "k-status-time" |
| 89 | + var currentTime = date; |
| 90 | + var timeOffset = ganttView._offset(currentTime); |
| 91 | + var element = $('<div class=\'' + lineClass + '\'></div>'); |
| 92 | + var viewStyles = kendo.ui.GanttView.styles; |
| 93 | + var tablesWrap = $("." + viewStyles.tasksWrapper); |
| 94 | + var tasksTable = $("." + viewStyles.tasksTable); |
| 95 | + var slot; |
| 96 | + if (!ganttView.content || !ganttView._timeSlots().length) { |
| 97 | + return; |
| 98 | + } |
| 99 | + ganttView.content.find('.' + lineClass).remove(); |
| 100 | + slot = ganttView._timeSlots()[ganttView._slotIndex('start', currentTime)]; |
| 101 | + if (currentTime < slot.start || currentTime > slot.end) { |
| 102 | + return; |
| 103 | + } |
| 104 | + if (tablesWrap.length && tasksTable.length) { |
| 105 | + timeOffset += tasksTable.offset().left - tablesWrap.offset().left; |
| 106 | + } |
| 107 | + element.css({ |
| 108 | + left: timeOffset + 'px', |
| 109 | + top: '0px', |
| 110 | + width: '1px', |
| 111 | + height: ganttView._contentHeight + 'px' |
| 112 | + }).appendTo(ganttView.content); |
| 113 | +
|
| 114 | + return element; |
| 115 | + } |
| 116 | +</script> |
| 117 | +``` |
0 commit comments