【问题标题】:How can we build charts on Polymer 1.0 using Chart.js?我们如何使用 Chart.js 在 Polymer 1.0 上构建图表?
【发布时间】:2015-07-27 06:26:55
【问题描述】:

我想知道我们是否有现成的聚合物 1.0 图表元素。我正在尝试将 chart-doughnut (https://github.com/robdodson/chart-elements/) 从使用 Chart.js 脚本的聚合物 0.5 迁移到 1.0。

我已通过以下方式迁移了 chart-doughnut.html 文件:

  <link rel="import" href="bower_components/polymer/polymer.html">
  <link rel="import" href="chart-js-import.html">
  
  <!--
  Pie and doughnut charts are probably the most commonly used chart there are. They are divided into segments, the arc of each segment shows a the proportional value of each piece of data.
  
  They are excellent at showing the relational proportions between data.
  
  Pie and doughnut charts in are effectively the same class in Chart.js, but have one different default value - their percentageInnerCutout. This equates what percentage of the inner should be cut out. This defaults to 0 for pie charts, and 50 for doughnuts.
  
  They are also registered under two aliases in the Chart core. Other than their different default value, and different alias, they are exactly the same.
  
  ##### Example
  
      <chart-doughnut values="[30, 50, 100, 40, 120]"></chart-doughnut>
  
  @element chart-doughnut
  @blurb A chart for showing the relational proportions between data.
  @status alpha
  @homepage http://robdodson.github.io/chart-elements
  -->
  <dom-module id="chart-doughnut">
    <template>
      <canvas id="canvas" width="{{width}}" height="{{height}}"></canvas>
    </template>
  <script>
    Polymer({
      is: 'chart-doughnut',
      properties: {
        colors: {
          type: Array,
          value: ['#F7464A',
              '#46BFBD',
              '#FDB45C',
              '#949FB1',
              '#4D5360'
            ];
          },
          notify: true,
          observer: 'updateChart'
        },
        height: {
          notify: true,
          observer: 'resize'
        },
        options: {
          type: Object,
          value: {};
          },
          notify: true,
          observer: 'updateChart'
        },
        values: {
          type: Array,
          value: [
              30,
              50,
              100,
              40,
              120
            ];
          },
          notify: true,
          observer: 'updateChart'
        },
        width: {
          notify: true,
          observer: 'resize'
        }
      },
      resize: function () {
        if (this.chart) {
          this.updateChart();
        }
      },
      updateChart: function () {
        this.async(function () {
          if (this.chart) {
            console.log("CHARTTTT"+this.chart);
            this.chart.destroy();  // Bindings don't seem to be taking effect properly so
                                   // manually set width and height
            // Bindings don't seem to be taking effect properly so
            // manually set width and height
            this.$.canvas.setAttribute('width', this.width);
            this.$.canvas.setAttribute('height', this.height);
          }
          this.data = [];
          this.values.forEach(function (val, i) {
            this.data.push({
              color: this.colors[i],
              value: val
            });
          }, this);
          this.ctx = this.$.canvas.getContext('2d');
          this.chart = new Chart(this.ctx).Doughnut(this.data, this.options);
        }, null, 0);
      }
    });
  </script>
   </dom-module>

但是在使用它时,我在 Chart.js 中收到以下错误

未捕获的 IndexSizeError:无法在“CanvasRenderingContext2D”上执行“arc”:提供的半径 (-0.5) 为负数。 Chart.js:1226 0 0 -0.5 4.71238898038469 4.713461589614612

请建议。

【问题讨论】:

    标签: javascript polymer-1.0 chart.js


    【解决方案1】:

    图表画布在初始化时没有宽度和高度。

    修复它的最简单方法是使用 setTimeout 将其移动到下一个刻度。

    var self = this;
    setTimeout(function() {
        self.chart = new Chart(self.ctx).Doughnut(self.data, self.options);
    }, 0)
    

    Plunkr - http://plnkr.co/edit/i3HrDxd5jUfTfLPVlNPg?p=preview


    注意,如果你对折线图做同样的事情,属性需要是正确的 JSON(即应该使用“而不是”)

    <chart-line width="400"
                height="200"
                labels='["Monday","Tuesday","Wednesday","thursday","Friday","Saturday","Sunday"]'
                values="[[10,14,20,25,13,9,40]]"
                colors='["253,180,92","247,70,74","70,191,189","148,159,177","77,83,96"]'>
    </chart-line>
    

    Plunkr - http://plnkr.co/edit/o74WRX404NWhHGiNqkC8?p=preview

    【讨论】:

    • 嗨,我们也可以用类似的方式创建面积图吗?你也可以为此上传一个示例项目吗?
    • Chart.js 没有面积图(但它有可以填写的折线图)。顺便说一句,我只是从您的问题中复制了大部分代码 - 除了更正一些括号和 ; 之外,我不需要做太多更改。和设置超时。所以你几乎走上了正确的轨道。
    • 你是对的。我的意思是图表线。你能在这里看到代码吗plnkr.co/edit/j2yYe1IyZ9kYFDknWB2s?p=preview 我似乎在这里错过了一些东西,因为我没有得到颜色属性。它失败并出现错误:无法读取图表行中 null 的属性 0。看起来颜色数组设置为空。
    • @RoshanChhetri - 你只需要正确的 JSON 值(见上面更新答案的结尾)。除此之外,我刚刚添加了 setTimeout (只是为了安全起见 - 没有它似乎可以正常工作,因为 draw() 实际上不会触发任何异常,例如甜甜圈的负值)。干杯!
    • @RosAng - 请参阅上面更新答案末尾的(第二个)Plnkr。您可以从中复制文件 - 感谢 RoshanChhetri 提供的大部分解决方案(我只是稍微修补了 HTML)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多