将日期格式更改为字符串d3 js

时间:2019-01-25 06:11:40

标签: javascript d3.js graph

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

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

grd = input('Enter the letter grade for your first class\n')
if grd != "":
    grd_num.append(grades[grd])
    cred.append(float(input('Enter the amount of credits that your first class is worth\n')))
else:
    grd_num.append(0.0)
    cred.append(0.0)

if grd != "":
    grd = input('Enter the letter grade for your second class\n')
    if grd != "":
        grd_num.append(grades[grd])
        cred.append(float(input('Enter the amount of credits that your second class is worth\n')))
    else:
        grd_num.append(0.0)
        cred.append(0.0)
else:
    grd_num.append(0.0)
    cred.append(0.0)


if grd != "":
    grd = input('Enter the letter grade for your third class\n')
    if grd != "":
        grd_num.append(grades[grd])
        cred.append(float(input('Enter the amount of credits that your third class is worth\n')))
    else:
        grd_num.append(0.0)
        cred.append(0.0)
else:
    grd_num.append(0.0)
    cred.append(0.0)

# REPEAT REPEAT REPEAT ...

if grd != "":
    grd = input('Enter the letter grade for your eighth  class\n')
    if grd != "":
        grd_num.append(grades[grd])
        cred.append(float(input('Enter the amount of credits that your eighth class is worth\n')))
    else:
        grd_num.append(0.0)
        cred.append(0.0)
else:
    grd_num.append(0.0)
    cred.append(0.0)

我想用字符串代替日期。

1 个答案:

答案 0 :(得分:1)

您可以根据自己的喜好使用tickFormat格式化轴刻度。只需将其传递给您自己的函数即可,该函数将根据数据返回所需的字符串(该函数可以访问当前的 d 数据和 i ndex)。

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

Codepen