Implementing a mobile optimized navigation menu

Contents[Hide]

1. Overview

With so much time now spent on our mobile devices, more users may want to be able to view a dashboard on their phone or tablet. For certain dashboards, designing a single version of the dashboard that adapts to fit its content onto any screen size can be possible by making use of Dundas BI's Responsive re-size mode. This will automatically re-organize the content as needed so that it is still readable, fills the given screen real estate, and allows all content to be accessed by scrolling in one direction.

For the smallest screen sizes, you may need to further minimize the amount of space taken up for navigation or other options on your dashboard. This article explains how to add a hamburger menu (also known as the menu button or navigation drawer) for those devices to navigate between dashboards.

Mobile view of the above dashboard with hamburger menu button
Mobile view of the above dashboard with hamburger menu button

2. Understanding the browser window resizing event

To determine when to show the full-size navigation buttons and when to show the hamburger menu button, it is important to understand when the dashboard window resizing event is fired. When the dashboard loads for the first time, Dundas BI detects the browser’s window size and adjusts the dashboard canvas to fit. For this example, it is required to detect the browser window size when loading the dashboard and hide or show the navigation buttons accordingly.

When using smaller screen devices (such as smartphones), users may rotate the device to change the display orientation between portrait and landscape mode. This is another case to consider for hiding or showing different navigation buttons, and if you decide to handle it, the dashboard's Re-Size actions can be used in a similar way as done for the dashboard's Ready actions below.

3. Example dashboard

Consider a simple dashboard based on the template grid example.

Sample dashboard with button navigation
Sample dashboard with button navigation

For the above dashboard, the default settings are given below:

  • Re-Size Mode: Responsive
  • Width: 1024
  • Height: 500

The dashboard also has two layers:

  • MainLayer, which contains two charts.
  • NavigationLayer, which contains three navigation buttons (Operations, Management, and Dispatch).

Note
The NavigationLayer buttons are inside a single cell and are not attached to any cell within the template grid, while the MainLayer charts are attached to different cells.

When the dashboard is viewed on a large monitor, it looks fine. However, when the dashboard is viewed on a smaller screen, the button texts are scrambled due to the space constraints.

Sample dashboard shown on a small screen
Sample dashboard shown on a small screen

4. Adding the hamburger menu

To replace the buttons above with a menu, add a new layer (HamburgerLayer) in the Layers window and add an Image component to that layer that displays the icon image.

Sample dashboard with HamburgerLayer (NavigationLayer is hidden)
Sample dashboard with HamburgerLayer (NavigationLayer is hidden)

Next, add another layer (DropdownLayer) and add three buttons to that layer, which will make up the menu.

Sample dashboard with dropdownLayer
Sample dashboard with dropdownLayer

5. Adding the scripts

For this example, we will show the hamburger menu if the browser window is less than 1020 pixels in width. In this case, we want to show the HamburgerLayer and hide the NavigationLayer; otherwise, we want to show the NavigationLayer and hide the HamburgerLayer.

To achieve this, use the following script under the Ready action of the dashboard:

// Get the current window width
var windowWidth = $(window).width();
// Get the canvas service
var canvasService = this.getService("CanvasService");
// Get the navigation layer
var navigationLayer = canvasService.getLayersByFriendlyName("NavigationLayer")[0];
// Get the hamburger layer
var hamburgerLayer = canvasService.getLayersByFriendlyName("HamburgerLayer")[0];

// Check the window width and show/hide appropriate layers
if (windowWidth < 1020){
    if (hamburgerLayer.hidden) {
        canvasService.showLayers([hamburgerLayer]);
        canvasService.hideLayers([navigationLayer]);
    }
} else {
    if (navigationLayer.hidden) {
        canvasService.showLayers([navigationLayer]);
        canvasService.hideLayers([hamburgerLayer]);
    }
}

While the dashboard is being viewed on a smaller screen, you want to show a dropdown menu if the user clicks/taps on the hamburger menu button. The idea is to show the DropdownLayer if it is hidden, and vice versa. To achieve this, add the following script under the Click action of the Hamburger menu button image.

// Get the canvas service
var canvasService = this.getService("CanvasService");
var dropdownLayer = canvasService.getLayersByFriendlyName("DropdownLayer")[0];

if (dropdownLayer.hidden) {
    canvasService.showLayers([dropdownLayer]);
}
else {
    canvasService.hideLayers([dropdownLayer]);
}

6. Testing the hamburger menu button

If you open the dashboard on a small screen device, it will detect the browser window size when the dashboard is at the Ready state and will show the Hamburger menu button on the top left corner, as per the design.

If you tap the Hamburger menu button, a dropdown menu with three buttons will appear underneath the Hamburger button.

7. See also

Dundas Data Visualization, Inc.
500-250 Ferrand Drive
Toronto, ON, Canada
M3C 3G8

North America: 1.800.463.1492
International: 1.416.467.5100

Dundas Support Hours: 7am-6pm, ET, Mon-Fri