|
<!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; } |
|
svg { |
|
width: 100%; |
|
height: 100%; |
|
} |
|
</style> |
|
</head> |
|
|
|
<body> |
|
<svg></svg> |
|
<script> |
|
var city = 'Austin'; |
|
var width = 800; |
|
var height = 300; |
|
var margin = {top: 20, bottom: 20, left: 30, right: 20}; |
|
|
|
// dataset of city temperatures across time |
|
d3.tsv('data.tsv', (err, data) => { |
|
// clean the data |
|
data.forEach(d => { |
|
d.date = new Date(d3.timeParse("%Y%m%d")(d.date)) |
|
d.temp = parseFloat(d[city]) |
|
}); |
|
|
|
// scales |
|
const tempScale = |
|
d3.scaleLinear() |
|
.domain(d3.extent(data,d=>d.temp)) |
|
.range([height- margin.bottom, margin.top]); |
|
|
|
const timeScale = d3.scaleTime() |
|
.domain(d3.extent(data, d=>d.date)) |
|
.range([margin.left,width-margin.right]); |
|
|
|
|
|
// create the rectangles |
|
|
|
d3.select('svg') |
|
.selectAll('rect') |
|
.data(data) |
|
.enter() |
|
.append('rect') |
|
.attr('data-i',(d,i)=>i) |
|
.attr('data-temp',d=>d.temp) |
|
.attr('data-date',d=>d.date) |
|
.attr('x', d => timeScale(d.date)) |
|
.attr('y', d => height - margin.bottom - tempScale(d.temp)) |
|
.attr('height', d=>tempScale(d.temp)) |
|
.attr('width',(800-margin.left-margin.right)/data.length) |
|
.attr('fill','#911') |
|
|
|
// add the axes |
|
const xAxis = d3.axisLeft(tempScale) |
|
d3.select('svg') |
|
.append("g") |
|
.attr('transform',`translate(${margin.left-5},0)`) |
|
.call(xAxis); |
|
|
|
const yAxis = d3.axisBottom(timeScale); |
|
|
|
d3.select("svg") |
|
.append("g") |
|
.attr('transform', |
|
`translate(0,${5+height-margin.bottom})`) |
|
.call(yAxis); |
|
}); |
|
</script> |
|
</body> |