Welcome to MSDN Blogs Sign in | Join | Help

Chart Pattern Fills

Today’s Author: Eric Patterson, a program manager on the Excel team.  Eric is going to discuss applying pattern fills to chart data points and includes a sample add-in for this purpose.

Overview

In Excel 2007, the interface for applying Pattern fills to chart elements has been removed in favor of the interface for applying Picture and Texture fills.  Charts formatted with pattern fills in previous versions of Excel will retain and display the pattern fills when opened in Excel 2007.

Here is an example of a chart with pattern fills applied:


While the interface for applying Pattern fills has been removed, support is still available via Excel’s object model.  The FillFormat.Patterned Method can be used to apply pattern fills to chart elements, such as datapoints in a series.  I will describe how the object model can be used to create the chart shown above.  A sample add-in with an interface for applying pattern fills to chart elements also accompanies this blog entry. 

The sample add-in with RibbonX and VBA code is here: http://officeblogs.net/excel/PatternUI.zip

Applying Patterns using the Visual Basic Immediate Window

To demonstrate how pattern formatting can be applied, let’s start with a simple Pie Chart.  After creating the chart, click on the Q1 Pie slice once to select the series, then a second time to select the Q1 data point.  It should look like this:


While the Q1 datapoint is selected, launch the Visual Basic Editor by pressing ALT+F11.  In the Visual Basic Editor, press CTRL+G to display and move to the Immediate Window.

In the Immediate Window, type the following and press enter:   selection.fill.patterned (4)

This will apply the 4th indexed pattern to the selected chart element.  At this point the chart will be updated to look like this:


Repeat the Process

To finish the chart, the above process can be repeated for the other 3 data points.  Select them in turn, switch to the VBE and type the Selection.Fill.Patterned command for each of them.  The indexes of the patterns that I used are 26, 15 and 12 for Q2, Q3 and Q4 respectively.  At this point, the chart will look like this:


Sample Add-in

Next I will demonstrate how an add-in can be created to quickly apply Pattern Fills using a gallery control in Excel.

THE RIBBONX

We’ll start off with the RibbonX code.  If you aren’t familiar with RibbonX, the short explanation is that it’s the XML that defines what the Ribbon will look like.  Specifically for us, the RibbonX will define where we want to put our gallery, and the items in the gallery (including pictures).  For more detailed information on RibbonX, see these links:

Office Fluent Ribbon Developer Portal
http://msdn2.microsoft.com/en-us/office/aa905530.aspx

Tools and Utilities for the 2007 Microsoft Office System
http://msdn2.microsoft.com/en-us/office/aa905356.aspx

The RibbonX we will create will live in the document and travel with it.  We will have to use a tool to get the RibbonX in the document.  The one I’ll use for this example is called the “Office 2007 Custom UI Editor”.  You can download it freely from here:

http://openxmldeveloper.org/articles/CustomUIeditor.aspx

After you download and install the tool, boot it up and open the “PatternUI.xlam” document that accompanies this blog entry.  You should see:


To create this, I started by using the Inset Icons command in the UI Editor and selected pictures of each of the pattern fills that I had previously created.  I made sure to name the files such that they matched the indexes for the patterns in the object model.

Once the icons were added, I then typed in the Ribbon XML below that specifies which chart tab (ChartToolsFormat) and what the name of the group should be (Patterns).  Within the Patterns group in the ribbon, I specified the items to be included in the gallery that corresponded to the icons that I had previously added.  I chose to size the gallery to 8 columns and 7 rows.

Here is the bulk of the XML.  The Gallery elements were repeated for all 54 elements:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
 <ribbon>
  <contextualTabs>
   <tabSet idMso="TabSetChartTools" >
    <tab idMso="TabChartToolsFormat" >
     <group id="customGroup" label="Patterns" insertAfterMso="GroupShapeStyles">
      <gallery id="PatternGallery" image="Pattern54" size ="large" columns="8" rows="7" onAction="InsertPattern" >
         <item id="Pattern1" image="Pattern1"/>
         <item id="Pattern2" image="Pattern2"/>
         <item id="Pattern3" image="Pattern3"/>
       .
       .
       .

After entering all of the Ribbon XML, I used the Generate Callbacks command in the UI Editor, which produced this callback:


This callback can then be entered into a module in the VBE.

VBA CODE

In the code module in VBA, just 2 lines of code need to be added to the callback.  The first line is very simple error checking in case you attempt to use the gallery to apply formatting when an invalid object is selected.  The second line is the same command that we typed into the Immediate pane above.  In this instance, the argument for the Patterned Method is “index+1”.  Index is the gallery index that is returned.  This index is zero based so the “+1” is used to ensure that the gallery index matches the pattern index.

'Callback for PatternGallery onAction
Sub InsertPattern(control As IRibbonControl, id As String, index As Integer)
    On Error Resume Next
    Selection.Fill.Patterned (index + 1)
End Sub

Make this an Add-in

The final step is to save this file as an addin using the Save As command.

The Finished Product

After defining the RibbonX and VBA code, opening up the add-in will show a pattern dropdown on the Chart Format tab when a chart is selected.  To use it, you select a chart series or data point and then click on one of the gallery entries.


Pattern fills are primarily used for Black and White printing.  If you are using pattern fills for this or other purposes we would like to hear from you.

Published Friday, November 16, 2007 4:43 PM by David Gainer
Filed under: ,

Comments

# MSDN Blog Postings &raquo; Chart Pattern Fills

Friday, November 16, 2007 8:21 PM by MSDN Blog Postings » Chart Pattern Fills

# Chart Pattern Fills

Friday, November 16, 2007 9:45 PM by Windows Vista News

Did you see the post at blogs.msdn.com

# re: Chart Pattern Fills

Sunday, November 18, 2007 2:50 AM by Francis

In addition to B&W printing, I have used patterns in a couple of cases in color charts:

- to indicate association (e.g., where slices in a pie are related in some way, kind of like the intersection in a Venn diagram, e.g. in a chart of fruit sales, all citrus fruit has orange hatching over individual background colors)

- to indicate a uncertainty or options (e.g., in a stacked column chart, where series 1 is filled solid up to a certain value, and series 2 is shaded to indicate possible increase from series 1 values, e.g. returns under optimal conditions)

# re: Chart Pattern Fills

Sunday, November 18, 2007 5:38 AM by Ute Simon

I wish this option were available for PowerPoint 2007, too. Though PPT-charts are now based on Excel, there seems to be no way to apply pattern fills to them. :-(

Aside from printing in b&w, patterns are a great help for colorblind people to help them distinguish the items.

# re: Chart Pattern Fills

Monday, November 19, 2007 2:56 AM by Yuvraj

hi

After saving the file as an add-in, can i apply rights to it? when i open the xla file the permission icon for assigning rights is grayed out.

Is password protection the only way to protect an add-in file?

# re: Chart Pattern Fills

Wednesday, November 21, 2007 1:32 PM by gkoehler

I hope patterns are restored as a built-in option from Ribbon with Office SP1.  Should not have to spend time creating custom solution to get something I already had in previous version.  

# re: Chart Pattern Fills

Wednesday, November 21, 2007 10:57 PM by kokkit

Posting in this blog is rather inconvenient. I apologize for spaming chart pattern fills with a theme color issue.

I was wondering if anybody realize, in Excel 2007, theme colors 0 to 3 do not match. Theme index of 0 in styles.xml is mapped to color 1 and theme index of 1 is mapped to color 0 in theme1.xml. Same thing happens to color 2 and 3.

This is contradicting to the spec (OOXML part 3, page 147). Perhaps, the engineer misused this erratic mapping that could be resulted from the clrSchemeMapping order in Word?

Thanks

# re: Chart Pattern Fills

Tuesday, November 27, 2007 9:55 AM by A

Kokkit:

Yes! I noticed that too. I had to stick in a little swap just for those four colours and only for Excel.  I'm thinking there is a map, like the explicit one in PowerPoint, but instead of being stored in the file it is internal to Excel since it is constant (or maybe they just wanted to keep us on our toes?).  Talking about constant things, try modifying fill style 0 in styles.xml by hand...

Talking about themes and colours, can anyone figure out how to apply tint in charts properly?  The ECMA documentation indicates some sort of linear formula but that's not what I'm seeing.  I've tried asking the OfficeArt guys but their blog is dead.  I've also asked this at the developper Forum but no one's replied there either.

http://openxmldeveloper.org/forums/thread/1627.aspx

Any hints would be greatly appreciated. The magic formula itself would earn my eternal gratitude.

# re: Chart Pattern Fills

Saturday, December 01, 2007 8:20 AM by Stephane Rodriguez

"I was wondering if anybody realize, in Excel 2007, theme colors 0 to 3 do not match. Theme index of 0 in styles.xml is mapped to color 1 and theme index of 1 is mapped to color 0 in theme1.xml. Same thing happens to color 2 and 3."

The first two colors are indeed swapped. It's a bug in Excel 2007. To fix this without killing existing Excel 2007 files, they will have to introduce a new disambiguation attribute, which in turn will make developers' lives harder (since they will have to implement both cases instead of just grabbing the color by index).

# re: Chart Pattern Fills

Wednesday, December 12, 2007 10:46 AM by Mbajiuka Uchechukwu

I am school lecturer needs to know all about the tutorial on microsoft excel chart.

# http://www.middle-empire.com

Thursday, December 13, 2007 10:59 PM by Middle Empire

Very good tips!

Middle-Empire

http://www.middle-empire.com

New Comments to this post are disabled
 
Page view tracker