3

For example, on a dygraph like that:

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)

I would like to customize the label as follows. Instead of

Jan, 1974
Feb, 1974
etc.

I would like to see:

Jan, 1974 (1)
Feb, 1974 (2)
etc.

I don't care if the counter is concatenated on the date, I just want to see the period incremental number, as the mouse moves over the series. (Of course without displaying it as a series on the graph)

Brani
  • 6,454
  • 15
  • 46
  • 49

2 Answers2

4

You can create a customised valueFormatter, and use the row argument to capture the period number. Here is an example:

library(dygraphs)
library(htmlwidgets)
lungDeaths <- cbind(mdeaths, fdeaths)

date_counter <- 'function(d,opts, seriesName, dygraph, row, col){
              var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
              date = new Date(d);
              return monthNames[date.getMonth()]+", " +date.getFullYear() + " (" + row.toString() + ")";
}'

dygraph(lungDeaths) %>%
  dyAxis("x",valueFormatter=JS(date_counter))
NicE
  • 21,165
  • 3
  • 51
  • 68
0

Quick hack -- make a column in the data but hide the visual cues for that row in the chart:

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths, row = 1:length(mdeaths))
dygraph(lungDeaths) %>%
  dySeries("row", strokeWidth = 0, pointSize = 0, color = "white")

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Although the graph is not shown, it exists and, unfortunately, it affects the y-axis boundaries.. (notice the white dot on the x axis, over Jan 1974) – Brani May 23 '19 at 06:01