Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Commit

Permalink
Custom function label - fixes #1098 (#2145)
Browse files Browse the repository at this point in the history
* Hide vertically hidden ranged items in groups that are not visible
* Add custom function format for time labels
* fix misspell
* remove spaces
* remove backspace
* add backspaces
* Add docs and examples
* Fix docs
  • Loading branch information
yotamberk authored and mojoaxel committed Oct 17, 2016
1 parent 4079d2c commit 12e2fa4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
10 changes: 9 additions & 1 deletion docs/timeline/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ <h2 id="Configuration_Options">Configuration Options</h2>

<tr>
<td>format</td>
<td>Object</td>
<td>Object or Function</td>
<td>none</td>
<td>
Apply custom date formatting of the labels on the time axis. The default value of <code>format</code> is:
Expand All @@ -583,8 +583,16 @@ <h2 id="Configuration_Options">Configuration Options</h2>
year: ''
}
}</pre>

For values which not provided in the customized <code>options.format</code>, the default values will be used.
All available formatting syntax is described in the <a href="http://momentjs.com/docs/#/displaying/format/">docs of moment.js</a>.
<br>
You can also use a function format for each label. The function accepts as arguments the date, scale and step in that order, and expects to return a string for the label.

<pre class="prettyprint lang-js">function format({
minorLabels: Function(date: Date, scale: Number, step: Number),
majorLabels: Function(date: Date, scale: Number, step: Number)
}</pre>
</td>
</tr>

Expand Down
141 changes: 141 additions & 0 deletions examples/timeline/other/functionLabelFormats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Timeline | Custom function label format example</title>

<style>
body, html {
font-family: arial, sans-serif;
font-size: 11pt;
}

#visualization {
box-sizing: border-box;
width: 100%;
height: 300px;
}
</style>

<!-- note: moment.js must be loaded before vis.js, else vis.js uses its embedded version of moment.js -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>

<script src="../../../dist/vis.js"></script>
<link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
<script src="../../googleAnalytics.js"></script>
</head>
<body>
<p>
This example demonstrate using custom function label formats.
</p>
<div id="visualization"></div>

<script>
var now = moment().minutes(0).seconds(0).milliseconds(0);
var groupCount = 3;
var itemCount = 20;

// create a data set with groups
var names = ['John', 'Alston', 'Lee', 'Grant'];
var groups = new vis.DataSet();
for (var g = 0; g < groupCount; g++) {
groups.add({id: g, content: names[g]});
}

// create a dataset with items
var items = new vis.DataSet();
for (var i = 0; i < itemCount; i++) {
var start = now.clone().add(Math.random() * 200, 'hours');
var group = Math.floor(Math.random() * groupCount);
items.add({
id: i,
group: group,
content: 'item ' + i +
' <span style="color:#97B0F8;">(' + names[group] + ')</span>',
start: start,
type: 'box'
});
}

// create visualization
var container = document.getElementById('visualization');
var options = {
format: {
minorLabels: function(date, scale, step) {
var now = new Date();
var ago = now - date;
var divider;
switch (scale) {
case 'millisecond':
divider = 1;
break;
case 'second':
divider = 1000;
break;
case 'minute':
divider = 1000 * 60;
break;
case 'hour':
divider = 1000 * 60 * 60;
break;
case 'day':
divider = 1000 * 60 * 60 * 24;
break;
case 'weekday':
divider = 1000 * 60 * 60 * 24 * 7;
break;
case 'month':
divider = 1000 * 60 * 60 * 24 * 30;
break;
case 'year':
divider = 1000 * 60 * 60 * 24 * 365;
break;
default:
return new Date(date);
}
return (Math.round(ago * step / divider)) + " " + scale + "s ago"
},
majorLabels: function(date, scale, step) {
var now = new Date();
var ago = now - date;
var divider;
switch (scale) {
case 'millisecond':
divider = 1;
break;
case 'second':
divider = 1000;
break;
case 'minute':
divider = 1000 * 60;
break;
case 'hour':
divider = 1000 * 60 * 60;
break;
case 'day':
divider = 1000 * 60 * 60 * 24;
break;
case 'weekday':
divider = 1000 * 60 * 60 * 24 * 7;
break;
case 'month':
divider = 1000 * 60 * 60 * 24 * 30;
break;
case 'year':
divider = 1000 * 60 * 60 * 24 * 365;
break;
default:
return new Date(date);
}
return (Math.round(ago * step / divider)) + " " + scale + "s ago"
}
}
};

var timeline = new vis.Timeline(container);
timeline.setOptions(options);
timeline.setGroups(groups);
timeline.setItems(items);

</script>
</body>
</html>
8 changes: 8 additions & 0 deletions lib/timeline/TimeStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ TimeStep.prototype.getLabelMinor = function(date) {
date = this.current;
}

if (typeof(this.format.minorLabels) === "function") {
return this.format.minorLabels(date, this.scale, this.step);
}

var format = this.format.minorLabels[this.scale];
return (format && format.length > 0) ? this.moment(date).format(format) : '';
};
Expand All @@ -546,6 +550,10 @@ TimeStep.prototype.getLabelMajor = function(date) {
if (date == undefined) {
date = this.current;
}

if (typeof(this.format.majorLabels) === "function") {
return this.format.majorLabels(date, this.scale, this.step);
}

var format = this.format.majorLabels[this.scale];
return (format && format.length > 0) ? this.moment(date).format(format) : '';
Expand Down
4 changes: 2 additions & 2 deletions lib/timeline/component/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ Group.prototype.redraw = function(range, margin, restack) {

// recalculate the height of the subgroups
this._calculateSubGroupHeights();

this.isVisible = this._isGroupVisible(range, margin);

// reposition visible items vertically
Expand Down Expand Up @@ -220,7 +221,7 @@ Group.prototype.redraw = function(range, margin, restack) {
stack.nostack(this.visibleItems, margin, this.subgroups);
}
}

if (!this.isVisible && this.height) {
return resized = false;
}
Expand Down Expand Up @@ -275,7 +276,6 @@ Group.prototype._calculateSubGroupHeights = function () {
* check if group is visible
* @private
*/

Group.prototype._isGroupVisible = function (range, margin) {
var isVisible =
(this.top <= range.body.domProps.centerContainer.height - range.body.domProps.scrollTop + margin.axis)
Expand Down
4 changes: 2 additions & 2 deletions lib/timeline/optionsTimeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let allOptions = {
day: {string,'undefined': 'undefined'},
month: {string,'undefined': 'undefined'},
year: {string,'undefined': 'undefined'},
__type__: {object}
__type__: {object, 'function': 'function'}
},
majorLabels: {
millisecond: {string,'undefined': 'undefined'},
Expand All @@ -58,7 +58,7 @@ let allOptions = {
day: {string,'undefined': 'undefined'},
month: {string,'undefined': 'undefined'},
year: {string,'undefined': 'undefined'},
__type__: {object}
__type__: {object, 'function': 'function'}
},
__type__: {object}
},
Expand Down

0 comments on commit 12e2fa4

Please sign in to comment.