Skip to content

Commit

Permalink
Use factory functions and dont specify custom element id
Browse files Browse the repository at this point in the history
  • Loading branch information
Tutuchan committed Oct 28, 2016
1 parent 3a6925e commit bc305de
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 64 deletions.
8 changes: 6 additions & 2 deletions NAMESPACE
Expand Up @@ -8,6 +8,10 @@ export(mjsLine)
export(morrisjs)
export(morrisjsOutput)
export(renderMorrisjs)
import(htmlwidgets)
import(zoo)
importFrom(htmlwidgets,createWidget)
importFrom(htmlwidgets,shinyRenderWidget)
importFrom(htmlwidgets,shinyWidgetOutput)
importFrom(magrittr,"%>%")
importFrom(xts,as.xts)
importFrom(zoo,as.Date.yearmon)
importFrom(zoo,index)
28 changes: 15 additions & 13 deletions R/baseChart.R
@@ -1,22 +1,24 @@
# Base line and bar charts creation function
#' @importFrom xts as.xts
#' @importFrom zoo index as.Date.yearmon
baseChart <- function(morrisjs, type, options){
tsData <- morrisjs$x$data

# Transform data to xts
xtsData <- switch(class(tsData)[1],
tbl_df = {
aux <- as.data.frame(tsData)
xts::as.xts(aux[, -1], order.by = aux[, 1])
},
data.frame = xts::as.xts(tsData[, -1], order.by = tsData[, 1]),
xts = tsData,
mts = xts::as.xts(tsData),
ts = xts::as.xts(tsData))
xtsData <- switch(
class(tsData)[1],
tbl_df = {
aux <- as.data.frame(tsData)
as.xts(aux[, -1], order.by = aux[, 1])
},
data.frame = as.xts(tsData[, -1], order.by = tsData[, 1]),
xts = tsData,
mts = as.xts(tsData),
ts = as.xts(tsData))

# Extract the values of the xkey depending on the class of the index
xkey <- switch(class(zoo::index(xtsData)),
yearmon = substr(zoo::as.Date.yearmon(index(xtsData)), 1, 7),
Date = zoo::index(xtsData))
xkey <- switch(class(index(xtsData)),
yearmon = substr(as.Date.yearmon(index(xtsData)), 1, 7),
Date = index(xtsData))

# Give a name to the data in case of only one timeseries
if (is.null(names(xtsData))) names(xtsData) <- "data"
Expand Down
9 changes: 4 additions & 5 deletions R/morrisjs.R
Expand Up @@ -11,26 +11,24 @@
#' @param width the width of the widget (in pixels),
#' @param height the height of the widget (in pixels).
#'
#' @import htmlwidgets zoo
#' @importFrom htmlwidgets createWidget shinyWidgetOutput shinyRenderWidget
#' @export
morrisjs <- function(data, width = NULL, height = NULL) {

# forward options using x
x = list(
data = data,
element = paste0("htmlwidget-", digest::digest(Sys.time())),
resize = TRUE,
hideHover = TRUE
)

# create widget
htmlwidgets::createWidget(
createWidget(
name = 'morrisjs',
x,
width = width,
height = height,
package = 'morrisjs',
elementId = x$element
package = 'morrisjs'
)
}

Expand All @@ -55,3 +53,4 @@ renderMorrisjs <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
shinyRenderWidget(expr, morrisjsOutput, env, quoted = TRUE)
}

2 changes: 1 addition & 1 deletion R/pipe.R
Expand Up @@ -8,5 +8,5 @@
#' @param rhs a charting function
#' @rdname pipe
#' @examples
#' morrisjs(mdeaths) %>% mjsLine
#' morrisjs(mdeaths) %>% mjsLine()
`%>%` <- magrittr::`%>%`
80 changes: 38 additions & 42 deletions inst/htmlwidgets/morrisjs.js
Expand Up @@ -3,49 +3,45 @@ HTMLWidgets.widget({
name: 'morrisjs',

type: 'output',

initialize: function(el, width, height) {


factory: function(el, width, height) {
return {
renderValue: function(x) {
x.element = el.id;

// Workaround if only one series is plotted
if ("ykeys" in x){
if (x.ykeys.constructor !== Array) {
x.ykeys = new Array(x.ykeys);
}
if (x.labels.constructor !== Array) {
x.labels = new Array(x.labels);
}
}

// Create the graph
var mjs;
switch(x.type){
case "Line":
mjs = new Morris.Line(x);
break;
case "Area":
mjs = new Morris.Area(x);
break;
case "Bar":
mjs = new Morris.Bar(x);
break;
case "Donut":
mjs = new Morris.Donut(x);
break;
}
// Draw it
mjs.draw();
},

resize: function(x, width, height) {

}
};

},

renderValue: function(el, x, instance) {

// Workaround if only one series is plotted
if ("ykeys" in x){
if (x.ykeys.constructor !== Array) {
x.ykeys = new Array(x.ykeys);
}
if (x.labels.constructor !== Array) {
x.labels = new Array(x.labels);
}
}

// Create the graph
var mjs;
switch(x.type){
case "Line":
mjs = new Morris.Line(x);
break;
case "Area":
mjs = new Morris.Area(x);
break;
case "Bar":
mjs = new Morris.Bar(x);
break;
case "Donut":
mjs = new Morris.Donut(x);
break;
}
// Draw it
mjs.draw();

},

resize: function(el, width, height, instance) {

}

});
2 changes: 1 addition & 1 deletion man/pipe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc305de

Please sign in to comment.