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 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | // // Open Web Analytics - An Open Source Web Analytics Framework // // Copyright 2010 Peter Adams. All rights reserved. // // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // $Id$ // /** * Javascript Heatmap Library * * @author Peter Adams <peter@openwebanalytics.com> * @web <a href="http://www.openwebanalytcs.com">Open Web Analytics</a> * @copyright Copyright © 2006-2010 Peter Adams <peter@openwebanalytics.com> * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.2.1 */ OWA.heatmap = function(w, h) { this.docDimensions = this.getDim(document); w = w || this.docDimensions.w; h = h || this.docDimensions.h; OWA.debug("Canvas size: %s by %s", w, h); this.createCanvas(w,h); this.canvas = document.getElementById('owa_heatmap'); this.context = this.canvas.getContext('2d'); this.calcRegions(); }; OWA.heatmap.prototype = { options: { dotSize: 12, numRegions: 40, alphaIncrement:50, demoMode: false, liveMode: false, mapInterval: 1000, randomDataCount: 200, rowsPerFetch: 100, strokeRegions: false, svgUrl: OWA.getSetting('baseUrl')+'/modules/base/i/test.svg#f1', baseUrl: '', apiUrl: '' }, canvas: null, context: null, docDimensions: null, regions: new Array(), regionsMap: new Array(), regionWidth: null, regionHeight: null, dirtyRegions: new Object(), timer: '', clicks: '', nextPage: 1, more: true, lock: false, /** * Marks a region as dirty so that it can be re-rendered */ markRegionDirty: function(region_num) { if (region_num >= 0) { this.dirtyRegions[region_num] = true; OWA.debug("marking region dirty: %s", region_num); } else { OWA.debug("no region to mark dirty!"); } }, showControlPanel: function() { var that = this; jQuery('body').append('<div id="owa_overlay"></div>'); jQuery('#owa_overlay').append('<div id="owa_overlay_logo"></div>'); jQuery('#owa_overlay').append('<div class="owa_overlay_control" id="owa_overlay_start">Start</div>'); jQuery('#owa_overlay_start').toggleClass('active'); jQuery('#owa_overlay').append('<div class="owa_overlay_control" id="owa_overlay_stop">Stop</div>'); jQuery('#owa_overlay').append('<div class="owa_overlay_control" id="owa_overlay_end">X</div>'); jQuery('#owa_overlay_start').click(function(){that.startTimer()}); jQuery('#owa_overlay_stop').click(function(){that.stopTimer()}); jQuery('.owa_overlay_control').bind('click', function(){ jQuery(".owa_overlay_control").removeClass('active'); jQuery(this).addClass('active'); }); jQuery('#owa_overlay_end').click(function(){that.endSession()}); //eliminate session cookie when window closes. jQuery(window).unload(function() {OWA.endOverlaySession()}); }, /** * Main generation method. kicks off the timer if in liveMode */ generate: function() { this.showControlPanel(); this.applyBlur(); if (this.options.liveMode === true) { this.startTimer(); } else { this.map(); } }, endSession: function() { OWA.util.eraseCookie('owa_overlay', document.domain); window.close(); }, startTimer: function() { var that = this; this.timer = setInterval(function(){that.map()}, this.options.mapInterval); }, stopTimer: function() { if (!this.timer) return false; clearInterval(this.timer); }, /** * Gets data and plots it */ map: function() { if (this.lock == true) { OWA.debug("skipping data fetch due to lock."); return; } else { this.lock = true; } if (this.options.liveMode === true) { var more = this.checkForMoreClicks(); if (more === true) { OWA.debug('there are more clicks to fetch.'); var data = this.getData(); } else { OWA.debug('there are no more clicks to fetch.'); this.stopTimer(); } } else { var data = this.getData(); } }, /** * Gets data, random if in demoMode */ getData: function() { // get data if (this.options.demoMode === true) { return this.getRandomData(this.options.randomDataCount); } else { var data = this.fetchData(this.getNextPage()); return; } }, checkForMoreClicks: function() { return this.more; }, getNextPage: function() { return this.nextPage; }, setNextPage: function(page) { OWA.debug("setNextpage received page as %d", page); this.nextPage++; OWA.debug("setNextpage is setting page as %d", this.nextPage); }, setMore: function(bool) { this.more = bool; }, /** * Fetches data via ajax request */ fetchData: function(page) { var p = OWA.util.readCookie('owa_overlay'); //alert(unescape(p)); var params = OWA.util.parseCookieStringToJson(p); //params.action = 'base.reportOverlay'; //params.document_url = OWA.util.urlEncode(document.location); params.action = 'getDomClicks'; params.pageUrl = OWA.util.urlEncode(document.location); //params.document_url = document.location; //OWA.debug('encoded url: '+OWA.util.urlEncode(document.location)); params.resultsPerPage = this.options.rowsPerFetch; params.format = 'jsonp'; // add page number if one was passed in if (page) { OWA.debug("fetchData will fetch page %s", page); params.page = page; } //closure var that = this; jQuery.ajax({ url: OWA.getApiEndpoint(), data: OWA.util.nsParams(params), dataType: 'jsonp', jsonp: 'owa_jsonpCallback', success: function(data) { that.plotClickData(data); } }); }, plotClickData: function(data) { if (data) { //OWA.debug('setClicks says data is defined'); this.clicks = data; //set more flag if (data.more === true && data.more != null) { OWA.debug("plotClickData says more flag was set to true"); this.setMore(true); //set next page this.setNextPage(data.page); } else { OWA.debug("plotClickData says more flag was set to false"); this.setMore(false); } //plot dots //this.plotDots(this.getClicks()); this.plotDotsRound(this.getClicks()); this.lock = false; return true; } else { return false; } }, getClicks: function() { //OWA.debug("getClicks is logging %s", this.clicks['page']); return this.clicks.rows; }, /** * Looks up the a region's top lower right corner plot points */ getRegion: function(num) { //OWA.debug("Getting dims for region %s", num); return this.regions[num]; }, /** * Sets the color of a pixels a region based on their alpha values */ setColor: function(num) { OWA.debug("About to set color for region %s", num); var dims = this.getRegion(num); OWA.debug("set color coords %s %s", dims.x, dims.y); // get the actual pixel data from the region var canvasData = this.context.getImageData(dims.x, dims.y, this.regionWidth, this.regionHeight); var pix = canvasData.data; // Loop over each pixel and invert the color. for (var i = 0, n = pix.length; i < n; i += 4) { var rgb = this.getRgbFromAlpha(pix[i+3]); pix[i ] = Math.round(parseInt(rgb.r)); // red pix[i+1] = Math.round(parseInt(rgb.g)); // green pix[i+2] = Math.round(parseInt(rgb.b)); // blue } // Draw the ImageData object at the given (x,y) coordinates. this.context.putImageData(canvasData,dims.x,dims.y); }, getRgbFromAlpha : function(alpha) { var rgb = {'r': null, 'g': null, 'b': null}; // set colors based on current alpha value if( alpha <= 255 && alpha >= 235 ){ tmp = 255 - alpha; rgb.r = 255 - tmp; rgb.g = tmp * 12; } else if ( alpha <= 234 && alpha >= 200 ){ tmp = 234 - alpha; rgb.r = 255 - ( tmp * 8 ); rgb.g = 255; } else if ( alpha <= 199 && alpha >= 150 ){ tmp = 199 - alpha; rgb.g = 255; rgb.b = tmp * 5; } else if ( alpha <= 149 && alpha >= 100 ){ tmp = 149 - alpha; rgb.g = 255 - ( tmp * 5 ); rgb.b = 255; } else { rgb.b = 255; } return rgb |