Built with blockbuilder.org
forked from DondeDijeDigo's block: Temperaturas Nueva York
forked from DondeDijeDigo's block: Temperaturas Nueva York - line chart
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; }
.axisLabel {}
</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: 20, bottom: 50, left: 50};
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)
.append("text")
.text("Mes")
.attr("x", width/2)
.attr("y", 50)
.attr("text-anchor", "middle")
.attr("fill","black")
.attr("fill","black")
.attr("class", "axiLabel")
chart.append('g')
.call(yAxis)
.append("text")
.text("Temperatura")
.attr("x", -height/2)
.attr("y", -30)
.attr("text-anchor", "middle")
.attr("fill","black")
.attr("class", "axiLabel")
.attr("transform","rotate(-90)")
var line = d3.line()
.x(d => xScale(d.date))
.y(d => yScale(d.temperature))
chart.append("path")
.attr("d",line(datos))
.attr("fill", "None")
.attr("stroke", "blue")
.attr("stroke-width", 0.5)
})
</script>
</body>
https://d3js.org/d3.v5.min.js