test
Last active
January 1, 2020 03:17
-
-
Save karen-izuka/4f35d103ac173aa38d3da5f96ce2d28f to your computer and use it in GitHub Desktop.
simple_barchart
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
letter | value | |
---|---|---|
A | .08167 | |
B | .01492 | |
C | .02780 | |
D | .04253 | |
E | .12702 | |
F | .02288 | |
G | .02022 | |
H | .06094 | |
I | .06973 | |
J | .00153 | |
K | .00747 | |
L | .04025 | |
M | .02517 | |
N | .06749 | |
O | .07507 | |
P | .01929 | |
Q | .00098 | |
R | .05987 | |
S | .06333 | |
T | .09056 | |
U | .02758 | |
V | .01037 | |
W | .02465 | |
X | .00150 | |
Y | .01971 | |
Z | .00074 |
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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Simple Bar Chart</title> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<h1>Simple Bar Chart</h1> | |
<div id="chart"></div> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<script src="index.js"></script> | |
</body> | |
</html> |
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
//load data | |
const load = async () => { | |
const data = await d3.csv('data.csv', ({letter, value}) => ({ | |
letter: letter, value: +value | |
})); | |
chart(data); | |
} | |
//chart function | |
const chart = (data) => { | |
const margin = {top: 25, right: 25, bottom: 25, left: 25}; | |
const width = 1250 - margin.left - margin.right; | |
const height = 600 - margin.top - margin.bottom; | |
const format = d3.format('.1%'); | |
const 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})`); | |
const x = d3.scaleBand() | |
.domain(data.map(d => d.letter)) | |
.range([0, width]) | |
.padding(0.1); | |
const y = d3.scaleLinear() | |
.domain([0, d3.max(data, d => d.value)]) | |
.range([height, 0]); | |
const xAxis = g => g | |
.call(d3.axisBottom(x).tickSizeOuter(0)); | |
const bar = svg.selectAll('.rect') | |
.data(data) | |
.join('rect') | |
.attr('class', 'rect') | |
.attr('x', d => x(d.letter)) | |
.attr('y', d => y(d.value)) | |
.attr('width', x.bandwidth()) | |
.attr('height', d => y(0)-y(d.value)); | |
const label = svg.selectAll('.label') | |
.data(data) | |
.join('text') | |
.text(d => format(d.value)) | |
.attr('class', 'label') | |
.attr('x', d => x(d.letter) + x.bandwidth()/2) | |
.attr('y', d => y(d.value) - 10) | |
.attr('text-anchor', 'middle'); | |
const gx = svg.append('g') | |
.attr('class', 'axis') | |
.attr('transform', `translate(0, ${height})`) | |
.call(xAxis); | |
} | |
load(); |
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
h1 { | |
font-family: Arial, Helvetica, sans-serif; | |
font-size: 24px; | |
text-align: center; | |
text-rendering: optimizeLegibility; | |
} | |
svg { | |
display: block; | |
margin: auto; | |
} | |
.axis font { | |
font-family: Arial, Helvetica, sans-serif; | |
font-size: 12px; | |
text-rendering: optimizeLegibility; | |
} | |
.label { | |
font-family: Arial, Helvetica, sans-serif; | |
font-size: 12px; | |
text-rendering: optimizeLegibility; | |
} | |
.rect { | |
fill: #e1bee7; | |
shape-rendering: geometricPrecision; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment