Ophir_Buchman
03-07-2022Data Integration
Pivot 2.0 - Replace the "-" Sign of a Negative Value by Parentheses
In Accounting, it is common to represent negative numbers with leading and trailing parentheses. 
For example, negative two hundred is displayed as "(200)"
To change a negative number formatting in a pivot 2.0 chart - Add the following script to the pivot widget:
function getWidgetDocumentObject(widget) {
  if (prism.activeWidget == null)
    return document.querySelector('[widgetid="' + widget.oid + '"]').querySelector('pivot2');
  else
    return document.querySelector('pivot2');
}
widget.on('domready', function(widget) {
  try {
    console.log('DOM Ready script starting (wid=' + widget.oid + ')');
    doc = getWidgetDocumentObject(widget);
    cells = doc.querySelectorAll('.table-grid__cell');
    negative_cells = Array.from(cells).filter(cell => !isNaN(parseInt(cell.innerText.replaceAll(',',''))) && parseInt(cell.innerText.replaceAll(',',''))<0)
    negative_cells.forEach(function(element) { 
      element.lastChild.innerText = element.lastChild.innerText.replace('-','(') + ')';
    });
  }
  catch (error) {
    debugger;
    console.error(error);
  }
  finally {
    console.log('DOM Ready script complete (wid=' + widget.oid + ')');
  }
});
| Before | After |