データに基づいてTOPOJSON D3マップの個々の状態をシェーディングする関数を実行しようとしています。D3 TOPOJSONマップのロード後に関数を実行する方法
ただし、SVGに状態がロード/表示される前に、関数は実行を継続します。マップを作成する関数は、文書準備関数で呼び出されます。私は状態にイベントリスナーを追加しようとしましたが、状態を陰にしてwindow.loadを使用した関数を呼び出す.on属性が追加されました。しかし、このように状態が画面に表示される前に実行し続ける状態を色合いや機能が
.background {
fill: none;
pointer-events: all;
}
#states {
fill: #aaa;
}
#states .active {
fill: orange;
}
#state-borders {
fill: none;
stroke: #fff;
stroke-width: 1.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
function buildmap(){
var width = 960,
height = 500,
centered;
var projection = d3.geo.albersUsa()
.scale(1070)
.translate([width/2, height/2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", clicked);
var g = svg.append("g");
d3.json("/mbostock/raw/4090846/us.json", function(error, us) {
if (error) throw error;
g.append("g")
.attr("id", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path);
g.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("id", "state-borders")
.attr("d", path);
});
};
function colorstates(){
var mainstate = document.getElementById("texas")
mainstate.style.fill="blue"
}
$(document).ready(function((){
buildmap()
colorstates() //I have also used window.load and adding .on attribute to svg/d3
これでも、mainstateがnullとして返されます。関数を定義する前と後で、colorstates()をd3.jsonに移動しました。どちらもmainstateをnullとして返します。 – auto
だから、別の*問題があります:ID = "texas"の要素がありません。どのようにIDを設定していますか? –
topojsonからd3でフィーチャを作成すると、「テキサス」が作成されます。 colorstates()にアラートを追加し、地図がブラウザに表示される前に警告が実行されます。 – auto