Skip to content

Commit

Permalink
Merge branch 'callicles-dynamicEventSet'; bumped patch version
Browse files Browse the repository at this point in the history
  • Loading branch information
mojoaxel committed Apr 9, 2015
2 parents c6681bb + 318f9bc commit 8e3da3f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ var plot = $.plot($('#plot'), [ data ], {
color: "#e8cfac"
},
});

/* It is also possible to add events dynamically
* after initialization by using the setEvents function
*/
$('button').click(function() {
plot.setEvents([{
min: 2.5,
max: 2.5,
eventType: "Simple",
title: "dynamically added event"
}]);
});
```

This plugin is tested to work together with these plugins:
Expand All @@ -67,6 +79,9 @@ This plugin is based on [Joel Oughtons](https://github.com/oughton) ["Event Grap

## Versions

### 0.2.5
* added function "setEvents" to dynamically add events (thx to callicles)

### 0.2.4
* removed `console.log`

Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "flot-events",
"version": "0.2.4",
"version": "0.2.5",
"homepage": "http://mojoaxel.github.io/flot-events",
"authors": [
"Alexander Wunschik",
"Joel Oughton"
"Joel Oughton",
"Nicolas Joseph"
],
"description": "Flot Charting Library Plugin to show Events-Markers",
"main": "jquery.flot.events.js",
Expand Down
11 changes: 11 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
cursor: "move"
}
});

/* wait 5 seconds and dynamicaly add a new marker */
setTimeout(function() {
plot.setEvents([{
min: 2.5,
max: 2.5,
eventType: "Simple",
title: "dynamically added event",
description: ""
}]);
}, 5000);
});
</script>
</body>
Expand Down
45 changes: 24 additions & 21 deletions jquery.flot.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* jquery.flot.events
*
* description: Flot plugin for adding events/markers to the plot
* version: 0.2.4
* version: 0.2.5
* authors:
* Alexander Wunschik <alex@wunschik.net>
* Joel Oughton <joeloughton@gmail.com>
* Nicolas Joseph <www.nicolasjoseph.com>
*
* website: https://github.com/mojoaxel/flot-events
*
Expand Down Expand Up @@ -35,18 +36,18 @@
_position = position;
_moveFunc(_object, _position);
};
}
};

/**
* Event class that stores options (eventType, min, max, title, description) and the object to draw.
*/
var VisualEvent = function(options, drawableEvent){
var _parent,
_options = options,
var _parent,
_options = options,
_drawableEvent = drawableEvent,
_hidden = false;

this.visual = function() { return _drawableEvent; }
this.visual = function() { return _drawableEvent; };
this.getOptions = function() { return _options; };
this.getParent = function() { return _parent; };
this.isHidden = function() { return _hidden; };
Expand All @@ -58,8 +59,7 @@
* A Class that handles the event-markers inside the given plot
*/
var EventMarkers = function(plot) {
var _events = [],
_eventsEnabled = false;
var _events = [];

this._types = [];
this._plot = plot;
Expand All @@ -71,7 +71,7 @@

this.setTypes = function(types) {
return this._types = types;
}
};

/**
* create internal objects for the given events
Expand All @@ -97,7 +97,6 @@
this.drawEvents = function() {
var that = this;
var o = this._plot.getPlotOffset();
var pleft = o.left, pright = this._plot.width() - o.right;

$.each(_events, function(index, event){
// check event is inside the graph range
Expand All @@ -123,17 +122,17 @@
event.visual().moveTo({ top: top, left: left });
});
};

/**
* remove all events from the plot
*/
this._clearEvents = function(){
this._clearEvents = function(){
$.each(_events, function(index, val) {
val.visual().clear();
});
_events = [];
};

/**
* create a new DOM element for the tooltip
*/
Expand All @@ -155,7 +154,7 @@
"z-index": "999",
"font-size": "smaller",
"cursor": "move"
})
});

$('<div id="title" style="font-weight:bold;">' + event.title + '</div>').appendTo($tooltip);
$('<div id="type" style="font-style:italic;">Type: ' + event.eventType + '</div>').appendTo($tooltip);
Expand Down Expand Up @@ -265,8 +264,7 @@
"border-left-style": lineStyle,
"border-left-color": color
})
.appendTo(container)
.hide();
.appendTo(container);

if (markerShow) {
marker = $('<div class="events_marker"></div>').css({
Expand All @@ -285,7 +283,7 @@
marker.css({
"top": top-markerSize-8 +"px",
"border-top": "none",
"border-bottom": markerSize+"px solid " + color,
"border-bottom": markerSize+"px solid " + color
});
} else {
marker.css({
Expand Down Expand Up @@ -388,6 +386,13 @@

that.eventMarkers.drawEvents();
};

// change events on an existing plot
plot.setEvents = function(events){
if (eventMarkers.eventsEnabled) {
eventMarkers.setupEvents(events);
}
};

plot.hooks.processOptions.push(function(plot, options){
// enable the plugin
Expand All @@ -396,9 +401,8 @@
}
});

plot.hooks.draw.push(function(plot, canvascontext){
plot.hooks.draw.push(function(plot){
var options = plot.getOptions();
var xaxis = plot.getXAxes()[options.events.xaxis - 1];

if (eventMarkers.eventsEnabled) {
// check for first run
Expand All @@ -412,8 +416,7 @@

eventMarkers.drawEvents();
});

};
}

var defaultOptions = {
events: {
Expand All @@ -428,6 +431,6 @@
init: init,
options: defaultOptions,
name: "events",
version: "0.2.4"
version: "0.2.5"
});
})(jQuery);

0 comments on commit 8e3da3f

Please sign in to comment.