echartY軸時間格式+顯示時間格式

option = {
    tooltip: {
        show: true,
        formatter: function (params) {
            return fomartTime(params.data)
        }
    },
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value',
        // interval :2,
        axisLabel: {
            formatter:function (value, index) {
                return fomartTime(value)
            }
        },
    },
    series: [{
        data: [5.5, 21.4, 15, 8, 158.5, 98, 32],//這個數組我的單位是分鐘
        type: 'bar'
    }]
};

function fomartTime (value) {
    let unit=['分鐘','小時','天'],
    day=0,hour=0,min=0,second=0,returnStr=0,
    arrVal=value.toString().split(".");
    if(arrVal.length>1){
       second=parseFloat("0."+arrVal[1]);
       second*=60;
       value=parseInt(arrVal[0]);
    }
    returnStr=value+unit[0];
    if(value>=60){
        hour=parseInt(value/60);
        min=value%60;
    }
    if(hour){
       returnStr=hour+unit[1];
       if(min){
           returnStr+=min+unit[0]
       }
    }
    if(second){
        returnStr+=second.toFixed(0)+'秒'
    }
     return returnStr
}


效果:

想繼續測試的直接到下面鏈接替換成上面代碼看效果

在線測試鏈接:https://www.echartsjs.com/examples/zh/editor.html?c=bar-simple

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章