| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
(function($) { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
$.jqplot.PointLabels = function(options) { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
this.show = $.jqplot.config.enablePlugins; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
this.location = 'n'; |
| 88 |
|
| 89 |
|
| 90 |
this.labelsFromSeries = false; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
this.seriesLabelIndex = null; |
| 95 |
|
| 96 |
|
| 97 |
this.labels = []; |
| 98 |
|
| 99 |
|
| 100 |
this._labels = []; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
this.stackedValue = false; |
| 105 |
|
| 106 |
|
| 107 |
this.ypadding = 6; |
| 108 |
|
| 109 |
|
| 110 |
this.xpadding = 6; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
this.escapeHTML = true; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
this.edgeTolerance = -5; |
| 120 |
|
| 121 |
|
| 122 |
this.formatter = $.jqplot.DefaultTickFormatter; |
| 123 |
|
| 124 |
|
| 125 |
this.formatString = ''; |
| 126 |
|
| 127 |
|
| 128 |
this.hideZeros = false; |
| 129 |
this._elems = []; |
| 130 |
|
| 131 |
$.extend(true, this, options); |
| 132 |
}; |
| 133 |
|
| 134 |
var locations = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w']; |
| 135 |
var locationIndicies = {'nw':0, 'n':1, 'ne':2, 'e':3, 'se':4, 's':5, 'sw':6, 'w':7}; |
| 136 |
var oppositeLocations = ['se', 's', 'sw', 'w', 'nw', 'n', 'ne', 'e']; |
| 137 |
|
| 138 |
|
| 139 |
$.jqplot.PointLabels.init = function (target, data, seriesDefaults, opts, plot){ |
| 140 |
var options = $.extend(true, {}, seriesDefaults, opts); |
| 141 |
options.pointLabels = options.pointLabels || {}; |
| 142 |
if (this.renderer.constructor === $.jqplot.BarRenderer && this.barDirection === 'horizontal' && !options.pointLabels.location) { |
| 143 |
options.pointLabels.location = 'e'; |
| 144 |
} |
| 145 |
|
| 146 |
this.plugins.pointLabels = new $.jqplot.PointLabels(options.pointLabels); |
| 147 |
this.plugins.pointLabels.setLabels.call(this); |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
$.jqplot.PointLabels.prototype.setLabels = function() { |
| 152 |
var p = this.plugins.pointLabels; |
| 153 |
var labelIdx; |
| 154 |
if (p.seriesLabelIndex != null) { |
| 155 |
labelIdx = p.seriesLabelIndex; |
| 156 |
} |
| 157 |
else if (this.renderer.constructor === $.jqplot.BarRenderer && this.barDirection === 'horizontal') { |
| 158 |
labelIdx = (this._plotData[0].length < 3) ? 0 : this._plotData[0].length -1; |
| 159 |
} |
| 160 |
else { |
| 161 |
labelIdx = (this._plotData.length === 0) ? 0 : this._plotData[0].length -1; |
| 162 |
} |
| 163 |
p._labels = []; |
| 164 |
if (p.labels.length === 0 || p.labelsFromSeries) { |
| 165 |
if (p.stackedValue) { |
| 166 |
if (this._plotData.length && this._plotData[0].length){ |
| 167 |
|
| 168 |
for (var i=0; i<this._plotData.length; i++) { |
| 169 |
p._labels.push(this._plotData[i][labelIdx]); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
else { |
| 174 |
|
| 175 |
var d = this.data; |
| 176 |
if (this.renderer.constructor === $.jqplot.BarRenderer && this.waterfall) { |
| 177 |
d = this._data; |
| 178 |
} |
| 179 |
if (d.length && d[0].length) { |
| 180 |
|
| 181 |
for (var i=0; i<d.length; i++) { |
| 182 |
p._labels.push(d[i][labelIdx]); |
| 183 |
} |
| 184 |
} |
| 185 |
d = null; |
| 186 |
} |
| 187 |
} |
| 188 |
else if (p.labels.length){ |
| 189 |
p._labels = p.labels; |
| 190 |
} |
| 191 |
}; |
| 192 |
|
| 193 |
$.jqplot.PointLabels.prototype.xOffset = function(elem, location, padding) { |
| 194 |
location = location || this.location; |
| 195 |
padding = padding || this.xpadding; |
| 196 |
var offset; |
| 197 |
|
| 198 |
switch (location) { |
| 199 |
case 'nw': |
| 200 |
offset = -elem.outerWidth(true) - this.xpadding; |
| 201 |
break; |
| 202 |
case 'n': |
| 203 |
offset = -elem.outerWidth(true)/2; |
| 204 |
break; |
| 205 |
case 'ne': |
| 206 |
offset = this.xpadding; |
| 207 |
break; |
| 208 |
case 'e': |
| 209 |
offset = this.xpadding; |
| 210 |
break; |
| 211 |
case 'se': |
| 212 |
offset = this.xpadding; |
| 213 |
break; |
| 214 |
case 's': |
| 215 |
offset = -elem.outerWidth(true)/2; |
| 216 |
break; |
| 217 |
case 'sw': |
| 218 |
offset = -elem.outerWidth(true) - this.xpadding; |
| 219 |
break; |
| 220 |
case 'w': |
| 221 |
offset = -elem.outerWidth(true) - this.xpadding; |
| 222 |
break; |
| 223 |
default: |
| 224 |
offset = -elem.outerWidth(true) - this.xpadding; |
| 225 |
break; |
| 226 |
} |
| 227 |
return offset; |
| 228 |
}; |
| 229 |
|
| 230 |
$.jqplot.PointLabels.prototype.yOffset = function(elem, location, padding) { |
| 231 |
location = location || this.location; |
| 232 |
padding = padding || this.xpadding; |
| 233 |
var offset; |
| 234 |
|
| 235 |
switch (location) { |
| 236 |
case 'nw': |
| 237 |
offset = -elem.outerHeight(true) - this.ypadding; |
| 238 |
break; |
| 239 |
case 'n': |
| 240 |
offset = -elem.outerHeight(true) - this.ypadding; |
| 241 |
break; |
| 242 |
case 'ne': |
| 243 |
offset = -elem.outerHeight(true) - this.ypadding; |
| 244 |
break; |
| 245 |
case 'e': |
| 246 |
offset = -elem.outerHeight(true)/2; |
| 247 |
break; |
| 248 |
case 'se': |
| 249 |
offset = this.ypadding; |
| 250 |
break; |
| 251 |
case 's': |
| 252 |
offset = this.ypadding; |
| 253 |
break; |
| 254 |
case 'sw': |
| 255 |
offset = this.ypadding; |
| 256 |
break; |
| 257 |
case 'w': |
| 258 |
offset = -elem.outerHeight(true)/2; |
| 259 |
break; |
| 260 |
default: |
| 261 |
offset = -elem.outerHeight(true) - this.ypadding; |
| 262 |
break; |
| 263 |
} |
| 264 |
return offset; |
| 265 |
}; |
| 266 |
|
| 267 |
|
| 268 |
$.jqplot.PointLabels.draw = function (sctx, options, plot) { |
| 269 |
var p = this.plugins.pointLabels; |
| 270 |
|
| 271 |
p.setLabels.call(this); |
| 272 |
|
| 273 |
for (var i=0; i<p._elems.length; i++) { |
| 274 |
|
| 275 |
|
| 276 |
p._elems[i].emptyForce(); |
| 277 |
} |
| 278 |
p._elems.splice(0, p._elems.length); |
| 279 |
|
| 280 |
if (p.show) { |
| 281 |
var ax = '_'+this._stackAxis+'axis'; |
| 282 |
|
| 283 |
if (!p.formatString) { |
| 284 |
p.formatString = this[ax]._ticks[0].formatString; |
| 285 |
p.formatter = this[ax]._ticks[0].formatter; |
| 286 |
} |
| 287 |
|
| 288 |
var pd = this._plotData; |
| 289 |
var ppd = this._prevPlotData; |
| 290 |
var xax = this._xaxis; |
| 291 |
var yax = this._yaxis; |
| 292 |
var elem, helem; |
| 293 |
|
| 294 |
for (var i=0, l=p._labels.length; i < l; i++) { |
| 295 |
var label = p._labels[i]; |
| 296 |
|
| 297 |
if (label == null || (p.hideZeros && parseInt(label, 10) == 0)) { |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
label = p.formatter(p.formatString, label); |
| 302 |
|
| 303 |
helem = document.createElement('div'); |
| 304 |
p._elems[i] = $(helem); |
| 305 |
|
| 306 |
elem = p._elems[i]; |
| 307 |
|
| 308 |
|
| 309 |
elem.addClass('jqplot-point-label jqplot-series-'+this.index+' jqplot-point-'+i); |
| 310 |
elem.css('position', 'absolute'); |
| 311 |
elem.insertAfter(sctx.canvas); |
| 312 |
|
| 313 |
if (p.escapeHTML) { |
| 314 |
elem.text(label); |
| 315 |
} |
| 316 |
else { |
| 317 |
elem.html(label); |
| 318 |
} |
| 319 |
var location = p.location; |
| 320 |
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) { |
| 321 |
location = oppositeLocations[locationIndicies[location]]; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
var ell = xax.u2p(pd[i][0]) + p.xOffset(elem, location); |
| 326 |
var elt = yax.u2p(pd[i][1]) + p.yOffset(elem, location); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
if (this._stack && !p.stackedValue) { |
| 331 |
if (this.barDirection === "vertical") { |
| 332 |
elt = (this._barPoints[i][0][1] + this._barPoints[i][1][1]) / 2 + plot._gridPadding.top - 0.5 * elem.outerHeight(true); |
| 333 |
} |
| 334 |
else { |
| 335 |
ell = (this._barPoints[i][2][0] + this._barPoints[i][0][0]) / 2 + plot._gridPadding.left - 0.5 * elem.outerWidth(true); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
if (this.renderer.constructor == $.jqplot.BarRenderer) { |
| 340 |
if (this.barDirection == "vertical") { |
| 341 |
ell += this._barNudge; |
| 342 |
} |
| 343 |
else { |
| 344 |
elt -= this._barNudge; |
| 345 |
} |
| 346 |
} |
| 347 |
elem.css('left', ell); |
| 348 |
elem.css('top', elt); |
| 349 |
var elr = ell + elem.width(); |
| 350 |
var elb = elt + elem.height(); |
| 351 |
var et = p.edgeTolerance; |
| 352 |
var scl = $(sctx.canvas).position().left; |
| 353 |
var sct = $(sctx.canvas).position().top; |
| 354 |
var scr = sctx.canvas.width + scl; |
| 355 |
var scb = sctx.canvas.height + sct; |
| 356 |
|
| 357 |
if (ell - et < scl || elt - et < sct || elr + et > scr || elb + et > scb) { |
| 358 |
elem.remove(); |
| 359 |
} |
| 360 |
|
| 361 |
elem = null; |
| 362 |
helem = null; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
} |
| 373 |
}; |
| 374 |
|
| 375 |
$.jqplot.postSeriesInitHooks.push($.jqplot.PointLabels.init); |
| 376 |
$.jqplot.postDrawSeriesHooks.push($.jqplot.PointLabels.draw); |
| 377 |
})(jQuery); |