javascript - Chartjs and Polymer 1.7.0 -
when using chartjs polymer setting dom: 'shadow'
, getting error
uncaught typeerror: failed execute 'getcomputedstyle' on 'window': parameter 1 not of type 'element
but if remove dom: 'shadow'
initial setting polymer, error gone. cant understand problem is.
shouldn't use 'dom':'shadow'
?
live preview(see console): https://s.codepen.io/jenishngl/debug/movjpm
my code in https://codepen.io/jenishngl/pen/movjpm
<script> window.polymer = { dom: 'shadow', // local dom rendered using shadow dom supported lazyregister: true // sets lazy registeration custom elements }; </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.3.0/chart.min.js"></script> <base href="https://cdn.rawgit.com/download/polymer-cdn/1.5.0/lib/"> <link rel="import" href="polymer/polymer.html"> <style> html, body{ padding: 0; margin: 0; box-sizing: border-box; height: 500px; width: 100%; } chart-element{ height: 100%; width: 100%; } </style> <dom-module id="chart-element"> <template> <style> :host{ display: block; height: 100%; width: 100%; } #chart{ height: 100%; width: 100%; } </style> <canvas id="chart"></canvas> </template> <script> polymer({ is: 'chart-element', ready: function(){ this.async(function(){ this._drawchart(); }.bind(this), 2000); }, _drawchart: function(){ var labels = ["jul", "aug", "sep", "oct"]; var rasied = ["1710", "0", "0", "0"]; var solved = ["1642", "0", "0", "0"]; var ctx = this.$$('#chart'); var chart = new chart(ctx, { type: 'line', data: { labels: labels, datasets: [ { label: "raised", fill: false, linetension: 0.1, backgroundcolor: "#f44336", bordercolor: "#f44336", bordercapstyle: 'butt', borderdash: [], borderdashoffset: 0.0, borderjoinstyle: 'round', pointbordercolor: "#f44336", pointbackgroundcolor: "#fff", pointborderwidth: 6, pointhoverradius: 5, pointhoverbackgroundcolor: "#f44336", pointhoverbordercolor: "rgba(220,220,220,1)", pointhoverborderwidth: 2, pointradius: 1, pointhitradius: 10, data: rasied }, { label: "solved", fill: false, linetension: 0.1, backgroundcolor: "#009688", bordercolor: "#009688", bordercapstyle: 'butt', borderdash: [], borderdashoffset: 0.0, borderjoinstyle: 'round', pointbordercolor: "#009688", pointbackgroundcolor: "#fff", pointborderwidth: 6, pointhoverradius: 5, pointhoverbackgroundcolor: "#009688", pointhoverbordercolor: "rgba(220,220,220,1)", pointhoverborderwidth: 2, pointradius: 1, pointhitradius: 10, data: solved } ] }, options: { responsive: true, maintainaspectratio: false, scales: { yaxes: [{ ticks: { beginatzero:true } }] } } }); } }); </script> </dom-module> <chart-element></chart-element>
when chartjs initializes view, tries read maximum height of given canvas' container calling getcomputedstyle()
on it. in case of shadow dom, container custom element's shadow root, documentfragment
, not support getcomputedstyle()
, resulting in runtime error you're seeing.
to workaround problem, wrap <canvas>
in htmlelement
, such <div>
.
<template> <div> <canvas id="chart"></canvas> <div> </template>
you might interested in using chart-elements
library, polymer wrapper around chartjs , not exhibit problem shadow dom enabled: codepen
Comments
Post a Comment