javascript 如何在 xAxis 上格式化时间使用 d3.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15471224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:53:17  来源:igfitidea点击:

how to format time on xAxis use d3.js

javascriptd3.js

提问by L.T

According the demo on http://bl.ocks.org/mbostock/3883245

根据http://bl.ocks.org/mbostock/3883245上的演示

I don't know how format time on xAxis

我不知道如何在 xAxis 上格式化时间

this is my code : js:

这是我的代码:js:

    var data = [{
            "creat_time": "2013-03-12 15:09:04",
            "record_status": "ok",
            "roundTripTime": "16"
        }, {
            "creat_time": "2013-03-12 14:59:06",
            "record_status": "ok",
            "roundTripTime": "0"
        }, {
            "creat_time": "2013-03-12 14:49:04",
            "record_status": "ok",
            "roundTripTime": "297"
        }, {
            "creat_time": "2013-03-12 14:39:06",
            "record_status": "ok",
            "roundTripTime": "31"
        },{
            "creat_time": "2013-03-12 14:29:03",
            "record_status": "ok",
            "roundTripTime": "0"
    }];
    var margin = {top: 20, right: 20, bottom: 30, left: 50};
    var width = 960 - margin.left - margin.right;
    var height = 500 - margin.top - margin.bottom;
    var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
    var x = d3.time.scale()
        .range([0, width]);

    var y = d3.scale.linear()
        .range([height, 0]);

    var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom");

    var yAxis = d3.svg.axis()
        .scale(y)
        .orient("left");

    var line = d3.svg.line()
        .x(function(d) { return x(d.creat_time); })
        .y(function(d) { return y(d.roundTripTime); });


    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
      .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    data.forEach(function(d) {
        d.creat_time = parseDate(d.creat_time);
        d.roundTripTime = +d.roundTripTime;
    });

    x.domain(d3.extent(data, function(d) { return d.creat_time; }));
    y.domain(d3.extent(data, function(d) { return d.roundTripTime;}));

    svg.append("g")
          .attr("class", "x axis")
          .attr("transform", "translate(0," + height + ")")
          .call(xAxis);

    svg.append("g")
          .attr("class", "y axis")
          .call(yAxis)
          .append("text")
          .attr("transform", "rotate(-90)")
          .attr("y", 6)
          .attr("dy", ".71em")
          .style("text-anchor", "end")
          .text("return time(ms)");

    svg.append("path")
          .datum(data)
          .attr("class", "line")
          .attr("d", line);

this is svg: enter image description here

这是 svg: 在此处输入图片说明

In svg , time is 12-hour clock ,but in my data time is 24-hour clock . how to keep the same format on svg and data?

在 svg 中,时间是 12 小时制,但在我的数据中时间是 24 小时制。如何在 svg 和数据上保持相同的格式?

Any help is appreciated. (ps:I hope you don't mind my English,it's so bad.)

任何帮助表示赞赏。(ps:我希望你不要介意我的英语,它太糟糕了。)

回答by olly_uk

You can use the tickFormat function on the axis object as below

您可以在轴对象上使用 tickFormat 函数,如下所示

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(d3.time.format("%H"));

The %Hspecifies hour (24-hour clock) as a decimal number [00,23]. Check this link D3 time formattingfor more information

%H指定hour (24-hour clock) as a decimal number [00,23]。检查此链接D3 时间格式以获取更多信息

You can check out a working example in this tributary 24hr time example

您可以在这个支流24 小时时间示例中查看一个工作示例

回答by pilotcam

The accepted answer is indeed correct, but in my case, I needed the flexibility for the formats to adjust to different scales (think zooming), but also to ensure the 24hr clock is used. The key is to define a multi-resolution time format. See the Documentationpage for details.

接受的答案确实是正确的,但就我而言,我需要灵活地调整格式以适应不同的比例(想想缩放),同时还要确保使用 24 小时时钟。关键是定义一个多分辨率的时间格式。有关详细信息,请参阅文档页面。

My code:

我的代码:

var axisTimeFormat = d3.time.format.multi([
    [".%L", function(d) { return d.getMilliseconds(); }],
    [":%S", function(d) { return d.getSeconds(); }],
    ["%H:%M", function(d) { return d.getMinutes(); }],
    ["%H:%M", function(d) { return d.getHours(); }],
    ["%a %d", function(d) { return d.getDay() && d.getDate() != 1; }],
    ["%b %d", function(d) { return d.getDate() != 1; }],
    ["%B", function(d) { return d.getMonth(); }],
    ["%Y", function() { return true; }]
 ]);

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(axisTimeFormat);

回答by Mikhail Shabrikov

I want to add this linkto an awesome demo page. It is a playground when you can choose what a format specifier do you need for your case. It is very useful when you do not remember/know what a format specifier you should pass to your d3.timeFormatfunction.

我想将此链接添加到一个很棒的演示页面。当您可以选择您的案例所需的格式说明符时,这是一个游乐场。当您不记得/不知道应该传递给d3.timeFormat函数的格式说明符时,它非常有用。

I also want to notice, that if you have d3 version 4, you should use d3.timeFormatfunction, instead of d3.time.format.

我还想注意,如果你有 d3 版本 4,你应该使用d3.timeFormatfunction,而不是d3.time.format.

回答by KimchiMan

In v4,

在 v4 中,

...
var scaleX = d3.scaleTime().range([0, width]);
var axisBottom = d3.axisBottom(scaleX)
                   .ticks(d3.timeMinute, 10); // Every 10 minutes
...

Note that use d3.timeMinute- not d3.timeMinutes

注意使用d3.timeMinute- 不d3.timeMinutes