charts.js 带有聚合物包装图表元素的堆积条

charts.js stacked bar with polymer's wrapper chart-elements

我一直在尝试让堆叠选项在 chart.js 的图表元素聚合物包装器中工作,但没有成功,我尝试了以下代码,但它只是给了我一个空白 space.这是我的专栏-chart.html

<link rel="import" href="../bower_components/polymer/polymer.html"> <link rel="import" href="../bower_components/jquery/dist/jquery.min.js"> <link rel="import" href="../bower_components/chart-elements/chart-bar.html">

 <chart-bar id="alarmsChart" data="[[data]]" style="width: 600px; height: 360px;"></chart-bar>

      Polymer({
               is: 'column-chart',
               ready: function () {
                 this.data = {
                   type: 'bar',
                   data: {
                       labels: ["January", "February", "March", "April", "May", "June", "July"],
                      datasets: [{
                        label: 'Dataset 1',
                        backgroundColor: "rgba(5,141,199,0.5)",
                        data: [100, 110, 95, 100, 100, 102, 101],
                        stack: 1
                     }, {
                        label: 'Dataset 2',
                        backgroundColor: "rgba(80,180,50,0.5)",
                       data: [94, 96, 98, 94, 97, 97, 96],
                       stack: 1
                }, {
                       label: 'Dataset 3',
                       backgroundColor: "rgba(237,86,27,0.5))",
                       data: [98, 97, 87, 85, 99, 84, 94],
                       stack: 1
                      }]
                  },
                options: {
                    responsive: true,
                    scales: {
                        xAxes: [{
                            stacked: true,
                        }],
                        yAxes: [{
                            stacked: true
                        }]
                        }
                    }
        }}
       });
</script>

1) 你的第 3 种背景颜色有一个多余的括号:

backgroundColor: "rgba(237,86,27,0.5))",

应该是:

backgroundColor: "rgba(237,86,27,0.5)",

2) 您尝试在 data 对象中插入 data 属性,这是多余的。

3) 您不必定义 type: 'bar' 属性 因为它是由控件的名称设置的。

4) options对象要单独设置,对于options属性:

<dom-module id="column-chart">

    <template>
        <div>
            <chart-bar data="[[data]]" options="[[options]]"></chart-bar>
        </div>
    </template>

    <script>
        Polymer({
            is: 'column-chart',
            ready: function() {

                this.data = {
                    labels: ["January", "February", "March", "April", "May", "June", "July"],
                    datasets: [
                    {
                        label: 'Dataset 1',
                        backgroundColor: "rgba(5,141,199,0.5)",
                        data: [100, 110, 95, 100, 100, 102, 101],
                        stack: 1
                    }, 
                    {
                        label: 'Dataset 2',
                        backgroundColor: "rgba(80,180,50,0.5)",
                        data: [94, 96, 98, 94, 97, 97, 96],
                        stack: 1
                    }, 
                    {
                        label: 'Dataset 3',
                        backgroundColor: "rgba(237,86,27,0.5)",
                        data: [98, 97, 87, 85, 99, 84, 94],
                        stack: 1
                    }]
                }

                this.options = {
                    responsive: true,
                    scales: {
                        xAxes: [{
                            stacked: true,
                        }],
                        yAxes: [{
                            stacked: true
                        }]
                    }
                }   
            }
        });
    </script>

</dom-module>