NVD3时间格式,线与焦点图
NVD3 time formatting , line with focus chart
我正在使用一个相当简单的带有焦点图的 nvd3 线示例。 myData
从我的 php 文件中返回一个 JSON 对象,其中 x cordinate 是 0-23 之间的数字。我想知道如何以小时格式格式化 x 轴。
d3.json('get_data.php', function (error, myData) {
// Renders a line chart
(function () {
nv.addGraph(function () {
var chart = nv.models.lineWithFocusChart();
chart.xAxis
.tickFormat(d3.format(''));
chart.yAxis
.tickFormat(d3.format(''));
chart.y2Axis
.tickFormat(d3.format(''));
d3.select("#chart svg")
.datum(myData)
.transition().duration(500)
.call(chart); //Define transition and pass the d3.selection to our lineChart.
nv.utils.windowResize(chart.update);
return chart; //Must return the enclosed chart variable so the global rendering queue can store it.
//});
});
})(); });
以下是 myData
中的示例 json 数据。我是否需要操纵它?
[{
"key": "data",
"values": [
{
"x": 0,
"y": 408175
},
{
"x": 1,
"y": 428739
},
{
"x": 2,
"y": 422141
},
{
"x": 3,
"y": 439864
},
{
"x": 4,
"y": 441409
}
]
}]
任何帮助,不胜感激。
我不起作用。chart.lines.xScale(d3.time.scale());chart.lines2.xScale(d3.time.scale());
在最新的 nvd3 中,您必须使用以下方法来更新 xScale。chart.xScale(d3.time.scale());chart.focus.xScale(d3.time.scale());
希望它对某人有所帮助。
经过大量搜索,它可以使用此代码。
chart.lines.xScale(d3.time.scale());
chart.lines2.xScale(d3.time.scale());
var tickMultiFormat = d3.time.format.multi([
["%-I:%M%p", function(d) { return d.getMinutes(); }], // not the beginning of the hour
["%-I%p", function(d) { return d.getHours(); }], // not midnight
["%b %-d", function(d) { return d.getDate() != 1; }], // not the first of the month
["%b %-d", function(d) { return d.getMonth(); }], // not Jan 1st
["%Y", function() { return true; }]
]);
chart.xAxis.tickFormat(function (d) { return tickMultiFormat(new Date(d)); });
也是我对角度版本的解决方案:
var tickMultiFormat = d3.time.format.multi([
["%-I:%M%p", function(d) { return d.getMinutes(); }], // not the beginning of the hour
["%-I%p", function(d) { return d.getHours(); }], // not midnight
["%b %-d", function(d) { return d.getDate() != 1; }], // not the first of the month
["%b %-d", function(d) { return d.getMonth(); }], // not Jan 1st
["%Y", function() { return true; }]
]);
$scope.fraud_cases_area_options = {
chart: {
type: 'lineWithFocusChart',
height: 300,
noData: $scope.noDataMessage || 'No Data Available.',
margin: {
top: 20,
right: 20,
bottom: 30,
left: 40
},
color: ['#d62728', '#5DB56A', '#9E9E9E', '#aec7e8'],
x: function (d) {
"use strict";
return d[0];
},
y: function (d) {
"use strict";
return d[1];
},
xScale: d3.time.scale(),
focus: {
xScale: d3.time.scale(),
},
useVoronoi: false,
clipEdge: true,
duration: 100,
useInteractiveGuideline: true,
showControls: false,
showTotalInTooltip: true,
xAxis: {
showMaxMin: false,
tickFormat: function (d) { return tickMultiFormat(new Date(d)); }
/*function (d) {
"use strict";
return d3.time.format("%d.%m.%Y")(new Date(d))
}*/,
},
x2Axis: {
showMaxMin: false,
tickFormat: /*function (d) { return tickMultiFormat(new Date(d)); }*/
function (d) {
"use strict";
return d3.time.format("%d.%m.%Y")(new Date(d))
},
},
yAxis: {
tickFormat: function (d) {
"use strict";
// console.log("error",d3.format(',.2f')(d));
return d3.format(',.0f')(d);
}
},
zoom: {
enabled: false,
scaleExtent: [1, 10],
useFixedDomain: false,
useNiceScale: false,
horizontalOff: false,
verticalOff: true,
unzoomEventType: 'dblclick.zoom'
},
interactiveLayer: {
"tooltip": {
"duration": 0,
"gravity": "w",
"distance": 25,
"snapDistance": 0,
"classes": null,
"chartContainer": null,
"enabled": true,
"hideDelay": 0,
"fixedTop": null,
"headerEnabled": true,
"headerFormatter": function (d) {
return 'at ' + d3.time.format("%-I:%M%p")(new Date(d));
},
"hidden": true,
"data": null,
},
"margin": {
"left": 0,
"top": 0
},
"width": null,
"height": null,
"showGuideLine": true,
"svgContainer": null
},
}
}
- keyup事件处理程序更改焦点不适用于快速键入
- 单击时将焦点更改为元素
- Java脚本时间添加
- Json数据包含日期和时间格式
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 如何根据时间运行不同的脚本
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 在wordpress一定时间后更改自定义字段
- 更改angularjs中的日期-时间格式
- 将当前时间添加到我的页面上的特定部分
- 门户网站:当地时间有多有用
- 显示时间的脚本
- 如何在窗体打开时从javascript倒计时计时器值中节省时间
- 通过JSON&比较时间
- Javascript时间轨迹
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- 输入焦点关闭键盘,需要长时间触摸
- NVD3时间格式,线与焦点图
- 如何添加固定垂直轴的焦点+上下文缩放时间序列