使用 d3 js 格式化此时间序列图表的 x 轴时遇到问题。
这是一个工作示例:http://tributary.io/inlet/7798421
问题:我只能在 x 轴上看到 1 个日期(标签),无论指定的刻度总数如何。如何在 x 轴上以 4-6 个刻度显示时间?
编辑:下面感谢 Lars 的解决方案。
这是我在 UTC 的时间:
var data = [
{"time": 1387212120, "open": 368, "close": 275, "high": 380, "low": 158},
{"time": 1387212130, "open": 330, "close": 350, "high": 389, "low": 310},
{"time": 1387212140, "open": 213, "close": 253, "high": 289, "low": 213}];
data.forEach(function(d){ d.time = new Date(d.time * 1000) });
然后 d3 以默认格式接受它,或者您可以自定义。
最佳答案
问题在于 Javascript Date 对象(您正在将时间戳隐式转换到其中)不是以秒为单位的时间戳,而是以毫秒为单位。如果将所有时间值乘以 1000,就可以了。
完整示例 here .我还明确了对 Date 的转换。
链接中的代码:
var sample2 = [
{"time": 1387212120, "open": 368, "close": 275, "high": 380, "low": 158},
{"time": 1387212130, "open": 330, "close": 350, "high": 389, "low": 310},
{"time": 1387212140, "open": 213, "close": 253, "high": 289, "low": 213},
{"time": 1387212150, "open": 180, "close": 150, "high": 189, "low": 110},
{"time": 1387212160, "open": 310, "close": 350, "high": 389, "low": 310},
{"time": 1387212170, "open": 213, "close": 253, "high": 289, "low": 213},
{"time": 1387212180, "open": 190, "close": 150, "high": 189, "low": 110},
{"time": 1387212190, "open": 362, "close": 530, "high": 589, "low": 510},
{"time": 1387212200, "open": 409, "close": 356, "high": 300, "low": 510},
{"time": 1387212210, "open": 334, "close": 275, "high": 369, "low": 185},
{"time": 1387212220, "open": 304, "close": 389, "high": 389, "low": 310},
{"time": 1387212230, "open": 395, "close": 235, "high": 289, "low": 213},
{"time": 1387212240, "open": 339, "close": 148, "high": 189, "low": 110},
{"time": 1387212250, "open": 310, "close": 350, "high": 389, "low": 310},
{"time": 1387212260, "open": 283, "close": 253, "high": 289, "low": 213},
{"time": 1387212270, "open": 290, "close": 350, "high": 189, "low": 110},
{"time": 1387212280, "open": 448, "close": 550, "high": 624, "low": 510},
{"time": 1387212290, "open": 419, "close": 299, "high": 194, "low": 510},
{"time": 1387212300, "open": 150, "close": 163, "high": 189, "low": 145},
{"time": 1387212310, "open": 330, "close": 350, "high": 356, "low": 310},
{"time": 1387212320, "open": 213, "close": 253, "high": 289, "low": 213},
{"time": 1387212330, "open": 180, "close": 150, "high": 189, "low": 110},
{"time": 1387212340, "open": 310, "close": 350, "high": 389, "low": 310},
{"time": 1387212350, "open": 213, "close": 253, "high": 289, "low": 213},
{"time": 1387212360, "open": 190, "close": 150, "high": 230, "low": 110},
{"time": 1387212370, "open": 408, "close": 301, "high": 382, "low": 245},
{"time": 1387212380, "open": 330, "close": 356, "high": 404, "low": 230},
{"time": 1387212390, "open": 183, "close": 143, "high": 190, "low": 31},
{"time": 1387212400, "open": 183, "close": 265, "high": 271, "low": 165},
{"time": 1387212410, "open": 395, "close": 253, "high": 424, "low": 213},
{"time": 1387212420, "open": 339, "close": 379, "high": 446, "low": 275},
{"time": 1387212430, "open": 310, "close": 350, "high": 389, "low": 310},
{"time": 1387212440, "open": 283, "close": 253, "high": 289, "low": 213},
{"time": 1387212450, "open": 162, "close": 350, "high": 189, "low": 122},
{"time": 1387212460, "open": 452, "close": 361, "high": 525, "low": 329},
{"time": 1387212470, "open": 173, "close": 281, "high": 312, "low": 141},
{"time": 1387212480, "open": 183, "close": 265, "high": 271, "low": 165},
{"time": 1387212490, "open": 395, "close": 253, "high": 424, "low": 213},
{"time": 1387212500, "open": 339, "close": 379, "high": 446, "low": 275},
{"time": 1387212510, "open": 310, "close": 350, "high": 389, "low": 310},
{"time": 1387212520, "open": 283, "close": 253, "high": 289, "low": 213},
{"time": 1387212530, "open": 162, "close": 350, "high": 189, "low": 122},
{"time": 1387212540, "open": 452, "close": 361, "high": 542, "low": 329},
{"time": 1387212550, "open": 173, "close": 281, "high": 312, "low": 91},
{"time": 1387212480, "open": 183, "close": 265, "high": 271, "low": 165},
{"time": 1387212490, "open": 395, "close": 253, "high": 424, "low": 213}
];
sample2.forEach(function(d) { d.time = new Date(d.time * 1000); });
var margin = {"top": 50, "right": 83, "bottom": 56, "left": 25, "axis": 55};
var width = 635 + margin.right + margin.left;
var height = 567 + margin.top + margin.bottom;
var timeFormat = d3.time.format("%I:%M %p %a %Y");
// set up chart
var svg = d3.select("svg").attr("width", width).attr("height", height);
var chart = d3.select("svg");
// find data range
var xMin = d3.min(sample2, function(d){ return Math.min(d.time); });
var xMax = d3.max(sample2, function(d){ return Math.max(d.time); });
var yMin = d3.min(sample2, function(d){ return Math.min(d.low); });
var yMax = d3.max(sample2, function(d){ return Math.max(d.high); });
/*
Ghetto Debugs
console.log("yMin" + " " + yMin);
console.log("yMax" + " " + yMax);
console.log("xMin" + " " + xMin);
console.log("xMax" + " " + xMax);
console.log(xMax - xMin);
*/
// scale using ranges
var xScale = d3.time.scale()
.domain([xMin, xMax])
.range([margin.left, width - margin.right]);
var xAxisScale = d3.time.scale()
.domain([xMin, xMax])
.range([margin.left, width - margin.axis]);
var yScale = d3.scale.linear()
.domain([yMin, yMax])
.range([height - margin.top, margin.bottom]);
// set up axes
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("right")
.tickValues(yScale.domain());
var xAxis = d3.svg.axis()
.scale(xAxisScale)
.orient("bottom")
.ticks(5)
.tickPadding(5)
.tickFormat(timeFormat);
// draw chart
chart.selectAll("line")
.data(sample2)
.enter()
.append("svg:line")
.attr({
"x1": function(d,i) { return xScale(d.time) + 5; },
"x2": function(d,i) { return xScale(d.time) + 5; },
"y1": function(d,i) { return yScale(d.high); },
"y2": function(d,i) { return yScale(d.low); },
"stroke": "black"
});
chart.selectAll("rect")
.data(sample2)
.enter()
.append("svg:rect")
.attr({
"width": 10,
"x": function(d,i) { return xScale(d.time); },
"y": function(d,i) { return yScale(Math.max(d.open, d.close)); },
"height": function(d,i) { return yScale(Math.min(d.open, d.close)) - yScale(Math.max(d.open, d.close)); },
"fill": function (d) { return d.open > d.close ? "red" : "green" },
"stroke": "black"
});
chart.append('g').call(yAxis)
.attr('transform', 'translate(' + (width - margin.axis) + ', 0)');
chart.append('g').call(xAxis)
.attr('transform', 'translate(0, ' + (height - margin.bottom) + ')');
关于javascript - d3 时间刻度 x 轴与 unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20645216/
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
因为我现在正在做一些时间测量,我想知道是否可以在不使用Benchmark类或命令行实用程序time的情况下测量用户时间或系统时间。使用Time类只显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵active的解决方案,例如time=TimeUtility.now#somecodeuser,system,real=TimeUtility.now-time原因是我有点不喜欢Benchmark,因为它不能只返回数字(编辑:我错了-它可以。请参阅下面的答案。)。当然,我可以解析输出,但感觉不对。*NIX系统的time实用程序也应该可以解决我的问题,但我想知道是否已经在Ruby中实
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
在Ruby中,以毫秒为单位获取自纪元(1970)以来的当前系统时间的正确方法是什么?我试过了Time.now.to_i,好像不是我想要的结果。我需要结果显示毫秒并且使用long类型,而不是float或double。 最佳答案 (Time.now.to_f*1000).to_iTime.now.to_f显示包含十进制数字的时间。要获得毫秒数,只需将时间乘以1000。 关于ruby-以毫秒为单位获取当前系统时间,我们在StackOverflow上找到一个类似的问题:
从一开始,我就是一个Windows高手。我从MS-DOS开始。我安装了Windows2.1以及此后的所有Windows。现在,我家里有10台不同的Windows机器在运行,从Windows7Ultimate到各种版本的WindowsServer。我还没有完成Windows8,也不想去那里。我在服务器和各种软件方面都有UNIX经验,但它并不是我的首选环境。但是,我想我正在转换。我试图假装使用Cygwin和MSYS在Windows下运行UNIX。我的目的是搭建一个开发环境。两者都让我失望了。我花了比开发更多的时间来解决一系列技术问题。这是NotAcceptable。到目前为止,我的Ruby
我想知道我应该如何着手这个项目。我需要每周向人们发送一次电子邮件。但是,这必须在每周的特定时间自动生成并发送。编码有多难?我需要知道是否有任何书籍可以提供帮助,或者你们中的任何人是否可以指导我。它必须使用rubyonrails进行编程。因此有一个网络服务和数据库集成。干杯 最佳答案 为什么这么复杂?您只需安排工作。您可以使用Delayed::Job例如。Delayed::Job让您可以使用run_at符号在特定时间安排作业,如下所示:Delayed::Job.enqueue(SendEmailJob.new(...),:run_
我在ruby1.9.3p0上运行rails3.2.1和rspec2.8.1,在运行我的测试时它显示负时间值。这很烦人,因为我正在尝试优化我的测试。Running:spec/models/transaction_spec.rb................................................Finishedin-7603162.49414seconds我已经尝试将rspec更新到2.9.0,但这没有帮助。 最佳答案 你在使用timecopgem吗?确保在卡住后Timecop.return。或者你在某处