map = {
var w = 200;
var h = 50;
var margin_left = 80;
var margin_histo = 20;
let container = d3.select(DOM.element("div"));
let context = this ? this.getContext("2d") : DOM.context2d(width, width);
container.append(() => context.canvas);
const canvas = context.canvas;
canvas.style.backgroundColor = 'black'
color.opacity = 0.4;
let toolTip = container.append("div").attr("id","tooltip");
let height = context.canvas.height;
let originalScale = height / 2.1;
let scale = originalScale;
let previousScaleFactor = 1;
let sphere = {type: 'Sphere'};
let path = d3.geoPath(projection, context);
let tooltipQuadtree = getQuadtree();
let tooltipPositions = [];
let sentences_fin = [];
let tooltipPositionsIndex = {};
let tooltipPositionsSentences = {};
let tooltipPositionsGeo = {};
let tooltipPositionsYear = {};
var year_colors = {
y_1869: "#35055a",
y_1870: "#3f0556",
y_1871: "#580556",
y_1872: "#54057e",
y_1873: "#5305b8",
y_1874: "#740556",
y_1875: "#a82bef",
y_1876: "#862bef",
y_1877: "#bc96ef",
y_1878: "#c581ee",
y_1879: "#c581ee"
};
//let twitterUserFeatures = twitterUsers.features;
//let {x,y} = globe_rotation
/**
* After each zoom/pan projection recalculation, force the svg(really canvas) paths to update
*/
function drawWorld() {
//console.log(year_colors["y_1873"])
context.clearRect(0, 0, width, height);
context.beginPath(), path(graticule), context.lineWidth = .4; context.strokeStyle = "#ccc", context.stroke();
context.lineWidth = lineWidth;
context.strokeStyle = "white"
context.shadowBlur = 5;
context.shadowColor = "violet"
context.beginPath(), path(land), context.fillStyle = 'transparent', context.fill(); context.strokeStyle = "#fff";
context.lineWidth = 0.5;
context.stroke();
context.lineWidth = 3.5;
context.fillStyle = "transparent";
context.shadowBlur = 5;
context.shadowColor = "violet"
context.shadowOffsetX = 2
context.shadowOffsetY = 2
context.beginPath(), path(sphere), context.strokeStyle = "white", context.stroke();
//set up the points
path.pointRadius(magnitudeRadius);
twitterUsers.forEach(d => {
var year_key = "y_" + d.properties.Year.toString()
context.beginPath(), context.shadowBlur = 10; context.shadowColor = year_colors[year_key]; context.fillStyle = year_colors[year_key]; path(d); context.fill()
});
path.pointRadius(null);
//reset the tooltip caches
tooltipPositions = []; tooltipPositionsIndex = {}; tooltipPositionsSentences = {}; tooltipPositionsGeo = {}; tooltipQuadtree = getQuadtree();
//update tooltip caches
for(let i=0, len=twitterUsers.length; i<len; i++){
let mid_arr = twitterUsers[i].properties.city_context;
mid_arr = mid_arr.sort((a, b) => b.length - a.length);
let pixelCoords = projection(twitterUsers[i].geometry.coordinates);
tooltipPositions.push(pixelCoords);
tooltipPositionsSentences[pixelCoords.join(",")] = mid_arr;
tooltipPositionsGeo[pixelCoords.join(",")] = twitterUsers[i].geometry.coordinates;
tooltipPositionsIndex[pixelCoords.join(",")] = twitterUsers[i].properties.City;
tooltipPositionsYear[pixelCoords.join(",")] = twitterUsers[i].properties.Year;
}
//update the quadtree
tooltipQuadtree.addAll(tooltipPositions);
//hide the tooltip if the map is being zoomed/panned
d3.select('#tooltip').style('opacity', 0);
//d3.select('#circle').style('opacity', 0);
}
/**
* Every time the globe is zoomed or panned, recalculate the correct projection parameters
* and then request that the map data be redrawn/updated
*/
d3.geoZoom()
.projection(projection)
.northUp(true)
.onMove(drawWorld)
(d3.select(context.canvas).node());
//initiate the first globe draw
drawWorld();
//bind the tooltips
d3.select(context.canvas).on("mousemove click",function(){
d3.select('h2').style('color', 'pink');
let mouse = d3.mouse(this);
let closest = tooltipQuadtree.find(mouse[0], mouse[1], 10);
var toolTip = d3.select('#tooltip')
//var circle = d3.select('#circle')
//tooltipPositionsSentences = tooltipPositionsSentences.sort((a, b) => b.length - a.length);
if(closest){
var list_length = tooltipPositionsSentences[closest.join(",")].length
if(list_length == 7)
{
sentences_fin = tooltipPositionsSentences[closest.join(",")].slice(2, list_length)
}
else if(list_length == 6){
sentences_fin = tooltipPositionsSentences[closest.join(",")].slice(1, list_length)
}
else if(list_length == 0){
sentences_fin = tooltipPositionsSentences[closest.join(",")]
}
else{
sentences_fin = tooltipPositionsSentences[closest.join(",")].slice(0, list_length)
}
var color_pick = year_colors["y_" + tooltipPositionsYear[closest.join(",")].toString()]
//"@"+ list_length + ', ' + sentences_fin
toolTip.style('opacity', 0.8)
.style('background', 'transparent')
.style('position', 'absolute')
.style('color', 'white')
.style('pointer-events', 'none')
.style('border-color', 'white')
.style('top', 15 + 'px')
.style('left', 15 + 'px')
.html(()=>{
let innerTableContent = '<h2 style="color:white;display:inlinet">' + tooltipPositionsIndex[closest.join(",")] + '</h2>'
innerTableContent += '<h3 style="color:' + color_pick + '";display:inline;font-family: "IBM Plex Mono", monospace;>' + tooltipPositionsYear[closest.join(",")] + '</h3>'
innerTableContent += '<ul style="list-style-type:none;">'
sentences_fin.forEach(element => {
innerTableContent += '<li style="border-bottom:1px solid white">' + element + '</li>'
});
innerTableContent += "</ul>"
return innerTableContent
});
} else {
d3.select('#tooltip')
.style('opacity', 0)
.style('cursor', 'default')
.style('pointer-events', 'none')
.html('');
}
});
return container.node();
}