viewof svg = {
let value='ALL';
const chart = d32
.create("svg")
.attr("class", "star-background")
.attr("width", 650)
.attr("height", height);
chart.append(() => styles);
const axisTime = d32
.axisBottom()
.scale(scaleTime)
.ticks(width > 500 ? 10 : 5)
.tickFormat(d32.timeFormat("%Y"))
.tickSize(5)
.tickPadding(8);
chart
.append("g")
.attr("class", "axis")
.attr("transform", `translate(0, ${height - margin})`)
.call(axisTime);
chart
.append("text")
.attr("x", width / 2 - 100)
.attr("y", height - 10)
.text("出道日期")
.style("opacity", 0.8)
.style("font-size", "12px");
const axisAge = d32
.axisLeft()
.scale(scaleAge)
.ticks(10)
.tickSize(5)
.tickPadding(8);//
chart
.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + margin + ", 0)")
.call(axisAge);
chart
.append("text")//纵坐标
.attr("x", 0)
.attr("y", 0)
.attr("transform", `translate(20,${height / 2 + 100}) rotate(270)`)
.text("出道时的年龄")
.style("opacity", 0.8)
.style("font-size", "12px");
chart.append("g").attr("class", "guideline-group");
const entry = chart.selectAll(null).data(list).join("g");
entry
.append("circle")//画散点图
.attr("cx", (d) => scaleTime(d.missions[0]))
.attr("cy", (d) => scaleAge(d.ageFirstMission))
.attr("r", 2)
.style("fill", (d) => getColor(d))//颜色设置
.style("stroke", "none");
entry
.selectAll(null)
.data((d) => d.missions)
.join("circle")
.attr("cx", function (d) {
return scaleTime(d32.select(this.parentNode).datum().missions[0]);
})
.attr("cy", function (d) {
return scaleAge(d32.select(this.parentNode).datum().ageFirstMission);
})
.attr("r", (d, i) => 4 + i * 4)//调整图标半径大小
.style("stroke", function (d) {
return getColor(d32.select(this.parentNode).datum());
})
.style("stroke-width", 0.5)
.style("fill", "none");
const tooltip = chart.append("g");
tooltip.on("click", (event) => {
event.stopPropagation();
});
let lastPoint = null;
chart.on("click", function (event) {
const node = chart.node();
const point = quadtree.find(...d32.mouse(this));
node.value = value = point.name;
node.dispatchEvent(new CustomEvent("input"));
if (lastPoint !== point) {
if (lastPoint)
entry
.filter((e) => e === lastPoint)
.select("circle")
.attr("r", 2);
removeGuidelines(chart);
entry
.filter((e) => e === point)
.select("circle")
.attr("r", 6);
tooltip
.select("#name")
.text(`${point.name} - ${point.countries.join(", ")}`);
tooltip.select("#description").html(getDescription(point));
addGuideline("horizontal", point);
addGuideline("vertical", point);
}
lastPoint = point;
});
createTooltip(tooltip);
return Object.assign(chart.node(), { value});
}