cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Widget/Dashboard script to return full digit numbers

vg30
Sisense Team Member
Sisense Team Member

Disclaimer: Please note, that this blog post contains one possible custom workaround solution for users with similar use cases. We cannot guarantee that the custom code solution described in this post will work in every scenario or with every Sisense software version. As such, we strongly advise users to test solutions in their own environment prior to deploying them to ensure that the solutions proffered function as desired in their environment. For the avoidance of doubt, the content of this blog post is provided to you “as-is” and without warranty of any kind, express, implied or otherwise, including without limitation any warranty of security and or fitness for a particular purpose. The workaround solution described in this post incorporates custom coding which is outside the Sisense product development environment and is therefore not covered by not covered by Sisense warranty and support services.

This article does cover the use case when you would like to display full size of number instead of the default logic approach we use when numbers are shorted, example: 

vg30_0-1726778610187.png

After the solution would be applied:

vg30_1-1726778654209.png

 

Here is the widget script that you could use to effect the specific widget Chart:

 

// Function to convert shortened numbers to full numbers
function convertToFullNumber(value) {
    if (typeof value !== 'string') {
        throw new Error("Input must be a string.");
    }

    let multiplier = 1;
    let number = parseFloat(value);

    if (isNaN(number)) {
        throw new Error("Invalid numeric value.");
    }

    const lowerValue = value.toLowerCase();

    if (lowerValue.includes('m')) {
        multiplier = 1e6; // Million
    } else if (lowerValue.includes('k')) {
        multiplier = 1e3; // Thousand
    } else if (lowerValue.includes('b')) {
        multiplier = 1e9; // Billion
    } else if (!/^\d+(\.\d+)?$/.test(lowerValue)) {
        throw new Error("Invalid format. Only numbers with 'k', 'm', or 'b' are allowed.");
    }

    return Math.round(number * multiplier);
}

widget.on('beforedatapointtooltip', (widget, query) => {
    let value = query.context.points[0].value;

    try {
        const convertedValue = convertToFullNumber(value);
        query.context.points[0].value = convertedValue;
    } catch (error) {
        console.error("Conversion error: " + error.message);
        
        query.context.points[0].value = value; // Keep the original value if conversion fails
    }
});

 

Dashboard script which effects many chart widgets:

 

 

// Function to convert shortened numbers to full numbers
function convertToFullNumber(value) {
    if (typeof value !== 'string') {
        throw new Error("Input must be a string.");
    }

    let multiplier = 1;
    let number = parseFloat(value);

    if (isNaN(number)) {
        throw new Error("Invalid numeric value.");
    }

    const lowerValue = value.toLowerCase();

    if (lowerValue.includes('m')) {
        multiplier = 1e6; // Million
    } else if (lowerValue.includes('k')) {
        multiplier = 1e3; // Thousand
    } else if (lowerValue.includes('b')) {
        multiplier = 1e9; // Billion
    } else if (!/^\d+(\.\d+)?$/.test(lowerValue)) {
        throw new Error("Invalid format. Only numbers with 'k', 'm', or 'b' are allowed.");
    }

    return Math.round(number * multiplier);
}

// Widget event handler example


prism.on("dashboardloaded", function (e, args) {
    args.dashboard.on('widgetready', (dashboard, args) => {
        var array = [];
        dashboard.widgets.toArray().forEach(widget => {
            if (widget.type === 'chart/bar') {
                widget.on('beforedatapointtooltip', (widget, query) => {
					let value = query.context.points[0].value;

						try {
							const convertedValue = convertToFullNumber(value);
							query.context.points[0].value = convertedValue;
						} catch (error) {
							console.error("Conversion error: " + error.message);
							// Optionally handle the error or show a fallback value
							query.context.points[0].value = value; // Keep the original value if conversion fails
						}
				})
            }
        });
    });
});

 

0 REPLIES 0