This bar chart was inspired by a something I learning about in nutrition class 101 back in 2012. Here's a reputible source to back me up http://www.nutristrategy.com/nutrition/calories.htm
Built with blockbuilder.org
forked from curran's block: Extremist Murders in the US
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Bitter|Rubik+Mono+One" rel="stylesheet">
<style>
body {
margin:0px;
}
.title {
font-size: 39pt;
font-family: 'Bitter';
fill: #7B7B7B;
}
.label {
font-size: 18pt;
font-family: 'Rubik Mono One';
alignment-baseline: middle;
fill: white;
}
.number {
font-size: 82pt;
font-family: 'Rubik Mono One';
alignment-baseline: middle;
fill: #FFFFFF;
}
.bar {
fill: #7B7B7B;
}
</style>
</head>
<body>
<script>
const width = 960;
const height = 500;
const margin = {
left: 50,
right: 200,
top: 80,
bottom: 50
}
const svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append('text')
.attr('class', 'title')
.attr('x', margin.left)
.attr('y', 80)
.text('Calories from one gram of...');
const g = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
const data = [
{
name: "Fat",
value: 9
},
{
name: "Alcohol",
value: 7
},
{
name: "Protein",
value: 4
},
{
name: "Carbs",
value: 4
}
];
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const xValue = d => d.value;
const yValue = d => d.name;
const xScale = d3.scaleLinear()
.domain([0, d3.max(data, xValue)])
.range([0, innerWidth]);
const yScale = d3.scaleBand()
.domain(data.map(yValue))
.range([innerHeight, 0])
.padding(0.1);
const groups = g.selectAll('g').data(data);
const groupsEnter = groups
.enter().append('g')
.attr('transform', d => `translate(0, ${yScale(yValue(d))})`);
groupsEnter
.append('rect')
.attr('class', 'bar')
.attr('width', d => xScale(xValue(d)))
.attr('height', yScale.bandwidth());
groupsEnter
.append('text')
.attr('class', 'label')
.attr('x', 5)
.attr('y', yScale.bandwidth() / 1.18)
.text(yValue);
groupsEnter
.append('text')
.attr('class', 'number')
.attr('x', d => xScale(xValue(d)) - 87)
.attr('y', yScale.bandwidth() / 2)
.text(d => xValue(d));
</script>
</body>
https://d3js.org/d3.v4.min.js