Built with blockbuilder.org
forked from DondeDijeDigo's block: Temperaturas Nueva York
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.barras div {
margin: 2px;
padding: 10px;
text-align: right;
color: white;
}
</style>
</head>
<body>
<h1>Temperaturas Nueva York</h1>
<script>
const rectWidth = 41;
const city = "New York";
//Altura y anchura con márgenes
const totalWidth = 800;
const totalHeight = 300;
var margin = {top: 20, right: 50, bottom: 20, left: 40};
var width = totalWidth - margin.left - margin.right;
var height = totalHeight - margin.top - margin.bottom;
var svg = d3.select("body")
.append("svg")
.attr("width", totalWidth)
.attr("height", totalHeight)
var chart = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
function typeClean(row){
r = {};
r.temperature = +row[city];//y
fecha = d3.timeParse("%Y%m%d")(row.date);
r.date = new Date(fecha);//x
// if (r.temperature <60) {
// return r;
// }
return r;
}
d3.tsv("data.tsv",
row => typeClean(row)).then(datos =>
{
//Scales
const xExtent = d3.extent(datos, d => d.date)
const xScale = d3.scaleTime()
.domain(xExtent)
.range([0, width])
.nice()
const yMax = d3.max(datos, d => d.temperature)
const yScale = d3.scaleLinear()
.domain([0, yMax])
.range([height, 0])
.nice()
const heightScale = d3.scaleLinear()
.domain([0, yMax])
.range([0, height])
.nice()
const xAxis = d3.axisBottom()
.scale(xScale)
.tickFormat(d3.timeFormat('%b %Y'))
const yAxis = d3.axisLeft()
.scale(yScale)
chart.append('g')
.attr("transform", `translate(0,${height})`)
.call(xAxis)
chart.append('g')
.call(yAxis)
chart.selectAll("circle")
.data(datos)
.enter().append("circle")
.attr("cx", d => xScale(d.date))
.attr("cy", d => yScale(d.temperature))
.attr("r",5)
.attr("width",2)
.attr("height",d => heightScale(d.temperature))
.attr("fill", "green")
.attr("stroke", "#FFF")
})
</script>
</body>
https://d3js.org/d3.v5.min.js