你可以調整文件uStates.js達到你想要的東西。創建draw()
方法的副本並應用一些更改:
- 傳遞您希望「禁用」的狀態ID的列表。
- 如果狀態的ID在列表中,而不是使用定義的
fill
,請使用「灰色禁用」顏色。
- 在事件控制器中,檢查狀態ID是否在列表中;如果是,則不要應用更改(因爲狀態將「禁用」)。
功能會是這樣的:
// notice the new parameter "list" that will contain the disabled states
uStates.drawSpecial = function(id, data, toolTip, list){
function mouseOver(d){
// only show the tooltip if the state is not in the disabled list
if (list.indexOf(d.id) < 0) {
d3.select("#tooltip").transition().duration(200).style("opacity", .9);
d3.select("#tooltip").html(toolTip(d.n, data[d.id]))
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}
}
function mouseOut(){
d3.select("#tooltip").transition().duration(500).style("opacity", 0);
}
d3.select(id).selectAll(".state")
.data(uStatePaths)
.enter()
.append("path")
.attr("class","state")
.attr("d",function(d){ return d.d;})
// if the state id is in the list, select gray instead of the state color
.style("fill",function(d){ return list.indexOf(d.id) >= 0 ? "#dddddd" : data[d.id].color; })
.on("mouseover", mouseOver).on("mouseout", mouseOut);
}
然後,而不是調用draw
方法,調用自己的personlized drawSpecial
方法。例如,在一個類似的代碼在tutorial you linked發現,如果要禁用德州(TX)和佛蒙特州(VT)的人,你會調用該函數是這樣的:
uStates.draw("#statesvg", sampleData, tooltipHtml, ['TX', 'VT']);
非常感謝你的所有的答覆和答案! – SuzukiBlue