<commit_before>///<reference path="../../reference.ts" />

module Plottable {
export module Abstract {
  export class Stacked<X, Y> extends Abstract.NewStylePlot<X, Y> {
    private stackedExtent = [0, 0];
    public _isVertical: boolean;

    public _onDatasetUpdate() {
      super._onDatasetUpdate();
      // HACKHACK Caused since onDataSource is called before projectors are set up.  Should be fixed by #803
      if (this._datasetKeysInOrder != null &&
          this._projectors["x"] != null &&
          this._projectors["y"] != null) {
        this.stack();
      }
    }

    private stack() {
      var positiveDatasets: any[][] = [];
      var negativeDatasets: any[][] = [];
      this.populatePositiveNegativeDatasets(positiveDatasets, negativeDatasets);
      this.setDatasetStackOffsets(this._stack(positiveDatasets), this._stack(negativeDatasets));

      var datasets = this._getDatasetsInOrder();
      var valueAccessor = this._isVertical ? this._projectors["y"].accessor : this._projectors["x"].accessor;
      var maxY = _Util.Methods.max(datasets, (dataset: any) => {
        return _Util.Methods.max(dataset.data(), (datum: any) => {
          return valueAccessor(datum) + datum["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"];
        });
      });
      this.stackedExtent[1] = Math.max(0, maxY);

      var minY = _Util.Methods.min(datasets, (dataset: any) => {
        return _Util.Methods.min(dataset.data(), (datum: any) => {
          return valueAccessor(datum) + datum["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"];
        });
      });
      this.stackedExtent[0] = Math.min(minY, 0);
    }

    /**
     * Goes through the datasets so that only negative values / 0 are placed in negativeDatasets
     * and positive values / 0 are placed in positiveDatasets
     */
    private populatePositiveNegativeDatasets(positiveDatasets: any[], negativeDatasets: any[]) {
      var keyAccessor = this._isVertical ? this._projectors["x"].accessor : this._projectors["y"].accessor;
      var valueAccessor = this._isVertical ? this._projectors["y"].accessor : this._projectors["x"].accessor;
      this._getDatasetsInOrder().forEach((dataset) => {
        var positiveDataset: any[] = [];
        var negativeDataset: any[] = [];

        dataset.data().forEach((datum) => {
          var key = keyAccessor(datum);
          var value = valueAccessor(datum);

          var positiveValue = value >= 0 ? value : 0;
          positiveDataset.push({key: key, value: positiveValue});

          var negativeValue = value <= 0 ? value : 0;
          negativeDataset.push({key: key, value: negativeValue});
        });

        positiveDatasets.push(positiveDataset);
        negativeDatasets.push(negativeDataset);
      });
    }

    /**
     * Feeds the data through d3's stack layout function which will calculate
     * the stack offsets and use the the function declared in .out to set the offsets on the data.
     */
    private _stack(datasets: any[][]): any[] {
      var outFunction = (d: any, y0: number, y: number) => {
        d.stackOffset = y0;
      };

      d3.layout.stack()
               .x((d) => d.key)
               .y((d) => d.value)
               .values((d) => d)
               .out(outFunction)(datasets);

      return datasets;
    }

    /**
     * After the stack offsets have been determined on each separate dataset, the offsets need
     * to be determined correctly on the overall datasets
     */
    private setDatasetStackOffsets(positiveDatasets: any[], negativeDatasets: any[]) {
      this._getDatasetsInOrder().forEach((dataset, datasetIndex) => {
        var valueAccessor = this._isVertical ? this._projectors["y"].accessor : this._projectors["x"].accessor;
        dataset.data().forEach((datum: any, datumIndex: number) => {
          var positiveOffset = positiveDatasets[datasetIndex][datumIndex]["stackOffset"];
          var negativeOffset = negativeDatasets[datasetIndex][datumIndex]["stackOffset"];
          datum["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"] = valueAccessor(datum) >= 0 ? positiveOffset : negativeOffset;
        });
      });
    }

    public _updateScaleExtents() {
      super._updateScaleExtents();
      var primaryScale: Abstract.Scale<any,number> = this._isVertical ? this._yScale : this._xScale;
      if (primaryScale == null) {
        return;
      }
      if (this._isAnchored && this.stackedExtent.length > 0) {
        primaryScale._updateExtent(this._plottableID.toString(), "_PLOTTABLE_PROTECTED_FIELD_STACK_EXTENT", this.stackedExtent);
      } else {
        primaryScale._removeExtent(this._plottableID.toString(), "_PLOTTABLE_PROTECTED_FIELD_STACK_EXTENT");
      }
    }
  }
}
}
<commit_msg>Newline add<commit_after>///<reference path="../../reference.ts" />

module Plottable {
export module Abstract {
  export class Stacked<X, Y> extends Abstract.NewStylePlot<X, Y> {
    private stackedExtent = [0, 0];
    public _isVertical: boolean;

    public _onDatasetUpdate() {
      super._onDatasetUpdate();
      // HACKHACK Caused since onDataSource is called before projectors are set up.  Should be fixed by #803
      if (this._datasetKeysInOrder != null &&
          this._projectors["x"] != null &&
          this._projectors["y"] != null) {
        this.stack();
      }
    }

    private stack() {
      var positiveDatasets: any[][] = [];
      var negativeDatasets: any[][] = [];
      this.populatePositiveNegativeDatasets(positiveDatasets, negativeDatasets);
      this.setDatasetStackOffsets(this._stack(positiveDatasets), this._stack(negativeDatasets));

      var datasets = this._getDatasetsInOrder();
      var valueAccessor = this._isVertical ? this._projectors["y"].accessor : this._projectors["x"].accessor;

      var maxY = _Util.Methods.max(datasets, (dataset: any) => {
        return _Util.Methods.max(dataset.data(), (datum: any) => {
          return valueAccessor(datum) + datum["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"];
        });
      });
      this.stackedExtent[1] = Math.max(0, maxY);

      var minY = _Util.Methods.min(datasets, (dataset: any) => {
        return _Util.Methods.min(dataset.data(), (datum: any) => {
          return valueAccessor(datum) + datum["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"];
        });
      });
      this.stackedExtent[0] = Math.min(minY, 0);
    }

    /**
     * Goes through the datasets so that only negative values / 0 are placed in negativeDatasets
     * and positive values / 0 are placed in positiveDatasets
     */
    private populatePositiveNegativeDatasets(positiveDatasets: any[], negativeDatasets: any[]) {
      var keyAccessor = this._isVertical ? this._projectors["x"].accessor : this._projectors["y"].accessor;
      var valueAccessor = this._isVertical ? this._projectors["y"].accessor : this._projectors["x"].accessor;
      this._getDatasetsInOrder().forEach((dataset) => {
        var positiveDataset: any[] = [];
        var negativeDataset: any[] = [];

        dataset.data().forEach((datum) => {
          var key = keyAccessor(datum);
          var value = valueAccessor(datum);

          var positiveValue = value >= 0 ? value : 0;
          positiveDataset.push({key: key, value: positiveValue});

          var negativeValue = value <= 0 ? value : 0;
          negativeDataset.push({key: key, value: negativeValue});
        });

        positiveDatasets.push(positiveDataset);
        negativeDatasets.push(negativeDataset);
      });
    }

    /**
     * Feeds the data through d3's stack layout function which will calculate
     * the stack offsets and use the the function declared in .out to set the offsets on the data.
     */
    private _stack(datasets: any[][]): any[] {
      var outFunction = (d: any, y0: number, y: number) => {
        d.stackOffset = y0;
      };

      d3.layout.stack()
               .x((d) => d.key)
               .y((d) => d.value)
               .values((d) => d)
               .out(outFunction)(datasets);

      return datasets;
    }

    /**
     * After the stack offsets have been determined on each separate dataset, the offsets need
     * to be determined correctly on the overall datasets
     */
    private setDatasetStackOffsets(positiveDatasets: any[], negativeDatasets: any[]) {
      this._getDatasetsInOrder().forEach((dataset, datasetIndex) => {
        var valueAccessor = this._isVertical ? this._projectors["y"].accessor : this._projectors["x"].accessor;
        dataset.data().forEach((datum: any, datumIndex: number) => {
          var positiveOffset = positiveDatasets[datasetIndex][datumIndex]["stackOffset"];
          var negativeOffset = negativeDatasets[datasetIndex][datumIndex]["stackOffset"];
          datum["_PLOTTABLE_PROTECTED_FIELD_STACK_OFFSET"] = valueAccessor(datum) >= 0 ? positiveOffset : negativeOffset;
        });
      });
    }

    public _updateScaleExtents() {
      super._updateScaleExtents();
      var primaryScale: Abstract.Scale<any,number> = this._isVertical ? this._yScale : this._xScale;
      if (primaryScale == null) {
        return;
      }
      if (this._isAnchored && this.stackedExtent.length > 0) {
        primaryScale._updateExtent(this._plottableID.toString(), "_PLOTTABLE_PROTECTED_FIELD_STACK_EXTENT", this.stackedExtent);
      } else {
        primaryScale._removeExtent(this._plottableID.toString(), "_PLOTTABLE_PROTECTED_FIELD_STACK_EXTENT");
      }
    }
  }
}
}

{'fraction_non_alphanumeric': 0.12919016707459827, 'fraction_numerical': 0.004469511546238161, 'mean_word_length': 2.927287923109068, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, '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': '1768337', 'n_tokens_mistral': 2792, 'n_tokens_neox': 2748, 'n_words': 593}