Skip to content

Commit

Permalink
Merge pull request USGS-VIZLAB#57 from jread-usgs/master
Browse files Browse the repository at this point in the history
hover and show
  • Loading branch information
mbucknell authored Mar 10, 2017
2 parents dfea8b7 + 9e3cae9 commit d1d5dfb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
16 changes: 9 additions & 7 deletions scripts/process/process_bar_chart.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
process.bar_chart <- function(viz){
data.in <- readDepends(viz)

size <- size_map_svg(data.in[['state-map']])
sites <- data.in$`disch-data`
library(dplyr)
bars <- filter(sites, year >= viz[['min-year']], year <= viz[['max-year']]) %>%
Expand All @@ -10,17 +10,19 @@ process.bar_chart <- function(viz){
library(xml2)
g.bars <- read_xml("<g id='year-bars'/>")
# since all of the style and formatting stuff will happen in visualize, this will assume a 100 x 100 px plot that can be scaled and fit elsewhere.
w <- 100
w <- round(size[['x']]*72, 1)
h <- 100
spc <- 0.2
bin.w <- round((w-(length(bars$n)-1)*spc)/length(bars$n),3)
bin.h <- round(bars$n/max.sites*h, 3)
spc <- round(0.002*w, 2)
bin.w <- round((w-(length(bars$n)-1)*spc)/length(bars$n),2)
bin.h <- round(bars$n/max.sites*h, 2)
warning('just starting on this')
for (i in 1:length(bars$year)){
xml_add_child(g.bars, 'rect',
xml_add_child(g.bars, 'rect', class = "gage-count-bar",
x = as.character((i-1)*(bin.w+spc)),
height = as.character(bin.h[i]),
width = as.character(bin.w), y = as.character(h - bin.h[i]), id = paste0('yr', bars$year[i]))
width = as.character(bin.w), y = as.character(h - bin.h[i]), id = paste0('yr', bars$year[i]),
onmousemove = sprintf("hovertext('%s gages');vizlab.showyear('%s')", bars$n[i], bars$year[i]),
onmouseoff = sprintf("hovertext(' ');vizlab.playyear('%s')", bars$year[i]))
}
write_xml(x = g.bars, file = viz[['location']])

Expand Down
16 changes: 13 additions & 3 deletions scripts/visualize/visualize-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
site.chunk <- 1000
group.names <- 'sites-group-%s'

size_map_svg <- function(sp){
apply(sp::bbox(sp), 1, diff)/500000
}

visualize.states_svg <- function(viz){
data <- readDepends(viz)
states <- data[['state-map']]
Expand All @@ -14,7 +18,7 @@ visualize.states_svg <- function(viz){

library(svglite)
library(sp)
size <- apply(bbox(states), 1, diff)/500000
size <- size_map_svg(states)
svg <- svglite::xmlSVG({
par(mai=c(0,0,0,0), omi=c(0,0,0,0))
sp::plot(states, ylim=bbox(states)[2,], xlim=bbox(states)[1,], setParUsrBB = TRUE)
Expand Down Expand Up @@ -74,9 +78,15 @@ visualize.states_svg <- function(viz){
}

add_bar_chart <- function(svg, bars){
vb <- as.numeric(strsplit(xml_attr(svg, "viewBox"), '[ ]')[[1]])
xml_attr(bars, 'transform') <- sprintf("translate(0,%s)", vb[4])


h <- xml_find_all(bars, '//*[local-name()="rect"]') %>% xml_attr('height') %>% as.numeric() %>% max
vb[4] <- vb[4] + h

xml_attr(svg, "viewBox") <- paste(vb, collapse=' ')

xml_attr(svg, "viewBox") <- "0 0 658.75 550.10" # hack, but do the math!
xml_attr(bars, 'transform') <- "translate(0,440)scale(4,1)" # hack, but need to do the math!
xml_add_child(svg, bars)
return(svg)
}
Expand Down
4 changes: 2 additions & 2 deletions viz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ process:
id: bar-data
location: cache/bar-data.xml
processor: bar_chart
scripts: scripts/process/process_bar_chart.R
depends: disch-data
scripts: ["scripts/process/process_bar_chart.R", "scripts/visualize/visualize-map.R"]
depends: ["disch-data", "state-map"]
reader: filepath
min-year: 1890
max-year: 2015
Expand Down

0 comments on commit d1d5dfb

Please sign in to comment.