| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
(function($) { |
| 16 |
var Canvas = function(cls, container) { |
| 17 |
var element = container.getElementsByClassName(cls)[0]; |
| 18 |
|
| 19 |
if (!element) { |
| 20 |
element = document.createElement('canvas'); |
| 21 |
element.className = cls; |
| 22 |
element.style.direction = 'ltr'; |
| 23 |
element.style.position = 'absolute'; |
| 24 |
element.style.left = '0px'; |
| 25 |
element.style.top = '0px'; |
| 26 |
|
| 27 |
container.appendChild(element); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
if (!element.getContext) { |
| 32 |
throw new Error('Canvas is not available.'); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
this.element = element; |
| 37 |
|
| 38 |
var context = this.context = element.getContext('2d'); |
| 39 |
this.pixelRatio = $.plot.browser.getPixelRatio(context); |
| 40 |
|
| 41 |
|
| 42 |
var width = $(container).width(); |
| 43 |
var height = $(container).height(); |
| 44 |
this.resize(width, height); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
this.SVGContainer = null; |
| 49 |
this.SVG = {}; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this._textCache = {}; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
Canvas.prototype.resize = function(width, height) { |
| 66 |
var minSize = 10; |
| 67 |
width = width < minSize ? minSize : width; |
| 68 |
height = height < minSize ? minSize : height; |
| 69 |
|
| 70 |
var element = this.element, |
| 71 |
context = this.context, |
| 72 |
pixelRatio = this.pixelRatio; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if (this.width !== width) { |
| 82 |
element.width = width * pixelRatio; |
| 83 |
element.style.width = width + 'px'; |
| 84 |
this.width = width; |
| 85 |
} |
| 86 |
|
| 87 |
if (this.height !== height) { |
| 88 |
element.height = height * pixelRatio; |
| 89 |
element.style.height = height + 'px'; |
| 90 |
this.height = height; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
context.restore(); |
| 98 |
context.save(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
context.scale(pixelRatio, pixelRatio); |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
Canvas.prototype.clear = function() { |
| 113 |
this.context.clearRect(0, 0, this.width, this.height); |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
Canvas.prototype.render = function() { |
| 122 |
var cache = this._textCache; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
for (var layerKey in cache) { |
| 128 |
if (hasOwnProperty.call(cache, layerKey)) { |
| 129 |
var layer = this.getSVGLayer(layerKey), |
| 130 |
layerCache = cache[layerKey]; |
| 131 |
|
| 132 |
var display = layer.style.display; |
| 133 |
layer.style.display = 'none'; |
| 134 |
|
| 135 |
for (var styleKey in layerCache) { |
| 136 |
if (hasOwnProperty.call(layerCache, styleKey)) { |
| 137 |
var styleCache = layerCache[styleKey]; |
| 138 |
for (var key in styleCache) { |
| 139 |
if (hasOwnProperty.call(styleCache, key)) { |
| 140 |
var val = styleCache[key], |
| 141 |
positions = val.positions; |
| 142 |
|
| 143 |
for (var i = 0, position; positions[i]; i++) { |
| 144 |
position = positions[i]; |
| 145 |
if (position.active) { |
| 146 |
if (!position.rendered) { |
| 147 |
layer.appendChild(position.element); |
| 148 |
position.rendered = true; |
| 149 |
} |
| 150 |
} else { |
| 151 |
positions.splice(i--, 1); |
| 152 |
if (position.rendered) { |
| 153 |
while (position.element.firstChild) { |
| 154 |
position.element.removeChild(position.element.firstChild); |
| 155 |
} |
| 156 |
position.element.parentNode.removeChild(position.element); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (positions.length === 0) { |
| 162 |
if (val.measured) { |
| 163 |
val.measured = false; |
| 164 |
} else { |
| 165 |
delete styleCache[key]; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
layer.style.display = display; |
| 174 |
} |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
Canvas.prototype.getSVGLayer = function(classes) { |
| 186 |
var layer = this.SVG[classes]; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if (!layer) { |
| 191 |
|
| 192 |
|
| 193 |
var svgElement; |
| 194 |
|
| 195 |
if (!this.SVGContainer) { |
| 196 |
this.SVGContainer = document.createElement('div'); |
| 197 |
this.SVGContainer.className = 'flot-svg'; |
| 198 |
this.SVGContainer.style.position = 'absolute'; |
| 199 |
this.SVGContainer.style.top = '0px'; |
| 200 |
this.SVGContainer.style.left = '0px'; |
| 201 |
this.SVGContainer.style.height = '100%'; |
| 202 |
this.SVGContainer.style.width = '100%'; |
| 203 |
this.SVGContainer.style.pointerEvents = 'none'; |
| 204 |
this.element.parentNode.appendChild(this.SVGContainer); |
| 205 |
|
| 206 |
svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
| 207 |
svgElement.style.width = '100%'; |
| 208 |
svgElement.style.height = '100%'; |
| 209 |
|
| 210 |
this.SVGContainer.appendChild(svgElement); |
| 211 |
} else { |
| 212 |
svgElement = this.SVGContainer.firstChild; |
| 213 |
} |
| 214 |
|
| 215 |
layer = document.createElementNS('http://www.w3.org/2000/svg', 'g'); |
| 216 |
layer.setAttribute('class', classes); |
| 217 |
layer.style.position = 'absolute'; |
| 218 |
layer.style.top = '0px'; |
| 219 |
layer.style.left = '0px'; |
| 220 |
layer.style.bottom = '0px'; |
| 221 |
layer.style.right = '0px'; |
| 222 |
svgElement.appendChild(layer); |
| 223 |
this.SVG[classes] = layer; |
| 224 |
} |
| 225 |
|
| 226 |
return layer; |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
Canvas.prototype.getTextInfo = function(layer, text, font, angle, width) { |
| 272 |
var textStyle, layerCache, styleCache, info; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
text = '' + text; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if (typeof font === 'object') { |
| 281 |
textStyle = font.style + ' ' + font.variant + ' ' + font.weight + ' ' + font.size + 'px/' + font.lineHeight + 'px ' + font.family; |
| 282 |
} else { |
| 283 |
textStyle = font; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
layerCache = this._textCache[layer]; |
| 289 |
|
| 290 |
if (layerCache == null) { |
| 291 |
layerCache = this._textCache[layer] = {}; |
| 292 |
} |
| 293 |
|
| 294 |
styleCache = layerCache[textStyle]; |
| 295 |
|
| 296 |
if (styleCache == null) { |
| 297 |
styleCache = layerCache[textStyle] = {}; |
| 298 |
} |
| 299 |
|
| 300 |
var key = generateKey(text); |
| 301 |
info = styleCache[key]; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
if (!info) { |
| 306 |
var element = document.createElementNS('http://www.w3.org/2000/svg', 'text'); |
| 307 |
if (text.indexOf('<br>') !== -1) { |
| 308 |
addTspanElements(text, element, -9999); |
| 309 |
} else { |
| 310 |
var textNode = document.createTextNode(text); |
| 311 |
element.appendChild(textNode); |
| 312 |
} |
| 313 |
|
| 314 |
element.style.position = 'absolute'; |
| 315 |
element.style.maxWidth = width; |
| 316 |
element.setAttributeNS(null, 'x', -9999); |
| 317 |
element.setAttributeNS(null, 'y', -9999); |
| 318 |
|
| 319 |
if (typeof font === 'object') { |
| 320 |
element.style.font = textStyle; |
| 321 |
element.style.fill = font.fill; |
| 322 |
} else if (typeof font === 'string') { |
| 323 |
element.setAttribute('class', font); |
| 324 |
} |
| 325 |
|
| 326 |
this.getSVGLayer(layer).appendChild(element); |
| 327 |
var elementRect = element.getBBox(); |
| 328 |
|
| 329 |
info = styleCache[key] = { |
| 330 |
width: elementRect.width, |
| 331 |
height: elementRect.height, |
| 332 |
measured: true, |
| 333 |
element: element, |
| 334 |
positions: [] |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
while (element.firstChild) { |
| 339 |
element.removeChild(element.firstChild); |
| 340 |
} |
| 341 |
element.parentNode.removeChild(element); |
| 342 |
} |
| 343 |
|
| 344 |
info.measured = true; |
| 345 |
return info; |
| 346 |
}; |
| 347 |
|
| 348 |
function updateTransforms (element, transforms) { |
| 349 |
element.transform.baseVal.clear(); |
| 350 |
if (transforms) { |
| 351 |
transforms.forEach(function(t) { |
| 352 |
element.transform.baseVal.appendItem(t); |
| 353 |
}); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
Canvas.prototype.addText = function(layer, x, y, text, font, angle, width, halign, valign, transforms) { |
| 370 |
var info = this.getTextInfo(layer, text, font, angle, width), |
| 371 |
positions = info.positions; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
if (halign === 'center') { |
| 376 |
x -= info.width / 2; |
| 377 |
} else if (halign === 'right') { |
| 378 |
x -= info.width; |
| 379 |
} |
| 380 |
|
| 381 |
if (valign === 'middle') { |
| 382 |
y -= info.height / 2; |
| 383 |
} else if (valign === 'bottom') { |
| 384 |
y -= info.height; |
| 385 |
} |
| 386 |
|
| 387 |
y += 0.75 * info.height; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
for (var i = 0, position; positions[i]; i++) { |
| 393 |
position = positions[i]; |
| 394 |
if (position.x === x && position.y === y && position.text === text) { |
| 395 |
position.active = true; |
| 396 |
|
| 397 |
updateTransforms(position.element, transforms); |
| 398 |
|
| 399 |
return; |
| 400 |
} else if (position.active === false) { |
| 401 |
position.active = true; |
| 402 |
position.text = text; |
| 403 |
if (text.indexOf('<br>') !== -1) { |
| 404 |
y -= 0.25 * info.height; |
| 405 |
addTspanElements(text, position.element, x); |
| 406 |
} else { |
| 407 |
position.element.textContent = text; |
| 408 |
} |
| 409 |
position.element.setAttributeNS(null, 'x', x); |
| 410 |
position.element.setAttributeNS(null, 'y', y); |
| 411 |
position.x = x; |
| 412 |
position.y = y; |
| 413 |
|
| 414 |
updateTransforms(position.element, transforms); |
| 415 |
|
| 416 |
return; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
position = { |
| 426 |
active: true, |
| 427 |
rendered: false, |
| 428 |
element: positions.length ? info.element.cloneNode() : info.element, |
| 429 |
text: text, |
| 430 |
x: x, |
| 431 |
y: y |
| 432 |
}; |
| 433 |
|
| 434 |
positions.push(position); |
| 435 |
|
| 436 |
if (text.indexOf('<br>') !== -1) { |
| 437 |
y -= 0.25 * info.height; |
| 438 |
addTspanElements(text, position.element, x); |
| 439 |
} else { |
| 440 |
position.element.textContent = text; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
position.element.setAttributeNS(null, 'x', x); |
| 445 |
position.element.setAttributeNS(null, 'y', y); |
| 446 |
position.element.style.textAlign = halign; |
| 447 |
|
| 448 |
updateTransforms(position.element, transforms); |
| 449 |
}; |
| 450 |
|
| 451 |
var addTspanElements = function(text, element, x) { |
| 452 |
var lines = text.split('<br>'), |
| 453 |
tspan, i, offset; |
| 454 |
|
| 455 |
for (i = 0; i < lines.length; i++) { |
| 456 |
if (!element.childNodes[i]) { |
| 457 |
tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan'); |
| 458 |
element.appendChild(tspan); |
| 459 |
} else { |
| 460 |
tspan = element.childNodes[i]; |
| 461 |
} |
| 462 |
tspan.textContent = lines[i]; |
| 463 |
offset = (i === 0 ? 0 : 1) + 'em'; |
| 464 |
tspan.setAttributeNS(null, 'dy', offset); |
| 465 |
tspan.setAttributeNS(null, 'x', x); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
Canvas.prototype.removeText = function(layer, x, y, text, font, angle) { |
| 488 |
var info, htmlYCoord; |
| 489 |
if (text == null) { |
| 490 |
var layerCache = this._textCache[layer]; |
| 491 |
if (layerCache != null) { |
| 492 |
for (var styleKey in layerCache) { |
| 493 |
if (hasOwnProperty.call(layerCache, styleKey)) { |
| 494 |
var styleCache = layerCache[styleKey]; |
| 495 |
for (var key in styleCache) { |
| 496 |
if (hasOwnProperty.call(styleCache, key)) { |
| 497 |
var positions = styleCache[key].positions; |
| 498 |
positions.forEach(function(position) { |
| 499 |
position.active = false; |
| 500 |
}); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
} else { |
| 507 |
info = this.getTextInfo(layer, text, font, angle); |
| 508 |
positions = info.positions; |
| 509 |
positions.forEach(function(position) { |
| 510 |
htmlYCoord = y + 0.75 * info.height; |
| 511 |
if (position.x === x && position.y === htmlYCoord && position.text === text) { |
| 512 |
position.active = false; |
| 513 |
} |
| 514 |
}); |
| 515 |
} |
| 516 |
}; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
Canvas.prototype.clearCache = function() { |
| 527 |
var cache = this._textCache; |
| 528 |
for (var layerKey in cache) { |
| 529 |
if (hasOwnProperty.call(cache, layerKey)) { |
| 530 |
var layer = this.getSVGLayer(layerKey); |
| 531 |
while (layer.firstChild) { |
| 532 |
layer.removeChild(layer.firstChild); |
| 533 |
} |
| 534 |
} |
| 535 |
}; |
| 536 |
|
| 537 |
this._textCache = {}; |
| 538 |
}; |
| 539 |
|
| 540 |
function generateKey(text) { |
| 541 |
return text.replace(/0|1|2|3|4|5|6|7|8|9/g, '0'); |
| 542 |
} |
| 543 |
|
| 544 |
if (!window.Flot) { |
| 545 |
window.Flot = {}; |
| 546 |
} |
| 547 |
|
| 548 |
window.Flot.Canvas = Canvas; |
| 549 |
})(jQuery); |