Skip to content

Panels

Panels are instances of StockPanel class (which in turn extends an XYChart) comprising the Stock chart. Zoom state of their X axes as well as cursor are automatically synced by the stock chart itself.

To add a panel, we need to:

  • Create an instance of an StockPanel.
  • Configure it fully (remember it’s an XYChart so we can use any configuration options available to it), including axes and series that will go into it.
  • Push it into stock chart’s panels list.

The following code will add a single panel. Let’s call it a “main chart”.

let mainPanel = stockChart.panels.push(am5stock.StockPanel.new(root, {
wheelY: "zoomX",
panX: true,
panY: true
}));

If the stock chart has more than one panel, a special set of controls is enabled on each panel.

Controls are buttons that allow:

  • Moving panel up and down.
  • Expanding panel to full chart size.
  • Closing panel.

Panel controls is an object of type PanelControls, and is accessible via panel’s panelControls property.

To disable panel controls, simply set its forceHidden: true:

mainPanel.panelControls.set("forceHidden");

Individual button elements from the controls are available via the following properties of the PanelControls instance:

  • upButton
  • downButton
  • expandButton
  • closeButton

To selectively disable them, use their forceHidden setting:

mainPanel.panelControls.expandButton.set("forceHidden");
mainPanel.panelControls.closeButton.set("forceHidden");

The easiest way to disabling resizing of panels is via custom theme:

let myTheme = am5.Theme.new(root);
myTheme.rule("Rectangle", ["panelresizer"]).setAll({
forceInactive: true
});
root.setThemes([
am5themes_Animated.new(root),
myTheme
]);

We can push as many StockPanel instances into stock chart’s panels list as we need to.

They will appear in the same order as the were pushed: the first panel will be displayed on top, with the rest following.

Panels can also be re-ordered by user via panel controls, described earlier in this tutorial.

Each panel instance offers two methods, that allow changing its position: moveUp() and moveDown().

Their functionality is self-explanatory.

Normally, all panels will divide the vertical space between them equally.

We can modify that manually, using panel/chart’s height setting.

It accepts both absolute pixel as well as percent values.

let mainPanel = stockChart.panels.push(am5stock.StockPanel.new(root, {
wheelY: "zoomX",
panX: true,
panY: true
}));
let volumePanel = stockChart.panels.push(am5stock.StockPanel.new(root, {
wheelY: "zoomX",
panX: true,
panY: true,
height: am5.percent(30)
}));

In the above example, only “volumePanel” has its height set to 30%.

That means that it will take 30% of the height of the stock chart, with “mainPanel” taking up the rest 70% even if it does not have height explicitly set.

The panels are resizable out-of-the-box.

User can drag space between panels to resize them manually.

Cursors between panels will be automatically synced across all panels.

To disable it, use this line of code for each panel after it has been added to the chart:

panel.removePrivate("otherCharts");

To remove a panel programmatically, all we need to do is call panel’s close() method:

volumePanel.close();
stockChart.panels.autoDispose = false;

If panel/chart has panel controls added to it, it can be removed by the user by pressing the “close” button in control bar.

Panels have a few events you can tap into:

Event Kicks in when
closed Panel is closed (removed).
collapsed Panel returns from its expanded state.
expanded Panel is expanded.
moved Panel is moved up and down using its controls or API.
volumePanel.events.on("closed", function(ev) {
console.log("Buh bye, Volume Panel");
});

Below is a working multi-panel stock chart:

Open in CodePen