|
| 1 | +/** |
| 2 | + * Tree-shakeable plotly.js factory. |
| 3 | + * |
| 4 | + * Instead of registering all components at import time (which forces |
| 5 | + * the bundler to include everything), consumers import only what they |
| 6 | + * need and pass it to createPlotly(): |
| 7 | + * |
| 8 | + * import { createPlotly } from 'plotly.js/core'; |
| 9 | + * import scatter from 'plotly.js/traces/scatter'; |
| 10 | + * import bar from 'plotly.js/traces/bar'; |
| 11 | + * import legend from 'plotly.js/components/legend'; |
| 12 | + * |
| 13 | + * const Plotly = createPlotly({ |
| 14 | + * traces: [scatter, bar], |
| 15 | + * components: [legend], |
| 16 | + * }); |
| 17 | + * Plotly.newPlot(el, data, layout); |
| 18 | + */ |
| 19 | +import './lib/d3-compat.js'; |
| 20 | +import 'd3-transition'; |
| 21 | +import 'native-promise-only'; |
| 22 | +import Registry from './registry.js'; |
| 23 | +import plotApi from './plot_api/index.js'; |
| 24 | +import FxModule from './components/fx/index.js'; |
| 25 | +import PlotsModule from './plots/plots.js'; |
| 26 | +import { version } from './version.js'; |
| 27 | +import './plotcss.js'; |
| 28 | + |
| 29 | +// Essential components that every plot needs (always registered) |
| 30 | +import colorscale from './components/colorscale/index.js'; |
| 31 | +import fx from './components/fx/index.js'; |
| 32 | +import modebar from './components/modebar/index.js'; |
| 33 | + |
| 34 | +import localeEn from './locale-en.js'; |
| 35 | +import localeEnUs from './locale-en-us.js'; |
| 36 | +import Icons from './fonts/ploticon.js'; |
| 37 | +import Snapshot from './snapshot/index.js'; |
| 38 | +import PlotSchema from './plot_api/plot_schema.js'; |
| 39 | + |
| 40 | +export function createPlotly({ traces = [], components = [] } = {}) { |
| 41 | + var register = Registry.register; |
| 42 | + var Plotly = { version, register, Icons, Snapshot, PlotSchema }; |
| 43 | + |
| 44 | + // Register API methods |
| 45 | + var methodNames = Object.keys(plotApi); |
| 46 | + for(var i = 0; i < methodNames.length; i++) { |
| 47 | + var name = methodNames[i]; |
| 48 | + if(name.charAt(0) !== '_') Plotly[name] = plotApi[name]; |
| 49 | + register({ moduleType: 'apiMethod', name: name, fn: plotApi[name] }); |
| 50 | + } |
| 51 | + |
| 52 | + // Register essential components (always needed) |
| 53 | + register([colorscale, fx, modebar]); |
| 54 | + |
| 55 | + // Register user-chosen traces and components |
| 56 | + for(var t = 0; t < traces.length; t++) register(traces[t]); |
| 57 | + for(var c = 0; c < components.length; c++) register(components[c]); |
| 58 | + |
| 59 | + // Register locales |
| 60 | + register([localeEn, localeEnUs]); |
| 61 | + if(typeof window !== 'undefined' && window.PlotlyLocales && Array.isArray(window.PlotlyLocales)) { |
| 62 | + register(window.PlotlyLocales); |
| 63 | + delete window.PlotlyLocales; |
| 64 | + } |
| 65 | + |
| 66 | + Plotly.Plots = { |
| 67 | + resize: PlotsModule.resize, |
| 68 | + graphJson: PlotsModule.graphJson, |
| 69 | + sendDataToCloud: PlotsModule.sendDataToCloud, |
| 70 | + }; |
| 71 | + Plotly.Fx = { |
| 72 | + hover: FxModule.hover, |
| 73 | + unhover: FxModule.unhover, |
| 74 | + loneHover: FxModule.loneHover, |
| 75 | + loneUnhover: FxModule.loneUnhover, |
| 76 | + }; |
| 77 | + |
| 78 | + return Plotly; |
| 79 | +} |
| 80 | + |
0 commit comments