123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- (function($) {
-
-
- $.jqplot.PointLabels = function(options) {
-
-
-
-
- this.show = $.jqplot.config.enablePlugins;
-
-
-
- this.location = 'n';
-
-
- this.labelsFromSeries = false;
-
-
-
- this.seriesLabelIndex = null;
-
-
- this.labels = [];
-
-
- this._labels = [];
-
-
-
- this.stackedValue = false;
-
-
- this.ypadding = 6;
-
-
- this.xpadding = 6;
-
-
-
- this.escapeHTML = true;
-
-
-
-
- this.edgeTolerance = -5;
-
-
- this.formatter = $.jqplot.DefaultTickFormatter;
-
-
- this.formatString = '';
-
-
- this.hideZeros = false;
- this._elems = [];
-
- $.extend(true, this, options);
- };
-
- var locations = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
- var locationIndicies = {'nw':0, 'n':1, 'ne':2, 'e':3, 'se':4, 's':5, 'sw':6, 'w':7};
- var oppositeLocations = ['se', 's', 'sw', 'w', 'nw', 'n', 'ne', 'e'];
-
-
- $.jqplot.PointLabels.init = function (target, data, seriesDefaults, opts, plot){
- var options = $.extend(true, {}, seriesDefaults, opts);
- options.pointLabels = options.pointLabels || {};
- if (this.renderer.constructor === $.jqplot.BarRenderer && this.barDirection === 'horizontal' && !options.pointLabels.location) {
- options.pointLabels.location = 'e';
- }
-
- this.plugins.pointLabels = new $.jqplot.PointLabels(options.pointLabels);
- this.plugins.pointLabels.setLabels.call(this);
- };
-
-
- $.jqplot.PointLabels.prototype.setLabels = function() {
- var p = this.plugins.pointLabels;
- var labelIdx;
- if (p.seriesLabelIndex != null) {
- labelIdx = p.seriesLabelIndex;
- }
- else if (this.renderer.constructor === $.jqplot.BarRenderer && this.barDirection === 'horizontal') {
- labelIdx = 0;
- }
- else {
- labelIdx = (this._plotData.length === 0) ? 0 : this._plotData[0].length -1;
- }
- p._labels = [];
- if (p.labels.length === 0 || p.labelsFromSeries) {
- if (p.stackedValue) {
- if (this._plotData.length && this._plotData[0].length){
-
- for (var i=0; i<this._plotData.length; i++) {
- p._labels.push(this._plotData[i][labelIdx]);
- }
- }
- }
- else {
-
- var d = this.data;
- if (this.renderer.constructor === $.jqplot.BarRenderer && this.waterfall) {
- d = this._data;
- }
- if (d.length && d[0].length) {
-
- for (var i=0; i<d.length; i++) {
- p._labels.push(d[i][labelIdx]);
- }
- }
- d = null;
- }
- }
- else if (p.labels.length){
- p._labels = p.labels;
- }
- };
-
- $.jqplot.PointLabels.prototype.xOffset = function(elem, location, padding) {
- location = location || this.location;
- padding = padding || this.xpadding;
- var offset;
-
- switch (location) {
- case 'nw':
- offset = -elem.outerWidth(true) - this.xpadding;
- break;
- case 'n':
- offset = -elem.outerWidth(true)/2;
- break;
- case 'ne':
- offset = this.xpadding;
- break;
- case 'e':
- offset = this.xpadding;
- break;
- case 'se':
- offset = this.xpadding;
- break;
- case 's':
- offset = -elem.outerWidth(true)/2;
- break;
- case 'sw':
- offset = -elem.outerWidth(true) - this.xpadding;
- break;
- case 'w':
- offset = -elem.outerWidth(true) - this.xpadding;
- break;
- default:
- offset = -elem.outerWidth(true) - this.xpadding;
- break;
- }
- return offset;
- };
-
- $.jqplot.PointLabels.prototype.yOffset = function(elem, location, padding) {
- location = location || this.location;
- padding = padding || this.xpadding;
- var offset;
-
- switch (location) {
- case 'nw':
- offset = -elem.outerHeight(true) - this.ypadding;
- break;
- case 'n':
- offset = -elem.outerHeight(true) - this.ypadding;
- break;
- case 'ne':
- offset = -elem.outerHeight(true) - this.ypadding;
- break;
- case 'e':
- offset = -elem.outerHeight(true)/2;
- break;
- case 'se':
- offset = this.ypadding;
- break;
- case 's':
- offset = this.ypadding;
- break;
- case 'sw':
- offset = this.ypadding;
- break;
- case 'w':
- offset = -elem.outerHeight(true)/2;
- break;
- default:
- offset = -elem.outerHeight(true) - this.ypadding;
- break;
- }
- return offset;
- };
-
-
- $.jqplot.PointLabels.draw = function (sctx, options, plot) {
- var p = this.plugins.pointLabels;
-
- p.setLabels.call(this);
-
- for (var i=0; i<p._elems.length; i++) {
-
-
- p._elems[i].emptyForce();
- }
- p._elems.splice(0, p._elems.length);
- if (p.show) {
- var ax = '_'+this._stackAxis+'axis';
-
- if (!p.formatString) {
- p.formatString = this[ax]._ticks[0].formatString;
- p.formatter = this[ax]._ticks[0].formatter;
- }
-
- var pd = this._plotData;
- var ppd = this._prevPlotData;
- var xax = this._xaxis;
- var yax = this._yaxis;
- var elem, helem;
- for (var i=0, l=p._labels.length; i < l; i++) {
- var label = p._labels[i];
-
- if (p.hideZeros && parseInt(p._labels[i], 10) == 0) {
- label = '';
- }
-
- if (label != null) {
- label = p.formatter(p.formatString, label);
- }
- helem = document.createElement('div');
- p._elems[i] = $(helem);
- elem = p._elems[i];
- elem.addClass('jqplot-point-label jqplot-series-'+this.index+' jqplot-point-'+i);
- elem.css('position', 'absolute');
- elem.insertAfter(sctx.canvas);
- if (p.escapeHTML) {
- elem.text(label);
- }
- else {
- elem.html(label);
- }
- var location = p.location;
- if ((this.fillToZero && pd[i][1] < 0) || (this.fillToZero && this._type === 'bar' && this.barDirection === 'horizontal' && pd[i][0] < 0) || (this.waterfall && parseInt(label, 10)) < 0) {
- location = oppositeLocations[locationIndicies[location]];
- }
- var ell = xax.u2p(pd[i][0]) + p.xOffset(elem, location);
- var elt = yax.u2p(pd[i][1]) + p.yOffset(elem, location);
-
-
- if (this._stack && !p.stackedValue) {
- if (this.barDirection === "vertical") {
- elt = (this._barPoints[i][0][1] + this._barPoints[i][1][1]) / 2 + plot._gridPadding.top - 0.5 * elem.outerHeight(true);
- }
- else {
- ell = (this._barPoints[i][2][0] + this._barPoints[i][0][0]) / 2 + plot._gridPadding.left - 0.5 * elem.outerWidth(true);
- }
- }
- if (this.renderer.constructor == $.jqplot.BarRenderer) {
- if (this.barDirection == "vertical") {
- ell += this._barNudge;
- }
- else {
- elt -= this._barNudge;
- }
- }
- elem.css('left', ell);
- elem.css('top', elt);
- var elr = ell + elem.width();
- var elb = elt + elem.height();
- var et = p.edgeTolerance;
- var scl = $(sctx.canvas).position().left;
- var sct = $(sctx.canvas).position().top;
- var scr = sctx.canvas.width + scl;
- var scb = sctx.canvas.height + sct;
-
- if (ell - et < scl || elt - et < sct || elr + et > scr || elb + et > scb) {
- elem.remove();
- }
- elem = null;
- helem = null;
- }
-
-
-
-
-
-
- }
- };
-
- $.jqplot.postSeriesInitHooks.push($.jqplot.PointLabels.init);
- $.jqplot.postDrawSeriesHooks.push($.jqplot.PointLabels.draw);
- })(jQuery);
|