<commit_before>
// @require ./config.js
// @require core/widget/widget.js

/* CHART */

(function ( $, _, Svelto, Widgets, Factory, ChartLib ) {

  /* CHECK IF LOADED */

  if ( !ChartLib ) return;

  /* CONFIG */

  let config = {
    name: 'chart',
    plugin: true,
    selector: 'canvas.chart',
    options: {
      defaults: {
        type: 'line',
        colors: ['#1565c0'], // Primary color
        labels: ['Dataset'],
        datas: [[]],
        ticks: undefined,
        tooltips: undefined,
        chartOptions: {}
      },
      datas: {
        type: 'type',
        colors: 'colors',
        labels: 'labels',
        datas: 'datas',
        ticks: 'ticks',
        tooltips: 'tooltips',
        chartOptions: 'chart-options'
      }
    }
  };

  /* CHART */

  class Chart extends Widgets.Widget {

    /* SPECIAL */

    _variables () {

      this.$chart = this.$element;
      this.chart = this.element;

      Object.keys ( this.options.datas ).forEach ( property => {

        this[property] = this.$chart.data ( this.options.datas[property] ) || this.options.defaults[property];

      });

      if ( !_.isArray ( this.datas[0] ) ) this.datas = [this.datas];

    }

    _init () {

      let settings = this._getSettings ();

      this.chartInstance = new ChartLib ( this.chart, settings );

    }

    _destroy () {

      this.chartInstance.destroy ();

    }

    /* PRIVATE */

    _getSettings () {

      const settings = {
        type: this.type,
        data: {
          labels: this.ticks || this.datas[0].map ( ( point, index ) => index + 1 ),
          datasets: this.labels.map ( ( label, index ) => ({
            label,
            data: this.datas[index],
            backgroundColor: this.colors[index]
          }))
        },
        options: this.chartOptions
      };

      if ( this.tooltips ) {

        _.merge ( settings.options, {
          tooltips: {
            callbacks: {
              title: datas => this.tooltips[datas[0].index]
            }
          }
        });

      }

      return settings;

    }

  }
<commit_msg>Chart: added an `update` method<commit_after>
// @require ./config.js
// @require core/widget/widget.js

/* CHART */

(function ( $, _, Svelto, Widgets, Factory, ChartLib ) {

  /* CHECK IF LOADED */

  if ( !ChartLib ) return;

  /* CONFIG */

  let config = {
    name: 'chart',
    plugin: true,
    selector: 'canvas.chart',
    options: {
      defaults: {
        type: 'line',
        colors: ['#1565c0'], // Primary color
        labels: ['Dataset'],
        datas: [[]],
        ticks: undefined,
        tooltips: undefined,
        chartOptions: {}
      },
      datas: {
        type: 'type',
        colors: 'colors',
        labels: 'labels',
        datas: 'datas',
        ticks: 'ticks',
        tooltips: 'tooltips',
        chartOptions: 'chart-options'
      }
    }
  };

  /* CHART */

  class Chart extends Widgets.Widget {

    /* SPECIAL */

    _variables () {

      this.$chart = this.$element;
      this.chart = this.element;

    }

    _init () {

      this._initDatas ();

      let settings = this._getSettings ();

      this.chartInstance = new ChartLib ( this.chart, settings );

    }

    _initDatas () {

      Object.keys ( this.options.datas ).forEach ( property => {

        this[property] = this.$chart.data ( this.options.datas[property] ) || this.options.defaults[property];

      });

      if ( !_.isArray ( this.datas[0] ) ) this.datas = [this.datas];

    }

    _destroy () {

      this.chartInstance.destroy ();

    }

    /* PRIVATE */

    _getSettings () {

      const settings = {
        type: this.type,
        data: {
          labels: this.ticks || this.datas[0].map ( ( point, index ) => index + 1 ),
          datasets: this.labels.map ( ( label, index ) => ({
            label,
            data: this.datas[index],
            backgroundColor: this.colors[index]
          }))
        },
        options: this.chartOptions
      };

      if ( this.tooltips ) {

        _.merge ( settings.options, {
          tooltips: {
            callbacks: {
              title: datas => this.tooltips[datas[0].index]
            }
          }
        });

      }

      return settings;

    }

    /* API */

    update () {

      this.$chart.removeData ();

      this._initDatas ();

      const {data} = this._getSettings ();

      this.chartInstance.data.labels = data.labels;
      this.chartInstance.data.datasets.forEach ( ( dataset, i ) => {
        _.merge ( dataset, data.datasets[i] );
      });

      this.chartInstance.update ();

    }

  }

{'fraction_non_alphanumeric': 0.1510947322783438, 'fraction_numerical': 0.0039020160416215043, 'mean_word_length': 1.6547756041426926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3486053', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1369, 'n_words': 278}