/* * The MIT License Copyright (c) 2012 by Matt Burland Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* This plugin will draw labels next to the plotted points on your graph. Tested on a scatter graph, may or may not work with other graph types. Best suited to situations involving a smaller number of points. usage - For each series that you want labeled you need to set showLabels to true, set labels to an array of label names (strings), set the labelClass to a css class you have defined for your labels and optionally set labelPlacement to one of "left", "right", "above" or "below" (below by default if not specified). Placement can be fine tuned by setting the margins in your label class. Note: if the labelClass is not explicitly supplied in the development version of flot (> v0.7), the plugin will auto generate a label class as "seriesLabelx" where x is the 1-based index of the data series. I.e. the first dataseries will be seriesLabel1, the second seriesLabel2, etc. For the names, the array should be the same length as the data. If any are missing (null) then the label for that point will be skipped. For example, to label only the 1st and 3rd points: var names = ["foo", null, "bar"]; Update: Version 0.2 Added support for drawing labels using canvas.fillText. The advantages are that, in theory, drawing to the canvas should be faster, but the primary reason is that in some browsers, the labels added as absolutely positioned div elements won't show up if you print the page. So if you want to print your graphs, you should probably use canvasRender. The disadvantage is that you lose the flexibility of defining the label with a CSS class. Options added to series (with defaults): canvasRender: false, // false will add divs to the DOM rather than use canvas.fillText cColor: "#000", // color for the text if using canvasRender cFont: "9px, san-serif", // font for the text if using canvasRender cPadding: 4 // Padding to add when using canvasRender (where padding is added depends on // labelPlacement) Also, version 0.2 takes into account the radius of the data points when placing the labels. */ (function ($) { function init(plot) { plot.hooks.drawSeries.push(drawSeries); plot.hooks.shutdown.push(shutdown); if (plot.hooks.processOffset) { // skip if we're using 0.7 - just add the labelClass explicitly. plot.hooks.processOffset.push(processOffset); } } function processOffset(plot, offset) { // Check to see if each series has a labelClass defined. If not, add a default one. // processOptions gets called before the data is loaded, so we can't do this there. var series = plot.getData(); for (var i = 0; i < series.length; i++) { if (!series[i].canvasRender && series[i].showLabels && !series[i].labelClass) { series[i].labelClass = "seriesLabel" + (i + 1); } } } function drawSeries(plot, ctx, series) { if (!series.showLabels || !(series.labelClass || series.canvasRender) || !series.labels || series.labels.length == 0) { return; } ctx.save(); if (series.canvasRender) { ctx.fillStyle = series.cColor; ctx.font = series.cFont; } /* The following was modified by P. Linstrom on Ocotber 9, 2015. Previusly this code did not account for the left and top margins of the plot box. This resulted in labels not being displayed for items near the bottom or left side of the plot box. Also labels on bar plots with bars close to each other (like mass spectra) were unreadable. Code was added to supress labels when they would extend into an adjacent bar. This feature only works when labels are written to the canvas. */ var offset = plot.getPlotOffset(); left_margin = offset.left; right_margin = offset.left + plot.width(); top_margin = offset.top; bottom_margin = offset.top + plot.height(); if(series.bars && series.canvasRender) { // Get all of the coordinates (and labels) that correspond to // visible points. labels = [] for (var i = 0; i < series.data.length; i++) { var loc = plot.pointOffset({x: series.data[i][0], y: series.data[i][1] }) if (loc.left > left_margin && loc.left < right_margin && loc.top > top_margin && loc.top < bottom_margin) labels.push({x: loc.left, y: loc.top, negative: (series.data[i][1] < 0), label: series.labels[i], left: left_margin, right: right_margin}); } // Sort the points to make sure they are in orfder of increasing X. labels.sort(function(a, b) {a.x - b.x}); // Compute the left and right margins for each point. for(i = 0; i < labels.length; i++) { var y = labels[i].y; for(var n = i - 1; n >= 0; n--) { if(labels[n].negative ? (labels[n].y > y) : (labels[n].y < y)) { labels[i].left = Math.max(labels[i].left, labels[n].x); break; } } for(n = i + 1; n < labels.length; n++) { if(labels[n].negative ? (labels[n].y > y) : (labels[n].y < y)) { labels[i].right = Math.min(labels[i].right, labels[n].x); break; } } } // Write out each label that fits. var radius = series.points.radius; for(i = 0; i < labels.length; i++) { var label = labels[i].label; if(!label) continue var x = labels[i].x; var y = labels[i].y; var text_width = ctx.measureText(labels[i].label).width; switch (series.labelPlacement) { case "above": x = x - text_width / 2; y -= (series.cPadding + radius); ctx.textBaseline = "bottom"; break; case "left": x -= text_width + series.cPadding + radius; ctx.textBaseline = "middle"; break; case "right": x += series.cPadding + radius; ctx.textBaseline = "middle"; break; default: ctx.textBaseline = "top"; y += series.cPadding + radius; x = x - text_width / 2; } if((x >= labels[i].left) && ((x + text_width) <= labels[i].right)) ctx.fillText(labels[i].label, x, y); } } else { for (i = 0; i < series.data.length; i++) { if (series.labels[i]) { var loc = plot.pointOffset({x: series.data[i][0], y: series.data[i][1] }); if (loc.left > left_margin && loc.left < right_margin && loc.top > top_margin && loc.top < bottom_margin) drawLabel(series.labels[i], loc.left, loc.top); } } } /* End of modified section. */ ctx.restore(); function drawLabel(contents, x, y) { var radius = series.points.radius; if (!series.canvasRender) { var elem = $('