Skip to content

Instantly share code, notes, and snippets.

@GitNoise
Last active January 23, 2020 20:15
Show Gist options
  • Save GitNoise/9cf7f63a1451ef885c56a5160664bf05 to your computer and use it in GitHub Desktop.
Save GitNoise/9cf7f63a1451ef885c56a5160664bf05 to your computer and use it in GitHub Desktop.
multiarea with threshold
license: mit
Free sugars intake (% of total energy) in all age groups for all paired years of the NDNS Rolling Programme  (2008/09 - 2009/10) (2010/11 - 2011/12) (2012/13 - 2013/14) (2014/15-2015/16)
Children 1.5-3 years 12.1 13 12.8 11.3
Children 4-10 years 14.7 15.5 14 13.5
Children 11-18 years 15.9 15.8 15.8 14.1
Adults 19-64 years 11.8 11.7 12.1 11.1
Men 19-64 years 12 12.3 12.5 11.1
Women 19-64 years 11.6 11.2 11.6 11.2
Adults 65 years and over 10.9 11.4 10.8 11.2
Men 65 years and over 9.7 11 11.6 11.8
Women 65 years and over 10.1 11.3 9.5 10.4
Adults 65-74 years 9.9 11.2 10.5 11
Men 65-74 years 9.7 11 11.6 11.8
Women 65-74 years 10.1 11.3 9.5 10.4
Adults 75 years and over 12.2 11.8 11.3 11.3
Men 75 years and over 11.8 12.3 11.5 12.5
Women 75 years and over 12.4 11.2 11.2 10.4
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body {
margin:auto;
padding: 25px;}
.data path {
stroke: black;
stroke-width: 1.5px;
fill-opacity: 0.1;
}
</style>
</head>
<body>
<svg id="chart" width="600" height="300"></svg>
<script>
d3.csv("2020W3.csv").then(d => chart(d))
const chart = data => {
const threshold = 11;
const margin = {top: 15, right: 35, bottom: 15, left: 50};
const keys = data.columns.slice(1);
const result = data
.filter(d => d[data.columns[0]].indexOf('Adults') > -1)
.map( d => ({
id: d[data.columns[0]],
values: keys.map(key => ({year: key, value: +d[key]}))
}));
const extent = {
min: +d3.min(result, row => d3.min(row.values, v => v.value)),
max: +d3.max(result, row => d3.max(row.values, v => v.value)),
}
const svg = d3.select("#chart"),
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
const xScale = d3.scalePoint()
.range([margin.left, width - margin.right])
.domain(keys);
const yScale = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
.domain([0, extent.max])
var line = d3.area()
.y0(yScale.range()[0])
.y1(d => yScale(d.value))
.x(d => xScale(d.year));
svg.append("g")
.classed("axis", true)
.attr("transform", "translate(0," + (height - margin.bottom) + ")")
.call(d3.axisBottom(xScale));
svg.append("g")
.classed("axis", true)
.attr("transform", "translate(" + margin.left + ",0)")
.call(d3.axisLeft(yScale));
svg.append('g')
.classed("data", true)
.selectAll("path")
.data(result)
.join(enter =>
enter.append('path')
.attr("d", d => line(d.values))
.style('fill', 'url(#threshold)')
)
drawThreshold(svg, yScale, xScale, height, threshold);
}
const drawThreshold = (svg, yScale, xScale, height, threshold) => {
const group = svg.append('g').classed('threshold', true);
svg.append('defs').append("linearGradient")
.attr("id", 'threshold')
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", 0)
.attr("y2", height)
.selectAll("stop")
.data([
{offset: 0, color: "blue"},
{offset: yScale(threshold) / height, color: "blue"},
{offset: yScale(threshold) / height, color: "red"},
{offset: 1, color: "red"}
])
.join("stop")
.attr("offset", d => d.offset)
.attr("stop-color", d => d.color);
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment