Kenny Dewhirst Kenny Dewhirst - 6 months ago 26
Javascript Question

How do I make a number of milliseconds into a timespan for use in Google Charts?

I have a column that is number of milliseconds that I'd like to use in a Google Charts chart to represent a duration. How do I turn that number into a time span?

Answer

To expand on Kenny's answer, if you input your data as milliseconds, you can use a DataView to convert that to a timeofday data type:

var timeFormatter = new google.visualization.DateFormat('HH:mm:ss.SSS'); // set this pattern however you need to format your time display
var view = new google.visualization.DataView(data);
view.setColumns([/* columns before timeofday */, {
    type: 'timeofday',
    label: 'Time of Day',
    calc: function (dt, row) {
        var timeOfDay = toTimeSpan(data.getValue(row, /* time column index */);
        var formattedTime = timeFormatter.formatValue(timeOfDay);
        return {v: timeOfDay, f: formattedTime};
    }
}, /* columns after timeofday */]);
Comments