{
const svg = d3.select(DOM.svg(width, height)).style("overflow", "visible");
const background = svg.append('rect')
.attr('width', width)
.attr('height', height)
.attr('fill', bgColor)
const createLine = (band, length, color)=> {
return svg.append('line')
.attr('x1',xScale(0)+stationR/2)
.attr('y1',yScale(band))
.attr('x2',xScale(length)-stationR/2)
.attr('y2',yScale(band))
.attr('style',`stroke:${color};stroke-width:${metroLineWidth};`)
.attr('stroke-linecap','round')
}
const lineLen = (lineStations) => {
return lineStations[lineStations.length-1].distance_first + lineStations.length*stationLen
}
const guideLine = svg.append('line')
.attr('x1',xScale(0))
.attr('y1',0)
.attr('x2',xScale(0))
.attr('y2',height)
.attr('style','stroke:white;stroke-width:1;opacity:0.2;visibility:hidden')
const guideText = svg.append('text')
svg.on("mousemove", function () {
const mouse = d3.mouse(this)
if( mouse[0] >= margin.left && mouse[0] <= width-margin.right) {
guideLine.style('visibility','visible').attr('x1', mouse[0]).attr('x2', mouse[0])
guideText.style('visibility','visible')
guideText.text(`${(xScale.invert(mouse[0])/1000).toFixed(2)}km`)
.attr("x", mouse[0]+10)
.attr("y", mouse[1])
.style("font", "11px sans-serif")
.style("fill", 'white')
}else{
guideLine.style('visibility','hidden');
guideText.style('visibility','hidden')
}
})
.on("mouseout", ()=>{
guideLine.style('visibility','hidden')
guideText.style('visibility','hidden')
});
const lines = {};
for (let line of [1,2,4,5]) {
let lineStations = selectStationsByLine(d3.csvParse(stationsData, d3.autoType), line)
lines[line] = createLine(line, lineLen(lineStations), lineColors[line])
}
const tooltip = svg.append("g");
const stations = svg.append('g')
.selectAll('line')
.data(d3.csvParse(stationsData, d3.autoType))
.enter()
.append('circle')
.attr('cx', d => xScale(d.distance_first+(stationLen/2)+(stationLen*(d.order-1)) ))
.attr('cy', d => yScale(d.line) )
.attr('r', stationR )
.attr('fill',stationColor)
.attr('id',d => `station-${d.line}-${d.order}`)
.on("mouseover", d =>
tooltip
.attr("transform", `translate(${xScale(d.distance_first+(stationLen/2)+(stationLen*(d.order-1)) )},${yScale(d.line)-metroLineWidth/2})`)
.call(callout, d.station)
)
.on('mouseout', d => tooltip.call(callout, null))
return svg.node()
}