簡體   English   中英

D3 - 格式經過時間

[英]D3 - format elapsed time

我是 d3.js 的新手,但我真的認為這是一個非常強大的工具。

我今天的問題是格式化時間刻度。

我有一個用 d3v5 繪制的實時圖表,時間在 x 軸上。 自頁面開始以來的時間以毫秒為單位編碼(加載后時間=0)。 我希望刻度顯示 15s、30s、45s、:1、15s、30s、45s、:2 等等。

我的問題是,使用 scaletime function,我實際上最終以 1970 年開頭。

誰能指導我? 我已經開始查看格式標簽 function,但最終沒有找到一種方法來做我想做的事。

誰能告訴我一個方向?

謝謝,

我最終設法自己找到了解決方案。

我已將我的比例從時間比例更改為比例線性。 我創建了一個 function,它根據輸入 integer 返回正確的 d3.format 值

      var TimeFormatLive = function(d) {
    if (d < 0) {//checking a condition based on that do foarmat
      //provide a format
      return '';
    }
    else if (d ==0){
      //provide some other format  
      return ":" + d3.format(".0f")(d/1000);
  }
    else if (d % (3600*1000)==0){
        //provide some other format  
        return d3.format(".0f")(d/(3600*1000)) + "h"; 
    }
    else if (d % (60*1000)==0){
          //provide some other format  
          return d3.format(".0f")((d /(1000*60)) % 60) + "min"; 
    }
    else {
     //provide some other format  
     return ":" + d3.format(".0f")((d/1000) % 60 ) ; 
    }
  };

然后我將此“過濾器”應用於 my.tick object

.call(d3.axisBottom(x).ticks(15).tickFormat(TimeFormatLive)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM