The original article shows how to color the area between two lines in a chart where the lines

*never cross*. I'm going to assume you're like me and you read that blog every day so you already know what I'm talking about.

What if you want to cross the lines? The good news is that crossing the lines doesn't break anything, the area between the lines is still filled in perfectly. The bad news is that the fill series is only one color and as soon as you show it to someone they'll ask why the positive and negative areas aren't green and red, respectively (which you shouldn't do anyway because a lot of people are red/green colorblind). Before today, you would have said that it would take too long to add a second color, that it was impossible, or you would have just drawn auto shapes on top of the chart. Today though there is a much easier solution:

- Download the workbook at the end of this post.
- Plug in your data values.
- Profit.

I'm assuming we're all starting from this point:

Okay, so the first thing that is obvious is that you'll need to add another data series if you want an additional color. Creating separate positive and negative data series doesn't work by itself though, you need your positive and negative shading to end where the lines intersect, but the shading will continue on to the next point on the axis with the current configuration. What we need to do is find the points where the lines intersect and add them to our secondary x-axis (just a recap, the lines are on the primary x-axis, the shading on the secondary axis).

This isn't as hard as it sounds, but when searching for a way to find the point where lines intersect in Excel I found a lot of people suggesting Goalseek. Letting machines do math for you is only okay if you told it

*exactly*what to do. If you didn't tell it what to do, the computer is

*thinking for you*, and that is a slippery slope to Skynet. But that's a whole different post.

The common formula for a straight line is y = mx + b, where m is the slope and b is the y-axis intercept. At each point of intersection, we have two lines:

y1 = mx1 + b

y2 = nx2 + c

Because we want the point where the lines intersect, y1 = y2 and x1 = x2. Because both of the formulas equal y, they are also equal to each other and we can simplify to:

mx + b = nx + c

Solving for x gives us:

x = (c-b)/(m-n)

Excel has built in functions for calculating the slope and the intercept for a range of x and y values which they've cleverly named SLOPE() and INTERCEPT(). We'll only need two points to calculate each of our line segments, so our formulas will take the form

slope({y1,y2},{x1,x2}),

where (x1,y1) are the coordinates of the beginning of the line segment and (x2,y2) is the end point of the line segment.

Because the lines can potentially cross between any two data points, we're doubling the size of the original secondary x-axis. Every other point will be one of the original data points, and the in between points will either be NA() if the lines don't cross (Excel will skip over NA() errors when plotting most types of charts) or something like this formula if they do cross:

=(INTERCEPT(c4:c5,d4:d5)-INTERCEPT(b4:b5,d4:d5)) /

(SLOPE(b4:b5,d4:d5)-SLOPE(c4:c5,d4:d5))

Of course the formulas in the workbook are much uglier because I'm counting ROWS() and using the OFFSET() formula so I don't have to manually add each line segment from the original data set, but it's a little hard(er) to follow.

Your new secondary x axis should look something like column G. You stil run from 0 to 1,000, you still repeat the first and last value, and every other point is one of the original points as calculated in Mr. Peltier's post. The points in between are #N/A errors where the points don't cross, and the points of intersection we calculated with the INTERCEPT() and SLOPE() formulas if they do. An easy way to tell if the lines cross? In column F we had previously calculated the difference between the two original data series. If the product of any two adjacent points in column F is negative, the lines intersect between those two points.

The next step is to rebuild the three Area data series. Area1 is the transparent series , Area2 is the positive series, and Area3 is the negative series.

To create Area1, every other row should point to an entry in the original series in column E. The in-between points should once again be NA() errors if the lines don't intersect (same for Area1 and Area2 as well), and when they do intersect we just need to calculate the y value at that point. We already have the x value, so we can plug that into our y = mx + b equation. The formula for H9 would be:

=SLOPE(b6:b7,d6:d7)*g9+INTERCEPT(b6:b7,d6:d7)

You could also use the points c6:c7 for your y values as both line formulas will give the same result with that x value (g9).

Area2 and Area3 are incredibly straight forward. We still use the points in column F for every other entry, but the trick is that we only use the positive points in Area2 and only negative points in Area3. This is easy to do with MAX(f4,0) for Area1 and MIN(f4,0) for Area3. All other points are either NA() errors if there is no point of intersection or zero.

Now you just need to go into your graph and add these series to the chart. Series 1 and 2 are lines, Series 3 (the transparent series) should now point to h2:h20, Series 4 (the positive series) should point to the values in column I, and you'll have to add a Series 5 (negative) to point to the values in column J. The secondary x-axis values also need to be changed to the values in column G. Also, if your Series 5 didn't default over to the secondary axes, you'll have to switch it. Double click on each area series, change the colors, and you're all set.

If everything went correctly, you should have something like this:

If not, the workbook will make more sense than I ever could:

Let me know what you think in the comments.

-David

David -

ReplyDeleteThanks for elaborating on my technique. I've included your final figure into your comment.

any way to place this back on drop.io. I think the link is expired

ReplyDeleteLink is updated.

ReplyDeleteHello David,

DeleteYour site has been very useful, and I appreciate all of your tips and techniques. I want to make a similar type of chart as stated in your post. I have tried your technique as well as others, but it seems that I had made some mistake and I was struck in 2nd X axis calculation onwards. Please help me to solve the same at the earliest. Moreover I am not able to download the file from your link.

Thanks in Advance,

R.Vadivelan

hi david! thanks for this blogpost, however, the link is not working any more :( could you please update it? thanks a lot!

DeleteTrying to adapt it to a time series but hitting a wall...

ReplyDeleteplz David

ReplyDeleteI want to file

[http://drop.io/davidatwork/asset/twocolorxyareachart-xls] <-Do not go down

Email me plz

hday52@nate.com

Hi David, this looks great! I was trying to download the XLS file, but it times out.. Is the link still active?

ReplyDeletelink does not work.. can u fix please?

ReplyDeleteHi David,

ReplyDeleteAny way to place this back on drop.io. I think the link is expired again.

Please?

I would like to setup a chart like this but am having trouble following the post, could you please repost the excel file? Thanks!

ReplyDeleteAny chance to provide the XLS again? Link seems to be expired again.

ReplyDeleteThanks!

Hi, thanks for this post, but the link is broken/expired again ...

ReplyDeleteHey, could you update the link again? Thanks!

ReplyDeleteLink has expired :(

ReplyDeleteHi David,

ReplyDeleteCould you please update the link to the excel file?

Thank you

Hi David, can you please be so kind to update the file again...it's expired.

ReplyDeleteLink does not work :(

ReplyDeleteHey David, 2 years later and this article is still useful! Would you mind uploading the workbook again? it appears the link is no longer valid.

ReplyDeletecan anyone update this old link?

ReplyDeleteThe link is broken, David. Great blog, anyway.

ReplyDeleteLink is not working...pls update

ReplyDeletePlease send me the file at niftymama@gmail.com

ReplyDeleteWould it be possible to place this back on drop.io yet again? The link has probably expired again? Many thanks....

ReplyDeleteHi, it looks like the link has expired. Can you update it? Thanks!

ReplyDeleteI still cannot download the workbook. Any chance of updating the link?

ReplyDelete(Roger Berk)

link not working again... please update.

ReplyDeletecan you update the link possibly?

ReplyDeleteHi, not sure if you're still monitoring this blog, but the link has expired again. Any way to reactivate it?

ReplyDeleteAny way to get this workbook? Link expired again. I guess I'm really really really late to the party.

ReplyDeleteHi, I think the link to drop.io is expired again?

ReplyDeleteHi,

ReplyDeleteunable to download the file.

Any chance you can update the link again

ReplyDeleteAnyway to update the link... I know this is years later... but this is what I'm looking for. Thanks.

ReplyDeleteHi David, I was referred here from Stephanie Evergreen's blog (EvergreenData), but your magic spreadsheet link has expired again. Is there any chance that you could re-link your spreadsheet?

ReplyDeleteHi, could you update the link again? I'd love to be able to download the workbook. Thanks!

ReplyDeleteGood job- this sounds great! Could you update the link again, please? :)

ReplyDeleteSo I guess the secret to getting people to read my blog was to stop updating it for six years. Who knew?

ReplyDeleteThe last time I was playing around with this file (Stephanie Evergreen asked me about it earlier in 2015), it was no longer working in Excel 2010. Attempts to revive it were unsuccessful, but my Excel skills are a little rusty these days.

If you want to play around with it, Jon Peltier has a working link to the old file here: http://peltiertech.com/two-color-xy-area-combo-chart-guest-post/