Skip to main content

How to add custom button to top toolbar

This guide shows how to add buttons to the top toolbar using the following methods:

Before you start

Consider taking the following steps before proceeding with the guide:

  1. Set up the Widget Constructor and run the library. Refer to the First run tutorial for more information.
  2. Connect data to the library. Refer to the Connecting data tutorial for more information.

Add a dropdown button

  1. Call the widget's headerReady method that ensures the toolbar is fully loaded before adding custom elements. The method returns a promise that will be resolved once the chart’s top toolbar is available for customization.

    widget.headerReady().then(function () {});
  2. Use the widget's createDropdown method to add a dropdown button to the top toolbar. This button will set a symbol with a specific resolution to the chart.

    widget.headerReady().then(function () {
    var dropdown = widget.createDropdown({
    title: "Select symbol",
    tooltip: "Select one of the symbols to load the chart with",
    items: [
    {
    title: "MSFT (1M)",
    onSelect: () => {
    widget.setSymbol("MSFT", "1M");
    }
    },
    {
    title: "IBM (1D)",
    onSelect: () => {
    widget.setSymbol("IBM", "1D");
    }
    }
    ]
    });
    });

Add a custom button

  1. Call the widget's headerReady method that ensures the toolbar is fully loaded before adding custom elements. The method returns a promise that will be resolved once the chart’s top toolbar is available for customization.

    widget.headerReady().then(function () {});
  2. Use the widget's createButton method to add a button that triggers a confirmation dialog when clicked. The widget's showConfirmDialog method displays a dialog box with a title, body text, and two buttons: Yes and No.

    widget.headerReady().then(function () {
    var button = widget.createButton();
    button.textContent = "Show dialog";
    button.addEventListener("click", function () {
    widget.showConfirmDialog({
    title: "Accept Terms and Conditions",
    body: "By selecting Yes, you agree to the terms of use. Otherwise, select No to cancel.",
    callback(result) {
    result ? console.log("The terms are accepted.") : console.log("The terms are not accepted.");
    }
    });
    });
    });
  3. (Optional) If you're a Trading Platform user, you can alternatively use the showConfirmDialog method within the IBrokerConnectionAdapterHost interface.

    1. Use the widget's createButton method to add a button that triggers a confirmation dialog when clicked.

      widget.headerReady().then(function () {
      var buttonTwo = widget.createButton(); // Create a button
      buttonTwo.textContent = "Show dialog 2"; // Add the button title
      buttonTwo.addEventListener("click", function () {
      window.broker.showDialog(); // Trigger a custom dialog on click
      });
      });
    2. Define the showDialog method in the constructor of your Broker API class.

      async showDialog() {
      const actionConfirmed = await this.host.showConfirmDialog(
      "Confirm action", // Dialog title
      [
      "This action cannot be undone. Please confirm to continue.",
      "Otherwise, you may close this dialog to retain the current settings."
      ], // Dialog content
      "Confirm", // Text for the button that returns true
      "Dismiss" // Text for the button that returns false
      );
      if (actionConfirmed) {
      console.log("Action was confirmed.");
      } else {
      console.log("Action was not confirmed.");
      }
      }

What's next?

If you want to dive deeper into how to customize the chart, we recommend checking the following documentation articles: