Last active
August 29, 2015 14:27
-
-
Save nreynis/6cdb2d43ab535249a8a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Global Chart helpers object for utility methods and classes | |
var helpers = Chart.helpers = {}; | |
//-- Basic js utility methods | |
var each = helpers.each = function(loopable,callback,self){ | |
// [...] | |
getConstraintWidth = helpers.getConstraintWidth = function(domNode){ // returns Number or undefined if no constraint | |
var constrainedWidth, | |
constrainedWNode = document.defaultView.getComputedStyle(domNode)['max-width'], | |
constrainedWContainer = document.defaultView.getComputedStyle(domNode.parentNode)['max-width'], | |
hasCWNode = constrainedWNode !== null && constrainedWNode !== "none", | |
hasCWContainer = constrainedWContainer !== null && constrainedWContainer !== "none"; | |
if(hasCWNode || hasCWContainer){ | |
constrainedWidth = Math.min((hasCWNode ? parseInt(constrainedWNode, 10) : Number.POSITIVE_INFINITY), | |
(hasCWContainer ? parseInt(constrainedWContainer, 10) : Number.POSITIVE_INFINITY)); | |
} | |
return constrainedWidth; | |
}, | |
getConstraintHeight = helpers.getConstraintHeight = function(domNode){ // returns Number or undefined if no constraint | |
var constrainedHeight, | |
constrainedHNode = document.defaultView.getComputedStyle(domNode)['max-height'], | |
constrainedHContainer = document.defaultView.getComputedStyle(domNode.parentNode)['max-height'], | |
hasCHNode = constrainedHNode !== null && constrainedHNode !== "none", | |
hasCHContainer = constrainedHContainer !== null && constrainedHContainer !== "none"; | |
if(constrainedHNode || constrainedHContainer){ | |
constrainedHeight = Math.min((hasCHNode ? parseInt(constrainedHNode, 10) : Number.POSITIVE_INFINITY), | |
(hasCHContainer ? parseInt(constrainedHContainer, 10) : Number.POSITIVE_INFINITY)); | |
} | |
return constrainedHeight; | |
}, | |
getMaximumWidth = helpers.getMaximumWidth = function(domNode){ | |
var container = domNode.parentNode; | |
// TODO = check cross browser stuff with this. | |
var w = container.clientWidth; | |
var cw = getConstraintWidth(domNode); | |
if(cw !== undefined) w = Math.min(w, cw); | |
return w; | |
}, | |
getMaximumHeight = helpers.getMaximumHeight = function(domNode){ | |
var container = domNode.parentNode; | |
// TODO = check cross browser stuff with this. | |
var h = container.clientHeight; | |
var ch = getConstraintHeight(domNode); | |
if(ch !== undefined) h = Math.min(h, ch); | |
return h; | |
}, | |
// [...] | |
//Core methods that'll be a part of every chart type | |
extend(Chart.Type.prototype,{ | |
// [...] | |
resize : function(callback){ | |
this.stop(); | |
var canvas = this.chart.canvas, | |
newWidth = getMaximumWidth(this.chart.canvas), | |
newHeight = this.options.maintainAspectRatio ? newWidth / this.chart.aspectRatio : getMaximumHeight(this.chart.canvas); | |
// if aspect ratio must be respected we gotta explicitly | |
// check if we don't break the height constraint, if this | |
// is the case we recompute the width | |
if(this.options.maintainAspectRatio){ | |
var ch = getConstraintHeight(this.chart.canvas); | |
if(ch !== undefined && ch < newHeight){ | |
newHeight = ch; | |
newWidth = newHeight * this.chart.aspectRatio; | |
} | |
} | |
canvas.width = this.chart.width = newWidth; | |
canvas.height = this.chart.height = newHeight; | |
retinaScale(this.chart); | |
if (typeof callback === "function"){ | |
callback.apply(this, Array.prototype.slice.call(arguments, 1)); | |
} | |
return this; | |
}, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment