Published
Edited
Nov 9, 2020
Insert cell
Insert cell
Insert cell
function hover(svg, path) {
if ("ontouchstart" in document) svg
.style("-webkit-tap-highlight-color", "transparent")
.on("touchmove", moved2)
.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", 10)
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 3);

dot.append("text")
.attr("font-family", "Montserrat")
.attr("font-size", 18)
.attr("fill", "white")
.attr("text-anchor", "middle")
.attr("y", -15)
.attr("class", "hoverLabel");

function moved(event) {
event.preventDefault();
const pointer = d3.pointer(event, this);
console.log('pointer', pointer);
const xm = x.invert(pointer[0]);
const ym = y.invert(pointer[1]);
console.log('xm', xm);
console.log('ym', ym);
const i = d3.bisectCenter(data.gameweek, xm);
const s = d3.least(data.series, d => Math.abs(d.values[i] - ym));
console.log('ii', i);
console.log('ss', s);
path.attr("stroke", d => d === s ? colors2[0][s.name] : "#1C2124").filter(d => d === s).raise();
dot.attr("transform", `translate(${x(data.gameweek[i])},${y(s.values[i])})`);
dot.select("text").text(s.name);
}
function moved2(event) {
event.preventDefault();
const pointer = d3.pointers(event, this);
console.log('pointer', pointer);
const xm = x.invert(pointer[0][0]);
const ym = y.invert(pointer[0][1]);
console.log('xm', xm);
console.log('ym', ym);
const i = d3.bisectCenter(data.gameweek, xm);
const s = d3.least(data.series, d => Math.abs(d.values[i] - ym));
console.log('ii', i);
console.log('ss', s);
path.attr("stroke", d => d === s ? colors2[0][s.name] : "#1C2124").filter(d => d === s).raise();
dot.attr("transform", `translate(${x(data.gameweek[i])},${y(s.values[i])})`);
dot.select("text").text(s.name).attr("transform", `translate(0,-40)`);
}

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

function left() {
path.attr("stroke", (d, i) => colors2[0][d.name]);
dot.attr("display", "none");
}
}
Insert cell
w = width > 749? width*0.8 : width*1.1
Insert cell
Insert cell
colors2 = [
{"Arsenal FC": "#F00000", "Leicester City": "#0B56A4", "Chelsea FC": "#001389", "Newcastle United": "#000000", "Wolverhampton Wanderers": "#FEB913", "Liverpool FC": "#E41B23", "Crystal Palace": "#014A97", "Everton FC": "#00369D", "Aston Villa": "#670D36", "Burnley FC": "#70193D", "Manchester City": "#98C5EA", "Manchester United": "#D20223", "Leeds United": "#F7DA03", "Southampton FC": "#FF0028", "Tottenham Hotspur": "#FBFDFF", "Brighton & Hove Albion": "#004B95", "Sheffield United": "#BB0000", "West Ham United": "#7D2B3A", "Fulham FC": "#000000", "West Bromwich Albion": "#002F69"}
]
Insert cell
colors = ["red", "DodgerBlue", "blue", "black", "orange", "FireBrick", "Indigo", "CornflowerBlue", "Marron", "#70193D", "SkyBlue", "#D20223", "#1D4189", "#FF0028", "#FBFDFF", "#004B95", "#BB0000", "#7D2B3A", "#000000", {"West Bromwich Albion": "#002F69"}]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data.gameweek))
.range([margin.left, w - margin.right])
Insert cell
Insert cell
line = d3.line().curve(d3["curveCardinal"])
.defined(d => !isNaN(d))
.x((d, i) => x(data.gameweek[i]))
.y(d => y(d))
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height +10 - margin.bottom})`)
.call(d3.axisBottom(x).ticks(6))
.call(g => g.select(".domain").remove())
// Add label
.append('text')
.attr('class', 'axis-label')
.text("Gameweek")
.attr('x', margin.left + (w - margin.left - margin.right) / 2)
.attr('y', 50) // Relative to the x axis.
Insert cell
yAxis = g => g
.attr("transform", `translate(${w},0)`)
.call(d3.axisRight(y).ticks(20))
.call(g => g.select(".domain").remove())

Insert cell
d3.extent(data.gameweek)
Insert cell
function circlo (d) {
console.log("dum d",d)
return d
}
Insert cell
line(1)
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