chart = {
const innerHeight = height * data.names.length;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, innerHeight + margin.top + margin.bottom + 60])
.style("font", "10px sans-serif");
svg.append("g")
.attr("transform", "translate(60,610)")
.append(() => legend({color, title: "Word Frequency per year", width: 260}));
svg.append("g")
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(null, "d"))
.call(g => g.select(".domain").remove());
svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).tickSize(0))
.call(g => g.select(".domain").remove());
var tip = d3.select("body").append("tip")
.attr("class", "tooltip")
.style("padding", "300px")
.style("opacity", 0)
.style("width", "200px")
.style("height", "100px")
.style("fontsize", "200px")
.style("color", "red")
.style("position", "absolute")
.html("Democrat: 5")
const row = svg.append("g")
.selectAll("g")
.data(data.values)
.join("g")
.attr("transform", (d, i) => `translate(0,${y(data.names[i])})`);
row.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", (d, i) => x(data.years[i]) + 1)
.attr("width", (d, i) => 4)
.attr("height", y.bandwidth() - 1)
.attr("fill", d => isNaN(d) ? "#eee" : d === 0 ? "#e8e8e8" : color(d))
.on("mouseover", function(d) {
svg.selectAll("rect").style("opacity", 0.5);
d3.select(this).style("opacity", 1);
console.log("mouseover")
tip.style("opacity", d !== 0 ? 1 : 0)
.style("left", "5px")
.style("top", "5px");
console.log(tip.style)
})
.on("mouseout", function(d) {
console.log("mouseout")
tip.style("opacity", 0)
svg.selectAll("rect").style("opacity", 1);
})
.append("title");
svg.selectAll("circle")
.data(annotations)
.join("circle")
.attr("stroke", "black")
.attr("fill", "white")
.attr("cx", function(d, i) {
return x(d.year + .5);
})
.attr("cy", 570)
.attr("r", 4)
.on("mouseover", function(d,i) {
d3.select(this)
.attr("fill", "black");
svg.append("text")
.text(d.details)
.attr("id", "y" + d.year)
.attr("x", x(d.year-7))
.attr("y", 600);
svg.append("line")
.attr("id", "l" + d.year)
.attr("x1", x(d.year)+1)
.attr("y1", margin.top)
.attr("x2", x(d.year)+1)
.attr("y2", innerHeight + margin.top)
.attr("stroke-width", 2)
.attr("stroke", "white");
})
.on("mouseout", function(d,i) {
d3.select(this)
.attr("fill", "white");
svg.select("#y" + d.year).remove()
svg.select("#l" + d.year).remove();
});
return svg.node();
}