// enter code to define margin and dimensions for svg var margin = {top: 50, right: 150, bottom: 50, left: 100}, width = 1200 - margin.left - margin.right, height = 700 - margin.top - margin.bottom; // enter code to create svg var svg = d3.select("#choropleth").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // enter code to create color scale var color = d3.scaleQuantile().domain([0,99]).range(["steelblue","#aaa","lightgreen"]) // enter code to define projection and path required for Choropleth var projection = d3.geoMercator().translate([width/2, height/2]).scale(100).center([0,40]); var path = d3.geoPath().projection(projection); Promise.all([ // enter code to read files d3.json('world_countries.json'), d3.csv('ratings-by-country.csv'), ]).then( // enter code to call ready() with required arguments (data) => { ready(null, data[0], data[1]) } ); // this function should be called once the data from files have been read // world: topojson from world_countries.json // gameData: data from ratings-by-country.csv function ready(error, world, gameData) { // enter code to extract all unique games from gameData unique_games = [...new Set(gameData.map(d => d.Game))]; unique_games = Array.from(unique_games).sort() console.log(unique_games) // enter code to append the game options to the dropdown var selection = d3.select("#selection_menu").on("change", () => {createMapAndLegend(world, gameData, document.getElementById('selection_menu').value)}) var game_list = selection.selectAll('option').data(unique_games); game_list.enter().append("option").text(function(d) { return d }) // event listener for the dropdown. Update choropleth and legend when selection changes. Call createMapAndLegend() with required arguments. game_selected = unique_games[0] // create Choropleth with default option. Call createMapAndLegend() with required arguments. createMapAndLegend(world, gameData, game_selected) } // end of ready() // this function should create a Choropleth and legend using the world and gameData arguments for a selectedGame // also use this function to update Choropleth and legend when a different game is selected from the dropdown function createMapAndLegend(world, gameData, game_selected){ svg.selectAll("*").remove(); data = world.features; console.log(game_selected) console.log(data) tooltip = d3.tip().attr('class', 'd3-tip').offset([-3, 0]).html(function(d) { country = d[0].name console.log(country) data_for_game = gameData.filter(function(d){ return d.Game == selectedGame && d.Country == country}) // console.log(country) // console.log(gameData) }); // end of tooltip // tip = d3.tip().attr('class', 'd3-tip').offset([-3, 0]).html(function(d) { // // // return "Frequency: " + d.frequency + ""; // // current_country = d.properties.name // gd = gameData.filter((game) => game.Game == selectedGame && game.Country == current_country) // if(gd.length) //// { // return "
Country: " +d.properties.name+ "
"+
// " Game: "+ selectedGame +"
"+
// " Average Rating: "+ gd[0]['Average Rating']+"
"+
// " Number of Users: "+ gd[0]['Number of Users']
// +"
Country: " +d.properties.name+ "
"+
// " Game: "+ selectedGame +"
"+
// " Average Rating: "+ "N/A"+"
"+
// " Number of Users: "+ "N/A"
// +"