Indicator - converting seconds to HH:MM:SS format

Comments

6 comments

  • Avatar
    Brian Bontrager (Edited )

    This does not appear to work any more in 6.6.1. The indicator is using a canvas element now.

    Here is an update to this example that manipulates the result data before it is displayed by the canvas. ConvertRawTime is the same. We fire on an earlier event (processresult), and update the query results directly before rendering the widget.


    widget.on('processresult', function (s,e){

    function convertRawTime(num){

    var sign = num < 0 ? '-' : '';
    var hours = (parseInt( Math.abs(num) / 3600 )%24);
    var minutes = parseInt( Math.abs(num) / 60 )%60;
    var seconds = parseInt( Math.abs(num) % 60);

    var hoursText = hours < 10 ? "0" + hours : hours;
    var minutesText = minutes < 10 ? "0" + minutes : minutes;
    var secondsText = seconds < 10 ? "0" + seconds : seconds;
    if (hours==0){
    var secondsText = seconds < 10 ? "0" + seconds : seconds;
    return sign + minutes + ":" + secondsText
    } else {
    var hoursText = hours < 10 ? "0" + hours : hours;
    var minutesText = minutes < 10 ? "0" + minutes : minutes;
    var secondsText = seconds < 10 ? "0" + seconds : seconds;
    return sign + hoursText + ":" + minutesText + ":" + secondsText

    }
    };

    // update the primary indicator
    e.result.value.text=convertRawTime(e.result.value.data);

    // update the secondary indicator, if there is one
    if (e.result.hasOwnProperty('secondary')) {
    e.result.secondary.text=convertRawTime(e.result.secondary.data);
    }

    });
    1
    Comment actions Permalink
  • Avatar
    Malinda Jepsen

    I was using this code, and it doesn't work in 6.6.1 anymore either:

    /* FROM: https://support.sisense.com/entries/40938460-Convert-Seconds-to-Time-Format */


    widget.on('processresult',function(widget,result) {
    //Get the Widget ID
    var wid = widget.oid;
    //Get the value
    var num = result.result.value.data;
    num = num * 60 // we have minutes, but need to break down to seconds/milliseconds

    //Function to convert the time
    var convertTime = function(num,wid) {
    var nextText = '';
    //Only run if the value is numeric
    if ($.isNumeric(num)) {
    if (num >= 3600) {
    newText = parseInt(num/3600) + 'h ' + parseInt(num%3600/60) + 'm ' + parseInt(num%3600%60) + 's ';
    }
    else {
    newText = parseInt(num%3600/60) + 'm ' + parseInt(num%3600%60) + 's ' + parseInt(num%1 * 1000) + 'ms';
    }
    //Find the widget span
    var widget = $('widget[widgetid='+wid+']');
    var span = $('#number_span',widget);
    //Write back the text
    span.text(newText);
    }
    };

    //Write back the formatted text
    setTimeout(function(){
    convertTime(num,wid);
    },100);
    })

    0
    Comment actions Permalink
  • Avatar
    Malinda Jepsen

    And... thanks Brian, your code works for me in 6.6.1.

    0
    Comment actions Permalink
  • Avatar
    Amministratore BI (Edited )

    Hi,

    I had an issue with this indicator, as if there are more than 24 hours, it gives you only the remaining part ( I had 30 hours, and it wrote 6 hours, as 30-24 =6)

    I deleted the % 24  near the var hours = (parseInt( Math.abs(num) / 3600 )); line and everything is working fine now.

     

    1
    Comment actions Permalink
  • Avatar
    Tony Fonager

    Okay, so this can convert a number to hh:mm:ss

    But what happens if you have more numbers on the widget, but you only want 1 of them to be converted ? Right now it converts all numbers inside the widget to hh:mm:ss

    And how to use it on fx. pivots, but not on all numbers ?

    0
    Comment actions Permalink
  • Avatar
    Brian Bontrager

    The exact method will vary by widget. The method of transforming it can use the same convertRawTime() function here.

    For a pivot, you need to identify which cells you want to reformat and only reformat those cells.

    A short example of replacing cells in a pivot based on their value is here:

    https://support.sisense.com/hc/en-us/community/posts/221229348-Replace-Null-values-or-N-A-s-with-zeros-in-Pivot-Grid

     

    If want to modify all numbers in a certain (pivot widget) column, you can loop through the values for a single column:

    (This block is adapted from working code, but untested in its current form. It serves as an example of the technique but might need minor debugging). function convertRawTime() is assumed to be included elsewhere in the script.


    widget.on('ready', function(sender, ev){

    //pick a column to reformat, 0-indexed
    var ColumnNumber = 1;

    //get array of elements to replace 
    var ColumnData = $('td.p-value[fidx=' + ColumnNumber + '] .p-value', element);
    var L = ColumnData .length;

    var CellData;
    var CellDataValue;


    //loop through each cell in the column
    for (i=0; i<L; i++)
    {
    CellData = ColumnData [i];

    //get the numerical value for this cell
    CellDataValue = CellData.innerHTML.replace(/[^\d.-]/g, '');

    CellData.innerHTML = '<div class="p-value">'+convertRawTime(CellDataValue)+'</div>';
    }


    });

     

    0
    Comment actions Permalink

Please sign in to leave a comment.