__init__.py 1.3 KB

1234567891011121314151617181920212223242526
  1. from inspect import getmembers, isfunction
  2. from . import belief_charts
  3. from .defaults import apply_chart_defaults
  4. def chart_type_to_chart_specs(chart_type: str, **kwargs) -> dict:
  5. """Create chart specs of a given chart type, using FlexMeasures defaults for settings like width and height.
  6. :param chart_type: Name of a variable defining chart specs or a function returning chart specs.
  7. The chart specs can be a dictionary or an Altair chart specification.
  8. - In case of a dictionary, the creator needs to ensure that the dictionary contains valid specs
  9. - In case of an Altair chart specification, Altair validates for you
  10. :returns: A dictionary containing a vega-lite chart specification
  11. """
  12. # Create a dictionary mapping chart types to chart specs, and apply defaults to the chart specs, too.
  13. belief_charts_mapping = {
  14. chart_type: apply_chart_defaults(chart_specs)
  15. for chart_type, chart_specs in getmembers(belief_charts)
  16. if isfunction(chart_specs) or isinstance(chart_specs, dict)
  17. }
  18. # Create chart specs
  19. chart_specs_or_fnc = belief_charts_mapping[chart_type]
  20. if isfunction(chart_specs_or_fnc):
  21. return chart_specs_or_fnc(**kwargs)
  22. return chart_specs_or_fnc