Published
Edited
Dec 2, 2019
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: 30, bottom: 30, left: 40})
Insert cell
x = d3.scaleLinear()

.domain(d3.extent(data, d => d.Hora))
//.domain([new Date(2010, 7, 1), new Date(2012, 7, 1)])
.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(24)
.tickSizeOuter(10)
// .ticks(d3.timeMonth.every(3))
// .tickFormat(d => d <= d3.timeYear(d) ? d.getFullYear() : null))
//.call(g => g.select(".domain")
// .remove()
)
//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())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y))
Insert cell
callout = (g, value) => {
if (!value) return g.style("display", "none");

g
.style("display", null)
.style("pointer-events", "all")
.style("font", "10px sans-serif");

const path = g.selectAll("path")
.data([null])
.join("path")
.attr("fill", "white")
.attr("stroke", "black");

const text = g.selectAll("text")
.data([null])
.join("text")
.call(text => text
.selectAll("tspan")
.data((value + "").split(/\n/))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => `${i * 1.1}em`)
.style("font-weight", (_, i) => i ? null : "bold")
.text(d => d));

const {x, y, width: w, height: h} = text.node().getBBox();

text.attr("transform", `translate(${-w / 2},${15 - y})`);
path.attr("d", `M${-w / 2 - 10},5H-5l5,-5l5,5H${w / 2 + 10}v${h + 20}h-${w + 20}z`);
}


Insert cell
bisect = {
const bisect = d3.bisector(d => d.date).left;
return mx => {
const date = x.invert(mx);
const index = bisect(data, date, 1);
const a = data[index - 1];
const b = data[index];
return date - a.date > b.date - date ? b : a;
};
}
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
data = Object.assign((d3.csvParse(await FileAttachment("tabla_horas_transpuesta_6.csv").text(), d3.autoType)).map(({Hora, Recurrencia}) => ({Hora:Hora, Recurrencia})), {y: "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