Published
Edited
Jan 20, 2021
Insert cell
md`# Trajekt_orije`
Insert cell
projection = d3
.geoAlbers()
.rotate([-15, 0])
.fitExtent([[100, -height * 1.5], [width * 2.5, height * 3.2]], geojson)
Insert cell
random = 0.002
Insert cell
mapaRvacke = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style('background-color', 'lightblue');

const paths = svg
.selectAll('path')
.data(geojson.features)
.enter()
.append('path')
.attr('fill', '#eee6cc')
.attr('d', path);

const citiesGroup = svg
.selectAll('.cities')
.data(mjesta)
.enter()
.append('g')
.attr(
'transform',
d =>
`translate(${projection(d.coordinates)[0]},${
projection(d.coordinates)[1]
})`
);

addCityLabel(svg, mjesta);

for (var trip = 0; trip < data.length; trip++) {
const tripData = [...data[trip]];
if (trip % 2) tripData.reverse();

svg
.selectAll(`.jadroline${trip}`)
.data([tripData])
.enter()
.append('path')
.attr('class', `jadroline${trip}`)
.attr('stroke', 'grey')
.attr('stroke-width', 3)
.style('opacity', 0)
.attr('fill', 'none')
.attr('d', jadroline)
.call(reveal, trip);
}

return svg.node();
}
Insert cell
addCityLabel = (svg, data) => {
const citiesGroup = svg
.selectAll('.cities')
.data(data)
.enter()
.append('g')
.attr(
'transform',
d =>
`translate(${projection(d.coordinates)[0]},${
projection(d.coordinates)[1]
})`
);

citiesGroup
.append('circle')
.attr('r', 4)
.attr('fill', 'white');
citiesGroup.append('circle').attr('r', 2);

citiesGroup
.append('text')
.attr('class', 'text-shadow')
.attr('dx', 5)
.style('stroke', 'white')
.style('stroke-width', 2.5)
.style('opacity', 0.9)
.style('pointer-events', 'none')
.text(d => d.name);

citiesGroup
.append('text')
.attr('dx', 5)
.attr('fill', 'darkgrey')
.style('pointer-events', 'none')
.text(d => d.name);
}
Insert cell
duration = 1000
Insert cell
mjesta = [
{ name: "Pula", coordinates: [13.843699, 44.871084] },
{ name: "Mali Lošinj", coordinates: [14.462446, 44.536343] }
]
Insert cell
reveal = (path, index) =>
path
.transition()
.duration(duration)
.delay(index * duration + index * 100)
//.ease(d3.easeLinear)
.ease(d3.easeCubicInOut)
.style('opacity', 1)
.attrTween("stroke-dasharray", function() {
const length = this.getTotalLength();
return d3.interpolate(`0,${length}`, `${length},${length}`);
})
.transition()
.attr('stroke-width', 0.25)
Insert cell
jadroline = d3
.line()
// .curve(d3.curveCardinal)
.curve(d3.curveCatmullRom)
.x(d => projection(d)[0])
.y(d => projection(d)[1])
Insert cell
projection(pulaLosinj[1])
Insert cell
data = {
let retval = new Array();

for (var trip = 0; trip < 5; trip++) {
retval.push(
pulaLosinj.map((d, i) => {
if (i > 0 && i < pulaLosinj.length - 2)
return [
Math.random() * random * d[0] + (1 - random) * d[0],
Math.random() * random * d[1] + (1 - random) * d[1]
];
else return d;
})
);
}

return retval;
}
Insert cell
pulaLosinj = [
[13.831772, 44.868583],
[13.779453, 44.881714],
[13.907927, 44.753022],
[14.368040, 44.533403],
[14.430366, 44.557899],
[14.462446, 44.536343]
]
Insert cell
path = d3.geoPath(projection)
Insert cell
height = 600
Insert cell
Insert cell
topoHrv = FileAttachment("hrvatska.1.tomo.topojson (1).json").json()
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