Last active
November 6, 2016 07:47
-
-
Save nightire/5f008876ba24c75d12abcd2d64a57de4 to your computer and use it in GitHub Desktop.
d3 - data driven document
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 Route from 'ember-route'; | |
import { setProperties } from 'ember-metal/set'; | |
export default Route.extend({ | |
activate() { | |
document.body.classList.add('standard'); | |
}, | |
setupController(controller) { | |
this._super(...arguments); | |
setProperties(controller, { | |
name: 'data driven document', | |
version: d3.version | |
}); | |
} | |
}); |
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 Helper from 'ember-helper'; | |
import { capitalize } from 'ember-string'; | |
export function scale( | |
args, {type = "Linear", domain, range, clamp, invert = false} | |
) { | |
let value = args[0]; | |
type = capitalize(type); | |
if ("Time" === type) { | |
if (!(value instanceof Date)) { | |
domain = [new Date(domain[0]), new Date(domain[1])]; | |
value = new Date(value); | |
} | |
}; | |
const scaler = d3[`scale${type}`]() | |
.domain(domain) | |
.range(range); | |
if (clamp) scaler.clamp(clamp); | |
if (invert) { | |
if ("Quantize" === type) { | |
return scaler.invertExtent(value); | |
} | |
return scaler.invert(value); | |
} | |
return scaler(value); | |
} | |
export default Helper.helper(scale); |
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 Route from 'ember-route'; | |
export default Route.extend({ | |
model() { | |
return { | |
name: 'demo', | |
data: [ | |
{ name: 'Alice', score: 96 }, | |
{ name: 'Billy', score: 83 }, | |
{ name: 'Cindy', score: 91 }, | |
{ name: 'David', score: 96 }, | |
{ name: 'Emily', score: 88 } | |
] | |
} | |
} | |
}); |
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
*, *::before, &::after { | |
box-sizing: border-box; | |
} | |
.bar { | |
height: 2rem; | |
fill: skyblue; | |
stroke: black; | |
stroke-width: 1; | |
} |
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
{ | |
"version": "0.10.5", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": true, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js", | |
"hack": "//cdnjs.cloudflare.com/ajax/libs/hack/0.7.7/hack.css", | |
"standard": "//cdnjs.cloudflare.com/ajax/libs/hack/0.7.7/standard.css", | |
"d3": "//cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.js", | |
"ember": "2.9.0", | |
"ember-data": "2.9.0", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": { | |
"ember-composable-helpers": "*", | |
"ember-route-action-helper": "*", | |
"ember-truth-helpers": "*" | |
} | |
} |
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 Component from 'ember-component'; | |
import get from 'ember-metal/get'; | |
export default Component.extend({ | |
tagName: 'section', | |
classNames: 'chart', | |
width: '100%', | |
height: '200', | |
didInsertElement() { | |
this.chart = d3.select(this.element) | |
.append('svg') | |
.attr('width', get(this, 'width')) | |
.attr('height', get(this, 'height')) | |
.selectAll('g') | |
.data(get(this, 'model')) | |
.enter() | |
.append('g') | |
.attr('transform', (d, i) => `translate(0, ${32 * i})`) | |
// this.update.exit().remove(); | |
this.chart | |
.append('rect') | |
.text(d => d.name) | |
.attr('class', 'bar') | |
.style('width', d => d.score) | |
this.chart | |
.append('text') | |
.text(d => d.name) | |
.attr('y', 18) | |
} | |
}).reopenClass({ positionalParams: ['model'] }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment