简体   繁体   English

在D3.js的x轴上仅使用小时和分钟

[英]Use only hour and minutes on x-axis on D3.js

I have a data set that looks like this: 我有一个数据集,看起来像这样:

data_set=[{date: '2012,09,04,10,54,53'},
{date: '2012,09,05,10,58,57'},
{date: '2012,09,11,10,44,05'},
{date: '2012,09,04,11,25,30'},
{date: '2012,09,04,10,28,55'},
{date: '2012,09,04,12,30,17'},
{date: '2012,09,04,11,14,23'},
{date: '2012,09,04,12,16,10'},
{date: '2012,09,04,11,57,46'},
{date: '2012,09,04,11,15,53'},
{date: '2012,09,04,11,25,43'},];

I'm trying to make a scatter plot where the x-axis is the time of day, not the full date. 我正在尝试绘制一个散点图,其中x轴是一天中的时间,而不是整个日期。 How do I get the x-axis to show only a 24 period and how do I get the date points to plot appropriately along the x-axis? 如何使x轴仅显示24个周期,如何获取日期点以沿x轴正确绘制? Sound I be trying to use d3.time.scale() or should I simply use d3.linear.scale() and work around the problem in formatting? 声音我正在尝试使用d3.time.scale()还是应该简单地使用d3.linear.scale()并解决格式化问题? What I'd like to do is have each day of the week be a different point along the y-axis. 我想做的是让一周中的每一天都是沿着y轴的不同点。 An example plot is here . 这里有一个示例图。 In this example, 10=Monday, 20=Tuesday, etc. The different symbols represent the different days of the month for that particular day of the week. 在此示例中,10 =星期一,20 =星期二,依此类推。不同的符号代表一周中该天的不同日期。

So I've written the following functions to pull the date out of my string and particularly the hours and minutes: 因此,我编写了以下函数以将日期(特别是小时和分钟)从字符串中拉出:

var date_format = d3.time.format("%Y,%m,%d,%H,%M,%S");                                                                                                                               
var time_format = d3.time.format("%X");                                                                                                                                              

Using these functions I've written the following statements: 使用这些功能,我编写了以下语句:

dd = date_format.parse('2012,01,02,12,39,45')
Mon Jan 02 2012 12:39:45 GMT-0800 (PST)
time_format(dd)
"12:39:45"

So in the console I can see that I've taken a string, converted it to a date and then used that date to generate a time. 因此,在控制台中,我可以看到我已经接受了一个字符串,将其转换为日期,然后使用该日期来生成时间。 d3.time.scale() does not seem to accept the time I pass to it when I try to set the domain. 尝试设置域时,d3.time.scale()似乎不接受传递给它的时间。

http://i.imgur.com/DRtSW.png "completed plot" http://i.imgur.com/DRtSW.png “完成图”

Your dataset is a JSON array [...] , containing objects that are key-value pairs {date: '...'} . 您的数据集是一个JSON数组[...] ,其中包含键值对{date: '...'}

The value in this pairing is a comma-separated string. 该配对中的值是逗号分隔的字符串。

You could loop through the array and (for example) pull out the hour and minutes values, building a new dataset: 您可以遍历数组,并(例如)提取小时和分钟值,以建立新的数据集:

var new_data = [];
for (var idx = 0; idx < data_set.length; idx++) {
    var datum = data_set[idx];
    var date_value = datum.date;
    var date_elements = date_value.split(",");
    var hour = date_elements[3];
    var minutes = date_elements[4];
    var new_date = {};
    new_date.date = hour + "," + minutes;
    new_data.push(new_date);
};

Then you could pass your d3 code the modified dataset new_data , which only contains hour and minute data. 然后,您可以将修改后的数据集new_data传递给d3代码,该数据集仅包含小时和分钟数据。

EDIT 编辑

If it is useful, you might want to edit the code above to change the format of a new_date object instance to something that a d3 function can consume. 如果有用,则可能需要编辑上面的代码,以将new_date对象实例的格式更改为d3函数可以使用的格式。 I'm honestly not too familiar with the date/time portion of the API, so some changes may be needed. 老实说,我不太熟悉API的日期/时间部分,因此可能需要进行一些更改。 I'm just trying to show in a generic way what you could do to pre-process your data array, to build an array with the data that you need. 我只是想以一种通用的方式展示如何预处理数据数组,用所需的数据构建数组。

Alex Reynolds: Did you mean 亚历克斯·雷诺兹:你的意思是

    new_date.date = hour + ":" + minutes;

?

Also you can provide the time scale with a format function that will format the ticks to hour and minute. 此外,您还可以为时标提供格式化功能,该功能会将刻度线格式化为小时和分钟。 See https://github.com/mbostock/d3/wiki/Time-Formatting and https://github.com/mbostock/d3/wiki/SVG-Axes#wiki-tickFormat 参见https://github.com/mbostock/d3/wiki/Time-Formattinghttps://github.com/mbostock/d3/wiki/SVG-Axes#wiki-tickFormat

D3 (or maybe JavaScript in general) doesn't seem to do time of day very well without requiring a date object. 在不需要日期对象的情况下,D3(或者可能是一般的JavaScript)似乎无法很好地完成一天中的时间。 My solution is to store time of day as an integer number of minutes since 00:00:00 (or seconds if I want the accuracy). 我的解决方案是将一天中的时间存储为从00:00:00开始的整数分钟数(如果需要精度,则为秒)。 In minutes it's an int between 0 and 1440. 在几分钟内,它是一个介于0和1440之间的整数。

If you need to convert from string time (like "00:00:00") then this may help: 如果您需要从字符串时间转换(例如“ 00:00:00”),则可能会有所帮助:

time = (parseInt(d.x.split(':')[0]) * 60)  + parseInt(d.x.split(':')[1]);

If your data is an int already then in your tickformat you can use this: 如果您的数据已经是一个int,则可以在tick格式中使用:

.tickFormat(function(d) { 
  return Math.floor((d)/60) + ":" + ((d)%60);
});

or 要么

.tickFormat(function(d) { 
  return padZero(Math.floor((d)/60)) + ":" + padZero((d)%60));
});

where padZero is your own function that pads leading zeros so that you don't get "1:30" or "0:0" but "01:30" and "00:00" 其中padZero是您自己的函数,用于padZero前导零,这样您就不会得到“ 1:30”或“ 0:0”,而不会得到“ 01:30”和“ 00:00”

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

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