Skip to content

Instantly share code, notes, and snippets.

@vsapsai
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsapsai/8888013 to your computer and use it in GitHub Desktop.
Save vsapsai/8888013 to your computer and use it in GitHub Desktop.
All features.

Demonstrate all features present in ukraine_map_data/ukraine.json.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
background-color: rgb(191, 216, 242);
}
.country {
fill: #eee;
}
.country.UKR {
/*fill: #cdc;*/ /* Na zielonej Ukrainie */
fill: #fff;
}
.country-boundary {
fill: none;
stroke: #aaa;
}
.region {
fill: none;
}
.region-boundary {
fill: none;
stroke: #777;
stroke-dasharray: 2,2;
stroke-linejoin: round;
}
.region-label {
fill: #888;
fill-opacity: .8;
font-size: 15px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 300;
text-anchor: middle;
}
.coastline, .river, .lake {
stroke: rgb(83, 168, 231);
}
.coastline, .river {
fill: none;
}
.lake {
fill: rgb(191, 216, 242);
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 900,
height = 600;
var geometry_center = {"latitude": 48.360833, "longitude": 31.1809725};
var geography_center = {"latitude": 49.0275, "longitude": 31.482778};
var svg = d3.select("body").append("center").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("ukraine.json", function(error, ukraine_data) {
var projection = d3.geo.conicEqualArea()
.center([0, geometry_center.latitude])
.rotate([-geometry_center.longitude, 0])
.parallels([46, 52]) // vsapsai: selected these parallels myself, most likely they are wrong.
.scale(4000)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var countries = topojson.feature(ukraine_data, ukraine_data.objects.countries);
svg.selectAll(".country")
.data(countries.features)
.enter().append("path")
.attr("class", function(d) { return "country " + d.id; })
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(ukraine_data, ukraine_data.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "country-boundary")
.attr("d", path);
svg.append("path")
.datum(topojson.mesh(ukraine_data, ukraine_data.objects.countries, function(a, b) { return a === b; }))
.attr("class", "coastline")
.attr("d", path);
var water_group = svg.append("g")
.attr("id", "water-resources");
var rivers = topojson.feature(ukraine_data, ukraine_data.objects.rivers);
water_group.selectAll(".river")
.data(rivers.features)
.enter().append("path")
.attr("class", "river")
.attr("name", function(d) { return d.properties.name; })
.attr("d", path);
// Add lakes after rivers so that river lines connect reservoirs, not cross them.
var lakes = topojson.feature(ukraine_data, ukraine_data.objects.lakes);
water_group.selectAll(".lake")
.data(lakes.features)
.enter().append("path")
.attr("class", "lake") // Note: not necessary a lake, it can be a reservoir.
.attr("name", function(d) { return d.properties.name; })
.attr("d", path);
var regions = topojson.feature(ukraine_data, ukraine_data.objects.regions);
// -- areas
svg.selectAll(".region")
.data(regions.features)
.enter().append("path")
.classed("region", true)
.attr("id", function(d) { return d.id; })
.attr("d", path);
// -- boundaries
svg.append("path")
.datum(topojson.mesh(ukraine_data, ukraine_data.objects.regions, function(a, b) { return a !== b; }))
.classed("region-boundary", true)
.attr("d", path);
// -- labels
svg.selectAll(".region-label")
.data(regions.features)
.enter().append("text")
.attr("transform", function(d) { return "translate(" + projection(d.properties.label_point) + ")"; })
.classed("region-label", true)
.selectAll("tspan")
.data(function(d) { return d.properties.localized_name.ua.split(" "); })
.enter().append("tspan")
.attr("x", "0")
.attr("dy", function(d, i) { return i > 0 ? "1.1em" : "0"; })
.text(function(d) { return d + " "; });
});
d3.select(self.frameElement)
.style("width", width + "px")
.style("height", height + "px");
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment