Skip to main content Skip to footer

How to Build a JavaScript Retail Income Statement App

Retail Income Statements are issued regularly to measure the performance and success of many businesses. This blog will show how you can create and add these types of financial reports to your in-house or commercial JavaScript applications.

This blog will use PivotTables, a powerful data analysis tool, and the PivotTable Slicer to create and interact with a dynamic JavaScript Income Statement.

Let’s get started!

To create the income statement:

Feel free to download the sample and follow along.

Ready to Start Building? Download SpreadJS Today!

Set Up the Project

You can start by creating the HTML file and referencing the SpreadJS files you will need. In this case, you can add them to the project using NPM:

npm install @grapecity/spread-sheets @grapecity/spread-sheets-shapes @grapecity/spread-sheets-slicers @grapecity/spread-sheets-pivot-addon

You can then reference these in the HTML file and create the host for the SpreadJS instance:

<!DOCTYPE html>
<html>
<head>
    <title>Income Statement</title>
    <link href="./node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css" rel="stylesheet" type="text/css" />
	<script src="http://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
    <script type="text/javascript" src="./node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js"></script>
	<script src="./node_modules/@grapecity/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script>
    <script src="./node_modules/@grapecity/spread-sheets-slicers/dist/gc.spread.sheets.slicers.min.js"></script>
	<script src="./node_modules/@grapecity/spread-sheets-pivot-addon/dist/gc.spread.pivot.pivottables.min.js" type="text/javascript"></script>
    
	<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <div class="sample-tutorial">
     <div id="ss" class="sample-spreadsheets"></div>
	 <div class="sample-panel">
            <div id="panel"></div>
        </div>
</div>
	 </body>
</html>

Set Up Your Data

Now, you need to ensure your data is in a tabular format. This means your data respects the following conditions:

  1. Every record is housed in one row.
  2. Each Column contains a type of data.
  3. There are no blank rows or columns.
  4. There are no subtotals or totals in the data.

In this example, we have added the Account Group column to represent different sections of the report and have named the table containing the data tblIncome.

Pivot

Insert the Pivot Table

If you are using our Designer Component, insert the PivotTable following these steps:

  1. Select tblIncome table
  2. On the Insert tab, click PivotTable
  3. Choose "New Worksheet"
  4. Ok, at the end

JavaScript Income Statement

Or you can add it using the below code if you are using javascript:

let pivotTable = sheet.pivotTables.add("myPivotTable", "tblIncome", 1, 1);

Build the Pivot Table

To exactly build the PivotTable, we will be using the PivotTable Panel. If you are using Designer, the panel will be shown at the right of the sheet every time you click on the PivotTable.

You can also add it to your javascript instance using the code below:

var pt = spread.getActiveSheet().pivotTables.all()[0]; 

var panel = new GC.Spread.Pivot.PivotPanel('myPivotPanel', pt, document.getElementById("panel")); 

panel.sectionVisibility(GC.Spread.Pivot.PivotPanelSection.fields |GC.Spread.Pivot.PivotPanelSection.area);

Drag and drop the fields on the right areas to build the PivotTable. In our example, the Account Group and Account field to the Rows and add Actual and Budget to the Values.

JavaScript Income Statement

Notice: The PivotTable can work without PivotPanel; we have just added it for ease of use. 

Add Calculated Items

In addition to the existing items in a pivot table field, you can create one or more calculated items using custom formulas.

  1. Click PivotTable Analyze
  2. Fields, Items & Sets → Calculated Item
  3. Set the name 3. Gross Profit and as formula: ='Account Group'['1. Revenue']-'Account Group'['2. COGS']

Calculated Item

Add the calculated Item using only the code as follows:

pivotTable.addCalcItem("Account Group", "3. Gross Profit","='Account Group'['1. Revenue']-'Account Group'['2. COGS']");

Add the other Calculated Items following the above steps. The formulas for those are shown in the table below:

Item

Formula

'3. Gross Profit'

='Account Group'['1. Revenue']-'Account Group'['2. COGS']

'5. EBIT'

='Account Group'['3. Gross Profit']-'Account Group'['4. Expenses']

'7. Income from Continuing Operations'

='Account Group'['5. EBIT']-'Account Group'['6. Interests and Taxes']

'9. Net Profit'

='Account Group'['7. Income from Continuing Operations']+'Account Group'['8. Below the Line Items']

This will make your JavaScript Income Statement more readable for the user.

Add Calculated Fields

Income Statements often use the Variance Analysis for performance comparison. A budget variation is positive or favorable when actual revenue returns are higher than budget projections or expenses are lower than budget.

We will use the Calculated Fields feature to add the Difference and Difference % on our PivotTable.

  1. Click PivotTable Analyze.
  2. Fields, Items & Sets → Calculated Field.
  3. Set the name, Difference, of the calculated field.
  4. To add the fields on the formula, select the field and then click Insert Field.
  5. Click the Add button.

Fields

JavaScript code:

pivotTable.addCalcField('diff', '=Actual-Budget'); 
pivotTable.add("diff", "Difference", GC.Spread.Pivot.PivotTableFieldType.valueField); 

pivotTable.addCalcField('diff%', '=Actual/Budget-1'); 
pivotTable.add("diff%", "Difference %", GC.Spread.Pivot.PivotTableFieldType.valueField);

The two fields we have added are Difference and Difference %. The formulas used are as follows:

Field

Formula

Difference

=Actual-Budget

Difference %

=Actual/Budget-1

Add Slicers

On our SpreadJS v15 release, we added Slicers as a new feature to filter your PivotTable. Use this feature to filter your data by Region and Financial Year.

If you are using Designer Component, do as follows:

  1. Click PivotTable Analyze
  2. Insert Slicer
  3. Select Region and Financial Year

Slicers

To add slicers in JavaScript, use the code below:

var regionSlicer = sheet.slicers.add("Region", pivotTable.name(), "Region", GC.Spread.Sheets.Slicers.SlicerStyles.dark1(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable); 
var yearSlicer = sheet.slicers.add("Financial Year", pivotTable.name(), "Financial Year", GC.Spread.Sheets.Slicers.SlicerStyles.dark4(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable);

Make Some Appearance Adjustments

For the PivotTable to be more easily read, make some adjustments like:

  • Collapse the Calculated Items

This will hide the accounts summed to show the value of the calculated Item.

JavaScript Income Statement

  • Show SubTotals at the Top of the Group *
  1. Go to the Design tab.
  2. Click Subtotals
  3. Select "Show all SubTotals at the Top of the Group"

Subtotal

  • Insert Blank Line after Each Item *
  1. Go to the Design tab.
  2. Click Blank Rows
  3. Select "Insert Blank Line after Each Item"

Blank

  • Hide Buttons and Field Headers *
  1. Go to the PivotTable Analyze tab.
  2. Click Buttons and Field Headers

Buttons

  • Change the Pivot Layout *
  1. Go to the Design tab.
  2. Click Report Layout
  3. Select "Show in Outline Form"

Layout

The modifications mentioned above can be easily changed via code. SpreadJS provides many different options to customize the appearance and functionality of the PivotTables according to the needs of your application. You can change the PivotTables options and the layout as below:

let option = pivotTable.options;
option = {
        allowMultipleFiltersPerField: true,
        insertBlankLineAfterEachItem: true, // Insert Blank Line after Each Item *
        grandTotalPosition: GC.Spread.Pivot.GrandTotalPosition.row,
        subtotalsPosition: GC.Spread.Pivot.SubTotalsPosition.top, // Show SubTotals at the Top of the Group *
        displayFieldsInPageFilterArea: GC.Spread.Pivot.DisplayFields.downThenOver,
        reportFilterFieldsPerColumn: 1,
        bandRows:true,
        bandColumns: true,
        showRowHeader: true,
        showColumnHeader: true,
        showDrill: true, // Collapse Buttons *
        showMissing: true,
        showToolTip: true,
        missingCaption: 'something',
        fillDownLabels: false,
        repeatAllItemLabels: false,
        rowLabelIndent: 4,
        mergeItem: false,
        showHeaders: true // Collapse Field Headers *
    };
pivotTable.layoutType(1); // Change the Pivot Layout to Outline Form *
  • Conditional and Custom Formatting

Next, we are going to format the PivotTable fields. You can set the format using the pivot panel as below:

  1. Go to Values - > Value Field Settings
  2. Click Number Format
  3. Set Format. In our case: $#,##0
  4. Confirm.

Format

If you want to do that by code, see below:

//identify the area
var areaActual= {
            dataOnly: true,
            references: [
                {
                    fieldName: "Actual",
                    items: [fieldName]
                }
            ]
        };
 
var style = new GC.Spread.Sheets.Style();
style.formatter = "$#,##0";
//set style to the area
pivotTable.setStyle(areaActual, style);

We can use the same logic for the other fields. Use the corresponding formats in the table below:

Field

Format

Budget

$#,##0

Difference

[Green]$#,##0;[Red]\-$#,##0

Difference %

[Green]0.00%;[Red]\-0.00%

We recommend using Conditional Formatting to make it quicker for the viewer to see the biggest accounts. PivotTables offers the ability to set conditional rules to the specified dimensions. No matter how the PivotTable layout changes, the conditional rules only follow the specified dimensions.

If you are using Designer, follow the below steps to add conditional formatting rules:

  1. Select the cells: G7:H11
  2. Home → Conditional Formating → New Rule.
  3. Choose "Format all cells based on their values".
  4. Format style: 2-Color Scale (blue for the highest value, white for the lowest).

Conditional

Repeat the same for other Account Groups, considering using Yellow for the Expenses and Blue for the Incomes.

Final Report

Below, you have a screenshot of what your Income Statement Report would look like at the end:

Final Report

You can then load this into your application by first exporting as a JavaScript file using File>Export>SpreadJS JSON>Export JavaScript Files:

Export JavaScript Files

Once the file is saved within your project, you can add a script reference to the head section of the HTML:

<script src="data.js" type="text/javascript"></script>

This is how you can use our JavaScript component for generating the needed financial reports to assess your retail business performance and set the stage for discovering insights that can influence decision-making and drive change with a JavaScript income statement.

Discover more about the opportunities SpreadJS offers by getting the free trial.

If you have any questions or insights you would like to share, please post a comment below. Thanks!

Tags:

comments powered by Disqus