HomeGoogle DocsSpreadsheetSPARKLINE for Positive and Negative Bar Graph in Google Sheets (Array Formula)

SPARKLINE for Positive and Negative Bar Graph in Google Sheets (Array Formula)

Published on

Can we get the horizontal bars on the left and right sides of a (virtual) vertical line in a SPARKLINE bar chart in Google Sheets (left side for negative values and right side for positive values)?

Yes! This post explains how to use the SPARKLINE function for creating a positive and negative bar graph within the cells in Google Sheets.

We will write a SPARKLINE non-array formula first and convert that to an array formula using the MAP Lambda function and then to a Named Function which is SPARKLINE_NEGATIVE_BAR().

When using the said Named Function, you only need to specify the range, the only argument within it. I am sure you are going to enjoy it.

SPARKLINE in Google Sheets for Positive and Negative Bar

SPARKLINE Non-Array Formula for Creating a Positive and Negative Bar Graph in Google Sheets

In three steps, we can code a SPARKLINE bar graph with positive and negative bars in Google Sheets.

We may require one more step to convert the coded formula to an array formula, which expands down.

The final step is possible because of the availability of the LAMBDA functions in Google Sheets.

Three Steps - One Color, Two Color, and Negative and Positive Bars

The SPARKLINE function has two arguments which are data and options.

Syntax:- SPARKLINE(data, [options])

In this tiny in-cell chart, the width of the horizontal bars will be proportional to the absolute values they represent.

In other words, the function uses absolute values from the specified cell or cell range.

So, negative and positive values will have the same impact on the bar graph.

Step 1 Formula (In B2, copy-pasted down):

=ArrayFormula(SPARKLINE(A2,{"charttype","bar";"max",max(abs($A$2:$A))}))

You only have the option to specify two colors (of your choice) based on their signs, e.g., red for negative and green for positive.

Step 2 Formula (In C2, copy-pasted down):

=ArrayFormula(SPARKLINE(A2,{"charttype","bar";"max",max(abs($A$2:$A));"color1",if(A2>0,"green","red")}))

How do we move the horizontal bars on the left and right-hand sides of a virtual vertical bar based on positive and negative data points?

Step 3 Formula (In D2, copy-pasted down):

=sparkline({if(A2>0,min($A$2:$A),A2-min($A$2:$A)),A2},{"charttype","bar";"max",max(0,$A$2:$A)-min(0,$A$2:$A);"color1","white";"color2",if(A2>0,"green","red")})

Step 3 Formula Explanation

The SPARKLINE formula above creates positive and negative bars proportional to the values they represent.

The negative and positive values will be aligned on the left and right-hand sides of a (virtual) vertical line.

Unlike the step 1 and 2 formulas, the step 3 formula returns a two-color bar chart – white, red/green.

That is the key part of the SPARKLINE formula for positive and negative bars in Google Sheets.

SPARKLINE data Points for Positive and Negative Bars:

{if(A2>0,min($A$2:$A),A2-min($A$2:$A)),A2}

White – The cyan-bluish-grey logical part of the data returns data point 1 for the white bar.

Red/Green – The light green cyan highlighted part of the data returns data point 2 for the red/green bar.

To understand what the formula returns in each row, please check the table below.

SPARKLINE Max options for Positive and Negative Bars:

The maximum value along the horizontal axis of all the bar charts will be 20, i.e., max(0,$A$2:$A)-min(0,$A$2:$A).

Table # 1

Cell Reference and Value
(A2:A13)
Data Point 1
(White)
Data Point 2
(Red/Green)
Max Value
A2, -511-520
A3, -214-220
A4, -160-1620
A5, 0.25-160.2520
A6, 1-16120
A7, 2-16220
A8, 4-16420
A9, 1-16120
A10, 0-16020
A11, 0.5-160.520
A12, -115-120
A13, 1-16120

Note:- The SPARKLINE formula will treat all the values in data points 1 and 2 as absolute values.

If the values in A2 is less than 0, the total of data point 1 (white) and 2 (red) will be 16, which is equal to the absolute min value in the range A2:A13. It applies to A3, A4, and so on.

If the values in A2 is greater than 0, the total of data point 1 (white) and 2 (green) will be 16+ (white=16, balance will be green). It also applies to A3, A4, and so on.

The max (20) in the options argument part aligns the bars.

This way, we can use the SPARKLINE function for creating positive and negative bar charts within cells in Google Sheets.

SPARKLINE Array Formula and Named Function for Positive and Negative Bar Graph

Array Formula:

Let’s convert the step 3 formula to an array formula.

You can either use the BYROW or MAP function for that. I’m using the latter one here.

Generic Formula:

=map(A2:A,lambda(r,non_array_formula))

Replace non_array_formula with the step 3 formula and all the cell reference A2 in that formula with the name r.

SPARKLINE Array Formula for Positive and Negative Bar Graph:

=map(A2:A,lambda(r,sparkline({if(r>0,min($A$2:$A),r-min($A$2:$A)),r},{"charttype","bar";"max",max(0,$A$2:$A)-min(0,$A$2:$A);"color1","white";"color2",if(r>0,"green","red")})))

This SPARKLINE negative and positive bar graph formula will spill the result down. So first empty the range B2:B, then enter it in cell B2.

Custom Named Function

I’ve already explained the steps to create a Named Function in Google Sheets.

We can copy the above SPARKLINE Positive and Negative Bar Array Formula and convert it to a named function which is SPARKLINE_NEGATIVE_BAR().

You can download it from my sample sheet below.

Syntax:

SPARKLINE_NEGATIVE_BAR(range)

As per our above sample data, the custom SPARKLINE_NEGATIVE_BAR formula will be as follows.

=SPARKLINE_NEGATIVE_BAR(A2:A)

Make sure you have emptied the column for the formula to expand down.

Example Sheet 131022

Resources:

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

SUMPRODUCT Differences: Excel vs. Google Sheets

The core purpose of the SUMPRODUCT function remains consistent across both Excel and Google...

More like this

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

6 COMMENTS

  1. Thank you for the instruction. I have been looking for a solution for quite a long time.

    How can I change the formula in such a way that the virtual vertical line is always in the center of the cell?

    Many Thanks again!

    • Hi, Sebastian,

      Syntax: SPARKLINE_NEGATIVE_BAR(range)

      Assume the range is A2:A13.

      If you use my custom-named function for the -ve and +ve sparklines, insert the following formula at the bottom of the range, here in cell A14.

      =if(abs(min(A2:A13))>=max(A2:A13),abs(min(A2:A13)),max(A2:A13)*-1)

      Use A2:A14 instead of A2:A13 and hide row#14.

  2. Hi, I’ve been trying for two days, but still, I cannot figure out the final formula. It seems to be wrong.

    There are three arguments in the IF function, whereas there should be just two.

    There must be something wrong with the punctuation, i.e., , used instead of \ or ;.

    Many thanks.

    • Hi, Mauri,

      You can follow the below steps to get the correct formula as per your locale.

      1. Open a new spreadsheet.
      2. Go to File > Settings and change the Locale to the United Kingdom.
      3. Enter my formula within the tutorial above.
      4. Then revert the Locale.

      • My issue is as follows: I have set the regional settings for Spain. While the formula functions properly, the problem arises in the visualization of the bars. Negative values fail to display on the left, and positive values on the right. I’ve observed that it only displays correctly when the regional settings are set to the United Kingdom.

        Could you advise on how to rectify this?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.