-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from lindsaycarr/d3
successfully inject map d3 into target/index.html (correct branch this time!)
- Loading branch information
Showing
5 changed files
with
110 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Width and height | ||
var chart_width = 1000; | ||
var chart_height = 600; | ||
|
||
// Projection | ||
var projection = d3.geoMercator() //changing to geoAlbers makes the map disappear | ||
.scale(1500) | ||
.center([-91.34397, 32.25196]) | ||
.translate([chart_width / 2, chart_height / 2]); | ||
var path = d3.geoPath() | ||
.projection(projection); | ||
|
||
// Create SVG | ||
var svg = d3.select("body") | ||
.append("svg") | ||
.attr("width", chart_width) | ||
.attr("height", chart_height); | ||
|
||
var map = svg.append( 'g' ) | ||
.attr( 'id', 'map' ); | ||
|
||
// Add map features | ||
d3.json('../cache/state_map.geojson', function(us_data){ | ||
|
||
map.selectAll( 'path' ) | ||
.data(us_data.features) | ||
.enter() | ||
.append('path') | ||
.attr('d', path) | ||
.attr('fill', "cornflowerblue") | ||
.attr('stroke', '#fff') | ||
.attr('stroke-width', 0.5) | ||
.on("mouseover", mouseover) | ||
.on("mouseout", mouseout); | ||
|
||
}); | ||
|
||
function mouseover(d){ | ||
d3.select(this).style('fill', 'orange'); | ||
} | ||
|
||
function mouseout(d){ | ||
d3.select(this).style('fill', "cornflowerblue"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
fetchTimestamp.save_map <- vizlab::alwaysCurrent | ||
|
||
#' Saves sf data as geojson. | ||
#' @description Converts sf data to sp, then saves as geojson for use in d3. | ||
#' | ||
#' @param viz a vizlab object including \code{viewbox_limits} and \code{fetch_args} | ||
#' @details | ||
#' Depends on: \code{map_data}: an sf representation of the x and y | ||
#' (geographic coordinates) values of map_data shapes (counties, states, countries, etc). | ||
#' | ||
process.save_map <- function(viz){ | ||
deps <- readDepends(viz) | ||
checkRequired(deps, "map_data") | ||
map_data <- deps[["map_data"]] | ||
|
||
# spatial data needs to be sp to use writeOGR | ||
# saves empty file if there is not any map features | ||
if(nrow(map_data) > 0){ | ||
map_data_sp <- as(map_data, "Spatial") | ||
rgdal::writeOGR(map_data_sp, viz[['location']], | ||
layer="map_data_sp", driver="GeoJSON") | ||
} else { | ||
write.table(data.frame(), viz[["location"]]) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters