【问题标题】:C3js change axis time format on loadC3js在加载时更改轴时间格式
【发布时间】:2014-12-18 15:10:22
【问题描述】:

我有一个 C3.js 图表,其中每分钟的时间值格式为 %H:%M。 现在我想加载新数据,而不是每小时的数据,我想将每天的数据显示为 %d-%m。 所以刻度格式需要改变,但是我该怎么做呢?

使用 C3.js 生成图表

  chart = c3.generate({
        bindto: element,
        data: {
            x: "keys",
            xFormat: '%Y-%m-%d %H:%M:%S',
            url: dataurl + '/today',
            mimeType: 'json',
            type: 'bar',

        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    format: '%H:%M'
                }
            }
        }
    });

加载新数据

 chart.load({
        x: "keys",
        xFormat: '%Y-%m-%d %H:%M:%S',
        url: url,
        mimeType: 'json',
    });

【问题讨论】:

    标签: javascript d3.js c3.js


    【解决方案1】:

    Masayuki on Github - 的最新消息说没有 API。

    但是你可以定义一个全局变量来动态改变刻度,see Masayuki's fiddle

    From Github -

    var formatter;
    function updateFormatter(byMonth) {
      formatter = d3.time.format(byMonth ? '%Y-%m' : '%Y-%m-%d')
    }
    
    updateFormatter();
    
    var chart = c3.generate({
        data: {
            x: 'date',     
            columns: [
                ['date', '2014-01-01', '2014-01-02', '2014-01-03'],
                ['data1', 100, 200, 300],
            ]
        },
        axis: {
          x: {
            type: 'timeseries',
            tick: {
                format: function (x) { // x comes in as a time string.
                  return formatter(x);
                }
            }
          }
        }
    });
    
    d3.select('#btnByMonth').on('click', function () {
      updateFormatter(true);
      chart.flush();            
    });
    d3.select('#btnByWeek').on('click', function () {
      updateFormatter(false);
      chart.flush();            
    });
    

    【讨论】:

    • 对于较新版本的 d3,请使用 d3.timeFormat(your-format-in-here)(your-date-in-here)
    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多