d3.jsでtopojsonマップを作っています。異なるマップを描画する 1 つの大きな topojson 内に 3 つのデータ セットがあり、マウスクリックでマップを切り替えたいと考えています。
.datum()
これは、mouseclick イベントに関数を追加し、結果を演算子に入れることで実現できると考えました。
更新: これが動作するコードです。Lars に感謝します!
var mapPath = d3.geo.path().projection(mapProjection),
jsondata,
jsonobject,
jsonobjectkeys,
numberOfKeys,
currentMap
mapNumber;
d3.json("test.json", function(error, json){
if (error) return console.warn(error);
jsondata = json; //Store data in the variable "jsondata"
jsonobject = json.objects;
jsonobjectkeys = [];
numberOfKeys = 0;
//Get the maps(keys) from the jsonobject
for(var k in jsonobject) jsonobjectkeys.push(k);
//Find number of objects in jsondata
for (objects in jsonobject){
if((jsonobject).hasOwnProperty(objects)){
numberOfKeys++;
}
}
mapNumber = jsonobjectkeys[0];
currentMap = eval("jsonobject." + (mapNumber));
//Map
var mapSVG = d3.select(".the_map")
.append("svg")
.attr("width", mapW)
.attr("height", mapH);
mapSVG.append("path")
.datum(topojson.object(jsondata, currentMap))
.attr("d", mapPath)
.attr("width", mapW)
.attr("height", mapH)
.attr("class", "land");
//Timeline
//Create scale
var xScale = d3.scale.linear()
.domain([0, (numberOfKeys-1)])
.range([timelinePadding, timelineW - timelinePadding]);
//Axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(numberOfKeys-1);
var timeline = d3.select("#timeline")
.append("svg")
.attr("width", timelineW)
.attr("height", timelineH);
timeline.append("g")
.attr("class", "axis")
.attr("transform", "translate(0, " + timelinePadding + ")")
.call(xAxis);
timeline.selectAll("circle")
.data(jsonobjectkeys)
.enter()
.append("circle")
.attr("width", timelineW)
.attr("height", timelineH)
.attr("cx", function(d,i){return xScale(i);})
.attr("cy", timelinePadding)
.attr("r", 7)
.attr("class", "events")
.style("cursor", "hand")
.on("click", function(d){
redrawMap(d);
});
function redrawMap(i){
currentMap = eval("jsonobject." + (i));
//Update
mapSVG.selectAll("path")
.datum(topojson.object(jsondata, currentMap))
.attr("d", mapPath);
}
});
元の、機能しないコード:
var mapPath = d3.geo.path().projection(mapProjection),
jsondata,
jsonobject,
jsonobjectkeys,
numberOfKeys;
d3.json("test.json", function(error, json){
if (error) return console.warn(error);
jsondata = json; //Store data in the variable "jsondata"
jsonobject = json.objects;
jsonobjectkeys = [];
numberOfKeys = 0;
//Get the maps(keys) from the jsonobject
for(var k in jsonobject) jsonobjectkeys.push(k);
//Find number of objects in jsondata
for (objects in jsonobject){
if((jsonobject).hasOwnProperty(objects)){
numberOfKeys++;
}
}
var mapNumber = jsonobjectkeys[0];
var currentMap = eval("jsonobject." + (mapNumber));
currentMapData(mapNumber);
//Map
var mapSVG = d3.select(".the_map")
.append("svg")
.attr("width", mapW)
.attr("height", mapH);
mapSVG.append("path")
.datum(topojson.object(jsondata, currentMap))
.attr("d", mapPath)
.attr("width", mapW)
.attr("height", mapH)
.attr("class", "land");
//Timeline
//Create scale
var xScale = d3.scale.linear()
.domain([0, (numberOfKeys-1)])
.range([timelinePadding, timelineW - timelinePadding]);
//Axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(numberOfKeys-1);
var timeline = d3.select("#timeline")
.append("svg")
.attr("width", timelineW)
.attr("height", timelineH);
timeline.append("g")
.attr("class", "axis")
.attr("transform", "translate(0, " + timelinePadding + ")")
.call(xAxis);
timeline.selectAll("circle")
.data(jsonobjectkeys)
.enter()
.append("circle")
.attr("width", timelineW)
.attr("height", timelineH)
.attr("cx", function(d,i){return xScale(i);})
.attr("cy", timelinePadding)
.attr("r", 7)
.attr("class", "events")
.style("cursor", "hand")
.on("click", function(d,i){
currentMapData(i);
});
function currentMapData(i){
mapNumber = jsonobjectkeys[i];
console.log("showing this map: " + mapNumber);
currentMap = eval("jsonobject." + (mapNumber));
return currentMap;
}
});