chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 980, 710]);
svg.append("g")
.attr("transform", "translate(610,20)")
.append(() => legend(color, Crimes));
svg.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.join("path")
.attr("fill", d => {
if (data.get(d.properties.name) != undefined ) {
return color(data.get(d.properties.name))}
else {return "gray"}
})
.attr("d", path)
.on("click", clicked)
.on("mousemove", function(event, d) {
tooltip.style("visibility", "hidden");
})
.append("title")
.text(d => `${d.properties.name}
${format(data.get(d.properties.name))}`);
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);
svg.append("g")
.attr("fill", "brown")
.attr("fill-opacity", 0.5)
.attr("stroke", "#fff")
.attr("stroke-width", 0.5)
.selectAll("circle")
.data(data_unemp
.filter(d => d.position)
.sort((a, b) => d3.descending(a.value, b.value)))
.join("circle")
.attr("transform", d => `translate(${d.position})`)
.attr("r", d => radius(d.value))
.append("title")
.text(d => `${d.title}
${format(d.value)}`);
function clicked(event, d) {
console.log("here")
if (data.get(d.properties.name) != undefined ) {
var state_name = d.properties.name;
//console.log(d.properties.name, "translate(" + p[0] + "," + p[1] + ")")
console.log("I'm in" ,d.properties.name)
var wanted = data1.filter( function(item){return (item.State==d.properties.name);} )[0];
console.log("Won", wanted)
var label_list = ['murder', 'Robbery', 'Aggravated_assault', "Property_crime" , "Burglary", "rape", "theft"]
var value_list = label_list.map(function (value, label) {
return wanted[value];
})
console.log("parfait", value_list)
const myDiv = document.getElementById("svg-tooltip");
//let canvas = DOM.canvas(200, 200);
//let context = canvas.getContext("2d");
//image.onload = function() {}
myDiv.innerHTML = "";
//myDiv.append(canvas);
myDiv.append(pie_chart(label_list, value_list, state_name));
tooltip.style("visibility", "visible")
.style("top", event.pageY - 10 + "px")
.style("left", event.pageX + 10 + "px");
}
else {
const myDiv = document.getElementById("svg-tooltip");
tooltip.style("visibility", "visible")
.style("top", event.pageY - 10 + "px")
.style("left", event.pageX + 10 + "px")
.style("background-color", "white")
.html("Sorry, no data is available<br> for this state yet.");} }
return svg.node();
}