Last active
December 6, 2017 21:45
-
-
Save tgotwig/e8b0e3890db1eeebf2fe1b3b1753f1e8 to your computer and use it in GitHub Desktop.
Simple Barplot (+tooltip, random)
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<svg width="960" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://code.jquery.com/jquery-3.2.1.min.js" | |
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" | |
crossorigin="anonymous"></script> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script> | |
<script> | |
;(function (d3, $) { | |
window.drawBarPlot = function () { | |
const svg = d3.select('svg') | |
const margin = {top: 20, right: 20, bottom: 30, left: 40} | |
const width = +svg.attr('width') - margin.left - margin.right | |
const height = +svg.attr('height') - margin.top - margin.bottom | |
const x = d3.scaleBand().rangeRound([0, width]).padding(0.1) | |
const y = d3.scaleLinear().rangeRound([height, 0]) | |
var g = svg.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 })]) | |
g.append('g') | |
.attr('class', 'axis axis--x') | |
.attr('transform', 'translate(0,' + height + ')') | |
.call(d3.axisBottom(x)) | |
g.append('g') | |
.attr('class', 'axis axis--y') | |
.call(d3.axisLeft(y).ticks(10, '%')) | |
.append('text') | |
.attr('transform', 'rotate(-90)') | |
.attr('y', 6) | |
.attr('dy', '0.71em') | |
.attr('text-anchor', 'end') | |
.text('Frequency') | |
g.selectAll('.bar') | |
.data(data) | |
.enter().append('rect') | |
.attr('class', 'bar') | |
.attr('x', function (d) { return x(d.letter) }) | |
.attr('y', function (d) { return y(d.frequency) }) | |
.attr('width', x.bandwidth()) | |
.attr('height', function (d) { return height - y(d.frequency) }) | |
.attr('data-toggle', 'tooltip') | |
.attr('title', (d) => d.letter) | |
.style('fill', 'steelblue') | |
.on('mouseover mousemove', function (d) { | |
$(this).css('fill', 'tomato') | |
}) | |
.on('mouseout', function (d) { | |
$(this).css('fill', 'steelblue') | |
}) | |
.call(_ => { | |
$('[data-toggle="tooltip"]').tooltip({ | |
container: 'body' | |
}) | |
}) | |
} | |
const data = [ | |
{ | |
'letter': 'A', | |
'frequency': d3.randomUniform(1, 5)() | |
}, | |
{ | |
'letter': 'B', | |
'frequency': d3.randomUniform(1, 5)() | |
}, | |
{ | |
'letter': 'C', | |
'frequency': d3.randomUniform(1, 5)() | |
}, | |
{ | |
'letter': 'D', | |
'frequency': d3.randomUniform(1, 5)() | |
}, | |
{ | |
'letter': 'E', | |
'frequency': d3.randomUniform(1, 5)() | |
} | |
] | |
window.drawBarPlot() | |
}(window.d3, window.$)) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment