Published
Edited
Mar 8, 2021
4 forks
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
AlcoholConsumptionRows
Insert cell
Insert cell
{
const unpack = key =>
AlcoholConsumptionRows.map(function(row) {
return row[key];
});
const alcohol = AlcoholConsumptionRows.map(d => d.alcohol);
const bubbleSizeScale = d3
.scaleSqrt()
.domain(d3.extent(alcohol))
.range([1, 10]);
const bubbleSize = alcohol.map(d => bubbleSizeScale(d));

var data = [
{
type: 'scattergeo',
locationmode: 'country names',
locations: unpack('location'),
marker: {
size: bubbleSize
},
text: alcohol
}
];

var layout = {
title: 'Pure alcohol consumption<br>among adults (age 15+) in 2010',
height: 500,
width: 900,
geo: {
projection: {
type: projectionOption
}
}
};
const myDiv = DOM.element("div");
Plotly.newPlot(myDiv, data, layout);
return myDiv;
}
Insert cell
{
const rows = populationRows;
const unpack = (rows, key) =>
rows.map(function(row) {
return row[key];
});
var data = [
{
type: 'choropleth',
locationmode: 'USA-states',
locations: unpack(rows, 'State').map(state => stateNameToPostal[state]),
z: unpack(rows, 'Population'),
text: unpack(rows, 'State'),
autocolorscale: true
}
];

var layout = {
title: '2014 US Popultaion by State',
geo: {
scope: 'usa'
}
};
const myDiv = DOM.element("div");
Plotly.newPlot(myDiv, data, layout);
return myDiv;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mapbox(myRasterDiv,rasterStyle,0)
Insert cell
mapbox = (div, style, vectorFlag) => {
const rows = EarthquateRows;
function unpack(rows, key) {
return rows.map(function(row) {
return row[key];
});
}

var data = [
{
lon: unpack(rows, 'Longitude'),
lat: unpack(rows, 'Latitude'),
radius: 10,
z: unpack(rows, 'Magnitude'),
type: "densitymapbox",
coloraxis: 'coloraxis',
hoverinfo: 'skip'
}
];

var layout = {
mapbox: { center: { lon: 0, lat: 0 }, style: style, zoom: 0.55 },
coloraxis: { colorscale: "Viridis" },
title: { text: "Earthquake Magnitude" },
width: 900,
height: 500,
margin: { t: 30, b: 0 }
};

var config = { mapboxAccessToken: yourToken };

Plotly.newPlot(div, data, layout, vectorFlag ? config : {});
//Plotly.newPlot(myDiv, data, layout);
return div;
}
Insert cell
Insert cell
Insert cell
myVectorDiv = DOM.element("div");
Insert cell
mapbox(myVectorDiv, vectorStyle, 1)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
locations
Insert cell
medianIncome
Insert cell
{
const data = [
{
type: 'choroplethmapbox',
locations: locations,
z: medianIncome,
geojson: fips_geojson,
coloraxis: 'coloraxis',
hoverinfo: 'skip'
}
];

const layout = {
mapbox: {
center: { lat: 37.0902, lon: -95.7129 },
style: "open-street-map", //"carto-positron", //"light", //
zoom: 3,
//colorscale: "Viridis",
zmin: 0,
zman: 12,
marker_opacity: 0.5
},

marker_opacity: 0.1,
marker_line_width: 0,

marker_line_width: 0,
title: { text: "Median_Income" },
width: 900,
height: 500,
margin: { t: 30, b: 0 }
};

const myDiv = DOM.element("div");
Plotly.newPlot(myDiv, data, layout, { mapboxAccessToken: yourToken });
return myDiv;
}
Insert cell
Insert cell
{
const data = [
{
type: 'scattermapbox',
lat: lats,
lon: longs,
mode: 'markers',
marker: {
size: population.map(d =>
d3
.scaleSqrt()
.domain(d3.extent(population))
.range([3, 50])(d)
), //population.map(d=>d/100000).map(d=>d<3?3:d),
line: {
color: 'black',
width: 1
}
},
geojson: fips_geojson,
text: countyStatePopulation
}
];

const layout = {
mapbox: {
center: { lat: 37.0902, lon: -95.7129 },
style: "open-street-map",
zoom: 3
},
title: { text: "population" },
width: 900,
height: 500,
margin: { t: 30, b: 0 }
};

const myDiv = DOM.element("div");
Plotly.newPlot(myDiv, data, layout, { mapboxAccessToken: yourToken });
return myDiv;
}
Insert cell
fips_geojson = d3.json("https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json")
Insert cell
medianIncome = locations.map(d=>Math.random()*10000) // Random
Insert cell
population = countyFIPSLatLong.map(d=>d.POPULATION)
Insert cell
Insert cell
lats = countyFIPSLatLong.map(d=>+d.LATITUDE)
Insert cell
longs = countyFIPSLatLong.map(d=>+d.LONGITUDE)
Insert cell
countyStatePopulation = countyFIPSLatLong.map(d=>d.COUNAME + ", "+d.STNAME+" : "+d.POPULATION)
Insert cell
countyFIPSLatLong = d3.csvParse(await FileAttachment("CenPop2010_Mean_CO.csv").text(), d3.autoType) // Data Source: https://www2.census.gov/geo/docs/reference/cenpop2010/county/
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md `## Using GeoJson`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
airlines = [...new Set(globalAirportTraffic.map(d => d.airline))]
Insert cell
airlines.filter(d => d == "Delta")
Insert cell
Insert cell
mutable feedback = ""
Insert cell
Insert cell
zeroPad = (num, places) => String(num).padStart(places, '0') //https://stackoverflow.com/questions/2998784/how-to-output-numbers-with-leading-zeros-in-javascript
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