From ada3f924489ffee3614785bb18d1a3f7ef64167c Mon Sep 17 00:00:00 2001 From: Jordan S Read Date: Fri, 10 Mar 2017 13:08:20 -0600 Subject: [PATCH 1/2] for #41 --- scripts/process/process_year-json.R | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 scripts/process/process_year-json.R diff --git a/scripts/process/process_year-json.R b/scripts/process/process_year-json.R new file mode 100644 index 0000000..596b902 --- /dev/null +++ b/scripts/process/process_year-json.R @@ -0,0 +1,36 @@ +process.year_json <- function(viz){ + data.in <- readDepends(viz) + library(dplyr) + sites <- data.in[['site-map']] + sites.w.data <- data.in[["disch-data"]] + + chunk.s <- seq(1,by=site.chunk, to=length(sites)) + chunk.e <- c(tail(chunk.s, -1L), length(sites)) + json.out <- list() + for (i in 1:length(chunk.s)){ + chunk <- list() + for (yr in viz[['min-year']]:viz[['max-year']]){ + sites.n.chunk <- sites$site_no[chunk.s[i]:chunk.e[i]] + sites.yr <- sites.w.data %>% filter(year == yr ) %>% .$site_no + now.i <- which(sites.n.chunk %in% sites.yr) + if (yr == viz[['min-year']]){ + tmp <- list(list(gn = now.i, ls = numeric())) + } else { + #last year's data: + last.yr <- sites.w.data %>% filter(year == yr-1 ) %>% .$site_no + last.i <- which(sites.n.chunk %in% last.yr) + gained <- now.i[!now.i %in% last.i] + lost <- last.i[!last.i %in% now.i] + tmp <- list(list(gn = gained, ls = lost)) + } + + names(tmp) <- yr + chunk <- append(chunk, tmp) + } + tmp <- list(chunk) + names(tmp) <- sprintf(group.names, i) + json.out <- append(json.out, tmp) + } + json.text <- jsonlite::toJSON(json.out) + cat(json.text, file = viz[['location']]) +} \ No newline at end of file From 401021deabf9a26c2e538414b6b96abd29e42c0f Mon Sep 17 00:00:00 2001 From: Jordan S Read Date: Fri, 10 Mar 2017 13:08:26 -0600 Subject: [PATCH 2/2] clean up and mousers --- scripts/process/process_bar_chart.R | 2 +- scripts/visualize/visualize-map.R | 26 +------------------------- viz.yaml | 4 ++-- 3 files changed, 4 insertions(+), 28 deletions(-) diff --git a/scripts/process/process_bar_chart.R b/scripts/process/process_bar_chart.R index 5b5aee2..1a88c15 100644 --- a/scripts/process/process_bar_chart.R +++ b/scripts/process/process_bar_chart.R @@ -21,7 +21,7 @@ process.bar_chart <- function(viz){ 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]), - onmousemove = sprintf("hovertext('%s gages');vizlab.showyear('%s')", bars$n[i], bars$year[i]), + onmousemove = sprintf("hovertext('%s gages', evt);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']]) diff --git a/scripts/visualize/visualize-map.R b/scripts/visualize/visualize-map.R index c8c4a4d..9c68229 100644 --- a/scripts/visualize/visualize-map.R +++ b/scripts/visualize/visualize-map.R @@ -91,31 +91,7 @@ add_bar_chart <- function(svg, bars){ return(svg) } -process.time_json <- function(viz){ - data.in <- readDepends(viz) - library(dplyr) - sites <- data.in[['site-map']] - sites.w.data <- data.in[["disch-data"]] - - chunk.s <- seq(1,by=site.chunk, to=length(sites)) - chunk.e <- c(tail(chunk.s, -1L), length(sites)) - json.out <- list() - for (i in 1:length(chunk.s)){ - chunk <- list() - for (yr in viz[['min-year']]:viz[['max-year']]){ - sites.n.chunk <- sites$site_no[chunk.s[i]:chunk.e[i]] - sites.yr <- sites.w.data %>% filter(year == yr ) %>% .$site_no - tmp <- list(which(sites.n.chunk %in% sites.yr)) #which of the sites - names(tmp) <- yr - chunk <- append(chunk, tmp) - } - tmp <- list(chunk) - names(tmp) <- sprintf(group.names, i) - json.out <- append(json.out, tmp) - } - json.text <- jsonlite::toJSON(json.out) - cat(json.text, file = viz[['location']]) -} + #' do the things to the svg that we need to do every time if they come from svglite: #' diff --git a/viz.yaml b/viz.yaml index 2c605c8..0426968 100644 --- a/viz.yaml +++ b/viz.yaml @@ -171,8 +171,8 @@ process: - id: year-data location: cache/year-data.json - processor: time_json - scripts: scripts/visualize/visualize-map.R + processor: year_json + scripts: ["scripts/visualize/visualize-map.R", "scripts/process/process_year-json.R"] depends: ["site-map", "disch-data"] min-year: 1890 max-year: 2015