【发布时间】:2017-10-16 15:05:24
【问题描述】:
我需要使用像小时:分钟这样的时间格式来格式化我的 x 轴,但它的标签需要更完整的年/月/日小时:分钟:秒
有没有办法做到这一点?我为我的 xAxis 使用选项 tickFormat 但它显示在我的 x 轴上并标记相同的信息...
如何做到这一点?我读过一些关于“tickValues”的内容,但无法弄清楚。
我的代码:
var rangeSelector = document.getElementsByTagName('select')[0]; //1h 24h 1w custom
controllerScope.statusViewChartOptions = {
chart: {
type: 'lineChart',
height: 600,
staggerLabels: true,
margin: {
top: 20,
right: 20,
bottom: 70,
left: 30
},
forceY: [0],
x: function (d) {
return d.x;
},
y: function (d) {
return d.y;
},
useInteractiveGuideline: true,
duration: 500,
noData: 'No data to show in the specified range',
xAxis: {
axisLabel: 'Hours',
axisLabelDistance: 20,
showMaxMin: false,
tickFormat: function (d) {
if(rangeSelector.value == "1h"){
return d3.time.format("%H:%M")(new Date(d));
} else if(rangeSelector.value == "24h"){
return d3.time.format("%H:%M")(new Date(d));
} else if(rangeSelector.value == "1w"){
return d3.time.format("%d-%m")(new Date(d));
} else if(rangeSelector.value=="custom"){
return d3.time.format("%d %b")(new Date(d));
}
},
rotateLabels: -45
},
yAxis: {
showMaxMin: false,
tickFormat: function (d) {
return d3.format('d')(d);
}
},
title: {
enable: true,
text: 'Status'
},
dispatch: {
stateChange: function (e) {
console.log("stateChange");
$scope.api.refresh();
},
changeState: function (e) {
console.log("changeState");
$scope.api.refresh();
},
tooltipShow: function (e) {
console.log("tooltipShow");
$scope.api.refresh();
},
tooltipHide: function (e) {
console.log("tooltipHide");
$scope.api.refresh();
}
}
}
};
};
【问题讨论】:
标签: javascript angularjs d3.js nvd3.js