question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart does not resize inside shadow DOM (e.g. Polymer)

See original GitHub issue

Expected Behavior

The Bar Chart should resize when the window size is changed.

Current Behavior

The wrapper div does resize, but the Chart does not.

Steps to Reproduce (for bugs)

https://stackblitz.com/edit/polymer310-testcase-chartjs

import '../../../node_modules/chart.js/dist/Chart.js'

class BarChartElement extends PolymerElement {
  static get template() {
    return html`  
        <style include="shared-styles2">
        #content{
            height: 100%;
            width: 100%;
        }
        </style>
        <div id="content"></div>
      `;
  }
ready(){
super.ready();

        let iCan = document.createElement('canvas');
        iCan.id = 'temp';

        this.$.content.appendChild(iCan);

       setTimeout(function () {
           let ctx = (this.$.content.firstElementChild);
           this.chart = new Chart(ctx, {
               type: 'bar',
               data: {
                   datasets: [{
                       backgroundColor: color,
                       data: data,
                   }],
               },

               options: {
                   responsive: true,
                   maintainAspectRatio: true,
                   legend: {
                       display: false,
                   },
                   tooltips: {
                       enabled: false,
                   },
                   scales: {
                       xAxes: [{
                           scaleLabel: {
                               display: true,
                               labelString: 'Wochen',
                               fontSize: 16,
                           },
                           ticks:{
                               stepSize:1,
                               autoSkip: false,
                           },
                       }],
                       yAxes: [{
                           scaleLabel: {
                               display: true,
                               labelString: 'Auslastung in %',
                               fontSize: 16,
                           },
                       }]
                   }
               }
           });
       }.bind(this), 1);
    }
}
window.customElements.define('bar-chart', BarChartElement);

Environment

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
etimbergcommented, Dec 6, 2018

@jwillmer one caution on that is that it only fires if the whole page resizes, but it won’t fire if the div containing the chart resizes.

0reactions
Simon-Tangcommented, Sep 12, 2019

In the meantime, I was able to get the Chart to resize inside the Shadow DOM with this snippet:

this.shadow = this.attachShadow({ mode: 'open' });

const resizeStylesheet = document.createElement('link');
resizeStylesheet.setAttribute('href', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css');
resizeStylesheet.setAttribute('rel', 'stylesheet');

this.shadow.appendChild(resizeStylesheet);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Outside styles not overriding Shadow DOM styles
Try putting box-shadow inside --app-drawer-content-container . This is working for me: app-drawer { z-index: 3; ...
Read more >
Shadow DOM concepts - Polymer Project
Shadow DOM is a new DOM feature that helps you build components. You can think of shadow DOM as a scoped subtree inside...
Read more >
The Shadow DOM. From Web Components in Action by…
When the component is resized or collapsed/hidden and shown again, these list elements are destroyed and then redrawn using the internal data ...
Read more >
plotly-plot documentation - GitHub Pages
js library fails for all plotly plots rendered inside a shadow DOM, whether by Polymer or any other means. The element cannot tell...
Read more >
Shadow DOM - W3C
Table of Contents ... Shadow DOM Example; A. Acknowledgements; B. References ... A shadow root is not a child node of the shadow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found