Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from spenceralger/negatives
Browse files Browse the repository at this point in the history
Negatives
  • Loading branch information
stormpython committed Feb 5, 2015
2 parents 0923e73 + 0210e56 commit fc55557
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions src/kibana/components/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,32 +287,31 @@ define(function (require) {
* @returns {Array} Value objects
*/
Data.prototype.flatten = function () {
var data = this.chartData();
var series = _.chain(data).pluck('series').pluck().value();
var values = [];

series.forEach(function (d) {
values.push(_.chain(d).flatten().pluck('values').value());
});

return values;
return _(this.chartData())
.pluck('series')
.flatten()
.pluck('values')
.flatten()
.value();
};

/**
* Determines whether histogram charts should be stacked
* TODO: need to make this more generic
*
* @method shouldBeStacked
* @param series {Array} Array of data objects
* @returns {boolean}
*/
Data.prototype.shouldBeStacked = function (series) {
Data.prototype.shouldBeStacked = function () {
var isHistogram = (this._attr.type === 'histogram');
var isArea = (this._attr.type === 'area');
var isOverlapping = (this._attr.mode === 'overlap');
var grouped = (this._attr.mode === 'grouped');

var stackedHisto = isHistogram && !grouped;
var stackedArea = isArea && !isOverlapping;

// Series should be an array
return (isHistogram && series.length > 1 || isArea && !isOverlapping && series.length > 1);
return stackedHisto || stackedArea;
};

/**
Expand All @@ -339,30 +338,28 @@ define(function (require) {
Data.prototype.getYMinValue = function () {
var self = this;
var arr = [];
var grouped = (this._attr.mode === 'grouped');

if (this._attr.mode === 'percentage' || this._attr.mode === 'wiggle' ||
this._attr.mode === 'silhouette') {
return 0;
}

// When there is only one data point,
// the yMin should default to zero.
if (!this.flatten().length || this.flatten()[0][0].length === 1 && this.flatten()[0][0][0].y > 0) {
var flat = this.flatten();
// if there is only one data point and its less than zero,
// return 0 as the yMax value.
if (!flat.length || flat.length === 1 && flat[0].y > 0) {
return 0;
}

// Calculate the min value of the dataArray
var min = Infinity;

// for each object in the dataArray,
// push the calculated y value to the initialized array (arr)
_.forEach(this.flatten(), function (series) {
if (self.shouldBeStacked(series) && !grouped) {
return arr.push(self._getYExtent(series, self._getYStack, 'min'));
}
return arr.push(self._getYExtent(series, self._getY, 'min'));
_.each(this.chartData(), function (chart) {
min = Math.min(min, self._getYExtent(chart, 'min'));
});

return _.min(arr);
return min;
};

/**
Expand All @@ -377,28 +374,27 @@ define(function (require) {
Data.prototype.getYMaxValue = function () {
var self = this;
var arr = [];
var grouped = (self._attr.mode === 'grouped');

if (self._attr.mode === 'percentage') {
return 1;
}

var flat = this.flatten();
// if there is only one data point and its less than zero,
// return 0 as the yMax value.
if (!this.flatten().length || this.flatten()[0][0].length === 1 && this.flatten()[0][0][0].y < 0) {
if (!flat.length || flat.length === 1 && flat[0].y < 0) {
return 0;
}

var max = -Infinity;

// for each object in the dataArray,
// push the calculated y value to the initialized array (arr)
_.forEach(this.flatten(), function (series) {
if (self.shouldBeStacked(series) && !grouped) {
return arr.push(self._getYExtent(series, self._getYStack, 'max'));
}
return arr.push(self._getYExtent(series, self._getY, 'max'));
_.each(this.chartData(), function (chart) {
max = Math.max(max, self._getYExtent(chart, 'max'));
});

return _.max(arr);
return max;
};

/**
Expand All @@ -418,10 +414,21 @@ define(function (require) {
* Returns the max Y axis value for a `series` array based on
* a specified callback function (calculation).
*/
Data.prototype._getYExtent = function (series, calculation, extent) {
return d3[extent](this.stackData(series), function (data) {
return d3[extent](data, calculation);
});
Data.prototype._getYExtent = function (chart, extent) {
var calculation = this._getY;

if (this.shouldBeStacked()) {
this.stackData(_.pluck(chart.series, 'values'));
calculation = this._getYStack;
}

var points = chart.series
.reduce(function (points, series) {
return points.concat(series.values);
}, [])
.map(calculation);

return d3[extent](points);
};

/**
Expand Down

0 comments on commit fc55557

Please sign in to comment.