Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use composite bar in markdown? #13

Closed
ghost opened this issue Sep 25, 2015 · 13 comments
Closed

How to use composite bar in markdown? #13

ghost opened this issue Sep 25, 2015 · 13 comments

Comments

@ghost
Copy link

ghost commented Sep 25, 2015

How can I use composite bar?
When I run this in Rstudio:
sparkline(as.vector(unlist(table_to_chart$procent_to_call[1] )), type="bar")

sparkline(as.vector(unlist(table_to_chart$procent_to_call[1] )), type="line", composite = T)

I get error:
Attempted to attach a composite sparkline to an element with no existing sparkline

@ramnathv
Copy link
Contributor

Can you point me to the equivalent javascript code?

@ghost
Copy link
Author

ghost commented Sep 25, 2015

I do not know Java. Is there possibility to create composite bar in R ?

@ghost
Copy link
Author

ghost commented Sep 28, 2015

@ramnathv
I'd like to do to something like this:
$('#sparkline').sparkline([5, 4, 5, -2, 0, 3], { type: 'bar', barColor: '#aaf' }); $('#sparkline').sparkline([4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7], { composite: true, fillColor: false, lineColor: 'red' });

with that kind of code:
{"x":{"values":[0,12,22,38,20,32,32,21],"options":{"type":"bar", "height":20,"width":60},"width":60,"height":20},"evals":[]} {"x":{"values":[0,12,22,38,20,32,32,21],"options":{"composite":true, "type":"line", "height":20,"width":60},"width":60,"height":20},"evals":[]}

but I do not understand syntax. Can you help me?

@ramnathv
Copy link
Contributor

I get it now. This package does not support composite plots currently, but I have a pending PR that will make that possible. I don't have an ETA on when those changes would get merged in, but when I do merge them in, I will leave a note for you here.

@ghost
Copy link
Author

ghost commented Sep 29, 2015

Thanks for response. I can't wait you drop this feature. It'll be very useful for me!

@jaredlander
Copy link

Did this get merged?

@danklotz
Copy link

danklotz commented Jan 7, 2016

Hm... isn't there any possibility to do in the meantime?

@timelyportfolio
Copy link
Collaborator

timelyportfolio commented May 13, 2016

@ramnathv, I would love to see this. Will push forward with this unless you tell me no :) I know yours will be way more elegant though.

@timelyportfolio
Copy link
Collaborator

timelyportfolio commented May 13, 2016

@tazik, I'll make this much better.

With existing sparkline using onRender

With the newest Github version of htmlwidgets,

devtools::install_github("ramnathv/htmlwidgets")

we get onRender, so we could do something like this.

# hack to provide composite charts
# https://github.com/htmlwidgets/sparkline/issues/13

library(htmlwidgets)
library(sparkline)

onRender(
  sparkline(
    c(5,4,5,-2,0,3),
    type='bar',
    barColor="#aaf",
    chartRangeMin=-5,
    chartRangeMax=10
  ),
"
function(el,x){
$(el).sparkline(
  [4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7],
  {
    composite: true,
    fillColor: false,
    lineColor: 'red',
    chartRangeMin: -5,
    chartRangeMax: 10
  }
)
}
"
)

With hack in experimental forked branch


# here is another way if we use experimental forked branch
#  but I still don't like this way of accomplishing
#  I think a addComposite function would be better

# first install from Github
#devtools::install_github("timelyportfolio/sparkline@update/sparkline")

library(sparkline)
library(htmltools)

sl1 <- sparkline(
  c(5,4,5,-2,0,3),
  type='bar',
  barColor="#aaf",
  chartRangeMin=-5,
  chartRangeMax=10,
  # set an id that will make it easier to refer
  #  in the next sparkline
  elementId="sparkline-for-composite"
)
sl2 <- sparkline(
  c(4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7),
  type="line",
  fillColor = FALSE,
  lineColor ='red',
  chartRangeMin = -5,
  chartRangeMax = 10,
  # will need to set composite = TRUE
  composite = TRUE,
  # and set renderSelector to the id we set in the previous sparkline
  renderSelector = "#sparkline-for-composite"
)
browsable(tagList(sl1,sl2))

With helper function and experimental forked branch

# first install from Github
#devtools::install_github("timelyportfolio/sparkline@update/sparkline")
sl1 <- sparkline(
  c(5,4,5,-2,0,3),
  type='bar',
  barColor="#aaf",
  chartRangeMin=-5,
  chartRangeMax=10
)

# try to make a function to add a composite to an existing sparkline
#  of course if we did this for real, we would make much more robust
#  also, I added some JavaScript code to handle composites
addComposite <- function(sparkline=NULL, ...){
  stopifnot(!is.null(sparkline),inherits(sparkline,"sparkline"))

  sparkline_options <- list(...)
  sparkline_options$options$composite <- TRUE
  sparkline$x$composites[[length(sparkline$x$composites)+1]] <- sparkline_options
  return(sparkline)
}

addComposite(
  sl1,
  values=c(4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7),
  options = list(
    type="line",
    fillColor = FALSE,
    lineColor ='red',
    chartRangeMin = -5,
    chartRangeMax = 10
  )
)

@timelyportfolio
Copy link
Collaborator

cc @diegocgaona, if interested in potential new sparkline functionality.

@timelyportfolio
Copy link
Collaborator

As I work through this, I think a composite function addComposite like this would be more flexible.

# first install from Github
#devtools::install_github("timelyportfolio/sparkline@update/sparkline")
sl1 <- sparkline(
  c(5,4,5,-2,0,3),
  type='bar',
  barColor="#aaf",
  chartRangeMin=-5,
  chartRangeMax=10,
  # set an id that will make it easier to refer
  #  in the next sparkline
  elementId="sparkline-for-composite"
)
sl2 <- sparkline(
  c(4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7),
  type="line",
  fillColor = FALSE,
  lineColor ='red',
  chartRangeMin = -5,
  chartRangeMax = 10,
  # will need to set composite = TRUE
  composite = TRUE,
  # and set renderSelector to the id we set in the previous sparkline
  renderSelector = "#sparkline-for-composite"
)

# try to make a function to add a composite to an existing sparkline
addComposite <- function(sparkline=NULL, sparklineToAdd=NULL, ...){
  stopifnot(
    !is.null(sparkline),
    inherits(sparkline,"sparkline")
  )

  sparkline_options <- list()

  # if a sparkline is provided to add
  #   then get its values and options
  if(!is.null(sparklineToAdd)) {
    sparkline_options <- list(
      values = sparklineToAdd$x$values,
      options = sparklineToAdd$x$options
    )
  }

  # if ... are provided
  #   then use these for values and options
  if(length(list(...)) > 0) {
    sparkline_options <- modifyList(sparkline_options,list(...))
  }

  sparkline_options$options$composite <- TRUE
  sparkline$x$composites[[length(sparkline$x$composites)+1]] <- sparkline_options
  return(sparkline)
}

# add sparkline as a composite
addComposite(sl1, sl2)

# add values and options as a composite
addComposite(
  sl1,
  values=c(4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7),
  options = list(
    type="line",
    fillColor = FALSE,
    lineColor ='red',
    chartRangeMin = -5,
    chartRangeMax = 10
  )
)

# add combination of sparkline and options as a composite
addComposite(
  sl1,
  sl2,
  options = list(
    type="box"
  )
)

timelyportfolio added a commit to timelyportfolio/sparkline that referenced this issue May 16, 2016
@diegocgaona
Copy link

diegocgaona commented May 16, 2016

Hi @timelyportfolio , sorry for this dumb question, but how can I install your dev version to test?
thanks!

EDIT: Sorry for may lack of attention, I was in my work and didn't read the code, now I see the fork to install

Now I tested it, and I loved it! When you finish the update I will modify my project to use it in several graphs.

One doubt, how I set the size of the spark? I tried some like this (as I did in the without composite) but the size don't change

sl1 <- sparkline(
    c(5,4,5,-2,0,3),
    type='bar',
    barColor="#aaf",
    chartRangeMin=-5,
    chartRangeMax=10,
    width = 200, # the normal argument to set the width
    height = 32, # the normal argument to set the heigth
    # set an id that will make it easier to refer
    #  in the next sparkline
    elementId="sparkline-for-composite"
)
sl2 <- sparkline(
    c(4,1,5,7,9,9,8,7,6,6,4,7,8,4,3,2,2,5,6,7),
    type="line",
    fillColor = FALSE,
    lineColor ='red',
    chartRangeMin = -5,
    chartRangeMax = 10,
    width = 200,
    height = 32,
    # will need to set composite = TRUE
    composite = TRUE,
    # and set renderSelector to the id we set in the previous sparkline
    renderSelector = "#sparkline-for-composite"
)

# add combination of sparkline and options as a composite
addComposite(
    sl1,
    sl2,
    options = list(
        type="line",
        width=200, # I tried with this line and without, but I had the same result, the default size.
        heigth=32 # I tried with this line and without, but I had the same result, the default size.
        )
)

Thanks!!

@timelyportfolio
Copy link
Collaborator

I will close this for now. Please reopen if not addressed. I will open a separate issue for composites of different types no longer lining up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants