rCharts nvd3 lineWithFocusChart Customization

JavascriptRnvd3.jsRcharts

Javascript Problem Overview


I am working with nvd3 on rCharts and was wondering if there was a way to customize the axes for the lower view finder graph on a lineWithFocusChart. I have provided a reproducible example below, where I customize the x and y axes to have commas separating the thousands place, but that formatting does not show up on the lower view finder chart. How could this be solved? Thank you!

      library(rCharts)
      temp <- data.frame(x = 1:2000, y = 1:2000, z = c(rep(1,1000), rep(0,1000)))
      g <- nPlot(y ~ x, group = "z", data = temp, type = "lineWithFocusChart")
      g$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle_styled.html"
      g$set(title = "Example")  
      g$chart(transitionDuration = -1,
              tooltipContent = "#! function(key, x, y) {
                                return 'z: ' + key + '<br/>' + 'x: ' + x + '<br/>' + 'y: ' + y 
                              }!#", 
              showLegend = FALSE, margin = list(left = 200, 
                                                right = 100, 
                                                bottom = 100,
                                                top = 100))               
      g$xAxis(axisLabel = "x",
              tickFormat = "#!function(x) {return d3.format(',.0f')(x);}!#")
      g$yAxis(axisLabel = "y", 
              width = 100,
              tickFormat = "#!function(y) {return d3.format(',.0f')(y);}!#",
              showMaxMin = FALSE)
      g

Javascript Solutions


Solution 1 - Javascript

I just discovered this as I was looking at unanswered questions tagged R. Sorry I missed it. rCharts has stalled, but look for an new version based on the more flexible htmlwidgets infrastructure. I am sure this answer is far too late, but I have changed the template to allow formatting for y2Axis.

# uncomment this to install the fix
#devtools::install_github("timelyportfolio/rCharts")

library(rCharts)
temp <- data.frame(x = 1:2000, y = 1:2000, z = c(rep(1,1000), rep(0,1000)))
g <- nPlot(y ~ x, group = "z", data = temp, type = "lineWithFocusChart")
g$templates$script <- "c:/users/kent.tleavell_nt/dropbox/development/r/rCharts_nvd3_templates/chartWithTitle_styled.html"
g$set(title = "Example")  
g$chart(transitionDuration = -1,
        tooltipContent = "#! function(key, x, y) {
        return 'z: ' + key + '<br/>' + 'x: ' + x + '<br/>' + 'y: ' + y 
        }!#", 
        showLegend = FALSE, margin = list(left = 200, 
                                          right = 100, 
                                          bottom = 100,
                                          top = 100))               
g$xAxis(axisLabel = "x",
        tickFormat = "#!function(x) {return d3.format(',.0f')(x);}!#")
g$yAxis(axisLabel = "y", 
        width = 100,
        tickFormat = "#!function(y) {return d3.format(',.0f')(y);}!#",
        showMaxMin = FALSE)
g$x2Axis(tickFormat = "#!function(x) {return d3.format('1.2s')(x);}!#")

# now we have a new y2Axis function
g$y2Axis(
  tickFormat = "#!function(y) {return d3.format('1.2s')(y);}!#"
)

g

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjohnny838View Question on Stackoverflow
Solution 1 - JavascripttimelyportfolioView Answer on Stackoverflow