Published
Edited
Dec 3, 2019
Fork of Line Chart
1 fork
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#FF3FAA")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
return svg.node();
}
Insert cell
height = 400
Insert cell
margin = ({top: 20, right: 40, bottom: 30, left: 40})
Insert cell
md`Usamos una escala lineal para las horas, ya que esta dimensión está compuesta de números enteros`
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.Hora))
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.Recurrencia)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.ticks(data.length)
.tickSizeOuter(10)
.tickValues(data.map(d=>{return d.Hora}))
)
//seleccionamos el último 'tick' del eje X, y lo clonamos
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 10) //margen izquierda de 10px
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text('Hora')) //La etiqueta al final de la escala
//https://observablehq.com/@d3/axis-ticks?collection=@d3/d3-axis
///https://observablehq.com/@d3/styled-axes
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
//seleccionamos el último 'tick' del eje Y, y lo clonamos
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)//margen izquierda de 3px
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text('Recurrencia')) // La etiqueta al final de la escala
Insert cell
line = d3.line()
.curve(d3.curveLinear)
.defined(d => !isNaN(d.Recurrencia))
.x(d => x(d.Hora))
.y(d => y(d.Recurrencia))
Insert cell
Insert cell
data = Object.assign((d3.csvParse(await FileAttachment("tabla_horas_transpuesta_6.csv").text(), d3.autoType)).map(({Hora, Recurrencia}) => ({Hora:Hora, Recurrencia})))
Insert cell
d3 = require("d3@5", "d3-scale@^3.0.1")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more