From 71352895657a60f3619c24d5f75ca352b5791d8d Mon Sep 17 00:00:00 2001 From: Liyun Chen Date: Thu, 30 Apr 2015 23:22:27 -0700 Subject: [PATCH] 1. use mids not breaks in echart_hist() 2. simple functions for add line and points to plot. Not tested yet. 3. start to wrap an eSeries function to add Series attributes. --- R/add_line.R | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ R/add_point.R | 18 +++++++++++++++ R/echart.R | 4 ---- R/echart_hist.R | 4 +++- R/options.R | 6 +++++ 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 R/add_line.R create mode 100644 R/add_point.R diff --git a/R/add_line.R b/R/add_line.R new file mode 100644 index 0000000..2bf3c1a --- /dev/null +++ b/R/add_line.R @@ -0,0 +1,61 @@ +echart_stat_line = function ( stat, name = NULL){ + # stat is the statisitic of such hline, + check_stat(stat) + if (length(stat) > 1) stop("Only one stat each time.") + 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) + # end is a vector with two values (x,y) + if (!is.numeric(start) | !is.numeric(end)) stop("start and end should be numeric vectors.") + if (length(start)!=2 | length(end) != 2) stop("start and end should be a vector of two numeric values.") + if (Inf %in% c(start, end)) { + warning("Cannot draw infinitie line.") # check if echart can handle Inf; if not, replace Inf + + } + list(markLine = + list(data = + c(list( name = 'line start', value = 1, x = start[1], y = start[2]), + list( name = 'line end', x = end[1], y = end[2])) + )) + +} + +# wrap of abline(); add line by intercept and slope +echart_abline = function ( intercept, slope, name = NULL, + # put some defaults here, could be extended + xmin = -100, + xmax = 100, + ymin = -100, + ymax = 100){ + # start is a vector with two values (x,y) + # end is a vector with two values (x,y) + y_min_actual = xmin * slope + intercept + y_max_actual = xmax * slope + intercept + + start = c(xmin, y_min_actual) + end = c(xmax, y_max_actual) + + # ignore ymin and ymax for now + echart_abline_point(start, end, name = name) +} + +# wrap function for horizontal line +echart_hline = function (yintercept, name = NULL){ + if (!is.numeric(y)) stop("yintercept must be numeric.") + if (length(yintercept)>1) stop("only one line each time.") + echart_abline(intercept = yintercept, slope = 0) +} + +# wrap function for vertical line + +echart_vline = function (xintercept, name = NULL){ + if (!is.numeric(y)) stop("yintercept must be numeric.") + if (length(xintercept)>1) stop ("one line each time.") + start = c(xintercept, -Inf) + end = c(xintercept, Inf) + echart_abline_point(start, end, name = name) +} + diff --git a/R/add_point.R b/R/add_point.R new file mode 100644 index 0000000..fb50f74 --- /dev/null +++ b/R/add_point.R @@ -0,0 +1,18 @@ +# function to add points on plot +echart_point = function (x, y, name = NULL, value = NULL){ + if (!is.numeric(x) | !is.numeric(y)) { stop("x and y should be numeric.")} + list(markPoint = list(data = list(name = name, value = value, x = x, y = y))) +} + +# add statistic point directly +echart_stat_point = function (stat, name = NULL, value = NULL){ + check_stat(stat) + list(markPoint = list(data = list(name = name, type = stat))) +} + +check_stat = function (stat){ + if (!stat %in% c("min","max","average")){ + stop("stat should be either min, max or avergae.") + } +} + diff --git a/R/echart.R b/R/echart.R index e135598..0585e4e 100644 --- a/R/echart.R +++ b/R/echart.R @@ -67,10 +67,6 @@ echart.data.frame = function( params$legend = list(data = levels(as.factor(series))) } - if("barCategoryGap" %in% names(list(...))){ - for (i in 1:length(params$series)) - {params$series[[i]]$barCategoryGap = list(...)$barCategoryGap} - } chart = htmlwidgets::createWidget( 'echarts', params, width = width, height = height, package = 'recharts', diff --git a/R/echart_hist.R b/R/echart_hist.R index 4a2c2f8..eb6da9a 100644 --- a/R/echart_hist.R +++ b/R/echart_hist.R @@ -17,6 +17,8 @@ echart_hist = function(data, binwidth = NULL){ } else{ bar_hist = hist(data, plot = FALSE , binwidth = binwidth) } + # adjust the breaks + # bar_hist$x = round(bar_hist$mids,2) - echart(bar_hist, ~breaks, ~counts, type ="bar", barCategoryGap='0%') + echart(bar_hist, ~mids, ~counts, type ="bar", barCategoryGap='0%') } diff --git a/R/options.R b/R/options.R index e090f71..dbe0a36 100644 --- a/R/options.R +++ b/R/options.R @@ -67,3 +67,9 @@ axisType = function(data, which = c('x', 'y')) { str(data) 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 + +}