Skip to content

Commit

Permalink
start on USGS-VIZLAB#84 and some clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan S Read committed Mar 11, 2017
1 parent 560d89c commit 4612ba5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
9 changes: 9 additions & 0 deletions layout/css/svg.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@
fill:none;
stroke:#e0e0e0;
stroke-width:0.5;
}

.svg-text {
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Chrome/Safari/Opera */
-khtml-user-select: none; /* Konqueror */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
cursor: default;
}
8 changes: 5 additions & 3 deletions scripts/process/process_bar_chart.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ process.bar_chart <- function(viz){
root <- read_xml("<g/>")
g.bars <- xml_add_child(root, 'g', id='year-bars')
g.mousers <- xml_add_sibling(g.bars, 'g', id = 'year-bars-mouser')
# 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 <- round(size[['x']]*72, 1)

lm <- 45
w <- round(size[['x']]*72, 1) - lm
h <- 100

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)
Expand All @@ -25,7 +27,7 @@ process.bar_chart <- function(viz){
width = as.character(bin.w), y = as.character(h - bin.h[i]),
id = paste0('yr', bars$year[i]), class = paste0('total-bar-', bars$year[i])) #
xml_add_child(g.mousers, 'rect', opacity = "0.0",
x = as.character((i-1)*(bin.w+spc)),
x = as.character((i-1)*(bin.w+spc)-spc/2),
height = as.character(h),
width = as.character(bin.w+spc), y = "0",
onmousemove = sprintf("hovertext('%s gages in %s', evt);", bars$n[i], bars$year[i]),
Expand Down
39 changes: 30 additions & 9 deletions scripts/visualize/visualize-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ visualize.states_svg <- function(viz){
g.states <- xml_add_child(svg, 'g', 'id' = 'state-polygons')
g.sites <- xml_add_child(svg, 'g', 'id' = 'site-dots')
g.watermark <- xml_add_child(svg, 'g', id='usgs-watermark',
transform = sprintf('translate(2,%s)scale(0.20)', as.character(vb.num[4]+15)))
transform = sprintf('translate(45,%s)scale(0.20)', as.character(vb.num[4]+5)))


for (i in 1:length(state.name)){
Expand Down Expand Up @@ -97,14 +97,37 @@ visualize.states_svg <- function(viz){
}

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

h <- xml_find_all(xml_children(bars)[1], '//*[local-name()="rect"]') %>% xml_attr('height') %>% as.numeric() %>% max
vb[4] <- vb[4] + h
ax.buff <- 5
all.bars <- xml_children(xml_children(bars)[1])
all.mousers <- xml_children(xml_children(bars)[2])
last.attrs <- tail(all.bars, 1) %>% xml_attrs() %>% .[[1]]
full.width <- as.numeric(last.attrs[['x']]) + as.numeric(last.attrs[['width']])
xml_attr(bars, 'transform') <- sprintf("translate(%s,%s)", vb[3]-full.width, vb[4])

heights <- all.bars %>% xml_attr('height') %>% as.numeric()
h <- max(heights)
max.i <- which(h == heights)[1]
max.gages <- xml_attr(all.mousers[max.i], 'onmousemove') %>% # this is a hack to get the gage count from the element. Brittle
stringr::str_extract_all("\\(?[0-9.]+\\)?") %>% .[[1]] %>% .[1] %>% as.numeric

tick.labs <- pretty(c(0,max.gages))[pretty(c(0,max.gages)) < max.gages]
y.ticks <- (h+ax.buff-round(tick.labs*h/max.gages,1)) %>% as.character()


vb[4] <- vb[4] + h + ax.buff
xml_attr(svg, "viewBox") <- paste(vb, collapse=' ')
g.axes <- xml_add_child(bars, 'g', id='axes')
xml_add_child(g.axes, 'path', d=sprintf("M-%s,%s v%s", ax.buff, ax.buff, h+ax.buff), id='y-axis', stroke='black')
xml_add_child(g.axes, 'path', d=sprintf("M-%s,%s h%s", ax.buff, h+ax.buff, ax.buff+full.width), id='x-axis', stroke='black')
g <- xml_add_child(g.axes, 'g', id = 'y-axis-labels', class='axis-labels svg-text')
for (i in 1:length(y.ticks)){
xml_add_child(g, 'text', tick.labs[i], y = y.ticks[i],
x=as.character(-ax.buff), 'text-anchor' = 'end', dx = "-0.33em")
}


xml_add_child(svg, bars)
return(svg)
}
Expand All @@ -119,10 +142,8 @@ add_bar_chart <- function(svg, bars){
#' @return a modified version of svg
clean_up_svg <- function(svg, viz){
# let this thing scale:
xml_attr(svg, "preserveAspectRatio") <- "xMidYMid meet"
xml_attr(svg, "xmlns") <- 'http://www.w3.org/2000/svg'
xml_attr(svg, "xmlns:xlink") <- 'http://www.w3.org/1999/xlink'
xml_attr(svg, "id") <- viz[["id"]]
xml_set_attrs(svg, c("preserveAspectRatio" = "xMidYMid meet", "xmlns" = 'http://www.w3.org/2000/svg',
"xmlns:xlink" = 'http://www.w3.org/1999/xlink', id = viz[["id"]]))

r <- xml_find_all(svg, '//*[local-name()="rect"]')

Expand Down

0 comments on commit 4612ba5

Please sign in to comment.