Today’s post is written by David Montgomery, who has a new blog called David @ Work. David read two of my recent posts, Fill Below an XY Chart Series (XY-Area Combo Chart) and Fill Between XY Chart Series (XY-Area Combo Chart), and noted the lack of an explanation for different colors between XY series that cross.
David attacked the problem in his blog post Two Color XY -Area Combo Chart, and he has graciously accepted my offer to repost his article below.
Two Color XY-Area Combo Chart
Last Wednesday, Jon explained the technique for creating an XY-area combo chart. 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 read the article and that we’re all starting at this point:
But what if you want to cross the lines? The good news is that this isn’t Ghostbusters and crossing the lines doesn’t break anything, the area between the lines is still filled in perfectly. If we flip the values in B5:C5, B7:C7, and B9:C9, we get the following:
The bad news is that the fill series is only one color and as soon as you show it to your boss she’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.
But if you’re like me, and I know I am, chances are you’re reading this blog because you want to learn how to do it yourself. At this time I should note that Excel 2003 and I have a monogamous relationship and that some of these steps may be different for other versions of Excel.
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 x-axis with the current configuration.
What we need to do is find the x values at 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 x-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. This deeply offends the math geek in me. 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 I digress…
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 in our chart, we have two lines:
y1 = m1x1 + b1
y2 = m2x2 + b2
Because we want the point where the lines intersect, y1 = y2 and x1 = x2. Also, because both of the formulas equal y, they are also equal to each other and we can simplify to:
m1x + b1 = m2x + b2
Solving for x gives us:
x = (b2-b1)/(m1-m2)
Slope m1 and intercept b1 correspond with the top line, the red one in the example, and m2 and b2 refer to the bottom/blue line. For now, let’s focus on the first point of intersection where the two bolded line segments cross:
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 to calculate the two slopes (m1 & m2) between the first and second points will look like this:
Our two y-axis intercepts are calculated in the exact same way, just replace SLOPE with INTERCEPT. We’ll need to calculate the slope and intercept for both line segments at every point where the lines cross. If the lines don’t cross, we can skip the calculation.
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, which are the potential points of intersection, will either be NA() if the lines don’t cross (Excel will skip over #N/A errors when plotting most types of charts) or something like this formula (for cell G5) if they do cross:
Remember that we’re just replacing the m and b values in x = (b2-b1)/(m1-m2) with the SLOPE() and INTERCEPT() functions. It isn’t as bad as it looks. In this image I’ve added the new secondary x-axis points in column G; the red shaded cells are the original points, and the blue points are the potential points of intersection that we’ve calculated:
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. Another trick to avoid manually entering each formula would be to insert blank cells between your original points in D4:F11. That way the original secondary x-axis and the new one in column G will be the same size and your formulas should copy down correctly.
So to recap, our new secondary x-axis should look something like column G. You still run from 0 to 1000, you still repeat the first and last value, and every other point is one of the original points as calculated in Jon’s original post. The points in between are #N/A errors if the lines don’t cross and the x values we just calculated for the points of intersection if the lines do cross. 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. You should be able to add an IF() checking this at each potential point of intersection without too much trouble.
The next step is to rebuild the three Area data series. Area1 is the transparent base series, Area2 is the positive series, and Area3 is the new 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 Area2 and Area3 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 H5 would be:
You could also use the points C4:C5 for your y values as both line formulas will give the same result with that x value (G5).
Area2 and Area3 are incredibly straight forward. We still use the values in column F for every other cell, but the trick is that we only use the positive points in Area2 and only negative points in Area3, with the rest of the points being zero. This is easy to do with something like MAX(F4,0) for Area1 and MIN(F4,0) for Area3. All of the in-between points are either NA() errors (if there is no point of intersection) or zero. If everything worked correctly, your workbook should look like this:
Now you just need to add these new data series to the chart. Series 1 and 2 are the lines which should already be done, 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.
If your Series 5 didn’t default over to the secondary axes, you’ll have to switch it. Double click on Series 5, click on the “Axis” tab, and select the “Secondary axis” option.
Finally, double click on each area series, go to the “Patterns” tab, choose the two colors you think are the prettiest, 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:
And that’s all there is to it. Let me know what you think in the comments.