Coder Social home page Coder Social logo

Comments (13)

 avatar commented on August 31, 2024 1

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

from sparkline.

timelyportfolio avatar timelyportfolio commented on August 31, 2024 1

@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
  )
)

from sparkline.

ramnathv avatar ramnathv commented on August 31, 2024

Can you point me to the equivalent javascript code?

from sparkline.

 avatar commented on August 31, 2024

@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?

from sparkline.

ramnathv avatar ramnathv commented on August 31, 2024

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.

from sparkline.

 avatar commented on August 31, 2024

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

from sparkline.

jaredlander avatar jaredlander commented on August 31, 2024

Did this get merged?

from sparkline.

danklotz avatar danklotz commented on August 31, 2024

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

from sparkline.

timelyportfolio avatar timelyportfolio commented on August 31, 2024

@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.

from sparkline.

timelyportfolio avatar timelyportfolio commented on August 31, 2024

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

from sparkline.

timelyportfolio avatar timelyportfolio commented on August 31, 2024

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"
  )
)

from sparkline.

diegocgaona avatar diegocgaona commented on August 31, 2024

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!!

from sparkline.

timelyportfolio avatar timelyportfolio commented on August 31, 2024

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.

from sparkline.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.