Zooming in and out of Excel Charts

Interactive zooming in and out on Charts in Microsoft Excel

Zoom in and out on Excel ChartsIf you have an XY scatter chart with a lot of data points in Microsoft Excel or even using an XY scatter chart mimicking a map in Excel, an interactive zooming function (allowing to magnify user-defined parts of the chart) can be very helpful.

Microsoft Excel does not provide such a feature natively. There have been a few workarounds published. Most of them use interactive controls like spinners or sliders to change the axes scales with OFFSET formulas and dynamic cell ranges or by VBA.

A more intuitive way of zooming into a chart would be to select an area on the chart with the mouse and zoom exactly into this user-defined part of the chart (or map).

We recently had a couple of posts taking advantage of Andy Pope’s stellar idea to use a label control on top of a chart for interactive chart features:

Another Technique for Interactive Excel Charts

Selecting and Highlighting Areas on Excel Charts

Select Areas on a USA Map in Microsoft Excel

Andy’s technique is very versatile and can be a viable solution for many different use cases, including the requirement of an interactive zooming feature.

Today’s post describes an implementation of an interactive zooming feature for a map (XY scatter chart) in Microsoft Excel. As always, the workbook is provided for free download.


The Features

The example workbook to demonstrate the interactive zooming feature shows a map of the 48 contiguous United States and all cities with a user defined minimum of inhabitants. The sizes of the bubbles represent the population of the cities:

USA Map with cities - click to enlargeWith the drop down lists at top right, you define which data labels shall be displayed for the states (none, full names, abbreviations) and for the cities (none, city name, city name and population). The slider beneath the drop downs lets you change the number of cities shown on the map by defining a minimum number of inhabitants:

Zoom on Excel Map - Interactive Controls

The database contains all cities in the United States with more than 5,000 residents.

The most interesting feature is of course the zooming function. Hover over the map with your mouse cursor and it will turn into a cross. Click somewhere on the map and move the mouse right and down. A transparent rectangle appears to define the area you want to zoom into:

Select Zoom Area on Excel Charts - click to enlargeIf you are done with selecting the zoom area, simply click again and the code zooms into this area:

Zoomed Excel Map - click to enlargeYou can zoom again and again to higher level of details. Right clicking somewhere on the map resets the zoom and takes you back to the original map.

The Implementation

The Basic Idea

Mouse interactions with Excel charts are usually done with the mouse event subs of the Chart Object. Microsoft Excel MVP Jon Peltier provides the necessary VBA code for free here: Get XY on any Chart. The downside of using the Chart Object events is the requirement of activating the chart first, before the mouse events are fired.

Microsoft Excel MVP Andy Pope had a stellar idea how to overcome this: instead of using the mouse events of the chart, he inserts an invisible ActiveX label control on top of the chart and uses the mouse events of the label control instead. No activation of the chart necessary anymore.

The US cities map workbook (download link see below) has two charts (XY scatter chart for the borders and the state labels and a bubble chart for the cities) and the label control. The size and the position of the label control has to be identical with the size and position of the plot areas of the charts. Furthermore the order of these objects is crucial. The charts are in the background and the label is sitting on top of them. The Selection Pane looks like this:

Selection Pane

The Charts and the Label Control

The charts are standard Excel chart: an XY scatter (with lines) and a bubble chart. The data series of the state centroids is formatted to be invisible (no marker, no fill, no line).

The label control needs to be invisible, too, i.e. no border, and transparent fill (BackStyle):Label Properties - click to enlargeThe Data

The workbook contains 3 data sheets:

  • Data Cities
    The names of the cities, the positions (latitude and longitude) and the population. In an additional column, a simple CHOOSE formula creates the data labels based on the user selection. The table is sorted descending by formulas. This is important for the option to let the user define the minimum number of residents. So, please do not change the sort order here.
  • Data Centroids
    This sheet contains the centroids of the states. The data is used to display the state names in the middle of the states. Again, an extra column creates the labels by formulas based on the user selection.
  • Data Polygons
    This table contains the polygon data of all states and is used in the XY scatter chart to draw the state borders.

Worksheet [control]

On the control worksheet, the form controls (drop down and slider) are managed and a few simple formulas calculate the minimum and maximum latitudes and longitudes and the size of the plot area.

The Named Range / Named Formulas

The workbook contains 4 named formulas. Two of them (“myLatitudes” and “myLongitues”) are dynamic ranges based on the user selection of the minimum number of residents (using OFFSET). The other two names (“myChtAxesScales” and “myChtPlotAreaSize”) are simple range references used in the VBA code.

The VBA Code

Finally the heart of the solution: the VBA code. The basic idea is to manipulate the axes scales, i.e. the minimum and maximum values of both axes according to what the user selected during the zoom, respectively to set it back when the user zooms out.

The mouse events of the label control are defined in the sheet object of worksheet [map]. The mouse move event resizes the rectangle to define the zoom area. The mouse down event manages the clicks. The code is pretty slim (104 lines), open and commented. Hence, I think I do not have to go into the details here. If you are interested in how this was implemented, download the workbook and have a look. If you have any questions, please leave me a comment.

The Disadvantages

Andy’s technique of using a label control sitting on top of one or several charts is very versatile and works well. However, it is still just a workaround and comes with some considerable disadvantages:

  • Worksheet zoom level has to be 100%:
    The simplified way I implemented it, the technique works only at a worksheet zoom level of 100% and if the entire label is visible in the active Excel window. You can zoom into the chart itself, but the native zoom of the worksheet has to be 100%. The code takes care of this requirement by forcing the zoom level to 100% and by maximizing the size of the application window. But this is still a substantial disadvantage.
  • Unusual way of selecting the zoom area:
    The usual way of selecting areas or zooming into charts in other applications is to click somewhere, keep the mouse key pressed, span the area and apply the zoom by releasing the mouse. Users will probably expect that the zooming works the same way in this Excel implementation, but it doesn’t. You have to click once, define the area by moving the mouse and click again to apply the zoom. Root cause for this is a little annoyance (or shall I say bug) of the label control: if you click on it, it either changes the background from transparent to opaque or disappears. I do not exactly know what is happening here. The only workaround I could find for this strange behavior is making the label invisible and visible again with every click and mouse move. As clunky as this may sound, it works well, but you are losing the option to use the mouse up event. That’s why the extra mouse click is necessary.
  • Limited options (directions) to define the zoom area
    The rectangle defining the zoom area can only be resized South-East, i.e. to the right and down from the point you started. There is no flipping over above or left to the position where you clicked first.
  • No step-by-step zooming out
    You can zoom in more than one time, but you cannot get back step-by-step. For instance: if you zoomed in to the South West of the US, then zoomed into New Mexico and finally into the region around Albuquerque, you cannot go back to the “New Mexico selection”. A right click on the map will always take you back to the original map.
  • The polygon data:
    The technique itself will also work for standard XY scatter charts, but if you want to have a map to zoom in, you need the polygon data to draw the borders. That means, you have to organize the polygon data for your country and regions, you have to keep this data set in the workbook (which can get huge, depending on the map you want to plot) and you can’t color the map by regions.

Some serious disadvantages, no doubt about it. However, it works and provides an interesting feature to directly interact with an Excel chart. If you have an idea how to improve the workbook and code and/or how to overcome some of the limitations, please share it with us in the comment section.

The Download Link

The user defined labeling of the data points (states and cities) can be achieved in Excel 2013 without VBA by using the new Excel feature of linking data labels to a cell range (“Label contains Value From Cells”). Excel versions 2010 or earlier do not provide this option. With some extra VBA code, it is still possible, though. That’s why I created 2 versions of the workbook, one for Excel 2010 and one for Excel 2013. Here are the workbooks for free download:

Download Zoom on a Chart 2010 (zipped Microsoft Excel 2010 workbook, 937K)

Download Zoom on a Chart 2013 (zipped Microsoft Excel 2013 workbook, 996K)

Acknowledgements

Special thanks go again to Andy Pope for sharing this stellar idea.

Stay tuned.

Comments

13 responses to “Zooming in and out of Excel Charts”

  1. Laurent Bosc Avatar
    Laurent Bosc

    Very interesting.
    However for a world map, we need to have all polygons DB. I don’t know how do you found the polygon info of each shape?
    I use another technique to zoom/in and out by recently solving the problem you pointed out :
    “The downside of using the Chart Object events is the requirement of activating the chart first, before the mouse events are fired.”
    Then the idea was to resize the bubble, not the map/shapes (when I zoom in, bubble size is reduced. If I zoom out, the reverse)
    By using the hover technique, I’m able to automatically select the chart when the mouse enter in the targeted area (so without clicking on then chart).
    Then I have alslo added some code in the hover event to also manage the zoom level, and resize bubble in consequence, and it works great.
    Advantages of this zooming technique:
    – No polygon DB, You adapt the bubbles size, not the map/shapes size
    – I can zoom in/out easily in the sheet and map (but you cannot select a specific area like you), and the bubble size is automatically updated
    – No need to be at 100% zoom level
    Drawbacks:
    – Zoom max is 400%, but it is sufficient for most of cases.
    – If the mouse cursor is not in the area, and you zoom in/out, the hover event is not fired (so bubbles size are not updated).
    But from the moment the cursor hover the area, the bubbles size are updated/refreshed, so it is not really a drawback.
    I will try to send you a sample file, but I need to create it, and it is not so easy (not time to do it currently)
    Hope this helps 🙂

  2. Robert Avatar

    Laurent,
    many thanks for your comment and detailed explanation. Interesting approach, although I do not really understand what you mean by “hover technique”. Are you referring to Jordan Goldmeier’s roll over technique (a UDF inside a hyperlink)?
    I am sure your approach works well and it is an interesting alternative. I am looking forward to see your workbook.
    However, the main idea of this article was a feature allowing the user to zoom exactly into an interactively defined area. Furthermore, with my approach, all other parts of the dashboard stay visible, i.e. you can still see and use the form controls, the legend and – if we have a dashboard with more than one chart – the entire dashboard.
    Having said that: I agree, it comes with a lot of disadvantages.

  3. Laurent Bosc Avatar
    Laurent Bosc

    Yes this is the technique , UDF and hyperlink.
    Ah your right about zoom indeed ! All the sheet zoom, which can be annoying indeed.
    My purpose was just to explain another alternative to zoom, i think both techniques are useful, and yours is very inventive ! Like all of your posts 🙂 ).
    I will try to prepare a file to illustrate my words.

  4. RK Lata Avatar
    RK Lata

    Robert,
    First of all THANK YOU for the nice post.
    I am new to Excel VBA. Out of interest I tried to implement your technique on a simple Excel chart. I am encountering the following error message: “The item with the specified name wasn’t found.” referring to the “With ActiveSheet.Shapes(“Zoom Area”)”. How do I name the shape created dynamically. I couldn’t figure out how by any means. May you please help?
    Thanks,
    RK Lata

  5. Robert Avatar

    RK Lata,
    you can either select the shape and rename it in the name box (the little box at top left above the worksheet) or open the Selection Pane (ALT-F10), select the shape (click on it twice) and rename it there.

  6. RK Lata Avatar
    RK Lata

    Please condone for asking a dumb question. As such I don’t see any shape in your example Excel file over the US Map area. Am I missing something?

  7. Robert Avatar

    RK Lata,
    the VBA code makes the shape “Zoom Area” visible only during the selection process, i.e. after you clicked on the map and while you are spanning the zoom area. After you clicked again, the code makes the shape invisible again. You can rename the shape in the Selection Pane as described in my previous comment even if it is invisible.

  8. RK LATA Avatar
    RK LATA

    Thanks a lot! I very much appreciate your prompt reply every time.

  9. Lucio 5295 Avatar
    Lucio 5295

    Good morning and Thanks !
    Your approach is elegant and fast; I applied it successfully to zoom XY scatter charts.
    In this case, to define with precision the Zoom Area it is essential that Label Area coincides exactly with the Grid Area.
    Also at the end the zoomed area must be displayed in the center of the Grid Area.
    For my applications I find that there is however an important limitation: this zooming don’t acts on pixels, but only on the chart by changing its axes. Therefore any other object (shapes) placed above it remain not zoomed.
    Microsoft should develop (and explain) better its charts.
    Best Regards

  10. Nelson Avatar
    Nelson

    Hello, thank you very much for the article, this method of zooming is very interesting indeed.
    I have tried to implement it for my chart and have everything working except for where the actual axes are set with a min and max value. The values that the macro sets for the axis bounds do not seem to match up with what the user selected in the box. I am not quite sure how you set yours up so that it works, but even in your workbook the selection and the zoom can be slightly off (for example, I tried to select a box around Wyoming and the bottom border is cut off). My graph is a lot more off, however, but I thought I had the PlotArea and LArea lined up correctly. I do not know a method that aligns them completely perfectly, but I think I got it extremely close. I set the Plot Area background to blue and the LArea background to white and lined it up until I could not see blue anymore. To test if it was accurate, if I shrunk any edge ever so slightly it would show blue. The Zoom Area is underneath LArea and the chart is underneath that. The only changes to your code that I made were removing the bubble chart parts (the loops) as I’m just using a normal XY Scatter Plot, and also taking off the data labels part because I have no data labels.
    If you have any information that could help me I’m all ears! Thank you! Also if you need more information to answer this question, I’d be happy to provide.

  11. Nelson Avatar
    Nelson

    Nevermind! I figured it out, I had Major tick units turned on in my graph and set to Auto. If they are set as Auto, excel seems to automatically modify the max and min values for the axis that get set by the macro. Just in case if anyone was wondering 🙂

  12. mikiyas Avatar
    mikiyas

    tank you gays i help me a lot!!!

  13. karthikcoep Avatar
    karthikcoep

    Thanks. I am also facing same problem. Can you share your example worksheet.

Leave a Reply

Your email address will not be published. Required fields are marked *