rstatcons / slides0

0 stars 0 forks source link

Localization of dygraph ts #2

Closed malexan closed 8 years ago

malexan commented 8 years ago

http://stackoverflow.com/q/20332764/3190110

Need to use axisLabelFormatter JS-function. The question is how to use this function from R.

malexan commented 8 years ago

OK, there is a axisLabelFormatter option in dyAxis() function:

JavaScript function to call to format the tick values that appear along an axis (see the dygraphs documentation for additional details).

And based on SOF answer the function should look like this:

function(date, granularity, opts, dygraph) {
                    var months = ['leden', 'únor', 'březen', 'duben', 'květen',
                                  'červen', 'červenec', 'srpen', 'září',
                                  'říjen', 'listopad', 'prosinec'];
                    return months[date.getMonth()] + " " + date.getFullYear()
}

So should I pass it just like a character string?

malexan commented 8 years ago

Passing JavaScript functions

malexan commented 8 years ago

Don't need to pass a function as shown in link above.

Here is a working example:

dygraph( samp.xts ) %>%
  dyAxis(name="x",
         valueFormatter="function(d){ var date = new Date(d); return (date.getSeconds()/10) }",
         axisLabelFormatter="function(d){ return (d.getSeconds()/10) }"
  )
malexan commented 8 years ago

So working example:

## Setting system locale to extract names of months from OS
## ru_RU.itf8 as in LInux. Probably UTF-8 in Windows
Sys.setlocale("LC_TIME","ru_RU.utf8")

## Extraction of months' names
months <- format(ISOdate(2004,1:12,1),"%B")

## Converting names to a string for JS-function
months <- paste0("'",
                 paste0(months, collapse = "', '"),
                 "'")

## Forming JS-function
axlabform <- paste0(
  "function(date, granularity, opts, dygraph) {
  var months = [",
  months,
  "]; return months[date.getMonth()] + \" \" + date.getFullYear()}")

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths) %>% 
        dyAxis("x", axisLabelFormatter = axlabform)
malexan commented 8 years ago

Closed with: https://github.com/rstatcons/demoapp/commit/47bbd20c6f49327f3ff66744e73c516aef81dfcc