javascript - 如何显示hh :mm:ss format at y-axis with c3?

标签 javascript datetime d3.js datetime-format c3.js

这是我的数据集:

x: '2014-01-01', '2014-02-02', '2014-03-02', ...
y: '01:30:00', '00:55:00', '01:45:50', ...

我想在我的图表上显示这些数据,使用 c3.js图书馆。

到目前为止我试过了(已经将时间转换为秒):

var chart = c3.generate({
    data : {
        x : 'x',
        columns : [
            ['x', '2010-01-01', '2011-01-01', '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01'],
            ['sample', 30, 200, 100, 400, 150, 250]
        ]
    },
    axis : {
        x : {
            type: 'timeseries'
        },
        y : {
            tick : {
                format: d3.time.format("%X")
                //or format: function (d) { return '$' + d; }
            }
        }
    }
});

最佳答案

我会做的,是这样的:

  1. 创建一个时间为 00:00:00 的新日期:

    date = new Date('01-01-2016 00:00:00')

  2. 将该日期转换为时间戳:

    timestamp = date.getTime()

  3. 将 Y 秒添加到该时间戳:

    时间戳 = 时间戳 + (y * 1000)

  4. 创建一个新日期,使用该值作为输入:

    date = new Date(时间戳)

  5. 使用 d3.time.format 相应地格式化该日期:

    time = d3.time.format("%H:%M:%S")(日期)

  6. 把所有的部分放在一起,把它变成一个内衬:

    time = d3.time.format("%H:%M:%S")(新日期(新日期('01-01-2016 00:00:00').getTime() + (y * 1000)));

现在,您可以将这个 oneliner 放入 Y 轴的 format 函数中并返回 time :

var chart = c3.generate({
    data : {
        x : 'x',
        columns : [
            ['x', '2010-01-01', '2011-01-01', '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01'],
            ['sample', 30, 200, 100, 400, 150, 250]
        ]
    },
    axis : {
        x : {
            type: 'timeseries'
        },
        y : {
            tick : {
                format : function (y) {
                    return d3.time.format("%H:%M:%S")(new Date(new Date('01-01-2016 00:00:00').getTime() + (y * 1000)));
                }
            }
        }
    }
});

参见 this Fiddle用于演示。


更多信息:

关于javascript - 如何显示hh :mm:ss format at y-axis with c3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35621487/

相关文章:

javascript - JavaScript 中的有效属性名称、属性分配和访问

javascript - 如何取消关注不关注 Twitter 的 Twitter 关注者

javascript - 使用 PrototypeJS 添加 CSS 样式

javascript - 定义自定义 D3 符号

javascript - 在这种情况下,多维数组值分配如何工作?

mysql - 在mysql中插入时格式化日期

iphone - 如何更改存储在字符串中的日期格式? objective-c

mysql - sqlalchemy - 在 MySQL 中按月选择记录

javascript - 让 d3 矩形跨越整行

javascript - d3 map鼠标悬停事件响应慢