X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=plugins%2Flegend.js;h=b73e9394ee5edf1a6e43905d50c8864f23205cc7;hb=250bb62bb4614a3c1189c9b5b8b99e182a11db6e;hp=2ff939879e74a2c55aa064faddad4c8b703f49b5;hpb=4d28164e5ad05d7221b332b5c26a2ef4d70125dd;p=dygraphs.git diff --git a/plugins/legend.js b/plugins/legend.js index 2ff9398..b73e939 100644 --- a/plugins/legend.js +++ b/plugins/legend.js @@ -128,11 +128,40 @@ legend.prototype.select = function(e) { var xValue = e.selectedX; var points = e.selectedPoints; + if(e.dygraph.getOption("legendFollow")) { + // create floating legend div + var area = e.dygraph.plotter_.area; + var labelsDivWidth = e.dygraph.getOption("labelsDivWidth"); + var yAxisLabelWidth = e.dygraph.getOption("yAxisLabelWidth"); + // determine floating [left, top] coordinates of the legend div + // within the plotter_ area + // offset 20 px to the left and down from the first selection point + // 20 px is guess based on mouse cursor size + var leftLegend = points[0].x * area.w + 20; + var topLegend = points[0].y * area.h - 20; + + // if legend floats to end of the plotting area, it flips to the other + // side of the selection point + if((leftLegend + labelsDivWidth + 1) > area.w) { + leftLegend = leftLegend - 2*20 - labelsDivWidth - (yAxisLabelWidth - area.x) + } + + e.dygraph.graphDiv.appendChild(this.legend_div_); + this.legend_div_.style.left = yAxisLabelWidth + leftLegend + "px"; + this.legend_div_.style.top = topLegend + "px"; + } + var html = generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_); this.legend_div_.innerHTML = html; }; legend.prototype.deselect = function(e) { + + if(e.dygraph.getOption("legendFollow")) { + // return legend to the default non-floating position + this.predraw(e); + } + // Have to do this every time, since styles might have changed. var oneEmWidth = calculateEmWidthInDiv(this.legend_div_); this.one_em_width_ = oneEmWidth;