odoo.define("project_report.GraphRenderer", function(require) {
var GraphRenderer = require("web.GraphRenderer");
var config = require("web.config");
var field_utils = require("web.field_utils");

// Hide top legend when too many items for device size
var MAX_LEGEND_LENGTH = 25 * (1 + config.device.size_class);

    GraphRenderer.include({
        init: function(parent, state, params) {
            this._super.apply(this, arguments);
        },

     _animationOptions: function () {
        var animationOptions = {};
        if (this.state.mode === 'bar') {
             var animationOptions = {
                duration: "0.1",
                "onComplete": function() {
                var chartInstance = this.chart,
                 ctx = chartInstance.ctx;
                ctx.font = Chart.helpers.fontString(12, 'bold', "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif");
                ctx.textAlign = 'center';
                ctx.textBaseline = 'bottom';
                this.data.datasets.forEach(function(dataset, i) {
                  var meta = chartInstance.controller.getDatasetMeta(i);
                  meta.data.forEach(function(bar, index) {
                    var data = dataset.data[index];
                    if(!!data){
                        ctx.fillText(data, bar._model.x, bar._model.y - 5);
                    }
                  });
                });
              }
               };
              }
        return animationOptions;
    },

    _prepareOptions: function (datasetsCount) {
        const options = {
            maintainAspectRatio: false,
            scales: this._getScaleOptions(),
            legend: this._getLegendOptions(datasetsCount),
            tooltips: this._getTooltipOptions(),
            elements: this._getElementOptions(),
            animation: this._animationOptions(),
        };
        if (this._isRedirectionEnabled()) {
            options.onClick = this._onGraphClicked.bind(this);
        }
        return options;
    },

    _renderBarChart: function (dataPoints) {
        var self = this;

        // prepare data
        var data = this._prepareData(dataPoints);

        data.datasets.forEach(function (dataset, index) {
            // used when stacked
            dataset.stack = self.state.stacked ? self.state.origins[dataset.originIndex] : undefined;
            // set dataset color
            var color = self._getColor(index);
            dataset.backgroundColor = color;
        });

        // prepare options
        var options = this._prepareOptions(data.datasets.length);       
        // create chart
        var ctx = document.getElementById(this.chartId);
        this.chart = new Chart(ctx, {
            type: 'bar',
            data: data,
            options: options,
        });
    },

    });
});
