If you use charts in your spreadsheets, you might not like the idea of a chart covering cells that contain important information.
To solve this issue, Google Sheets offers the SPARKLINE function, which inserts a mini-chart inside a selected cell! The SPARKLINE function does not offer the full set of features offered by the Chart widget, but it’s nonetheless a powerful tool to spice up how your spreadsheet looks. It is also an important formula in creating a powerful Google Sheets live dashboard!
In this tutorial, we will study how they work with various examples, plus a sample sheet at the end of this tutorial that you can tinker with!
By default, the SPARKLINE function produces a line chart. As a simple example, we consider a table containing the total sales per annum. We want to plot the per annum total from the sheet below:
We note that the range of values is on the array C3:C9, and we use it as the source array for the formula:
Producing the following chart:
The SPARKLINE function outputs the plot of the given values without annotations such as the title of the plot or the legend (especially used in charts and charts that use multiple sets of similar data).
Adding maximum and minimum value
By default, the SPARKLINE function sets the maximum value as the upper limit and the minimum value as the lower limit of the plot. We can add attributes to the function to set a different maximum and minimum value.
For the same example, we want to set the maximum value to $4,000,000 and the minimum value to $1,000,000. To do so, we write the following code:
The ymax and ymin attributes adjusted the plot to make it look more aesthetic.
Before we go to the next section, take note of the syntax of the attributes:
Keep in mind:
The attributes are enclosed inside a set of curly braces.
The name of the attribute is enclosed within a pair of quotation marks, its value following the name, separated by a comma.
The value of the attribute may also be enclosed within a pair of quotation marks if it is a word.
Each attribute name-value pair is separated from the next pair with a semicolon.
Changing the chart color
You can choose another color if black is too boring. Use the attribute color and set the color of the plot. Basic color words can be used or a specific color in hex notation. For example, we want to change the plot color to blue:
Another type of chart that the SPARKLINE function can create is the column chart. To specify the column chart as the chart type for plotting the same dataset, add the attribute charttype with the value column:
Producing the following chart:
To adjust the heights of the columns, use the ymax and ymin functions as specified in the previous section:
Specify the color of the tallest and shortest column
If you want the tallest and/or shortest column to stand out, you can specify their color. To do so, we use the highcolor and lowcolor attributes for the color of the tallest and shortest column, respectively. For our example, we wish to color the tallest column blue and the shortest column red:
Just a reminder: the ymax and ymin attributes interfere with the highcolor and lowcolor attributes, so do not combine them in the same sparkline chart!
Add an axis and choose the axis color
You can add a horizontal axis and choose its color by using the axis and axiscolor attributes, respectively. To add a horizontal axis, set the value of the axis to true. The axiscolor attributes accept both basic color names and hex notation as values. For our example, we set the following:
For the sake of displaying the axis, we flip one value from the input array to a negative sign. Else the axis will not be displayed in the sparkline chart.
Bar charts work differently from line charts and column charts. Bar charts combine a set of values into a single bar of varying length within a cell, so we will add bar charts not to our merged array of cells but to a single cell beside the source cells.
We use the same dataset but transpose them, with the total sales listed in a single column:
We cannot simply add the following formula to the cells beside the target cells:
As it produces the following result, which is not what we want:
Here, we can see that the chart does not help us, at the very least, identify which has the highest value. In order to make the differences in the values appear, we will specify the maximum value by using the max attribute. For our example, we specified the maximum value as $4,000,000:
Producing the following result:
One note: the max value can be individually specified for each bar chart, so you can have varying lengths for each bar chart. This is not recommended, however, as this will confuse you when you check the bar charts. A technique for setting the same settings to all sparkline charts will be outlined below (under "A trick for managing SPARKLINE charts").
Use a bar chart to visualize percentages
We can use the bar chart option to visualize what percentage of something came from a certain source. For our example, there are four branches contributing to the total annual sales. The bar chart can combine the four sources into a single bar chart. For our example, we use the following formula:
The only difference is with the input array. The results look like this:
Now, how do we visualize the distribution by percentage contribution? We simply change the value of the max attribute. The max attribute accepts the resulting value from a basic formula such as the SUM, which we will use. We will specify the SUM of the array of cells as the max.
You can specify a different pair of colors for the bar chart covering multiple cells. We will use the color1 and color2 for the first and second color that will appear. For our example, we will specify gold and a certain shade of dark blue as color1 and color2, respectively:
Another type of chart, relevant to businesses, is the win-loss chart. A win-loss chart tracks whether the value inside a cell is positive or negative. We will have a different example of percentages of stocks visually displayed using a win-loss chart. The formula will look like this:
And the resulting chart will look like this:
Specify the color of positive and negative values
It makes more sense if we choose the color for the positive values and another color for the negative values. To do so, we use the color and negcolor attributes, respectively. For our example above, we see the color of positive values to green and the negative colors to red:
Here are the steps in adding a “control panel” to our sparkline chart:
Step 1: Identify what kind of Sparkline chart you will use. This will help you determine what attributes shall be included in the “control panel”
Step 2: List the attributes in a column. Set the next column for the value of the attribute. If the value is a number, leave it blank. If the value is chosen from a list, add a drop-down list. You can check the tutorial linked above.
At this point, your sheet should look similar to this:
Step 3: Edit the formula by replacing the attributes with cell references. In our example, the change is from
To... (the exact cell references will differ depending on your sheet)
Your sheet should now look like this:
The Sparkline chart worked exactly the same! If we change the values in our control panel, we will see that the Sparkline chart changed as well:
Summary plus a sample sheet
While the SPARKLINE function cannot match the Chart tool of Google Sheets in its features, it is still a powerful function that will help you construct a dashboard in Google Sheets. Head over to our next article on creating a dashboard in Google Sheets to apply what you learned here (coming soon)! You cm also just search for our other blogs here.