chart_province = () => {
var margin = { top: 10, right: 30, bottom: 30, left: 60 },
width = 700 - margin.left - margin.right,
height = 550 - margin.top - margin.bottom;
var current_selected;
var last_selected;
const svg = d3
.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("viewBox", [0, 0, 960, 700])
.style("background-color", "#fafafa");
svg
.append("g")
.attr("transform", "translate(20, 10)")
.append(() =>
legend({
color,
title: "中车任务可视化图表(省份)",
ticks: 0
})
);
const g = svg
.append("g")
.selectAll(".provinces")
.data(provinceFeatures)
.join("path")
.attr("class", "provinces")
.attr("d", pathGenerator)
.attr("fill", (d) => {
let cases = d.properties.count;
return cases === "NA" || cases === undefined ? "#636363" : color(+cases);
})
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", "0.7px")
// ********** new stuff starts here **********
.on("mousemove", mouseMove)
.on("mouseleave", mouseLeave)
.on("click", mouseClick)
.append("title")
.html(
(d) => `省份区域:${d.properties.ProvCH}
任务总数量:${d.properties.count === undefined ? 0 : d.properties.count}
已完成任务数量:${
d.properties.count_finish === undefined ? 0 : d.properties.count_finish
}
正在执行任务数量:${
d.properties.count_notFinish === undefined
? 0
: d.properties.count_notFinish
}`
);
// handle hovering over a circle
function mouseMove(event, d) {
// make the circle larger
d3.select(this).attr("fill", "#D19F4A");
d3.selectAll(".provinces").style("opacity", 0.2);
d3.select(this).style("opacity", 1);
d3.select("svg")
.append("rect")
.attr("class", "rect_zuoxia")
.attr("x", 0) // 左上角的x坐标
.attr("y", 550) // 左上角的y坐标
.attr("width", 350) // 矩形的宽度
.attr("height", 150) // 矩形的高度
.style("fill", "white"); // 设置填充颜色为白色
// 添加文字
d3.select("svg")
.append("text") // 添加一个外部对象元素
.attr("class", "text_zuoxia") // 指定一个类名
.attr("x", 0) // 设置位置
.attr("y", 625)
.attr("width", 200) // 设置宽度
.text("此处为"+d3.select(this).datum().properties.ProvCH+"的描述信息");
}
// handle leaving a circle
function mouseLeave(event, d) {
// reset the size of the circle
d3.select(this).attr("fill", (d) => {
let cases = d.properties.count;
return cases === "NA" || cases === undefined ? "#636363" : color(+cases);
});
d3.selectAll(".provinces").style("opacity", 1);
d3.selectAll(".rect_zuoxia").remove();
d3.selectAll(".text_zuoxia").remove();
current_selected.attr("fill","yellow");
}
// onclick
function mouseClick(event, d) {
last_selected = current_selected;
current_selected = d3.select(this);
current_selected.attr("fill","yellow");
last_selected.attr("fill", (d) => {
let cases = d.properties.count;
return cases === "NA" || cases === undefined ? color(+0) : color(+cases);
})
window.outputRegionNumber = d3.select(this).datum().properties.GbProv;
}
return svg.node();
}