X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph.js;h=758d26db7a7421405fbb7de748eecc5b7402930b;hb=82c6fe4d4a5e55a9d5f7696b23a203905006d1ab;hp=66432f7f2a7773e9082d21daa3ecc30603c9fa5e;hpb=795b16307db2a673ba7aa3452f6f6b0e93baeb3a;p=dygraphs.git diff --git a/dygraph.js b/dygraph.js index 66432f7..758d26d 100644 --- a/dygraph.js +++ b/dygraph.js @@ -397,6 +397,7 @@ Dygraph.prototype.__init__ = function(div, file, attrs) { Dygraph.updateDeep(this.attrs_, Dygraph.DEFAULT_ATTRS); this.boundaryIds_ = []; + this.setIndexByName_ = {}; // Create the containing DIV and other interactive elements this.createInterface_(); @@ -840,11 +841,13 @@ Dygraph.prototype.createInterface_ = function() { this.createStatusMessage_(); this.createDragInterface_(); + this.resizeHandler = function(e) { + dygraph.resize(); + } + // Update when the window is resized. // TODO(danvk): drop frames depending on complexity of the chart. - Dygraph.addEvent(window, 'resize', function(e) { - dygraph.resize(); - }); + Dygraph.addEvent(window, 'resize', this.resizeHandler); }; /** @@ -868,7 +871,9 @@ Dygraph.prototype.destroy = function() { } } }; - + // remove event handlers + Dygraph.removeEvent(window,'resize',this.resizeHandler); + this.resizeHandler = null; // These may not all be necessary, but it can't hurt... nullOut(this.layout_); nullOut(this.plotter_); @@ -1526,29 +1531,113 @@ Dygraph.prototype.mouseMove_ = function(event) { Dygraph.prototype.idxToRow_ = function(idx) { if (idx < 0) return -1; - for (var i in this.layout_.datasets) { - if (idx < this.layout_.datasets[i].length) { - return this.boundaryIds_[0][0]+idx; + // make sure that you get the boundaryIds record which is also defined (see bug #236) + var boundaryIdx = -1; + for (var i = 0; i < this.boundaryIds_.length; i++) { + if (this.boundaryIds_[i] !== undefined) { + boundaryIdx = i; + break; + } + } + if (boundaryIdx < 0) return -1; + for (var setIdx = 0; setIdx < this.layout_.datasets.length; ++setIdx) { + var set = this.layout_.datasets[setIdx]; + if (idx < set.length) { + return this.boundaryIds_[boundaryIdx][0] + idx; } - idx -= this.layout_.datasets[i].length; + idx -= set.length; } return -1; }; /** * @private + * Generates legend html dash for any stroke pattern. It will try to scale the + * pattern to fit in 1em width. Or if small enough repeat the partern for 1em + * width. + * @param strokePattern The pattern + * @param color The color of the series. + * @param oneEmWidth The width in pixels of 1em in the legend. + */ +Dygraph.prototype.generateLegendDashHTML_ = function(strokePattern, color, oneEmWidth) { + var dash = ""; + var i, j, paddingLeft, marginRight; + var strokePixelLength = 0, segmentLoop = 0; + var normalizedPattern = []; + var loop; + // IE 7,8 fail at these divs, so they get boring legend, have not tested 9. + var isIE = (/MSIE/.test(navigator.userAgent) && !window.opera); + if(isIE) { + return "—"; + } + if (!strokePattern || strokePattern.length <= 1) { + // Solid line + dash = "
"; + } else { + // Compute the length of the pixels including the first segment twice, + // since we repeat it. + for (i = 0; i <= strokePattern.length; i++) { + strokePixelLength += strokePattern[i%strokePattern.length]; + } + + // See if we can loop the pattern by itself at least twice. + loop = Math.floor(oneEmWidth/(strokePixelLength-strokePattern[0])); + if (loop > 1) { + // This pattern fits at least two times, no scaling just convert to em; + for (i = 0; i < strokePattern.length; i++) { + normalizedPattern[i] = strokePattern[i]/oneEmWidth; + } + // Since we are repeating the pattern, we don't worry about repeating the + // first segment in one draw. + segmentLoop = normalizedPattern.length; + } else { + // If the pattern doesn't fit in the legend we scale it to fit. + loop = 1; + for (i = 0; i < strokePattern.length; i++) { + normalizedPattern[i] = strokePattern[i]/strokePixelLength; + } + // For the scaled patterns we do redraw the first segment. + segmentLoop = normalizedPattern.length+1; + } + // Now make the pattern. + for (j = 0; j < loop; j++) { + for (i = 0; i < segmentLoop; i+=2) { + // The padding is the drawn segment. + paddingLeft = normalizedPattern[i%normalizedPattern.length]; + if (i < strokePattern.length) { + // The margin is the space segment. + marginRight = normalizedPattern[(i+1)%normalizedPattern.length]; + } else { + // The repeated first segment has no right margin. + marginRight = 0; + } + dash += ""; + } + } + } + return dash; +}; + +/** + * @private * Generates HTML for the legend which is displayed when hovering over the * chart. If no selected points are specified, a default legend is returned * (this may just be the empty string). * @param { Number } [x] The x-value of the selected points. * @param { [Object] } [sel_points] List of selected points for the given * x-value. Should have properties like 'name', 'yval' and 'canvasy'. + * @param { Number } [oneEmWidth] The pixel width for 1em in the legend. */ -Dygraph.prototype.generateLegendHTML_ = function(x, sel_points) { +Dygraph.prototype.generateLegendHTML_ = function(x, sel_points, oneEmWidth) { // If no points are selected, we display a default legend. Traditionally, // this has been blank. But a better default would be a conventional legend, // which provides essential information for a non-interactive chart. - var html, sepLines, i, c; + var html, sepLines, i, c, dash, strokePattern; if (typeof(x) === 'undefined') { if (this.attr_('legend') != 'always') return ''; @@ -1559,8 +1648,10 @@ Dygraph.prototype.generateLegendHTML_ = function(x, sel_points) { if (!this.visibility()[i - 1]) continue; c = this.plotter_.colors[labels[i]]; if (html !== '') html += (sepLines ? '