【问题标题】:Change date format to string d3 js将日期格式更改为字符串 d3 js
【发布时间】:2019-01-25 06:11:39
【问题描述】:

我正在尝试将标签格式从日期更改为字符串。

我尝试更改格式,但没有成功。这是我的代码笔:https://codepen.io/sampath-PerOxide/pen/bzVOoG

var dateformat = "%Y-%m"
drawTimeSeriesGraph(data1, dateformat);

我想传递字符串来代替日期。

【问题讨论】:

标签: javascript d3.js graph


【解决方案1】:

您可以使用tickFormat 随意格式化轴刻度。只需将您自己的函数传递给它,该函数根据数据返回您想要的字符串(该函数可以访问当前的 datum 和 index)。

xAxis.tickFormat((d, i) => {
    const date = formatDate(d)
    return `${date} - Month ${i+1}`
});

Codepen

【讨论】:

    猜你喜欢
    • 2022-07-04
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2016-06-25
    • 1970-01-01
    • 2017-09-04
    相关资源
    最近更新 更多