Dygraphs:JS函数为axisLabelFormatter工作,但不适用于valueFormatter [英] Dygraphs: JS function working for axisLabelFormatter but not for valueFormatter
问题描述
我正在使用一个Shiny应用程序,用Dygraphs软件包来显示制造过程数据。我的数据看起来像这样(时间(秒)):
I am working on a Shiny app for visualizing a manufacturing proccess data with Dygraphs package. My data look like this (Time in seconds):
Time Var1 Var2
0.05 0.2199809 180.2101
0.10 0.2199809 180.1942
0.15 0.2358754 180.1465
0.20 0.2222697 180.1306
0.25 0.2222697 180.1306
0.30 0.1768748 180.1465
首先,我将数据转换为xts格式(我乘以1000,因为Dygraphs在同一秒内折叠观察结果,并且我无法解决这个问题):
First of all I transform my data into xts format (I multiply by 1000 because Dygraphs collapses the observations inside the same second when plotting and I cannot solve this problem):
data_xts<-xts(data, as.POSIXct(1000*data[,"Time"], origin = "1970-01-01"))
这样产生一个这样的xts对象:
This produces a xts object like this:
Time Var1 Var2
1970-01-01 00:00:50 0.05 0.2199809 180.2101
1970-01-01 00:01:40 0.10 0.2199809 180.1942
1970-01-01 00:02:30 0.15 0.2358754 180.1465
1970-01-01 00:03:20 0.20 0.2222697 180.1306
1970-01-01 00:04:10 0.25 0.2222697 180.1306
1970-01-01 00:05:00 0.30 0.1768748 180.1465
现在我想用几秒钟的x轴标签获得Dygraph图在时间变量中)以及图例x值(以秒为单位显示的值)。为此,我已经在我的server.R脚本中声明了一个JS函数来提取从时间起始的秒数:
Now I would like to get a Dygraph plot with x axis labels in seconds (like in Time variable) and also with legend x values -the ones which appear with mouseover- in seconds. For that purpose I have declared a JS function in my server.R script to extract seconds passed since time origin:
getsecs<- 'function(d) {
var day=d.getDate();
var hour=d.getHours();
var minute=d.getMinutes();
var second=d.getSeconds();
var secs=84600*(day - 1)+3600*hour+60*minute +second;
return 0.001*secs;}'
然后,当使用Dygraph for闪亮输出:
Then I use this function when consrtucting the Dygraph for Shiny output:
output$mygraph <- renderDygraph({
VAR<-data_xts()[,c('Var1','Var2')]
dygraph(VAR) %>%
dySeries(colnames(VAR)[2], axis = 'y2') %>%
dyAxis("x",axisLabelFormatter=JS(getsecs), valueFormatter=JS(getsecs)) %>%
dyAxis("y", label = colnames(VAR)[1]) %>%
dyAxis("y2", label = colnames(VAR)[2], independentTicks = TRUE) %>%
dyOptions(drawGrid = input$showgrid) %>%
dyOptions(drawPoints = TRUE, pointSize = 2) %>%
dyHighlight(highlightCircleSize = 3, highlightSeriesOpts = list(strokeWidth = 3)) %>%
dyLegend(width = 800) %>%
dyRangeSelector()
})
这样我就得到了x轴的标签,但是鼠标悬停并不会起作用k(系列不高亮,所以x图例是空白的)。当我在控制台(F12)中搜索出现这个错误:
Doing this I get the x axis labels as I wanted but the mouseover does not work (the series does not highlight and so the x legend is blank). When I search in Console (F12) it appears this error:
d.getDate is not a function
似乎错误与getsecs函数输出的格式有关,但我不明白为什么它与labelFormatter一起使用,没有valueFormatter。
It seems that the error has something to do with the format of getsecs function output but I do not understand why it works with labelFormatter but not with valueFormatter.
任何帮助将不胜感激。感谢提前。
Any help will be appreciated. Thanks in advance.
推荐答案
我也遇到这个问题,现在我终于弄清楚了。
I was also having this problem and now I finally figured it out.
valueFormatter
- 返回 valueOf
日期
axisLabelFormatter
- 返回实际日期本身
axisLabelFormatter
- returns the actual date itself
所以为了使它们相同,您将不得不稍微修改您的功能 valueFormatter
:
So in order to make them the same, you will have to modify your function slightly for valueFormatter
:
getsecs2 <- 'function(e) {
var d = new Date(e);
var day=d.getDate();
var hour=d.getHours();
var minute=d.getMinutes();
var second=d.getSeconds();
var secs=84600*(day - 1)+3600*hour+60*minute +second;
return 0.001*secs;}'
更改 valueFormatter = JS(getsecs)
to valueFormatter = JS(getsecs2)
你也可以可能只是向 getsec
函数添加一个 if-else
语句,但是我会把它留给你,因为我很新到 javascript
。
You could also probably just add an if-else
statement to the getsec
function, but I will leave that to you as I am very new to javascript
.
这篇关于Dygraphs:JS函数为axisLabelFormatter工作,但不适用于valueFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!