gpt4 book ai didi

javascript - 让 Dygraph.WEEKLY 标签使用星期一作为 R 中的刻度

转载 作者:可可西里 更新时间:2023-11-01 02:47:17 24 4
gpt4 key购买 nike

我正在使用 dygraphs 绘制每周数据图表包裹。 dygraphs 自动选择每周默认轴。但是,我希望有一种方法可以让默认轴(星期日)与我的数据(星期一)对齐。

# Required packages
library("magrittr")
library("dygraphs")
library("xts")

# Data
daily <- structure(c(4000, 5000, 3000, 7000, 2000, 5000, 7000,
2000, 3000, 6000, 5000, 9000, 2000, 2000, 2000,
7000, 9000, 2000, 1000, 13000), .Dim = c(10L, 2L), .Dimnames = list(
NULL, c("col1", "col2")), index = structure(c(1476032400,
1476637200, 1477242000, 1477846800, 1478451600, 1479056400, 1479661200,
1480266000, 1480870800, 1481475600), tzone = "Asia/Saigon", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "Asia/Saigon", tzone = "Asia/Saigon")

# Graph
dygraph(daily, main = "Stackoverflow") %>%
dyRangeSelector() %>%
dyOptions(useDataTimezone = TRUE)

dygraph render

最佳答案

您可以尝试使用自定义 ticker 函数,该函数从数据的第一天开始,每 7 天打一次勾。

这是一个受 dygraph-tickers.js Dygraph.getDateAxis 函数启发的示例:

dygraph(daily, main = "Stackoverflow") %>%
dyRangeSelector() %>%
dyAxis("x",ticker='function(start_time, end_time, pixels, opts, dygraph, vals) {
var formatter = opts("axisLabelFormatter");
var ticks = [];
var t;
//spacing is a week in milliseconds
var spacing = 1000 * 604800;

for (t = start_time; t <= end_time; t += spacing) {
ticks.push({ v:t,
label: formatter(new Date(t), Dygraph.WEEKLY, opts, dygraph)
});
}
return ticks;
}') %>%
dyOptions(useDataTimezone = TRUE)

关于javascript - 让 Dygraph.WEEKLY 标签使用星期一作为 R 中的刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41449198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com