Public
Edited
Sep 24, 2024
Insert cell
Insert cell
chcart = {
const width = 900,//pixel size of the map
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width-50, height-10]);

// Use Mercator projection
var projection = d3
.geoMercator()//projection system used
.fitSize([width, height], box_restaurant);

var path = d3.geoPath().projection(projection);//need one of these for each line layer from qgis
var path2 = d3.geoPath().projection(projection);
var path3 = d3.geoPath().projection(projection);
var path4 = d3.geoPath().projection(projection);
var path5 = d3.geoPath().projection(projection);


var g = svg.selectAll('g').attr("id", "paths");//qgis lines variable //one variable for each geometry type
var c = svg.selectAll("circle")//for circles
var p = svg.selectAll("polyline")//for polylines from rhino



//static lines from qgis
staticLines(path2, subways.features,"none",1,1,"rgb(0,200,100)")
staticLines(path3, parks.features,"rgb(200,225,200)",'.25','.1',"rgb(0,255,0)")
staticLines(path4, blocks.features,"none",1,.5,"rgb(8,8,8)",'.25','.1',"rgb(0,100,0)")
staticLines(path5, boroughs.features,"none",1,1,"rgb(8,8,8)")

function staticLines(path, data, sfill, sOpac, sW, stroke){

g.enter().append("path")
.data(data) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", sfill)
.style('stroke-opacity',sOpac)
.style("stroke-width", sW)
.style("stroke", stroke)
}



//static points from qgis

staticCircles(stations.features,'2','red','1',"0")


function staticCircles(data,r,sfill, sOpac, sWidth){

c.enter().append('circle')
.data(data) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('cx',function(d) {return path.centroid(d)[0]})
.attr("cy",function(d) {return path.centroid(d)[1]})
.attr('r', r)
.attr('fill', sfill)
.style('fill-opacity',sOpac)
.style("stroke-width", sWidth)
}
//lines from rhino
//polyline(graphic_test,'red','1','0','none')

//points from spreadsheet
c.enter().append('circle')
.data(restaurants) //get data to define path
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('class','spots')
.attr('cx',function(d) {return projection([d.Longitude,d.Latitude])[0]})
.attr("cy",function(d) {return projection([d.Longitude,d.Latitude])[1]})
.attr('r', 3)
.attr('fill', fillColor)
.style('fill-opacity','1')
.style("stroke-width", "1.5")
.style("stroke", "black")
.on("mouseover", addText)
.on("mouseout", removeText)
function fillColor(d){

var color = 'rgb(255,0,0)'

if(d.Rating == 1){color = 'rgb(255,0,0)'}
if(d.Rating == 1.5){color = 'rgb(255,100,0)'}
if(d.Rating == 2){color = 'rgb(255,165,0)'}
if(d.Rating == 2.5){color = 'rgb(255,255,0)'}
if(d.Rating == 3){color = 'rgb(0,100,0)'}
if(d.Rating == 3.5){color = 'rgb(144,238,144)'}
if(d.Rating == 4){color = 'rgb(50,205,50)'}
if(d.Rating == 4.5){color = 'rgb(50,205,50)'}
if(d.Rating == 5){color = 'rgb(0,255,0)'}
return color
}

function addText(event,d){

svg//draw text
.append("text") //appends path to data
.attr('class','mText')
.attr('x',projection([d.Longitude,d.Latitude])[0])
.attr("y",projection([d.Longitude,d.Latitude])[1])
.attr('fill', 'black')
.style('font-family','helvetica')
.style("font-size", "15px")
.text(d.Restaurant)

svg//draw text
.append("text") //appends path to data
.attr('class','mText')
.attr('x',50)
.attr("y",300)
.attr('fill', 'black')
.style('font-family','helvetica')
.style("font-size", "11px")
.text(d.Description)
svg//draw text
.append("text") //appends path to data
.attr('class','mText')
.attr('x',50)
.attr("y",200)
.attr('fill', 'black')
.style('font-family','helvetica')
.style("font-size", "25px")
.text(d.Rating)

var wrap = svg.selectAll("text.mText")
.each(function(d, i) { wrap_text(d3.select(this), 100) });
}

/* function addText2(event,d){

svg//draw text
.append("text") //appends path to data
.attr('class','mText')
.attr('x',50)
.attr("y",200)
.attr('fill', 'black')
.style('font-family','helvetica')
.style("font-size", "11px")
.text(d.Rating)
}
*/
function removeText(){
svg.selectAll('text.mText').remove()
}




return svg.node();
}
Insert cell
Insert cell
restaurants = d3.csv(restaurantLink,d3.autoType)
Insert cell
box_restaurant = FileAttachment("box_restaurant.geojson").json()
Insert cell
stations = FileAttachment("stations.geojson").json()
Insert cell
boroughs = FileAttachment("boroughs.geojson").json()
Insert cell
subways = FileAttachment("subway.geojson").json()
Insert cell
blocks = FileAttachment("blocks.geojson").json()
Insert cell
parks = FileAttachment("Park.geojson").json()
Insert cell
Insert cell
spots = d3.csv(spotsLink,d3.autoType)
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more