From da791b1be26ddd5ab7c2ad367d601e72eb0b2801 Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Tue, 31 Jul 2012 15:14:30 +0100 Subject: [PATCH] [#2458] Only draw stats chart when canvas is visible --- .../ckanext/stats/javascript/modules/plot.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ckanext/stats/public/ckanext/stats/javascript/modules/plot.js b/ckanext/stats/public/ckanext/stats/javascript/modules/plot.js index dae28f5d6b8..2702360ab53 100644 --- a/ckanext/stats/public/ckanext/stats/javascript/modules/plot.js +++ b/ckanext/stats/public/ckanext/stats/javascript/modules/plot.js @@ -1,5 +1,7 @@ this.ckan.module('plot', function (jQuery, _) { return { + graph: null, + canvas: null, options: { xaxis: {}, yaxis: {}, @@ -16,9 +18,12 @@ this.ckan.module('plot', function (jQuery, _) { this.setupCanvas(); this.sandbox.body.on("shown", this._onShown); + this.data = this.parseTable(this.el); - var data = this.parseTable(this.el); - this.graph = jQuery.plot(this.canvas, data, this.options); + this.draw(); + + window.g = window.g || []; + window.g.push(this.graph); }, teardown: function () { @@ -30,6 +35,12 @@ this.ckan.module('plot', function (jQuery, _) { this.el.replaceWith(this.canvas); }, + draw: function () { + if (!this.drawn && this.canvas.is(':visible')) { + this.graph = jQuery.plot(this.canvas, this.data, this.options); + } + }, + parseTable: function (table) { var data = []; var _this = this; @@ -71,10 +82,8 @@ this.ckan.module('plot', function (jQuery, _) { }, _onShown: function (event) { - if (!this._redrawn && jQuery.contains(jQuery(event.target.hash)[0], this.canvas[0])) { - this.graph.setupGrid(); - this.graph.draw(); - this.redrawn = true; + if (!this.drawn && jQuery.contains(jQuery(event.target.hash)[0], this.canvas[0])) { + this.draw(); } } };