Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Height of plot is 0 until resize after changing layout input #276

Open
jakehockey10 opened this issue Oct 24, 2024 · 2 comments
Open

Height of plot is 0 until resize after changing layout input #276

jakehockey10 opened this issue Oct 24, 2024 · 2 comments

Comments

@jakehockey10
Copy link

jakehockey10 commented Oct 24, 2024

Hello,

I'm having a bit of trouble with my charts in a dashboard. I've implemented theme switching in my application using system variables and Angular Material. I'm able to change my chart's look and feel according to the colors of my theme. However, the container will collapse when I switch to a new theme because the plotly-plot element has a height of 0 until I resize the window.

Injectable

import { DOCUMENT } from '@angular/common';
import {
  computed,
  effect,
  inject,
  InjectionToken,
  signal,
} from '@angular/core';
import { WA_WINDOW } from '@ng-web-apis/common';

export const THEME = new InjectionToken('Theme', {
  factory: () => {
    const w = inject(WA_WINDOW);
    const d = inject(DOCUMENT);
    const darkMode = signal(true);
    const setDarkMode = effect(() =>
      d.documentElement.classList.toggle('light', !darkMode())
    );
    return {
      darkMode,
      textColor: computed(() => {
        darkMode();
        return getComputedStyle(d.documentElement).getPropertyValue(
          '--mat-app-text-color'
        );
      }),
    };
  },
});

Component

...
  readonly #theme = inject(THEME);

  readonly ageGenderChartLayout = computed<Partial<Plotly.Layout>>(() => ({
    barmode: 'stack',
    autosize: true,
    margin: {
      l: 60,
      r: 50,
      b: 100,
      t: 50,
      pad: 4,
    },
    title: 'People by Age',
    paper_bgcolor: 'rgba(255,255,255,0)',
    plot_bgcolor: 'rgba(255,255,255,0)',
    font: {
      color: this.#theme.textColor(),
    },
  }));
...

Template

<mat-card>
  <mat-card-content>
    @if (mapData$ | async; as mapData) {
    <plotly-plot [data]="mapData"
                 [layout]="mapLayout()"
                 [config]="{responsive: true, mapboxAccessToken: mapboxToken, displaylogo: false, modeBarButtonsToRemove: ['toImage']}" />
    } @else {
    <mat-spinner style="margin: auto;"></mat-spinner>
    }
  </mat-card-content>
</mat-card>

Before switching themes:
Image

After switching themes (which updates the layout input of the plotly-plot component):
Image

How can I have responsive charts while still have their containers "see" their actual height? is this a Plotly chart issue?

@PSanetra
Copy link

I am experiencing a similar issue when I update the plot data. Sometimes the height of the .svg-container div is set to 100% instead of the desired 450px (the plotly default height). 100% is scaled to 0px automatically by the browser.

It looks like some race condition.

@PSanetra
Copy link

My workaround for now was to set style.height to a static value:

E.g.

<plotly-plot
  [style]="{ height: '600px'}"
  [data]="data"
  [layout]="layout"
  [config]="config"
  />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants