diff --git a/R/add_line.R b/R/add_line.R index 2bf3c1a..424144a 100644 --- a/R/add_line.R +++ b/R/add_line.R @@ -5,6 +5,7 @@ echart_stat_line = function ( stat, name = NULL){ list(markLine= list(data = list(type = stat, name = name ))) } + # a more general function to add any line echart_abline_point = function ( start, end, name = NULL){ # start is a vector with two values (x,y) @@ -59,3 +60,7 @@ echart_vline = function (xintercept, name = NULL){ echart_abline_point(start, end, name = name) } +#choose statistic +check_stat = function (stat) { + if(!stat %in% c( "max", "min", "average")) stop("Statistic line should be either max, min or average.") +} diff --git a/R/echart_hist.R b/R/echart_hist.R index eb6da9a..b242f93 100644 --- a/R/echart_hist.R +++ b/R/echart_hist.R @@ -20,5 +20,5 @@ echart_hist = function(data, binwidth = NULL){ # adjust the breaks # bar_hist$x = round(bar_hist$mids,2) - echart(bar_hist, ~mids, ~counts, type ="bar", barCategoryGap='0%') + echart(bar_hist, ~mids, ~counts, type ="bar") %>% eSeries ( barCategoryGap = 0) } diff --git a/R/options.R b/R/options.R index dbe0a36..30b6311 100644 --- a/R/options.R +++ b/R/options.R @@ -68,8 +68,116 @@ axisType = function(data, which = c('x', 'y')) { stop('Unable to derive the axis type automatically from the ', which, ' variable') } -eSeries = function (chart,barCategoryGap, ...){ - # basically we need to add barCategoryGap to series - params$series[[i]]$barCategoryGap = list(...)$barCategoryGap +#' Modify series properties +#' +#' Modify series properties +#' +#' @export + +eSeries = function (chart, + which = 'all' , # wich should be series name or index? go with index now + stack = NULL, # binary T or F + barGap = NULL, # a number between 0 and 1 + barCategoryGap = NULL, # a number between 0 and 1 + barMinHeight = NULL, # a number >= 0 + barWidth = NULL, # a number >= 0 + barMaxWidth = NULL, # a number >= 0 + symbol = NULL, # choose from 'circle' | 'rectangle' | 'triangle' | 'diamond' | 'emptyCircle' | 'emptyRectangle' | 'emptyTriangle' | 'emptyDiamond'| heart' | 'droplet' | 'pin' | 'arrow' | 'star' + symbolSize = NULL, # a number >= 0 + showAllSymbol = NULL, # T or F + symbolRotate = NULL, # between -180 and 180 + smooth = NULL, # T or F + dataFilter = NULL, + large = NULL, # T or F, use large scatter plot? + largeThreshold = NULL, # a number > 0 for large scatter plot + legendHoverLink= NULL, # highlight when hover on legend? T or F + ...){ + # change all series + if (which == "all") series = chart$x$series else series = chart$x$series[which] + + # usage of stack: echart allows to stack any bar but here we stack for now. + if (isTRUE(stack)) { + series = lapply(list(series), function(x) { + mergeList(x, eSeries_add_parameter(name = "stack", value = "grand total")) + }) + } + if (!is.null(barGap)) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "barGap", value = percent_scale(barGap))) + }) + if (!is.null(barCategoryGap)) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "barCategoryGap", value = percent_scale(barCategoryGap))) + + }) + if (!is.null(barMinHeight) && barMinHeight >= 0) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "barMinHeight", value = barMinHeight)) + }) + if (!is.null(barWidth) && barWidth >= 0) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "barWidth", value = barWidth)) + }) + if (!is.null(barMaxWidth) && barMaxWidth >= 0) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "barMaxWidth", value = barMaxWidth)) + }) + # symbol also supports pictures? ignore for now. + if (!is.null(symbol) && + symbol %in% c('circle' , 'rectangle' , 'triangle' , 'diamond' , 'emptyCircle' , + 'emptyRectangle' , 'emptyTriangle' , 'emptyDiamond', 'heart' , + 'droplet' , 'pin' , 'arrow' , 'star')) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "symbol", value = symbol)) + }) + # also handle symbolsize for bubble plots? + if (!is.null(symbolSize) && symbolSize >= 0) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "symbolSize", value = symbolSize)) + }) + if (!is.null(symbolRotate) && symbolRotate <= 180 && symbolRotate >= -180) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "symbolRotate", value = symbolRotate)) + }) + if (isTRUE(showAllSymbol)) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "showAllSymbol", value = showAllSymbol)) + }) + if (isTRUE(smooth)) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "smooth", value = smooth)) + }) + if (isTRUE(large)) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "large", value = large)) + }) + if (!is.null(largeThreshold) && largeThreshold >= 0 ) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "largeThreshold", value = largeThreshold)) + }) + if (isTRUE(legendHoverLink)) series = lapply(series, function(x) { + mergeList(x, eSeries_add_parameter(name = "legendHoverLink", value = legendHoverLink)) + }) + + if (which == "all") chart$x$series = series else chart$x$series[which] = series + + return(chart) +} + +eSeries_add_parameter = function (name, value){ + temp = list(name = value) + names(temp) = name + return(temp) +} + +percent_scale = function(number){ + if(! (number >=0 & number <=1)) stop("Number should be between 0 and 1.") + paste0(number * 100, "%") +} + +#' Add lines to graph +#' +#' Add lines to graph +#' +#' @export +eSeries_addline = function (chart, which = "all", + line_stat = NULL, # add a statistical line, choose from min, max, average + ...){ + + if (which == "all") series = chart$x$series else series = chart$x$series[which] + if (!is.null(line_stat)) series = lapply(series, function(x) { + mergeList(x, echart_stat_line(stat = line_stat, name = line_stat)) + }) + + if (which == "all") chart$x$series = series else chart$x$series[which] = series + return(chart) }