[英]Time Formatting in D3js
I have a d3 line plot that I am plotting out the dates by month/day. 我有一个d3线图,它按月/日绘制日期。 The problem is the x-axis defaults to showing year "1900" at the axis origin. 问题是x轴默认在轴原点显示年份“ 1900”。 Is there a way I can change this to default to "January" instead of "1900" as my lines are where I am highlighting different years. 有没有一种方法可以将其更改为默认值,即“ January”而不是“ 1900”,因为我要强调的是不同年份。 My plnkr is below 我的plnkr在下面
http://plnkr.co/edit/22Jc4Nte0WWLQzT7a9lr?p=preview http://plnkr.co/edit/22Jc4Nte0WWLQzT7a9lr?p=preview
I'm assuming the change is made in either of the below lines, however, I'm not sure the way to change it. 我假设在以下两行中进行了更改,但是,我不确定更改的方式。
var x = d3.scaleTime().range([0, width]),
or 要么
x.domain(d3.extent(data, function(d) { return d.date; }));
You have to define tickFormat
for the x-axis using %b
so it knows only to display abbreviated month names. 您必须使用%b
为x轴定义tickFormat
,以便它仅显示缩写的月份名称。 Check here for time formatting options in d3. 在此处查看d3中的时间格式选项。
.call(d3.axisBottom(x)
.tickFormat(d3.timeFormat("%b"))
);
Check the updated Plunkr - http://plnkr.co/edit/7ZUSQJduYPL5VNAxBG5u?p=preview 检查更新的Plunkr- http: //plnkr.co/edit/7ZUSQJduYPL5VNAxBG5u?p=preview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.