Source: plot/core/PlotlyRoot.js

  1. import Plotly from 'plotly.js-dist';
  2. import { UiElement } from '../../ui/UiElement.js'
  3. import { PlotlyExtensions } from './PlotlyExtensions.js';
  4. import { PlotlyPlotModeButton } from "../buttons/PlotlyPlotModeButton.js";
  5. import { PlotlyCustomModeBar } from "../buttons/PlotlyCustomModeBar.js";
  6. export { PlotlyRoot }
  7. /**
  8. * Root class for plotly inheritance.
  9. * @extends UiElement
  10. */
  11. class PlotlyRoot extends UiElement {
  12. /**
  13. * Create a new root plot object.
  14. * @param {HTMLElement} graphDiv - Plotly graphDiv configuration.
  15. * @param {Array} [data=[]] - Plotly data configuration.
  16. * @param {Object} [layout={}] - Plotly layout configuration.
  17. * @param {Object} [config={}] - Plotly config configuration.
  18. */
  19. constructor(graphDiv, data = [], layout = {}, config = {}) {
  20. super(graphDiv);
  21. this.graphDiv = this.getElement();
  22. this.data = data;
  23. this.layout = layout;
  24. this.config = config;
  25. const defaultButtonOrder = this.config.modeBarButtonsToAdd;
  26. this.extensions = new PlotlyExtensions(this.graphDiv, this.data, this.layout, this.config);
  27. this._modifyModeBar(defaultButtonOrder);
  28. }
  29. _modifyModeBar(defaultButtonOrder) {
  30. const customModeBar = new PlotlyCustomModeBar(this.graphDiv, this.data, this.layout, this.config);
  31. if (customModeBar.isButtonRequested('Change Mode')) {
  32. const plotModeButton = new PlotlyPlotModeButton(this.data);
  33. customModeBar.registerCustomButtons([plotModeButton.button])
  34. }
  35. customModeBar.applyButtonOrder(defaultButtonOrder);
  36. }
  37. /**
  38. * Dispose of the plot by purging Plotly and calling the base class's dispose method.
  39. */
  40. dispose() {
  41. Plotly.purge(this.graphDiv);
  42. super.dispose();
  43. }
  44. }