Published
Edited
Mar 25, 2019
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart_line = {
const svg = d3.select(DOM.svg(width, height_line));

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

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

const path = svg.append("g")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.selectAll("path")
.data(line_data.series)
.join("path")
.style("mix-blend-mode", "multiply")
.attr("d", d => line(d.values))
.attr("stroke", function(d, i){return z_line(i);} )
;

svg.call(hover, path);

return svg.node();
}
Insert cell
function hover(svg, path) {
svg
.style("position", "relative");
if ("ontouchstart" in document) svg
.style("-webkit-tap-highlight-color", "transparent")
.on("touchmove", moved)
.on("touchstart", entered)
.on("touchend", left)
else svg
.on("mousemove", moved)
.on("mouseenter", entered)
.on("mouseleave", left);

const dot = svg.append("g")
.attr("display", "none");

dot.append("circle")
.attr("r", 2.5);

dot.append("text")
.style("font", "10px sans-serif")
.attr("text-anchor", "middle")
.attr("y", -8);

function moved() {
d3.event.preventDefault();
const ym = y_line.invert(d3.event.layerY);
const xm = x_line.invert(d3.event.layerX);
const i1 = d3.bisectLeft(line_data.dates, xm, 1);
const i0 = i1 - 1;
const i = xm - line_data.dates[i0] > line_data.dates[i1] - xm ? i1 : i0;
const s = line_data.series.reduce((a, b) => Math.abs(a.values[i] - ym) < Math.abs(b.values[i] - ym) ? a : b);
path.attr("stroke", d => d === s ? null : "#ddd").filter(d => d === s).raise();
dot.attr("transform", `translate(${x_line(line_data.dates[i])},${y_line(s.values[i])})`);
dot.select("text").text(s.name);
}

function entered() {
path.style("mix-blend-mode", null).attr("stroke", "#ddd");
dot.attr("display", null);
}

function left() {
path.style("mix-blend-mode", "multiply")
.attr("stroke", function(d, i){return z_line(i);} )
dot.attr("display", "none");
}
}
Insert cell
Insert cell
margin_line = ({top: 20, right: 20, bottom: 30, left: 30})
Insert cell
Insert cell
Insert cell
xAxis_line = g => g
.attr("transform", `translate(0,${height_line - margin_line.bottom})`)
.call(d3.axisBottom(x_line).ticks(width / 80).tickSizeOuter(0))
Insert cell
yAxis_line = g => g
.attr("transform", `translate(${margin_line.left},0)`)
.call(d3.axisLeft(y_line))
.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(line_data.y))
Insert cell
line = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x_line(line_data.dates[i]))
.y(d => y_line(d))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
month_data = {
var to_return = [];
if (month != null){
var idx = ["Jan","Feb","Mar","Apr","May","Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].indexOf(month);
var i;
for(i = 0; i < incident.length; i++){
to_return.push({
name: incident[i],
value: transformed_data[idx][incident[i]]
});
}
}

return to_return;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
width = 975
Insert cell
height = width
Insert cell
innerRadius = 180
Insert cell
outerRadius = Math.min(width, height) / 2
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
Insert cell
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