Last active
December 25, 2015 20:39
-
-
Save selvagsz/7036939 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
.bar { | |
fill: steelblue; | |
} | |
a:hover { | |
text-decoration: none; | |
} | |
a.active { | |
color: maroon; | |
font-size: 18px; | |
} | |
.axis path { | |
fill: none; | |
} |
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
var App = window.App = Em.Application.create(); | |
App.Router.map(function(){ | |
this.resource('chart', {path: '/'}); | |
this.resource('normal'); | |
}); | |
App.ChartRoute = Em.Route.extend({ | |
model: function(){ | |
return chartData; | |
} | |
}); | |
App.NormalRoute = Em.Route.extend({}); | |
App.BarChartComponent = Em.Component.extend({ | |
drawChart: function(){ | |
var data = this.get('content'); | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 560 - margin.left - margin.right, | |
height = 300 - margin.top - margin.bottom; | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
var svg = d3.select('#'+this.elementId).append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
x.domain(data.map(function(d) { return d.letter; })); | |
y.domain([0, d3.max(data, function(d) { return d.frequency; })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
var bars = svg.selectAll(".bar") | |
.data(data) | |
bars | |
.enter().append("rect") | |
.attr("class", "bar") | |
.attr("x", function(d) { return x(d.letter); }) | |
.attr("width", x.rangeBand()) | |
.attr("y", function(d) { return y(0); }) | |
.attr("height",0) | |
.on('mouseover', function(d,i){ | |
console.log('mouseenter'); | |
}) | |
.on('mouseout', function(d,i){ | |
console.log('mouseout'); | |
}); | |
bars | |
.transition() | |
.duration(1500) | |
.attr("y", function(d) { return y(d.frequency); }) | |
.attr('height', function(d) { return height - y(d.frequency); }) | |
}.on('didInsertElement') | |
}); | |
var chartData = [ | |
{letter: 'A', frequency: 10 }, | |
{letter: 'B', frequency: 5 }, | |
{letter: 'C', frequency: 8 }, | |
{letter: 'D', frequency: 2 }] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment