PTS Blog

Main menu:

PTS Utilities

Commercial Utilities developed by Peltier Technical Services

  Waterfall Chart

Excel Books

Books that I own and use while developing in Excel

Goods and Services

Excel or charting related products and services which I use or feel are worthwhile additions

Subscribe

Site search


Recent Posts

Recently Commented

July 2008
S M T W T F S
« Jun   Aug »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Archive


 

Categories


 

Interactive Multiple Line Chart

by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2008. All rights reserved.

In the Microsoft charting newsgroup, SKP asked how to make a chart that he could easily change from one series to another. This post will detail the steps to make an interactive chart that allows the user to select from among a number of series using a listbox, and the selected series is displayed in a chart.

Line Chart and Listbox with Highlighted Series A

A similar technique is described in Interactive Parallel Coordinates Chart on my web site, and I used this technique in a workbook that supports my blog post Re: Abortion Ratios 1980-2003.

Update (22 July 2008)

My colleague Dermot Balson read this post and wondered why I was doing it the hard way. Well, he’s right. I’ve built a set of dynamic names that change based on the listbox selection, which is certainly a valid approach. However, for something as “simple” as plotting a column or row of data, it’s overkill. In Easier Interactive Multiple Line Chart I describe an easier approach.

Data by Columns

SKP had data for multiple companies in columns, with each row representing the respective values on a given date. I made up some sine functions for the company data; a portion of the data is shown below.

Portion of Data Range

For many situations a popular visualization approach is to plot one series in a contrasting color, and the rest of the data set is plotted in the background. I plotted this data on a single chart, and formatted all series to use a light gray line. This chart comprises the background.

Line Chart with 12 Series

We need to define some dynamic names to make this work. This process is covered in some detail in Dynamic Charts. First, the column headers, the range of cells containing the Company names, is named “Companies”. The first column, the range of cells containing the dates, is named “Dates”.

The listbox we will add requires a columnar (vertical) list, and the company names are in a horizontal list. I selected a range one column wide and 12 rows high (there are 12 companies in the table), typed this formula

=TRANSPOSE(Companies)
 

and held down CTRL+SHIFT while pressing ENTER to make it an array formula. When an array formula is correctly entered, Excel wraps it in curly braces:

{=TRANSPOSE(Companies)}
 

I named this range “CompanyList”, then selected another nearby cell (cell O2 in the shot below) and named it “SelectedItem”.

Defined Range Names

I added a listbox from the forms menu to the worksheet, and formatted it to use CompanyList as the input range and SelectedItem as the cell link. This means the list in CompanyList is displayed in the listbox, and the index of the selected item is displayed in Selecteditem.

Format Listbox

The listbox looks like this:

Listbox

A couple more names must be defined. Open the Defined Name dialog, add the name “SelectedSeries”, enter the following Refers To formula, and click Add.

=OFFSET(Dates,0,SelectedItem)
 

Add the name “SelectedName”, enter the following Refers To formula, and click Done.

=OFFSET(Dates,-1,SelectedItem,1,1)
 

Now the selected series can be added to the chart. Select the chart, go to the Chart menu, choose Source data, and click on the Series tab. Click Add, then enter the appropriate names for the series name, values, and category labels, prefixed with the sheet name. Click OK.

Add Selected Series

To create a chart without the background series (like the one shown at the top of this post), select an empty cell, run the chart wizard, and in Step 2, click on the Series tab, add the first series, and enter the range names as above.

Here is the chart, showing all series in the background gray and the selected Company A highlighted in blue.

Line Chart and Listbox with Highlighted Series A

Clicking another item in the list changes the highlighted series, to Company L below.

Line Chart and Listbox with Highlighted Series L

By Row

For this example I made up data for multiple companies in rows, with each column representing the respective values on a given date; a portion of the data is shown below.

Portion of Data Range

I plotted this data on a single chart, and formatted all series to use a light gray line. This chart comprises the background.

Line Chart with 12 Series

We need to define some dynamic names to make this work, as described in Dynamic Charts. First, the row headers, the range of cells containing the Company names, is named “TheCompanies”. The first row, the range of cells containing the dates, is named “TheDates”. I selected another nearby cell and named it “TheSelection”. (The names are different than in the first example, because both examples are in the same workbook, and I wanted to avoid naming conflicts.)

I added a listbox from the forms menu to the worksheet, and formatted it to use TheCompanies as the input range and TheSelection as the cell link. This means the list in TheCompanies is displayed in the listbox, and the index of the selected item is displayed in TheSelection.

Format Listbox

The listbox looks like this:

Listbox

A couple more names must be defined. Open the Defined Name dialog, add the name “TheSeries”, enter the following Refers To formula, and click Add.

=OFFSET(TheDates,TheSelection,0)
 

Add the name “TheName”, enter the following Refers To formula, and click Done.

=OFFSET(TheDates,TheSelection,-1,1,1)
 

Now the selected series can be added to the chart. Select the chart, go to the Chart menu, choose Source data, and click on the Series tab. Click Add, then enter the appropriate names for the series name, values, and category labels, prefixed with the sheet name. Click OK.

Add Selected Series

To create a chart without the background series (like the one shown at the top of this post), select an empty cell, run the chart wizard, and in Step 2, click on the Series tab, add the first series, and enter the range names as above.

Here is the chart, showing all series in the background gray and the selected Company A highlighted in orange.

Line Chart and Listbox with Highlighted Series A

Clicking another item in the list changes the highlighted series, to Company L below.

Line Chart and Listbox with Highlighted Series L

Sample Workbook

Download a workbook that contains these two examples: InteractiveLines.zip

Share/Save/Bookmark

Comments

Comment from dermot
Time: Tuesday, July 22, 2008, 4:11 am

Jon, I don’t know if I’m missing something, but to show a series selected from a listbox, I would add an extra column on the end of the data which I would use for the chart values, and I would use a formula to make the values in that column equal to those column selected by the user. That’s all it takes.


Comment from Jon Peltier
Time: Tuesday, July 22, 2008, 7:25 am

Dermot -

To-may-to, to-mah-to.

Both techniques are appropriate for this application. Using an additional column or row to hold the data for the selected series has the advantage of showing the data for the selected series (the extra column or row), and not requiring the complexity of defined names. In other examples I’ve posted, I have used the extra range. Depends which side of bed I got up on, I guess.

In Interactive Parallel Coordinates Chart I used extra ranges to hold the data for the selected series.


Pingback from Graphical Table - Abortion 1980 - 2003 | More Information per Pixel
Time: Tuesday, July 22, 2008, 12:17 pm

[...] Jon Peltier posted on Re: Abortion Ratios 1980-2003 and Interactive Multiple Line Chart some nice Excel techniques to create interactive charts to analyze the Abortion data [...]


Comment from Doug
Time: Tuesday, July 22, 2008, 6:54 pm

Great site….I have a series of monthly incomes with the months undernealth. When I need to show a negative income month, I can set the scale but the months stay in that negative area….how can I move the months out of the negative scale area….thanks.


Comment from Jon Peltier
Time: Tuesday, July 22, 2008, 7:42 pm

Doug - When formatting the axis, either on the Patterns tab in Exce l2003 and earlier, or on the main formatting tab in 2007, look for the axis tick label controls, and choose the “Low” position.


Comment from derek
Time: Wednesday, July 23, 2008, 10:03 am

You’ve populated the cell range O4:O15 with the array {=TRANSPOSE(Companies)}, and then named that range CompanyList. Am right in thinking you could have just defined the named formula as that array function without having to occupy any cells?


Comment from Jon Peltier
Time: Wednesday, July 23, 2008, 10:49 am

Derek -

I could have done just that, and in fact I tried. Any worksheet formulas would have had no problems using the named formula rather than a named range. But the listbox did not accept the named formula as its list source data.


Comment from derek
Time: Wednesday, July 23, 2008, 12:21 pm

But it accepted the named range. Silly Excel. Oh well, thanks for trying.


Comment from Jon Peltier
Time: Wednesday, July 23, 2008, 12:24 pm

Derek -

This is where I slowly rub my chin, and say in my best wise man’s voice, “Excel is like that sometimes.”


Write a comment





Create Excel dashboards quickly with Plug-N-Play reports.