Search code examples
rhighchartsrcharts

highcharts style modification in R


I hope to change attributes in highcharts, which is a part of rCharts R package. Also, I hope to do this by using R, not by web related or any other language.

In any highcharts example, we can see those default attributes under style tag are as follow:

 <style>
     .rChart {
          display: block;
          margin-left: auto; 
          margin-right: auto;
          width: 800px;
          height: 400px;
        }    
 </style>

I hope to modify this to:

<style>
    .rChart {
      display: block;
      margin-left: auto; 
      margin-right: auto;
      width: 100%;
      height: 100%;
      position: absolute
    }  
</style>

I tried to find how to do this in the reference (https://media.readthedocs.org/pdf/rcharts/latest/rcharts.pdf), but I could not find it. I'd appreciate if someone let me know this.


Solution

  • I think the best way to do it would be to generate the highcharts-specific code only and insert it in an HTML file containing your custom CSS. Otherwise, if you want to adjust the style direclty from R, you can access the width and height attributes of your chart through chart$params$width and chart$params$height respectively. However, it seems that you need to provide a value in pixels, hence my recommendation to adjust this property outside of rCharts.

    Here is a small example, based on the code provided in the quick start page of the package's website, to adjust the width and height from R:

    library(rCharts)
    h1 <- hPlot(x = "Wr.Hnd", y = "NW.Hnd", data = MASS::survey, type = c("line", 
    "bubble", "scatter"), group = "Clap", size = "Age")
    h1$params$width <- 1000
    h1$params$height <- 1000
    print(h1) # Display the chart
    

    If you want to get only the highcharts-specififc code (div + chart JS), for use in an external web page:

     chartCode <- capture.output(chart$print("chart_id"))
     chartCode <- paste(chartCode, collapse='') # If you want a single string containing the code, that can be exported as you please.