Public
Edited
Dec 15, 2022
2 forks
Insert cell
# 221214_DC test slider thing
Insert cell
Insert cell
viewof Selectyear = Inputs.range([1976, 2036], {step: 1, label: "DC Metro network in"})
Insert cell
chart = {
const width = 1300,
height = 800;
const svg = d3.create("svg")
.attr("viewBox", [25, 25, width-100, height-90]);

// Use Mercator projection
var projection = d3
.geoMercator()
.fitSize([width - 10, height - 40], DCbb);
var path1 = d3.geoPath().projection(projection);
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 path6 = d3.geoPath().projection(projection);
var path7 = d3.geoPath().projection(projection);
var path8 = d3.geoPath().projection(projection);
var path9 = d3.geoPath().projection(projection);
var path10 = d3.geoPath().projection(projection);
var path11 = d3.geoPath().projection(projection);
var path12 = d3.geoPath().projection(projection);
var g = svg.append("g").attr("id", "paths");




g.selectAll("path9") //d3 geopath
.data(DMV_stateboundaries.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','static')
.attr("d", path6) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style("stroke", "rgb(0,0,0)")
.style('stroke-opacity','.05')
.style("stroke-width", '3')



function fillHideDot(d,i){
// console.log(Global) //
var color = 'none'
if(d.YEAR_ADDED <= Selectyear) {color = 'white'}
return color
}
function fillHideStroke(d,i){
// console.log(Global) //
var color = 'none'
if(d.YEAR_ADDED <= Selectyear) {color = 'black'}
return color
}

function fillHideTrain(d,i){
// console.log(Global) //
var color = 'none'
if(d.properties.LINE_ADDED <= Selectyear &&
d.properties.LINE_REMOVED >= Selectyear &&
d.properties.NAME=='silver'){color = 'silver'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='blue'){color = 'dodgerblue'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='yellow'){color = 'gold'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='green'){color = 'olivedrab'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='red'){color = 'crimson'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='orange'){color = 'peru'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='purple'){color = 'rebeccapurple'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='pink'){color = 'orchid'}
if(d.properties.LINE_ADDED <= Selectyear && d.properties.NAME=='teal'){color = 'mediumturquoise'}
return color
}



g.selectAll("path6") //d3 geopath
.data(DClines.features) //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", path6) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.attr('stroke', fillHideTrain) // add function to control color by 'type'
.style('stroke-opacity','1')
.style("stroke-width", '3')

function lineType(d,i) {
var color = 'black'
if(d.properties.NAME=='silver'){color = 'silver'}
if(d.properties.NAME=='blue'){color = 'dodgerblue'}
if(d.properties.NAME=='yellow'){color = 'gold'}
if(d.properties.NAME=='green'){color = 'olivedrab'}
if(d.properties.NAME=='red'){color = 'crimson'}
if(d.properties.NAME=='orange'){color = 'peru'}
if(d.properties.NAME=='purple'){color = 'rebeccapurple'}
if(d.properties.NAME=='pink'){color = 'orchid'}
if(d.properties.NAME=='teal'){color = 'mediumturquoise'}
return color
}






var c = svg.selectAll("circle") //d3 geopath
.data(DCstationsGROWTH)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return projection([d.LONG,d.LAT])[0]})
.attr("cy", function(d) {return projection([d.LONG,d.LAT])[1]})
.attr('r',3)
.attr('class','stationDots')
.attr('fill',fillHideDot)
.style('fill-opacity','3')
.attr('stroke-opacity','1')
.attr("stroke-width", "1.25")
.attr("stroke", fillHideStroke)
.on('mouseover', spotText)
.on('mouseout', removeSpotText)


function spotText(event,d){
d3.select(this)
.attr('fill','hotpink')
.attr('r',8)
.style("stroke", "lightpink")
.style("stroke-opacity",'.75')
.style("stroke-width", '8')

svg.append('text')
.attr('class','spots')
.attr('x','1029')
.attr('y','69')
.attr('font-family','Tahoma')
.attr('font-size','0.9em')
.attr('fill','black')
.attr('text-anchor','start')
.attr('font-weight','600')
.text(d.NAME)
svg.append('text')
.attr('class','spots')
.attr('x','1029')
.attr('y','89')
.attr('font-family','Tahoma')
.attr('font-size','0.7em')
.attr('fill', 'black')
.attr('text-anchor','start')
.attr('font-weight','600')
.text(d.LINE)
svg.append('text')
.attr('class','spots')
.attr('x','1029')
.attr('y','109')
.attr('font-family','Tahoma')
.attr('font-size','0.7em')
.attr('fill','black')
.attr('text-anchor','start')
.attr('font-weight','400')
.text(d.YEAR_ADDED)
svg.append('line')
.attr('class','spotLine')
.attr('x1','1011')
.attr('y1','69')
.attr('x2',projection([d.LONG,d.LAT])[0])
.attr('y2','69')
.style('stroke-width','1')
.style('stroke','black')
.style('stroke-dasharray','4 2')
svg.append('line')
.attr('class','spotLine')
.attr('x1',projection([d.LONG,d.LAT])[0])
.attr('y1','69')
.attr('x2',projection([d.LONG,d.LAT])[0])
.attr('y2',projection([d.LONG,d.LAT])[1])
.style('stroke-width','1')
.style('stroke','black')
.style('stroke-dasharray','4 2')
}


function removeSpotText(event,d) {
d3.select(this).attr('fill','white')
d3.select(this).style('fill-opacity','3')
d3.select(this).style("stroke", "rgb(0,0,0)")
d3.select(this).style('stroke-opacity','.6')
d3.select(this).style("stroke-width", "1.25")
d3.select(this).attr('r',3)
d3.selectAll('text.spots').remove()
d3.selectAll('line.spotLine').remove()

}





/*
var c = svg.selectAll("circle") //d3 geopath
.data(DCstationsGROWTH)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return projection([d.LONG,d.LAT])[0]})
.attr("cy", function(d) {return projection([d.LONG,d.LAT])[1]})
.attr('r',4)
.attr('class','stationDots')
.attr('fill','white')
.style('fill-opacity','3')
.attr('stroke-opacity','1')
.attr("stroke-width", "1.25")
.attr("stroke", "black")
.on('mouseover', spotText)
.on('mouseout', removeSpotText)


function spotText(event,d){
d3.select(this)
.attr('fill','hotpink')
.attr('r',8)
.style("stroke", "lightpink")
.style("stroke-opacity",'.75')
.style("stroke-width", '8')

svg.append('text')
.attr('class','spots')
.attr('x','1029')
.attr('y','69')
.attr('font-family','Tahoma')
.attr('font-size','0.9em')
.attr('fill','black')
.attr('text-anchor','start')
.attr('font-weight','600')
.text(d.NAME)
svg.append('text')
.attr('class','spots')
.attr('x','1029')
.attr('y','89')
.attr('font-family','Tahoma')
.attr('font-size','0.7em')
.attr('fill', 'black')
.attr('text-anchor','start')
.attr('font-weight','600')
.text(d.LINE)
svg.append('text')
.attr('class','spots')
.attr('x','1029')
.attr('y','109')
.attr('font-family','Tahoma')
.attr('font-size','0.7em')
.attr('fill','black')
.attr('text-anchor','start')
.attr('font-weight','400')
.text(d.YEAR_ADDED)
svg.append('line')
.attr('class','spotLine')
.attr('x1','1011')
.attr('y1','69')
.attr('x2',projection([d.LONG,d.LAT])[0])
.attr('y2','69')
.style('stroke-width','1')
.style('stroke','black')
.style('stroke-dasharray','4 2')
svg.append('line')
.attr('class','spotLine')
.attr('x1',projection([d.LONG,d.LAT])[0])
.attr('y1','69')
.attr('x2',projection([d.LONG,d.LAT])[0])
.attr('y2',projection([d.LONG,d.LAT])[1])
.style('stroke-width','1')
.style('stroke','black')
.style('stroke-dasharray','4 2')
}




function removeSpotText(event,d) {
d3.select(this).attr('fill','white')
d3.select(this).style('fill-opacity','3')
d3.select(this).style("stroke", "rgb(0,0,0)")
d3.select(this).style('stroke-opacity','1')
d3.select(this).style("stroke-width", "1.25")
d3.select(this).attr('r',4)
d3.selectAll('text.spots').remove()
d3.selectAll('line.spotLine').remove()

}


*/


return svg.node();
}

Insert cell
DCbb = FileAttachment("DC_BB.geojson").json()
Insert cell
DMV_stateboundaries = FileAttachment("DMV_stateboundaries.geojson").json()
Insert cell
DCstationsGROWTH = FileAttachment("DCnewlines6.csv").csv()
Insert cell
DClinesT = FileAttachment("221214_ResplitMetroLines_1.geojson").json()
Insert cell
DClines = FileAttachment("test for resplit lines that get removed.geojson").json()
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell
color = d3.scaleQuantize([1, 30000], d3.schemeBlues[9])
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