angular-plotly.js
angular-plotly.js copied to clipboard
Set global defaults that apply to all graphs within a project
Is it possible to set global defaults, e.g. the default layout.colorway
property so that every graph I instantiate in a project obeys that setting? I can't seem to find how to do this.
Hello, @bluppfisk . Unfortunately, there isn't a way to do this as the code is. What I can recomend, as a quick fix, is creating a service which you can get the default. E.g.:
@Injectable
export class DefaultLayoutService {
private defaultLayoutObject = { /* the default data */ };
public getDefaultLayoutObject(layout: object): object {
return Object.assign({}, this.defaultLayoutObject, layout);
}
}
then use it in the controller
@Component()
export class MyComponent {
public layout: object = {};
constructor(public layoutService: DefaultLayoutService) {
this.layout = layoutService.getDefaultLayoutObject({ /* data which will overwrite the default */});
}
}
Now, regarding the "global defaults". Do you have an idea in how it could be implemented? Like, declaring a theme globally?
Thanks for the quick response. Global defaults: yes, a theme would be nice. My application uses plotly graphs in nearly every component and so I have to tell plotly to use my colourway in every component. Your workaround will probably work, but being able to override globals sounds even more attractive.
I'm no expert on Plotly nor Angular, and it's a little opaque to me as to how the angular wrapper for plotly works, but maybe this works:
Wherever you import the module, you could provide some defaults. In Python, Plotly is retrieved through the PlotlyService. I don't know if something like this happens at all under the hood, but the PlotlyService sounds like a good point to set global configs.
Is that something?
I know in other languages (i.e.: python) there are themes: https://plotly.com/python/templates/ I will look to see if we can add it to JS
is this resolved?
Not as far as I know; I still use the service to get an object with the default config. It's a workaround but it works.