chart = {
replay;
const width = 3000;
const height = 600;
const marginTop = 20;
const marginRight = 30;
const marginBottom = 30;
const marginLeft = 40;
const x = d3.scaleLinear()
.domain([7900,90000]).nice()
.range([marginLeft, width - marginRight]);
const y = d3.scaleLinear()
.domain([0,250]).nice()
.range([height - marginBottom, marginTop]);
const line = d3.line()
.curve(d3.curveCatmullRom)
.x(d => x(d.miles))
.y(d => y(d.gas));
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
const l = length(line(driving));
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x)
.tickValues([7927.39079512154, 17452.6259671028, 28949.9240890961, 31372.4674383127, 58193.0031719582, 62412.7297059271, 78066.1020967475, 84942.2615386223]).tickFormat((d, i) => ['Jomsom', 'Kāgbeni', 'Chhusang', 'Chele', 'Ghami', 'Dhakmar', 'Lo Manthang', 'Chhoser'][i]))
//.call(g => g.select(".domain").remove())// 移除x轴的轴线
.call(g => g.selectAll(".tick line").clone()
.attr("y2", -height)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", width - 4)
.attr("y", -4)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.attr("fill", "currentColor")
.text("Jomsom to LoManthang"));
// 画y轴的
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(3).tickValues([80, 179, 236]))
//.call(g => g.select(".domain").remove()) // 移除y轴的轴线
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width)
.attr("stroke-opacity", 0.1))
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text());
svg.append("path")
.datum(driving)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 2.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-dasharray", `0,${l}`)
.attr("d", line)
.transition()
.duration(5000)
.ease(d3.easeLinear)
.attr("stroke-dasharray", `${l},${l}`);
svg.append("g")
.attr("fill", "black")
//.attr("stroke", "black")设置圆是黑色描边
//.attr("stroke-width", 2)
.selectAll("circle")
.data(driving)
.join("circle")
.attr("cx", d => x(d.miles))
.attr("cy", d => y(d.gas))
.attr("r", 3); //圆圈半径设置三像素
// 设置点标签的
/* // 开始注释
const label = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll()
.data(driving)
.join("text")
.attr("transform", d => `translate(${x(d.miles)},${y(d.gas)})`)
.attr("fill-opacity", 0)
.text(d => d.year)
.attr("stroke", "white")
.attr("paint-order", "stroke")
.attr("fill", "currentColor")
.each(function(d) {
const t = d3.select(this);
switch (d.side) {
case "top": t.attr("text-anchor", "middle").attr("dy", "-0.7em"); break;
case "right": t.attr("dx", "0.5em").attr("dy", "0.32em").attr("text-anchor", "start"); break;
case "bottom": t.attr("text-anchor", "middle").attr("dy", "1.4em"); break;
case "left": t.attr("dx", "-0.5em").attr("dy", "0.32em").attr("text-anchor", "end"); break;
}
});
label.transition()
.delay((d, i) => length(line(driving.slice(0, i + 1))) / l * (5000 - 125))
.attr("fill-opacity", 1);
*/ // 结束注释
return svg.node();
}