};
/**
+ * TODO(danvk): move this logic into dygraph.js
* @param {Dygraph} g
* @param {Event} event
* @param {Object} context
var selectedPoint = null;
- // Find out if the click occurs on a point. This only matters if there's a
- // pointClickCallback.
- if (pointClickCallback) {
- var closestIdx = -1;
- var closestDistance = Number.MAX_VALUE;
-
- // check if the click was on a particular point.
- for (var i = 0; i < g.selPoints_.length; i++) {
- var p = g.selPoints_[i];
- var distance = Math.pow(p.canvasx - context.dragEndX, 2) +
- Math.pow(p.canvasy - context.dragEndY, 2);
- if (!isNaN(distance) &&
- (closestIdx == -1 || distance < closestDistance)) {
- closestDistance = distance;
- closestIdx = i;
- }
+ // Find out if the click occurs on a point.
+ var closestIdx = -1;
+ var closestDistance = Number.MAX_VALUE;
+
+ // check if the click was on a particular point.
+ for (var i = 0; i < g.selPoints_.length; i++) {
+ var p = g.selPoints_[i];
+ var distance = Math.pow(p.canvasx - context.dragEndX, 2) +
+ Math.pow(p.canvasy - context.dragEndY, 2);
+ if (!isNaN(distance) &&
+ (closestIdx == -1 || distance < closestDistance)) {
+ closestDistance = distance;
+ closestIdx = i;
}
+ }
- // Allow any click within two pixels of the dot.
- var radius = g.getNumericOption('highlightCircleSize') + 2;
- if (closestDistance <= radius * radius) {
- selectedPoint = g.selPoints_[closestIdx];
- }
+ // Allow any click within two pixels of the dot.
+ var radius = g.getNumericOption('highlightCircleSize') + 2;
+ if (closestDistance <= radius * radius) {
+ selectedPoint = g.selPoints_[closestIdx];
}
if (selectedPoint) {
- pointClickCallback.call(g, event, selectedPoint);
+ var e = {
+ cancelable: true,
+ point: selectedPoint,
+ canvasx: context.dragEndX,
+ canvasy: context.dragEndY
+ };
+ var defaultPrevented = g.cascadeEvents_('pointClick', e);
+ if (defaultPrevented) {
+ // Note: this also prevents click / clickCallback from firing.
+ return;
+ }
+ if (pointClickCallback) {
+ pointClickCallback.call(g, event, selectedPoint);
+ }
}
- // TODO(danvk): pass along more info about the points, e.g. 'x'
- if (clickCallback) {
- clickCallback.call(g, event, g.lastx_, g.selPoints_);
+ var e = {
+ xval: g.lastx_, // closest point by x value
+ pts: g.selPoints_,
+ canvasx: context.dragEndX,
+ canvasy: context.dragEndY
+ };
+ if (!g.cascadeEvents_('click', e)) {
+ if (clickCallback) {
+ // TODO(danvk): pass along more info about the points, e.g. 'x'
+ clickCallback.call(g, event, g.lastx_, g.selPoints_);
+ }
}
};
context.cancelNextDblclick = false;
return;
}
+
+ // Give plugins a chance to grab this event.
+ var e = {
+ canvasx: context.dragEndX,
+ canvasy: context.dragEndY
+ };
+ if (g.cascadeEvents_('dblclick', e)) {
+ return;
+ }
+
if (event.altKey || event.shiftKey) {
return;
}
var plugins = Dygraph.PLUGINS.concat(this.getOption('plugins'));
for (var i = 0; i < plugins.length; i++) {
var Plugin = plugins[i];
- var pluginInstance = new Plugin();
+
+ // the plugins option may contain either plugin classes or instances.
+ var pluginInstance;
+ if (typeof(Plugin.activate) !== 'undefined') {
+ pluginInstance = Plugin;
+ } else {
+ pluginInstance = new Plugin();
+ }
+
var pluginDict = {
plugin: pluginInstance,
events: {},
/**
* Triggers a cascade of events to the various plugins which are interested in them.
- * Returns true if the "default behavior" should be performed, i.e. if none of
- * the event listeners called event.preventDefault().
+ * Returns true if the "default behavior" should be prevented, i.e. if one
+ * of the event listeners called event.preventDefault().
* @private
*/
Dygraph.prototype.cascadeEvents_ = function(name, extra_props) {
- if (!(name in this.eventListeners_)) return true;
+ if (!(name in this.eventListeners_)) return false;
// QUESTION: can we use objects & prototypes to speed this up?
var e = {
*/
Dygraph.prototype.loadedEvent_ = function(data) {
this.rawData_ = this.parseCSV_(data);
+ this.cascadeDataDidUpdateEvent_();
this.predraw_();
};
};
/**
+ * Signals to plugins that the chart data has updated.
+ * This happens after the data has updated but before the chart has redrawn.
+ */
+Dygraph.prototype.cascadeDataDidUpdateEvent_ = function() {
+ // TODO(danvk): there are some issues checking xAxisRange() and using
+ // toDomCoords from handlers of this event. The visible range should be set
+ // when the chart is drawn, not derived from the data.
+ this.cascadeEvents_('dataDidUpdate', {});
+};
+
+/**
* Get the CSV data. If it's in a function, call that function. If it's in a
* file, do an XMLHttpRequest to get it.
* @private
if (Dygraph.isArrayLike(data)) {
this.rawData_ = this.parseArray_(data);
+ this.cascadeDataDidUpdateEvent_();
this.predraw_();
} else if (typeof data == 'object' &&
typeof data.getColumnRange == 'function') {
// must be a DataTable from gviz.
this.parseDataTable_(data);
+ this.cascadeDataDidUpdateEvent_();
this.predraw_();
} else if (typeof data == 'string') {
// Heuristic: a newline means it's CSV data. Otherwise it's an URL.
this.attributes_.reparseSeries();
if (file) {
+ // This event indicates that the data is about to change, but hasn't yet.
+ // TODO(danvk): support cancelation of the update via this event.
+ this.cascadeEvents_('dataWillUpdate', {});
+
this.file_ = file;
if (!block_redraw) this.start_();
} else {
};
// (defined below)
-var generateLegendHTML, generateLegendDashHTML;
+var generateLegendDashHTML;
/**
* This is called during the dygraph constructor, after options have been set
var xValue = e.selectedX;
var points = e.selectedPoints;
- var html = generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_);
+ var html = legend.generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_);
this.legend_div_.innerHTML = html;
};
var oneEmWidth = calculateEmWidthInDiv(this.legend_div_);
this.one_em_width_ = oneEmWidth;
- var html = generateLegendHTML(e.dygraph, undefined, undefined, oneEmWidth);
+ var html = legend.generateLegendHTML(e.dygraph, undefined, undefined, oneEmWidth);
this.legend_div_.innerHTML = html;
};
* relevant when displaying a legend with no selection (i.e. {legend:
* 'always'}) and with dashed lines.
*/
-generateLegendHTML = function(g, x, sel_points, oneEmWidth) {
+legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth) {
// TODO(danvk): deprecate this option in place of {legend: 'never'}
if (g.getOption('showLabelsOnHighlight') !== true) return '';
find . -path ./.git -prune -o -print | xargs chmod a+rX
# Copy everything to the site.
- rsync -avzr gallery common tests jsdoc experimental plugins datahandler polyfills $site \
+ rsync -avzr gallery common tests jsdoc experimental plugins datahandler polyfills extras $site \
&& \
rsync -avzr --copy-links dashed-canvas.js dygraph*.js gadget.xml excanvas.js thumbnail.png screenshot.png $temp_dir/* $site/
else
<script type="text/javascript" src="../dygraph-dev.js"></script>
<!-- Include the Javascript for the plug-in -->
- <script type="text/javascript" src="../plugins/unzoom.js"></script>
+ <script type="text/javascript" src="../extras/unzoom.js"></script>
</head>
<body>
<h2>Plugins Demo</h2>