Area Chart – Invert if Negative
by Jon Peltier
Monday, June 16th, 2008
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
A visitor to the Microsoft newsgroups wanted his area chart to show a different color for positive and negative values. This is easy enough to do in a column chart. One technique is simply to use the Invert if Negative formatting option, the other it to make a conditional chart with one series for positive values and the other for negative values. Neither of these techniques works for area charts, but it’s possible to adjust the data to make an area chart with two series, one for positive and one for negative.

Del Cotter has written up a very similar approach in Excel area chart with colour invert if negative. It’s the first I’ve seen of his Information Ocean blog, but I’ve added its feed to my reader, and I’ll keep up with it.
We’ll use the following sample data, selected because it crosses zero several times. The first two columns are the category labels and values, the second two columns have split out negative and positive values, and the fifth column will be used later in the exercise. The formulas that split out signed values are simple:
C2: =MIN(0,B2) D2: =MAX(0,B2)

Here is a simple column chart of the original data:

Without too much trauma you can use Invert if Negative to format negative values differently than positive values:

Excel 2007 partly breaks the Invert if Negative functionality described above: if you use solid fills, the negative column can only be filled with white. You’ll need to use the multiple-series approach shown below.
You can construct a conditional chart using the Positive and Negative values computed in columns C and D:

Here is a simple area chart of the original data, as boring as the column chart:

There’s no Invert if Negative setting for area chart fills, so the only apparent option is to plot the Negative and Positive series together. This chart uses the unstacked area chart; the stacked version is uglier and more confusing. The dotted line shows where the series is expected to go.

The problem is that each series goes to zero at the category where the other series has a nonzero value, rather then both meeting in between. What is needed is to interpolate between points that cross zero, in order to have a point at zero. Since this may occur anywhere between a given pair of original categories, we need to use a numerical scale, and a date scale axis is as close as we can get with an area (or line or column) chart. The following shows a second range of calculations in the worksheet.

Cells B10, B12, B14, B16, and B18 link back to cells B2:B6. The blue cells in column B contain either a value of zero or a value midway between the surrounding values, according to this formula:
B11: =IF(B10*B12<0,0,(B10+B12)/2)
What this formula does is enter zero if the connecting line crosses zero (i.e., if the values on either side have a negative product), otherwise it returns the average of the two values.
Cells A10, A12, A14, A16, and A18 contain numerical values of 1, 101, 201, 301, and 401. These are the pseudo-dates for our date scale interpolated axis. These values start at 1, because the first date that is reliably recognized in a chart is 1/1/1900 (i.e., 1). The spacing is 100, which should give sufficient resolution for a chart of reasonable size. The green cells in column A contain a formula that interpolates between the numbers on either side according to the value in the corresponding blue cell. The formula is a simple interpolation:
A11: =(B11-B10)/(B12-B10)*(A12-A10)+A10
Here’s an area chart of the Values data, with a dotted line showing where it should go for a smooth interpolation. (Note for example that the category axis places 67.67 equidistant between 1 and 101.)

We can plot the Negative and Positive series on the chart (stacked or unstacked, they are identical), and almost get what we want, although the category axis spacing is still off, as shown by the dotted lines.

After the category axis is converted to a date scale, and the base unit changed to days, the alignment of the chart series is correct.

The axis labels are not what we need to clearly show the data. Add a series using the original category labels in A2:A6, and the zeros in cells E2:E6 as Y values. Convert the series to a line series. There are only five points, not nine, so the points are plotted at the first five category labels on the interpolated date scale.

Move the new series to the secondary axis group. The new Y axis appears on the right, with a scale of zero to one.

We need a secondary category axis, and Excel usually provides only the value axis when we first move a series to the secondary axis group. Add a secondary category axis, which appears at the top of the chart.

Format the scale of the secondary Y axis so the category axis no longer crosses at the maximum. This makes it cross at zero (the bottom of the chart.

All that’s left is to hide what we don’t want to show. Format the primary category axis so it has no category tick labels. Format the secondary value (Y) axis so it shows no line, no tick marks, and no tick mark labels. Format the secondary category axis so it has no mile and no tick marks, but keep the labels.

An alternative labeling approach that doesn’t need to muck with secondary axes involves adding labels into column E of the second range. The labels are either the original category labels or blanks.

Start with the chart with Positive and Negative series that used the date scale axis to shape the area series correctly. Add a series using A10:A18 for X and B10:B18 for Y. It gets added as another area series (the chart below is a stacked area type).

Convert the added series to a line type.

Use Rob Bovey’s Chart Labeler to add the labels from A10:A18 to the new series.

Format the line series so it uses no line and no marker to hide it, and adjust the position of the labels as appropriate.

Related Posts:
- Area Chart With Gap
- Stacked Area Chart Challenge
- Category Labels That Don’t Overlap the Data
- Fill Below an XY Chart Series (XY-Area Combo Chart)
- Fill Between XY Chart Series (XY-Area Combo Chart)
- Date Axis with Centered Years
- US Employment Slump Chart – How To
- Growth Rates in a Panel Chart
- Two Color XY-Area Combo Chart – Guest Post
- Clustered-Stacked-Column Combo Chart With Lines
Posted: Monday, June 16th, 2008 under Formatting.
Comments: 12
Comments
Comment from derek
Time: Monday, June 16, 2008, 3:43 pm
With this technique, it’s now possible to go back to your chart challenge article with a new approach:

Comment from derek
Time: Monday, June 16, 2008, 5:22 pm
There was supposed to be a picture there, but I guess the comments don’t support that.
Comment from Jon Peltier
Time: Monday, June 16, 2008, 6:01 pm
Derek – It should support a picture, if you use the proper html. Give me the link and I’ll include it in the original comment.
I did post a follow-up to your first comment, but probably I forgot to submit it before navigating away.
Comment from derek
Time: Tuesday, June 17, 2008, 3:38 am
Here’s a tidied-up version of the general “crossing any two lines” method, with RAND() data. Psychedelic shapes!
Comment from Jon Peltier
Time: Tuesday, June 17, 2008, 6:25 am
Pretty slick!
Comment from Keith
Time: Saturday, November 29, 2008, 12:55 pm
I have tried to do the same thing to 3-D surface charts but with no luck. Any ideas?
Comment from Jon Peltier
Time: Saturday, November 29, 2008, 5:52 pm
Keith -
Invert if negative? in a surface chart, the color of a region is already dependent on value. It’s a matter of formatting the ranges the way you want each value to be represented.
Comment from Matt
Time: Monday, February 2, 2009, 5:42 am
Every time Mr Google brings me back here, and every time it blows my mind! Keep up the good work.
Comment from Random
Time: Tuesday, March 3, 2009, 5:19 pm
I was searching eveywhere to find out how to create a positive and negative bar chart, and i found this site. It was really useful and helped me to figure out how to work excel in this way. I recomend this site to anyone that wants to create these sorts of graphs.
Thank you for putting this on the web, it really helped with completing my coursework.
Comment from Kathryn Lambkin
Time: Wednesday, April 15, 2009, 2:52 am
Thanks Jon,
I found your forum post mde last year saying getting this to work was near on impossible
http://www.eggheadcafe.com/conversation.aspx?messageid=31756823&threadid=31750427
Seems you didn’t give up on that challenge!
Thanks for a great workaround to a Microsoft flaw.
Kathryn
Comment from asad naqvi
Time: Thursday, April 16, 2009, 7:14 am
hi,
i have a web site designed in which i have to plot dynamic charts from the data that is also going to be sent to it dynamically from an external artificially intelligent instrument. the graph has successfully been plotted using jfree charts but now i have to use the same data to plot the graph in x,-y co-ordinates instead of the usual x,y co-ordinates i.e. the origin should be on top with the y axis pointing downwards. can anyone help me out with this?
Comment from Jon Peltier
Time: Thursday, April 16, 2009, 7:40 am
I’ve never used JFreeCharts, but Google informs me there is a forum at JFreeChart – General.



















Write a comment
I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.
If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.
Read the PTS Blog Comment Policy.