3

例如,在这样的 dygraph 上:

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

我想自定义标签如下。代替

1974 年 1 月 1974 年
2 月

我想看看:

1974 年 1 月 (1)
1974 年 2 月 (2)
等。

我不在乎计数器是否在日期上连接,我只想查看期间增量数字,因为鼠标在系列上移动。(当然没有在图表上显示为系列)

4

2 回答 2

4

您可以创建一个自定义的valueFormatter,并使用row参数来捕获周期数。这是一个例子:

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))
于 2019-05-23T16:31:48.777 回答
0

快速破解——在数据中创建一列,但在图表中隐藏该行的视觉提示:

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

在此处输入图像描述

于 2019-05-22T23:02:05.430 回答