{
const indexOf1 = linedata.map(e => e.Mechanic).indexOf(line1);
const indexOf2 = linedata.map(e => e.Mechanic).indexOf(line2);
const totalWidth = width;
const totalHeight = 400;
const margin = ({top: 30, bottom: 45, left: 162, right: 20});
const visWidth = totalWidth - margin.left - margin.right;
const visHeight = totalHeight - margin.top - margin.bottom;
const svg = d3.create('svg')
.attr('width', totalWidth).attr('height', totalHeight)
.on('mouseover', mouseEnter)
.on("mousemove", mouseMove)
.on('mouseleave', mouseLeave);
const x = d3.scaleLinear()
.domain(domains.x)
.range([0, visWidth]);
const y = d3.scaleLinear()
.domain(domains.y).nice()
.range([visHeight, 0]);
const g = svg.append("g")
.attr('transform', `translate(${margin.left}, ${margin.top})`);
// set up axes
const xAxis = d3.axisBottom(x).tickFormat(d3.format("d"))
const yAxis = d3.axisLeft(y);
// set up y-axis and label (no label)
g.append("g")
.call(yAxis)
.append("text")
.attr("x", -40)
.attr("y", visHeight/2)
.attr("fill", "black")
.attr("dominant-baseline", "middle")
.text("Number of Games");
// set up x-axis and label
g.append('g')
.attr('transform', `translate(0, ${visHeight})`)
.call(xAxis)
.append('text')
.attr('fill', 'black')
.attr('x', visWidth / 2)
.attr('y', 40)
.text("Game's Publishing Year");
// set up path generator
const line = d3.line()
.x(d => x(d.yearpublished))
.y(d => y(d.count_per_mechanic));
// draw mark for each element in data
g.append('path')
.attr('stroke-width', 3)
.attr('fill', 'none')
.attr('d', line(linedata[indexOf1].values))
.attr('stroke', color(line1));
g.append('path')
.attr('stroke-width', 3)
.attr('fill', 'none')
.attr('d', line(linedata[indexOf2].values))
.attr('stroke', color(line2));
// throw a title on there
svg.append('text')
.attr('transform', `translate(${totalWidth/2}, 15)`)
.style('text-anchor', 'middle')
.text('Use of Alternative Group Mechanics Has Grown');
// create tooltip
const tooltip = svg.append('g')
.attr('visibility', 'hidden');
const tooltipHeight = 16;
// add a rectangle to the tooltip to serve as a background
const tooltipRect = tooltip.append('rect')
.attr('fill', 'white')
.attr('rx', 2)
.attr('y', -0.5*tooltipHeight)
.attr('x', 10)
.attr('height', tooltipHeight)
.style("stroke", "black")
.style("stroke-width", 1);
// add a text element to the tooltip to contain the label
const tooltipText = tooltip.append('text')
.attr('fill', 'royalblue')
.attr('font-family', 'sans-serif')
.attr('font-size', 12)
.attr('y', -0.5*tooltipHeight + 4) // offset it from the edge of the rectangle
.attr('x', 13) // offset it from the edge of the rectangle
.attr('dominant-baseline', 'hanging')
// handle entering a shape
function mouseEnter(event, d) {
// move the tooltip to the mouse position
// and make the tooltip visible
var coords = d3.pointer(event);
const xPos = coords[0];
const yPos = coords[1];
tooltip.attr('transform', `translate(${xPos},${yPos})`)
.attr('visibility', 'visible');
}
function mouseMove(event, d) {
// Recall we have attached the tooltip and events to the svg, so calcs must account for margins
console.log('Inverted pointer location: ' + x.invert(d3.pointer(event)[0]-margin.left));
var loc = x.invert(d3.pointer(event)[0]-margin.left);
// Bisect to get the closest year index...
const i = d3.bisectCenter(years, x.invert(d3.pointer(event)[0]-margin.left));
console.log('Mouse move returns year ' + (domains.x[0]+i));
console.log(x(loc));
/////////////////////////////////////////////////////////////////////
// TODO: Reference the y-values of the active lines at that x index.
// Calculate tooltip position using the found values and x and y scales.
// Transform tooltip to location.
// Update text to values.
// Placeholder below until done.
tooltipText.text(domains.x[0]+i);
const labelWidth = tooltipText.node().getComputedTextLength();
tooltipRect.attr('width', labelWidth + 6);
var coords = d3.pointer(event);
const xPos = coords[0];
const yPos = coords[1];
tooltip.attr('transform', `translate(${xPos},${yPos})`)
.attr('visibility', 'visible');
}
// handle leaving the shape
function mouseLeave(event, d) {
// make the tooltip invisible
tooltip
.attr('visibility', 'hidden');
}
return svg.node();
}