/**
* @license
* Copyright 2019 The FOAM Authors. All Rights Reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*/

foam.CLASS({
  package: 'org.chartjs',
  name: 'PieDAOChartView',
  extends: 'org.chartjs.AbstractChartView',
  requires: [
    'foam.nanos.analytics.Candlestick',
  ],
  implements: [
    'foam.mlang.Expressions'
  ],
  properties: [
    {
      name: 'config',
      factory: function () {
        return {
          type: 'pie'
        };
      }
    },
    {
      class: 'foam.mlang.ExprProperty',
      name: 'keyExpr',
      factory: function() { return this.Candlestick.KEY; }
    },
    {
      class: 'foam.mlang.ExprProperty',
      name: 'valueExpr',
      factory: function() { return this.Candlestick.AVERAGE; }
    },
  ],

  listeners: [
    {
      name: 'dataUpdate',
      isFramed: true,
      code: function() {
        var self = this;
        self.data
          .select(this.GROUP_BY(this.keyExpr, this.SUM(this.valueExpr)))
          .then(function(sink) {
            var config = foam.Object.clone(self.config);
            config.data = {
              datasets: [{
                data: Object.keys(sink.groups).map(key => sink.groups[key].value),
              }],
              labels: Object.keys(sink.groups)
            };
            self.config = config;
          });
      }
    }
  ]
});

@@ -991,24 +991,231 @@
 ion(sink) %7B%0A
+            // Clear data before cloning because we'll be clobbering%0A            // it below anyway and cloning it can sometimes result in%0A            // a stack overflow.%0A            self.config.data = %7B%7D;%0A
             

Fix stack overflow when cloning pie chart data.