Public
Edited
Feb 16, 2023
Fork of Assignment 3
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let container = html`<div style='height:1200px;' />`;
let map;
// Give the container dimensions.
yield container;
let restaurant_locations = filtered.map(
(restaurant) => restaurant.location
);
let restaurant_coords = filtered.map(
(restaurant) => restaurant.cords
);
map = new mapboxgl.Map({
container,
center: [-122.159846, 37.601883],
zoom: 9.95,
style: "mapbox://styles/mapbox/streets-v9"
});

function project(d) {
return map.project(new mapboxgl.LngLat(d[0], d[1]));
}

var svg = d3
.select(container)
.append("svg")
.attr("width", "100%")
.attr("height", "1200")
.style("position", "absolute")
.style("z-index", 2);

var dots = svg
.append("g")
.selectAll("circle")
.data(restaurant_locations)
.enter()
.append("circle")
.attr("r", 1.4)
.style("opacity", 0.85)
.style("fill", "grey");
var circleA = svg
.append("g")
.selectAll("circle")
.data([circleStartPt[0]])
.join("circle")
.attr("id", "circA")
.attr("r", radA)
.attr("cx", (d) => project(d).x)
.attr("cy", (d) => project(d).y)
.attr("fill", "white")
.attr("stroke", "black")
.attr("opacity", 0.6)
.call(drag);

var circleB = svg
.append("g")
.selectAll("circle")
.data([circleStartPt[1]])
.join("circle")
.attr("id", "circB")
.attr("r", radB)
.attr("cx", (d) => project(d).x)
.attr("cy", (d) => project(d).y)
.attr("fill", "red")
.attr("stroke", "black")
.attr("opacity", 0.35)
.call(drag);

function inCircle(a, b, x, y, r) {
var dist_points = (a - x) * (a - x) + (b - y) * (b - y);
r *= r;
if (dist_points < r) {
return true;
}
return false;
}

function inBothCircles(d) {
var dx = project(d).x;
var dy = project(d).y;
var c1x = circleA.attr("cx");
var c1y = circleA.attr("cy");
var c2x = circleB.attr("cx");
var c2y = circleB.attr("cy");
return inCircle(dx, dy, c1x, c1y, radA) && inCircle(dx, dy, c2x, c2y, radB);
}

function setCoords() {
for (let i = 0; i < restaurant_data.length; i++) {
var coords = map.project(new mapboxgl.LngLat(restaurant_data[i].location[0], restaurant_data[i].location[1]));
restaurant_data[i].cords = [coords.x, coords.y];
}
}

function getName(d) {
var dx = project(d).x;
var dy = project(d).y;
var index = restaurant_coords.findIndex(element => element[0] === dx && element[1] === dy);
return filtered[index].name;
}
function getRating(d) {
var dx = project(d).x;
var dy = project(d).y;
var index = restaurant_coords.findIndex(element => element[0] === dx && element[1] === dy);
return filtered[index].rating;
}

function render() {
dots
.attr("cx", function (z) {
return project(z).x;
})
.attr("cy", function (z) {
return project(z).y;
})
.style("fill", function (z) {
return inBothCircles(z) ? "#fa1090" : "grey";
})

.on("mouseover", function (event, z) {
var rest_name = getName(z);
var rest_rat = getRating(z);
const tooltipText = `Name-${rest_name}; Rating-${rest_rat}`;
d3.select("#tooltip")
.transition()
.duration(200)
.style("opacity", 1)
.text(tooltipText);
})
.on("mouseout", function () {
d3.select("#tooltip").style("opacity", 0);
})
.on("mousemove", function (event, z) {
d3.select("#tooltip")
.style("top", event.pageY + 30 + "px")
.style("left", event.pageX - 30 + "px");
})

.style("opacity", 1)
.raise();
}

// Call render method; modified to render every .1s
setCoords();
render();
setInterval(render, 100);
invalidation.then(() => map.remove());
}

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more