Retrieve the first value from the result set and write it to a text box with dynamic scaling

In this example the script is placed in the button, it returns the 1st value from the resultSet and writes it to a text box

// Get result set
var resultSet = Table_1.getDataSource().getResultSet();

if (resultSet.length > 0)
{
    // ---------------------------------
    // 1. Retrieve the first value
    // ---------------------------------
    var value = ConvertUtils.stringToNumber(resultSet[0][Alias.MeasureDimension].rawValue);

    // ---------------------------------
    // 2. Determine scale
    // ---------------------------------
    var absoluteValue = Math.abs(value);
    var scaledValue = value;
    var scaleSuffix = "";

    if (absoluteValue >= 1000000000.0)
    {
        scaledValue = value / 1000000000.0;
        scaleSuffix = "B";
    }
    else if (absoluteValue >= 1000000.0)
    {
        scaledValue = value / 1000000.0;
        scaleSuffix = "M";
    }
    else if (absoluteValue >= 1000.0)
    {
        scaledValue = value / 1000.0;
        scaleSuffix = "K";
    }

    // ---------------------------------
    // 3. Round and convert to text
    // ---------------------------------
    var roundedValue = Math.round(scaledValue * 10.0) / 10.0;

    var formattedValue = ConvertUtils.numberToString(roundedValue) + scaleSuffix;

    // ---------------------------------
    // 4. Write to text widget
    // ---------------------------------
    Text_1.applyText("Scaled Value: " + formattedValue);
}
Code language: JavaScript (javascript)
Scroll to Top