-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from twjohnston/stacked-bar
Add stacked-bar chart type
- Loading branch information
Showing
24 changed files
with
1,438 additions
and
1,266 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {stack as d3Stack} from 'd3-shape' | ||
import computed, {readOnly} from 'ember-computed-decorators' | ||
import {Component} from 'ember-frost-core' | ||
import {PropTypes} from 'ember-prop-types' | ||
import layout from '../templates/components/frost-chart-svg-plot-stacked-bar' | ||
|
||
export default Component.extend({ | ||
|
||
// == Dependencies ========================================================== | ||
|
||
// == Keyword Properties ==================================================== | ||
|
||
layout, | ||
tagName: 'g', | ||
|
||
// == PropTypes ============================================================= | ||
|
||
propTypes: { | ||
// options | ||
chartState: PropTypes.EmberObject.isRequired, | ||
data: PropTypes.array.isRequired, | ||
x: PropTypes.string.isRequired, | ||
y: PropTypes.string.isRequired, | ||
seriesKeys: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
seriesColors: PropTypes.array | ||
|
||
// state | ||
}, | ||
|
||
getDefaultProps () { | ||
return { | ||
// options | ||
seriesColors: [ | ||
'#009eef', | ||
'#a183db', | ||
'#009999', | ||
'#a1e7ff', | ||
'#e8dffb', | ||
'#9aefea' | ||
] | ||
|
||
// state | ||
} | ||
}, | ||
|
||
// == Computed Properties =================================================== | ||
|
||
@readOnly | ||
@computed('data.[]', 'x', 'chartState.range.x', 'chartState.range.y', | ||
'chartState.domain.x', 'chartState.domain.y', 'seriesKeys', 'seriesColors') | ||
_bars (data, xProp, xRange, yRange, xDomain, yDomain, seriesKeys, seriesColors) { | ||
if (!xRange || !yRange || !xDomain || !yDomain) { | ||
return [] | ||
} | ||
|
||
const stackLayout = d3Stack().keys(seriesKeys) | ||
const stackedData = stackLayout(data) | ||
|
||
const xScale = this.get('chartState.scale.x') | ||
const xTransform = xScale({domain: xDomain, range: xRange}) | ||
|
||
const yScale = this.get('chartState.scale.y') | ||
const yTransform = yScale({domain: yDomain, range: yRange}) | ||
|
||
return stackedData.reduce((arr, layer, index) => { | ||
return arr.concat(layer.map(entry => { | ||
return { | ||
x: xTransform(entry.data[xProp]), | ||
y: yTransform(entry[1]), | ||
width: xTransform.bandwidth(), | ||
height: yTransform(entry[0]) - yTransform(entry[1]), | ||
color: seriesColors[index % seriesColors.length], // Repeat colors if too few are provided | ||
seriesIndex: index | ||
} | ||
})) | ||
}, []) | ||
} | ||
|
||
// == Functions ============================================================= | ||
|
||
// == DOM Events ============================================================ | ||
|
||
// == Lifecycle Hooks ======================================================= | ||
|
||
// == Actions =============================================================== | ||
|
||
}) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Helper definition for the d3-band-scale helper | ||
*/ | ||
import {scaleBand} from 'd3-scale' | ||
import Ember from 'ember' | ||
const {Helper} = Ember | ||
|
||
export function bandScale () { | ||
return function ({domain, range}) { | ||
return scaleBand().domain(domain).rangeRound(range).paddingInner(0.2) | ||
} | ||
} | ||
|
||
export default Helper.helper(bandScale) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Ember from 'ember' | ||
const {A, Helper, Object: EmberObject} = Ember | ||
|
||
export function ordinalTicks () { | ||
return function (domain) { | ||
const _ticks = A() | ||
const numTicks = domain.length | ||
_ticks.addObjects(Array.from({length: numTicks}, (entry, index) => { | ||
const tick = domain[index] | ||
return EmberObject.create({ | ||
index: index + 1, | ||
value: tick | ||
}) | ||
})) | ||
|
||
return _ticks | ||
} | ||
} | ||
|
||
export default Helper.helper(ordinalTicks) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.frost-chart-svg-plot-stacked-bar { | ||
rect { | ||
fill-opacity: .6; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.frost-chart { | ||
position: relative; | ||
|
||
.frost-chart-svg { | ||
overflow: visible; | ||
} | ||
} |
This file contains 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
3 changes: 3 additions & 0 deletions
3
addon/templates/components/frost-chart-svg-plot-stacked-bar.hbs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#each _bars as |bar|}} | ||
{{yield bar}} | ||
{{/each}} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Simple re-export of frost-chart-svg-plot-stacked-bar in the app namespace | ||
*/ | ||
export {default} from 'ember-frost-chart/components/frost-chart-svg-plot-stacked-bar' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Re-export the d3-band-scale helper into the app namespace | ||
*/ | ||
export {default} from 'ember-frost-chart/helpers/d3-band-scale' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Re-export the ordinal-ticks helper into the app namespace | ||
*/ | ||
export {default} from 'ember-frost-chart/helpers/ordinal-ticks' |
Oops, something went wrong.