<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Bar chart?</title>
</head>
<body>
<meta charset="utf-8">
<div id="chart" class="svg-container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
</body>
</html>
var div = d3.select('#chart').append('div')
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
barheight = 20,
barwidth = 10;
var parseDate = d3.time.format("%Y-%m-%d").parse;
//var formatPct = d3.format('.0%')
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5)
.tickFormat(d3.time.format("%Y"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
//create line of values
var valueline = d3.svg.line()
.x(function(d) { return x(d.mes); })
.y(function(d) { return y(d.jornalistas); });
var svg = d3.select("#chart")
.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 + ")");
d3.json("https://raw.githubusercontent.com/voltdatalab/dados/master/passaralhos/total.json", function(error, data) {
data.forEach(function(d) {
d.mes = parseDate(d.mes);
d.jornalistas = +d.jornalistas;
d.total = +d.total;
});
var dataNested = d3.nest().key(function(d) { return d.midia })
.entries(data)
div.append('select')
.attr('id', 'variableSelect')
.on('change', variableChange)
.selectAll('option')
.data(dataNested)
.enter()
.append('option')
.attr('value', function(d) { return d.key })
.text(function(d) {return d.key})
var dataFiltered = dataNested.filter(function(d) {
return d.key === d3.select('#variableSelect')
.property('value')})
x.domain
(d3.extent(dataFiltered[0]
.values, function(d) {return d.mes;}));
y.domain
(d3.extent(dataFiltered[0]
.values, function(d) {return d.jornalistas;}));
svg.append("g")
.attr("class", "xAxis")
.attr("transform", "translate(0," + height + ")").
call(xAxis);
svg.append("g")
.attr("class", "yAxis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Demissões");
svg.append("path")
.datum(dataFiltered[0].values)
.attr("class", "line")
.attr("width", 0)
.attr("d", valueline);
function variableChange() {
var value = this.value
var dataFiltered = dataNested.filter(function(d) {
return d.key === value
})
x.domain(d3.extent(dataFiltered[0].values, function(d) {
return d.mes;
}));
y.domain(d3.extent(dataFiltered[0].values, function(d) {
return d.jornalistas;
}));
d3.select('.xAxis').transition().duration(1000).call(xAxis)
d3.select('.yAxis').transition().duration(1000).call(yAxis)
d3.select('.line').datum(dataFiltered[0].values).attr('d', valueline)
}
});
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |