Built with blockbuilder.org
Last active
November 14, 2017 21:13
-
-
Save aaronbalthaser/8eda7b85a626104690b73961c0dc9769 to your computer and use it in GitHub Desktop.
fresh block
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
license: mit |
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
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
.chart { | |
background: lightgray; | |
border: 1px solid black; | |
width: 400px; | |
height: 600px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="chart"></div> | |
<script> | |
var data = [ | |
{ score: 63, subject: 'Mathmatics'}, | |
{ score: 82, subject: 'Geography'}, | |
{ score: 74, subject: 'Spelling'}, | |
{ score: 97, subject: 'Reading'}, | |
{ score: 52, subject: 'Science'}, | |
{ score: 52, subject: 'Horror'}, | |
{ score: 52, subject: 'Flimsy'} | |
]; | |
var margin = { top: 10, right: 25, bottom: 70, left: 30 }; | |
var width = 400 - margin.left - margin.right; | |
var height = 600 - margin.top - margin.bottom; | |
var svg = d3.select('.chart') | |
.append('svg') | |
.attr('width', width + margin.left + margin.right) | |
.attr('height', height + margin.top + margin.bottom) | |
.call(responsivefy) | |
.append('g') | |
.attr('transform', `translate(${margin.left}, ${margin.top})`); | |
// Y Axis | |
var yScale = d3.scaleLinear() | |
.domain([0, 100]) | |
.range([height, 0]); | |
var yAxis = d3.axisLeft(yScale); | |
svg.call(yAxis); | |
// X Axis | |
var xScale = d3.scaleBand() | |
.padding(0.2) | |
.domain(data.map(d => d.subject)) | |
.range([0, width]); | |
var xAxis = d3.axisBottom(xScale) | |
.ticks(5) | |
svg.append('g') | |
.attr('transform', `translate(0, ${height})`) | |
.call(xAxis) | |
// Rotate Text | |
.selectAll('text') | |
.style('text-anchor', 'end') | |
.attr('transform', 'rotate(-45)'); | |
svg.selectAll('rect') | |
.data(data) | |
.enter() | |
.append('rect') | |
.attr('x', d => xScale(d.subject)) | |
.attr('y', d => yScale(d.score)) | |
.attr('width', d => xScale.bandwidth()) | |
.attr('height', d => height - yScale(d.score)) | |
.style('fill', 'steelblue'); | |
function responsivefy(svg) { | |
var container = d3.select(svg.node().parentNode); | |
var width = parseInt(svg.style('width')); | |
var height = parseInt(svg.style('height')); | |
var aspect = width / height; | |
svg.attr('viewBox', '0 0 ' + width + ' ' + height) | |
.attr('preserveAspectRatio', 'xMinYMid') | |
.call(resize); | |
d3.select(window).on('resize.' + container.attr('id'), resize); | |
function resize() { | |
var targetWidth = parseInt(container.style('width')); | |
svg.attr('width', targetWidth); | |
svg.attr('height', Math.round(targetWidth / aspect)); | |
} | |
} | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment