Public
Edited
Apr 16, 2024
Insert cell
Insert cell
Insert cell
pe = d3.json('https://cdn.jsdelivr.net/npm/latam-atlas@0.0.4/files/peru-100k.json')
Insert cell
rawData = FileAttachment("entel_movil_parsed.csv", ).csv({typed: true})
Insert cell
coverageInfo = [
{technology: '2g', color: '#984ea3' },
{technology: '3g', color: '#4daf4a' },
{technology: '4g', color: '#377eb8' },
{technology: '5g', color: '#e41a1c' }]
Insert cell
Insert cell
// prepare data
data = rawData.map((d, i) => ({
id: i,
department: d.region,
province: d.provincia,
district: d.distrito,
hamlet: d.centro_poblado,
lat: +d.lat,
lon: +d.lon,
technology: d.tecnologia
}))
Insert cell
features = topojson.feature(pe, pe.objects.level2)

Insert cell
borders = topojson.mesh(pe, pe.objects.level2, (a, b) => a !== b)
Insert cell
projection = d3.geoIdentity().reflectY(true).fitSize([width, height], features) // *3/4
Insert cell
colorDomain = coverageInfo.map(f => f.technology)
Insert cell
colorRange = coverageInfo.map(f => f.color)
Insert cell
names = new Map(d3.groups(data, d => d.id).map(([id, [d]]) => [id, `${d.hamlet}, ${d.province}`]))
Insert cell
positions = new Map(d3.groups(data, d => d.id).map(([id, [d]]) => [id, [d.lon, d.lat]]))
Insert cell
controlMap = ({
coordFeatures: {
longitude: 'lon',
latitude: 'lat'
},
category: 'technology',
dotRadius: 3
})
Insert cell
updatedData = data.filter(d => new Set(technology.map(k => k.technology)).has(d.technology))
Insert cell
d3.filter(data, d => new Set(technology.map(k => k.technology)).has(d.technology))
Insert cell
viewof technology = Inputs.checkbox(coverageInfo, {
value: coverageInfo.filter(d => ["2g", "4g"].includes(d.technology)),
label: html`<b>Mobile Internet Network</b>`,
format: x =>
html`<span style="
text-transform: capitalize;
border-bottom: solid 3px ${x.color};
margin-bottom: -2px;
">${x.technology}</span>`
})
// or instead of value; valueof: d => d.technology
Insert cell
d3.group(data, (d) => d.technology)
Insert cell
function getColor(data, key) {
const mapper = d3.group(data, (d) => d.technology)
return mapper.get(key)[0].color
}
Insert cell
getColor(coverageInfo, "2g")
Insert cell
Insert cell
peruChart = drawMap(updatedData, {
features,
borders,
projection,
domain: colorDomain,
range: colorRange,
radius: controlMap.dotRadius,
coordinatesLabel: [controlMap.coordFeatures.longitude, controlMap.coordFeatures.latitude],
category: controlMap.category
})
Insert cell
//chart.update(updatedData)
controlMap.coordFeatures
Insert cell
function drawMap (data, {
features,
borders,
projection,
domain,
range,
radius,
coordinatesLabel,
category
} = {}) {
let value = null

const colorScale = d3.scaleOrdinal()
.domain(domain)
.range(range)

const path = d3.geoPath(projection)

const svg = d3.create('svg')
.attr('viewBox', `0, 0, ${width}, ${height}`)

// Adding cartography
svg.append('g')
.attr('fill', '#ddd')
.selectAll('path')
.data(features.features)
.enter().append('path')
.attr('d', path)

svg.append('path')
.datum(borders)
.attr('fill', 'none')
.attr('stroke', 'white')
.attr('stroke-linejoin', 'round')
.attr('pointer-events', 'none')
.attr('d', path)

// Add data
let circle = svg.append('g')
.selectAll('circle')
.attr('fill-opacity', 0.5)
.attr('stroke', 'black')
.attr('stroke-width', 0.5)

circle
.data(data)
//.join('circle')
.join(
enter => enter.append('circle')
.call(circle => circle.append('title'))
)
.attr('cx', d => projection([d[coordinatesLabel[0]], d[coordinatesLabel[1]]])[0])
.attr('cy', d => projection([d[coordinatesLabel[0]], d[coordinatesLabel[1]]])[1])
.attr('r', radius)
.attr('fill', d => colorScale(d[category]))
.call(circle => circle.select('title').text(d => `${names.get(d.id)}\ntecnología:${d.technology}`))
return svg.node()
}
Insert cell
width = 800
Insert cell
height = 800
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