[英]D3 charts change the time format and its display on the axes
Im trying to change the time format of the data set of the charts 我正在尝试更改图表数据集的时间格式
https://jsfiddle.net/d0ogL90d/2/ https://jsfiddle.net/d0ogL90d/2/
I'v tried to do the follwoing but exception is throwing: 我尝试进行以下操作,但抛出异常:
var xExtents = d3.extent(d3.merge(dataset), function (d) {
return return d3.time.format('%A %m/%d/%y')(new Date(d.x));
});
if dx is been returned then it display date but not in the desired format 如果返回了dx,则显示日期,但格式不正确
I also tried to change the data array by: 我还尝试通过以下方式更改数据数组:
function createADateArray(){
var arr = [];
temparr = [];
var date = new Date();
arr.push(temparr);
temparr = [];
for(i=1; i < 6; i++){
var date2 = date.setDate(date.getDate() + 1);
var cdate = d3.time.format('%A %m/%d/%y')(new Date(date2))
var obj = {
'x': cdate,
'y': i
}
temparr.push(obj);
}
arr.push(temparr);
// }
return arr;
}
The data is correct anf with the desired format but again exception is throwing 数据正确且具有所需格式,但再次引发异常
*desired format: Tue Jan 10 *所需格式:1月10日星期二
To create your date entries in your data, just add 1 day offsets to your now
var using d3.time.day.offset(now, offset++)
要在数据中创建日期条目,只需使用
d3.time.day.offset(now, offset++)
将1天的偏移量添加到now
d3.time.day.offset(now, offset++)
Try these mods which are working for me, at least the date format of the X-axis labels: 尝试这些对我有用的模块,至少是X轴标签的日期格式:
var now = d3.time.hour.utc(new Date);
function createADateArray(){
var arr = [];
temparr = [];
// var date = new Date();
//for(j=0; j< 4; j++){
/*for(i=0; i < 1000; i++){
var obj = {
'x': i,
'y': i
}
temparr.push(obj);
arr.push(temparr);
}*/
temparr = [];
// for(i=1004; i < 2000; i++){
var offset = 0;
for(i=1004; i < 1010; i++){
//var date2 = date.setDate(date.getDate() + 1);
//date = new Date(date2);
var obj = {
// 'x': date,
'x': d3.time.day.offset(now, offset++),
'y': i
}
temparr.push(obj);
}
arr.push(temparr);
// }
return arr;
}
...
// d3.time.format("%A %B");
var dateFormat = d3.time.format("%a %b %e %I%p");
var xScale = d3.time.scale()
// .domain([new Date(xExtents[0]), d3.time.day.offset(new Date(xExtents[xExtents.length - 1]), 1)])
.domain(xExtents)
.range([padding, w - padding * 6]);
...
//Define X axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickFormat(dateFormat);
// .ticks(1000);
you can use .tickFormat(d3.time.format("%A %Y-%m-%d")) 您可以使用.tickFormat(d3.time.format(“%A%Y-%m-%d”))
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(-height)
.tickFormat(d3.time.format("%A %Y-%m-%d"))
.ticks(5);
http://codepen.io/anon/pen/mVMjvm?editors=101 http://codepen.io/anon/pen/mVMjvm?editors=101
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.