Created
December 13, 2015 04:50
-
-
Save ivanvanderbyl/f9504a1b348c7c5398eb to your computer and use it in GitHub Desktop.
Mixin for calculating dimensions of a component for use with d3 charts
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
import Ember from 'ember'; | |
const { run: { next }} = Ember; | |
export default Ember.Mixin.create({ | |
/** | |
* Internal for checking if we have a rendering context. If this is true | |
* the component have been rendered and we're in the next run loop step. | |
* | |
* @type {Boolean} | |
* @private | |
*/ | |
_isReady: false, | |
/** | |
* If we're able to render, this will be set to a d3 selection of our chart | |
* element. Otherwise it will be null. | |
* | |
* @type {D3[Element]} | |
*/ | |
chart: null, | |
/** | |
* Represents the width of the chart | |
* | |
* @type {Number} | |
*/ | |
plotWidth: 200, | |
/** | |
* Represents the height of the chart | |
* | |
* @type {Number} | |
*/ | |
plotHeight: 100, | |
/** | |
* A hook to schedule future renders from once we have the chart ready. | |
*/ | |
didInsertChart: Ember.K(), | |
didInsertElement() { | |
next(this, function() { | |
this._isReady = true; | |
this.measureDimensions(); | |
this.chart = d3.select(this.chartElement()); | |
this.didInsertChart(); | |
}); | |
}, | |
willDestroyElement() { | |
this._isReady = false; | |
this.chart = null; | |
}, | |
chartElement() { | |
if (!this._isReady) { return; } | |
return this.element.querySelector('svg'); | |
}, | |
measureDimensions() { | |
if (!this._isReady) { return; } | |
const rect = this.element.getBoundingClientRect(); | |
this.setProperties({ | |
plotWidth: rect.width, | |
plotHeight: rect.height, | |
}); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment