简体   繁体   English

从零开始d3轴时间刻度

[英]Starting a d3 axis timescale from zero

I'm creating a graph that uses a time scale. 我正在创建一个使用时间刻度的图形。 I'd like the X axis to show times starting from zero, and counting up in intervals of 5 seconds each. 我希望X轴显示从零开始的时间,并以5秒的间隔向上计数。

--------------------------------
:00  :05  :10  :15  :20  :25  :30

My data has timestamps, which are coerced to date objects. 我的数据有时间戳,这些时间戳被强制为日期对象。 The scales are based on those timestamps. 比例基于这些时间戳。

(Note: I'm using d3 v4/5). (注意:我正在使用d3 v4 / 5)。

D3 has a nifty function for displaying time intervals, but the intervals start from the time in the data objects. D3具有显示时间间隔的漂亮功能,但是间隔从数据对象中的时间开始。

I could convert my data so all the times start on zero, but it would create additional problems as I'm lining up multiple datasets along the same timescale. 我可以转换数据,使所有时间都从零开始,但是当我在同一时间范围内排列多个数据集时,它将产生其他问题。 I'm wondering if there is a more elegant way to use one of the tick formatting functions for displaying ticks that are different than the scale or to calculate on the fly to start from zero seconds. 我想知道是否有一种更优雅的方法来使用刻度线格式设置功能之一来显示与刻度不同的刻度线,或者从零秒开始即时计算。

Thanks in advance. 提前致谢。

EDIT: Here is a more completed code snippet. 编辑:这是一个更完整的代码段。

 var videoData = [{ "id": "1", "user_id": "_jlxvt8445494296", "video_id": "test", "time": "2018-09-11 15:39:20", "metric": "4" }, { "id": "2", "user_id": "_jlxvt8445494296", "video_id": "test", "time": "2018-09-11 15:39:26", "metric": "2" }, { "id": "3", "user_id": "_jlxvt8445494296", "video_id": "test", "time": "2018-09-11 15:39:27", "metric": "3" }, { "id": "4", "user_id": "_jlxvt8445494296", "video_id": "test", "time": "2018-09-11 15:39:38", "metric": "1" } ]; var svg = d3.select("svg"), width = 400, height = 200; var parseTime = d3.timeParse("%Y-%m-%d %H:%M:%S"); //coerce to d3 timeParse object videoData.forEach(function(d) { d.time = parseTime(d.time); }); var x = d3.scaleTime() .rangeRound([0, width]) .domain(d3.extent(videoData, function(d) { return d.time; })); var y = d3.scaleLinear() .rangeRound([height, 0]) .domain(d3.extent(videoData, function(d) { return d.metric; })); var line = d3.line() .x(function(d) { return x(d.time); }) .y(function(d) { return y(d.metric); }) .curve(d3.curveBasis); svg.append("g") .attr("transform", "translate(10," + height + ")") .call(d3.axisBottom(x).tickFormat(d3.timeFormat(":%S"))); svg.append("path") .datum(videoData) .attr("fill", "none") .attr("stroke", "steelblue") .attr("d", line); 
 <script src="https://d3js.org/d3.v5.min.js"></script> <svg width="420" height="220"></svg> 

You have to do the calculations all relative to the start of the video. 您必须相对于视频的开头进行所有计算。

Calculate the time difference in ms relative to the start of the video and convert that ms-time to a date new Date(ms) . 计算时差ms相对于视频的开始和MS-时间转换为日期new Date(ms) You get the ms-time of a Date with datevar.getTime() . 您可以使用datevar.getTime()获得日期的毫秒时间。

To get past the timezone you have to do all display of the time in UTC. 要越过时区,您必须以UTC显示所有时间。

Below an axis that displays the time from 00:00 to 01:40, the first 100 seconds with every 5 seconds a tick. 在显示时间从00:00到01:40的轴下方,这是前100秒,每5秒打一个勾。

 var parseTime = d3.timeParse("%Y-%m-%d %H:%M:%S");//2018-09-10 21:06:34 var formatSecond = d3.utcFormat(":%S"), formatMinute = d3.utcFormat("%M:%S"); function multiFormat(date) { return (d3.timeMinute(date) < date ? formatSecond : formatMinute)(date); } var width = 500; var height = 100; var g = d3.select("#chart").append("svg").attr("width", width + 40).attr("height", height + 40) .append("g").attr("transform", "translate(20,20)"); var x = d3.scaleUtc() .rangeRound([0, width]) //.domain(d3.extent(data, function(d) { return d.time; })); .domain([new Date(0), new Date(100*1000)]); g.append("g") .attr("transform", "translate(0," + height + ")") //.call(d3.axisBottom(x).ticks(d3.timeSecond.every(5))); .call(d3.axisBottom(x).ticks(d3.utcSecond.every(5)).tickFormat(multiFormat)); 
 <script src='http://d3js.org/d3.v5.min.js' charset='utf-8'></script> <div id="chart"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM