Skip to content

Commit 977c616

Browse files
committed
minor changes for v.0.3.1.3
1 parent fb0b8f0 commit 977c616

File tree

9 files changed

+261
-162
lines changed

9 files changed

+261
-162
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: echarty
22
Title: Minimal R/Shiny Interface to JavaScript Library 'ECharts'
33
Date: 2021-09-15
4-
Version: 0.3.1.2
4+
Version: 0.3.1.3
55
Authors@R: c(
66
person("Larry", "Helgason", email = "larry@helgasoft.com", role = c("aut", "cre", "cph")),
77
person("John", "Coene", email = "jcoenep@gmail.com", role = c("aut", "cph"))

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# history of echarty package development
22

3+
## v. 0.3.1.3
4+
5+
- _ec.init_: fix '3D' presets for groups and 'world' presets
6+
- _ec.clmn_: added new parameter _scale_
7+
- _echarty.js_ add resize parameter [resizeOpts](https://echarts.apache.org/en/api.html#echartsInstance.resize)
8+
- updates to examples.R
9+
- upgrade ECharts to v.5.2.1, liquidfill to v.3.1.0
10+
311
## v. 0.3.1.2
412

513
- _ec.init_ added ECharts parameters _locale_ and _useDirtyRect_

R/echarty.R

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,19 @@ ec.init <- function( df=NULL, preset=TRUE, group1='scatter', load=NULL,
228228

229229
# Plugins implemented as dynamic load on-demand
230230
if ('3D' %in% load) {
231-
if (preset) {
232-
wt$x$opts$xAxis <- NULL # replace 2D presets with 3D
231+
if (preset) { # replace 2D presets with 3D
232+
wt$x$opts$xAxis <- NULL
233233
wt$x$opts$yAxis <- NULL
234-
wt$x$opts$series[[1]] <- list(type='scatter3D') #NULL
235234
wt$x$opts$grid3D <- list(list())
236235
wt$x$opts$xAxis3D <- list(list())
237236
wt$x$opts$yAxis3D <- list(list())
238237
wt$x$opts$zAxis3D <- list(list())
238+
if (!is.null(group1) && dplyr::is.grouped_df(df)) {
239+
# make all series scatter3D
240+
wt$x$opts$series <- lapply(wt$x$opts$series,
241+
function(s) { s$type='scatter3D'; s })
242+
} else
243+
wt$x$opts$series[[1]] <- list(type='scatter3D')
239244
}
240245
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/echarts-gl@2.0.8/dist/echarts-gl.min.js', ask)
241246
}
@@ -244,13 +249,14 @@ ec.init <- function( df=NULL, preset=TRUE, group1='scatter', load=NULL,
244249
if (preset) {
245250
wt$x$opts$xAxis <- NULL
246251
wt$x$opts$yAxis <- NULL
247-
wt$x$opts$geo = list(map='world', roam=TRUE, zoom=6)
252+
wt$x$opts$series <- list(list(type='map', map='world', roam=TRUE))
253+
#wt$x$opts$geo = list(map='world', roam=TRUE) # duplicate of series
248254
# if (!is.null(df)) # cancelled: dont know if first 2 cols are lng,lat
249255
# wt$x$opts$geo$center= c(mean(unlist(df[,1])), mean(unlist(df[,2])))
250256
}
251257
}
252258
if ('liquid' %in% load)
253-
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/echarts-liquidfill@3.0.0/dist/echarts-liquidfill.min.js', ask)
259+
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/echarts-liquidfill@3.1.0/dist/echarts-liquidfill.min.js', ask)
254260

255261
if ('gmodular' %in% load)
256262
wt <- ec.plugjs(wt, 'https://cdn.jsdelivr.net/npm/echarts-graph-modularity@2.0.0/dist/echarts-graph-modularity.min.js', ask)
@@ -448,7 +454,8 @@ ec.data <- function(df, format='dataset', header=TRUE) {
448454
#'
449455
#' Helper function to address data column(s) by index or name
450456
#'
451-
#' @param col A column index(number), column name(string) or a \code{\link[base]{sprintf}} format string. \cr
457+
#' @param col A column index(number), column name(string) or a \code{\link[base]{sprintf}} format string.
458+
#' @param scale A decimal/integer number to multiply the column value by. Only when \emph{col} is a single index.
452459
#' @param ... Comma separated list of column indexes or names, when \emph{col} is \emph{sprintf}. This allows formatting of multiple columns, as for a tooltip.
453460
#'
454461
#' @details Column indexes are counted in R and start at 1.\cr
@@ -469,11 +476,12 @@ ec.data <- function(df, format='dataset', header=TRUE) {
469476
#' p
470477
#'
471478
#' @export
472-
ec.clmn <- function(col=NULL, ...) {
479+
ec.clmn <- function(col=NULL, scale=1, ...) {
473480
if (is.null(col)) stop('col is required', call.=FALSE)
481+
if (is.null(scale)) scale=1
474482
args <- list(...)
475483
if (is.na(suppressWarnings(as.numeric(col)))) { # col is string
476-
if (length(args)==0)
484+
if (length(args)==0)
477485
ret <- paste0('return x.data.',col,';')
478486
else { # col a sprintf
479487
spf <- paste("var sprintf = (str, argv) => !argv.length ? str :",
@@ -488,23 +496,22 @@ ec.clmn <- function(col=NULL, ...) {
488496
else { # multiple numeric, they could be in x, x.data, x.value
489497
tmp <- paste(tmp, collapse=',')
490498
ret <- paste0(sub('@','%d',spf),
491-
"let ss=[",tmp,"]; ",
492-
"ss=ss.map(e => x.value!=null ? x.value[e] : x.data!=null ? x.data[e] : x[e]);",
493-
"let c = sprintf(`",col,"`, ss); return c; ")
499+
"let ss=[",tmp,"]; ",
500+
"ss=ss.map(e => x.value!=null ? x.value[e] : x.data!=null ? x.data[e] : x[e]);",
501+
"let c = sprintf(`",col,"`, ss); return c; ")
494502
}
495503
}
496504
}
497-
else { # col is numeric thus solitary parameter
505+
else { # col is solitary numeric
498506
if (length(args) > 0) # { cat(length(args));
499507
warning('col is numeric, others are ignored', call.=FALSE)
500508
col <- as.numeric(col) - 1 # from R to JavaScript counting
501-
ret <- paste0('let c = (x.data) ? x.data[',col,'] : x[',col,']; return c;')
509+
scl <- if (scale==1) 'return c;' else paste0('return (parseFloat(c)*',scale,');')
510+
ret <- paste0('let c = (x.data) ? x.data[',col,'] : x[',col,']; ',scl)
502511
}
503-
#cat(ret)
504512
htmlwidgets::JS(paste0('function(x) {', ret, '}'))
505513
}
506514

507-
508515
#' Charts layout
509516
#'
510517
#' Set multiple charts in rows/columns format

R/examples.R

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,23 @@
9191
#'
9292
#'
9393
#' #------ Plugin leaflet
94-
#' tmp <- quakes %>% dplyr::relocate('long') # set in lon,lat order
94+
#' tmp <- quakes %>% dplyr::relocate('long') %>% # set order to lon,lat
95+
#' dplyr::mutate(size= exp(mag)/20) # add accented size
9596
#' p <- tmp %>% ec.init(load='leaflet')
96-
#' p$x$opts$legend = list(data=list(list(name='quakes')))
9797
#' p$x$opts$series[[1]]$name = 'quakes'
98-
#' p$x$opts$series[[1]]$symbolSize = ec.clmn(4)
98+
#' p$x$opts$series[[1]]$symbolSize = ec.clmn(6) # size column
99+
#' p$x$opts$legend = list(zz='')
100+
#' p
101+
#'
102+
#'
103+
#' #------ Plugin 'world' with visualMap
104+
#' cns <- data.frame(
105+
#' country = c('United States','China','Russia','Brazil','Canada','Algeria','Sudan','Australia'),
106+
#' value = runif(8, 1, 100)
107+
#' )
108+
#' p <- cns %>% ec.init(load= 'world')
109+
#' p$x$opts$visualMap <- list(calculable= TRUE, max= 100)
110+
#' p$x$opts$toolbox <- list(feature= list(restore= list()))
99111
#' p
100112
#'
101113
#'
@@ -122,6 +134,22 @@
122134
#' }
123135
#'
124136
#'
137+
#' #------ registerMap JSON
138+
#' json <- jsonlite::read_json("https://echarts.apache.org/examples/data/asset/geo/USA.json")
139+
#' dusa <- USArrests %>% dplyr::mutate(states = row.names(.))
140+
#' p <- ec.init(preset=FALSE)
141+
#' p$x$registerMap <- list(list(mapName = 'USA', geoJSON = json))
142+
#' # registerMap supports also maps in SVG format, see website gallery
143+
#' p$x$opts <- list(
144+
#' visualMap = list(type='continuous', calculable=TRUE,
145+
#' min=min(dusa$UrbanPop), max=max(dusa$UrbanPop))
146+
#' ,series = list( list(type='map', map='USA', name='UrbanPop', roam=TRUE,
147+
#' data = lapply(ec.data(dusa,'names'), function(x) list(name=x$states, value=x$UrbanPop))
148+
#' ))
149+
#' )
150+
#' p
151+
#'
152+
#'
125153
#' #------ Plugin 3D
126154
#' if (interactive()) {
127155
#' data <- list()
@@ -197,7 +225,7 @@
197225
#' #encode=list(x='day', y='CAC') # *2
198226
#' ))
199227
#' )
200-
#' p$x$opts$legend <- list(ey='')
228+
#' p$x$opts$legend <- list(zz='')
201229
#' p$x$opts$dataZoom <- list(type='slider', end=50)
202230
#' p
203231
#'
@@ -215,14 +243,19 @@
215243
#'
216244
#'
217245
#' #------ Boxplot
218-
#' bdf <- data.frame(vx = sample(LETTERS[1:3], size=20, replace=TRUE),
219-
#' vy = rnorm(20)) %>% group_by(vx) %>% group_split()
220-
#' dats <- lapply(bdf, function(x) boxplot.stats(x$vy)$stats )
246+
#' library(dplyr)
247+
#' bdf <- data.frame(vx= sample(LETTERS[1:3], size= 20, replace= TRUE),
248+
#' vy= rnorm(20)) %>% group_by(vx) %>% group_split()
249+
#' dats <- lapply(bdf, function(x) round(boxplot.stats(x$vy)$stats, 4) )
221250
#' p <- ec.init()
222251
#' p$x$opts <- list( # overwrite presets
223-
#' xAxis = list(ey=''),
224-
#' yAxis = list(type = 'category', data = unique(unlist(lapply(bdf, `[`, , 1))) ),
225-
#' series = list(list(type = 'boxplot', data = dats))
252+
#' xAxis = list(zz=''),
253+
#' yAxis = list(type = 'category', data= unique(unlist(lapply(bdf, `[`, , 1))) ),
254+
#' series = list(list(type= 'boxplot', data= dats,
255+
#' itemStyle= list(color= '#b8c5f2'),
256+
#' encode= list(tooltip= c('min', 'Q1', 'median', 'Q3', 'max'))
257+
#' ))
258+
#' ,tooltip = list(trigger= 'item')
226259
#' )
227260
#' p
228261
#'
@@ -301,22 +334,6 @@
301334
#' p
302335
#'
303336
#'
304-
#' #------ registerMap JSON
305-
#' json <- jsonlite::read_json("https://echarts.apache.org/examples/data/asset/geo/USA.json")
306-
#' dusa <- USArrests %>% dplyr::mutate(states = row.names(.))
307-
#' p <- ec.init(preset=FALSE)
308-
#' p$x$registerMap <- list(list(mapName = 'USA', geoJSON = json))
309-
#' # registerMap supports also maps in SVG format, see website gallery
310-
#' p$x$opts <- list(
311-
#' visualMap = list(type='continuous', calculable=TRUE,
312-
#' min=min(dusa$UrbanPop), max=max(dusa$UrbanPop))
313-
#' ,series = list( list(type='map', map='USA', name='UrbanPop', roam=TRUE,
314-
#' data = lapply(ec.data(dusa,'names'), function(x) list(name=x$states, value=x$UrbanPop))
315-
#' ))
316-
#' )
317-
#' p
318-
#'
319-
#'
320337
#' #------ Error Bars on grouped data
321338
#' library(dplyr)
322339
#' df <- mtcars %>% group_by(cyl,gear) %>% summarise(yy=round(mean(mpg),2)) %>%
@@ -325,7 +342,7 @@
325342
#' p <- df %>% ec.init(group1='bar', load='custom') %>%
326343
#' ecr.ebars(df, name = 'eb'
327344
#' ,tooltip = list(formatter=ec.clmn('high <b>%d</b><br>low <b>%d</b>', 4,3)))
328-
#' p$x$opts$tooltip <- list(ey='')
345+
#' p$x$opts$tooltip <- list(zz='')
329346
#' p
330347
#'
331348
#'

0 commit comments

Comments
 (0)