Published
Edited
Nov 27, 2018
2 forks
7 stars
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));
svg.append("g")
.call(xAxis);

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

const path = svg.append("g")
.attr("fill", "none")
.selectAll("path")
.data(data1.series)
.enter().append("path")
.style("mix-blend-mode", "multiply")
.attr("d", d => line(d.values));
path.attr("stroke", (d,i)=>color(i))
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")

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 years= ['2012','2013','2014','2015','2016','2017','2018'];
const ym = y.invert(d3.event.layerY);
const xm = x.invert(d3.event.layerX);
var month = xm.getMonth();
var day = xm.getDay();
var dd = new Date(2020,month,day)
var ddd = new Date(dd);
ddd.setDate(dd.getDate()+1);
var d_day = ddd.getDay();
var d_month = ddd.getMonth();
day = dd.getDay();
//find if this point is on one line
var min = Infinity;
var idx;
var final_x;
var final_y;
for (var i =0; i< years.length ;i++)
{
for (var j = 0 ;j <data1.series[i].values.length;j++)
{
var this_day = data1.series[i].values[j]['date'].getDay();
var this_month = data1.series[i].values[j]['date'].getMonth();
if (this_month=== month && this_day === day || this_month === d_month && this_day === d_day)
{
if (Math.abs(data1.series[i].values[j]['price']-ym) < min)
{
min = Math.abs(data1.series[i].values[j]['price']-ym);
final_x = data1.series[i].values[j]['date'];
final_y = data1.series[i].values[j]['price'];
idx = i;
}
}
}
}
const s = data1.series[idx];
path.attr("stroke", d => d === s ? color(idx) : "#ddd").filter(d => d === s).raise();
dot.attr("transform", `translate(${x(final_x)},${y(final_y)})`);
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", null);
dot.attr("display", "none");
}
}
Insert cell
bisectDate = d3.bisector(function(d, x) { return d.date - x; }).right
Insert cell
height = 600
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 40})
Insert cell
x = d3.scaleTime()
.domain([mindate,maxdate])
.range([margin.left, width - margin.right])
Insert cell
mindate = new Date(2020,0,1)
Insert cell
maxdate = new Date(2020,11,31)
Insert cell
color = d3.scaleLinear()
.domain([1, 5])
.range(d3.schemeDark2)
Insert cell
y = d3.scaleLinear()
.domain([0, 210]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
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(data1.y))
Insert cell
line = d3.line()
//.defined(d => !isNaN(d))
.x(d => x(d.date))
.y(d => y(d.price))
Insert cell
Insert cell
testdata = await d3.tsv("https://gist.githubusercontent.com"
+ "/mbostock/8033015/raw"
+ "/01e8225d4a65aca6c759fe4b8c77179f446c5815/unemployment.tsv")
Insert cell
data = {
const data = await d3.tsv("https://gist.githubusercontent.com"
+ "/mbostock/8033015/raw"
+ "/01e8225d4a65aca6c759fe4b8c77179f446c5815/unemployment.tsv", (d, i, columns) => {
return {
name: d.name.replace(/, ([\w-]+).*/, " $1"),
values: columns.slice(1).map(k => +d[k])
};
});
return {
y: "% Unemployment",
series: data,
dates: data.columns.slice(1).map(d3.timeParse("%Y-%m"))
};
}
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more