Peltier Tech Blog

Excel Chart Add-Ins | Training | Custom Solutions | Charts and Tutorials | PTS Blog

 

Main menu:

 
Peltier Tech
Chart Add-Ins

Peltier Tech Waterfall Chart Utility
Peltier Tech Cluster-Stack Chart Utility
Peltier Tech Box and Whisker Chart Utility
Peltier Tech Marimekko Chart Utility
Peltier Tech Dot Plot Utility
Peltier Tech Cascade Chart Utility

 
Excel Dashboards

Subscribe

Site search

Subscribe

Site search


Recent Posts

Recently Commented

Popular Posts

Archive


 

Categories


 

Privacy Policy

Creative Commons License
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Chart Event Class Module to Highlight a Series

by Jon Peltier
Thursday, August 14th, 2008
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

In Chart Event to Highlight a Series, I showed how to use a chart legend plus chart events instead of a listbox, to select which series to highlight in a busy multi-series chart. That technique is useful, but it is not versatile: it is applicable only to a chart sheet that has the code in the chart sheet’s code module.

This can be remedied by using a class module. The chart sheet’s module is itself a class module, for the chart, which is an object of the class Chart. Objects of this class which are embedded in other sheets (as opposed to residing on their own chart sheets) do not support their own modules. But we can create a new class module which can be applied to any chart.

To create a class module, select the workbook’s VB Project in the Project Explorer of the VB Editor. Right click on any of the Project’s members and choose Insert > Class Module, or from the Insert menu, choose Class Module. Name this class module CHighlight. Paste the code below into the class module.

Option Explicit

Public WithEvents cht As Chart

Const iCOLOR_HIGHLIGHT As Long = 5  ' BLUE
Const iCOLOR_BLAND As Long = 15  ' 25% GRAY

Private miSeries As Long

Private Sub cht_MouseDown(ByVal Button As Long, ByVal Shift As Long, _
                            ByVal x As Long, ByVal y As Long)
  HighlightSeries x, y
End Sub

Private Sub cht_MouseMove(ByVal Button As Long, ByVal Shift As Long, _
                            ByVal x As Long, ByVal y As Long)
  HighlightSeries x, y
End Sub

Private Sub HighlightSeries(ByVal x As Long, ByVal y As Long)
  Dim ElementID As Long
  Dim Arg1 As Long
  Dim Arg2 As Long

  cht.GetChartElement x, y, ElementID, Arg1, Arg2

  If ElementID = xlLegendEntry Or ElementID = xlLegendKey Then

    If miSeries <> Arg1 Then

      ResetSeries

      With cht.Legend.LegendEntries(Arg1)

        With .Font
          .ColorIndex = iCOLOR_HIGHLIGHT
          .Background = xlTransparent
          .FontStyle = "Bold"
        End With

        With .LegendKey.Border
          .ColorIndex = iCOLOR_HIGHLIGHT
          .Weight = xlMedium
          .LineStyle = xlContinuous
        End With

      End With

      miSeries = Arg1
    End If

  End If

End Sub

Private Sub ResetSeries()
  Dim lgnd As LegendEntry

  Application.ScreenUpdating = False

  For Each lgnd In cht.Legend.LegendEntries
    With lgnd.Font
      .ColorIndex = xlAutomatic
      .Background = xlTransparent
      .FontStyle = "Regular"
    End With

    With lgnd.LegendKey.Border
      .ColorIndex = iCOLOR_BLAND
      .Weight = xlThin
      .LineStyle = xlContinuous
    End With
  Next

  Application.ScreenUpdating = True

End Sub
 

This code is almost identical to the code in the chart sheet module in Chart Event to Highlight a Series. One difference is the public variable cht, which is declared WithEvents, which means it will respond to events, that is, actions applied to the object.

We still need to instantiate the class and define the variable cht. If necessary insert a module the same way you inserted a class module above. Paste the following code into the module.

Option Explicit

Global gclsHighlight As New CHighlight

Sub ActivateChart()
  If Not ActiveChart Is Nothing Then
    Set gclsHighlight.cht = ActiveChart
  Else
    MsgBox "Select a chart and try again.", vbExclamation, "No Active Chart"
  End If
End Sub

Sub DeactivateChart()
  Set gclsHighlight.cht = Nothing
End Sub
 

Declaring the global class variable gclsHighlight with the New keyword instantiates the class.

To assign events to a chart sheet or an embedded chart, select the chart and run the ActivateChart procedure. This procedure sets the cht variable of the class equal to the active chart, thus defining the active chart as the chart which will respond to the events.

When a new chart is activated, the previous one is deactivated automatically. If you need to deactivate the chart, run the DeactivateChart procedure (no chart needs to be selected first).

You can assign either or both commands to a Button or Shape, to an ActiveX Control, or to a Toolbar or Menu, run them from the Tools menu > Macro > Macros, or use the Alt+F8 shortcut function key.

You can download a simple workbook that illustrates this technique: ChartEventClassHighlightSeries.zip.

Related Posts:

Bookmark and share this entry:
  • Twitter
  • Digg
  • Facebook
  • LinkedIn
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Google Bookmarks
  • Reddit
  • MySpace
  • Slashdot
  • Yahoo! Buzz
  • Tumblr

Learn how to create Excel dashboards.

Comments


Pingback from Interactive English League Bumps Chart | More Information per Pixel
Time: Thursday, August 28, 2008, 2:10 am

[...] a little with Jon’s code I discovered that this is not true for all click related events. I used Jon’s code to handle events in embedded charts and added a select [...]


Comment from Thom Mitchell
Time: Tuesday, June 9, 2009, 8:32 am

Jon: When I use the file you provided in a zip file on the original posting, the whole chart “flashes” when the macro executes or the event occurs. It’s as if the whole chart object is being “refreshed” by the monitor. Is this Excel 2000, the essential nature of what the VBA is doing or is it something else entirely? (As you know, I have some charts that are pretty “busy,” so I’m trying to assess whether one or more of your series highlighting posts would be appropriate for me to “steal”!)


Comment from Thom Mitchell
Time: Tuesday, June 9, 2009, 8:38 am

P.S. In contrast, Gas Prices Line.xls does *not* behave in the same way:
The entire chart does not flash or “flicker” when a different line of the listbox is selected. For this reason, I find the gas prices solution better if (a) the chart is embedded *and* (b) there aren’t “too many” series.


Comment from Jon Peltier
Time: Tuesday, June 9, 2009, 11:17 am

Thom -

Which precise file was this? I ran through a few and didn’t see any noticeable flashing. I don’t see why 2000 through 2003 would have problems. 2007, well, ….


Comment from Thom Mitchell
Time: Tuesday, June 9, 2009, 11:25 am

Here is the path to the zip file:
http://peltiertech.com/WordPress/wp-content/img200808/ChartEventClassHighlightSeries.zip


Comment from Jon Peltier
Time: Tuesday, June 9, 2009, 12:37 pm

Thom – I had actually tried that one. I guess you could say that there’s a very slight flicker, but much less pronounced than it would be if the whole chart were being refreshed.

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.





Subscribe without commenting

Peltier Tech Waterfall Chart Utility Peltier Tech Cluster-Stack Chart Utility Peltier Tech Box and Whisker Chart Utility Peltier Tech Marimekko Chart Utility Peltier Tech Dot Plot Utility Peltier Tech Cascade Chart Utility

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