Skip to content

Commit

Permalink
+ eSeries() function
Browse files Browse the repository at this point in the history
+ add line and add points
  • Loading branch information
Liyun Chen committed May 4, 2015
1 parent 7135289 commit 6f14811
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
5 changes: 5 additions & 0 deletions R/add_line.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.")
}
2 changes: 1 addition & 1 deletion R/echart_hist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

This comment has been minimized.

Copy link
@cloudly

cloudly May 4, 2015

最大的改变是这里,直接用新的eSeries往里面传参数....

}
114 changes: 111 additions & 3 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6f14811

Please sign in to comment.