Skip to content

Commit

Permalink
Merge branch 'fix/elastic#1962' of github.com:stormpython/kibana
Browse files Browse the repository at this point in the history
  • Loading branch information
abh1nav committed Jan 23, 2015
2 parents 6362329 + 1b59b01 commit 1ad2c08
Show file tree
Hide file tree
Showing 21 changed files with 886 additions and 192 deletions.
144 changes: 107 additions & 37 deletions src/kibana/components/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,41 +114,6 @@ define(function (require) {
return visData;
};

/**
* Function to determine whether to display the legend or not
* Displays legend when more than one series of data present
*
* @method isLegendShown
* @returns {boolean}
*/
Data.prototype.isLegendShown = function () {
var isLegend = false;
var visData = this.getVisData();
var sameSeriesLabel = true;
var seriesLabel;

_.forEach(visData, function countSeriesLength(obj) {
var rootSeries = obj.series || (obj.slices && obj.slices.children);
var dataLength = rootSeries ? rootSeries.length : 0;
var label = dataLength === 1 ? rootSeries[0].label || rootSeries[0].name : undefined;
var children = (obj.slices && obj.slices.children && obj.slices.children[0] && obj.slices.children[0].children);

if (!seriesLabel) {
seriesLabel = label;
}

if (seriesLabel !== label) {
sameSeriesLabel = false;
}

if (dataLength > 1 || children || !sameSeriesLabel) {
isLegend = true;
}
});

return isLegend;
};

/**
* Returns array of chart data objects for pie data objects
*
Expand Down Expand Up @@ -220,7 +185,60 @@ define(function (require) {
var isOverlapping = (this._attr.mode === 'overlap');

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

/**
* Validates that the Y axis min value defined by user input
* is a number.
*
* @param val {Number} Y axis min value
* @returns {Number} Y axis min value
*/
Data.prototype.validateUserDefinedYMin = function (val) {
if (!_.isNumber(val)) {
throw new Error('validateUserDefinedYMin expects a number');
}
return val;
};

/**
* Calculates the min y value from this.dataArray
* for each object in the dataArray.
*
* @method getYMinValue
* @returns {Number} Min y axis value
*/
Data.prototype.getYMinValue = function () {
// 0 default option
// custom min option - where they select the min value

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()[0][0].length === 1 && this.flatten()[0][0][0].y > 0) {
return 0;
}

// Calculate the min value of the dataArray
// 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.getYStackMin(series));
}
return arr.push(self.getYMin(series));
});

return _.min(arr);
};

/**
Expand All @@ -241,6 +259,12 @@ define(function (require) {
return 1;
}

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

// for each object in the dataArray,
// push the calculated y value to the initialized array (arr)
_.forEach(this.flatten(), function (series) {
Expand All @@ -264,6 +288,29 @@ define(function (require) {
return this._attr.stack(series);
};

/**
* Calculates the smallest y stack value among all data objects
*
* @method getYStackMin
* @param series {Array} Array of data objects
* @returns {Number} Y stack max value
*/
Data.prototype.getYStackMin = function (series) {
var isOrdered = (this.data.ordered && this.data.ordered.date);
var minDate = isOrdered ? this.data.ordered.min : undefined;
var maxDate = isOrdered ? this.data.ordered.max : undefined;

return d3.min(this.stackData(series), function (data) {
return d3.min(data, function (d) {
if (isOrdered) {
return (d.x >= minDate && d.x <= maxDate) ? d.y0 + d.y : undefined;
}

return d.y0 + d.y;
});
});
};

/**
* Calculates the largest y stack value among all data objects
*
Expand All @@ -287,10 +334,33 @@ define(function (require) {
});
};

/**
* Calculates the Y domain min value
*
* @method getYMin
* @param series {Array} Array of data objects
* @returns {Number} Y domain min value
*/
Data.prototype.getYMin = function (series) {
var isOrdered = (this.data.ordered && this.data.ordered.date);
var minDate = isOrdered ? this.data.ordered.min : undefined;
var maxDate = isOrdered ? this.data.ordered.max : undefined;

return d3.min(series, function (data) {
return d3.min(data, function (d) {
if (isOrdered) {
return (d.x >= minDate && d.x <= maxDate) ? d.y : undefined;
}

return d.y;
});
});
};

/**
* Calculates the Y domain max value
*
* @method getMax
* @method getYMax
* @param series {Array} Array of data objects
* @returns {Number} Y domain max value
*/
Expand Down
1 change: 1 addition & 0 deletions src/kibana/components/vislib/lib/handler/types/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define(function (require) {
}),
yAxis: new YAxis({
el : vis.el,
yMin: data.getYMinValue(),
yMax : data.getYMaxValue(),
_attr: vis._attr
})
Expand Down
1 change: 1 addition & 0 deletions src/kibana/components/vislib/lib/handler/types/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ define(function (require) {
}),
yAxis: new YAxis({
el : vis.el,
yMin : data.getYMinValue(),
yMax : data.getYMaxValue(),
_attr: vis._attr
})
Expand Down
33 changes: 31 additions & 2 deletions src/kibana/components/vislib/lib/y_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ define(function (require) {
*/
function YAxis(args) {
this.el = args.el;
this.yMin = args.yMin;
this.yMax = args.yMax;
this._attr = _.defaults(args._attr || {}, {});
this._attr = args._attr || {};
}

_(YAxis.prototype).extend(ErrorHandler.prototype);
Expand All @@ -39,9 +40,37 @@ define(function (require) {
* @returns {D3.Scale.QuantitiveScale|*} D3 yScale function
*/
YAxis.prototype.getYScale = function (height) {

// yMin and yMax can never be equal for the axis
// to render. Defaults yMin to 0 if yMin === yMax
// and yMin is greater than or equal to zero, else
// defaults yMax to zero.
if (this.yMin === this.yMax) {
if (this.yMin > 0) {
this.yMin = 0;
} else if (this.yMin === 0) {
this.yMin = -1;
this.yMax = 1;
} else {
this.yMax = 0;
}
}

if (!this._attr.defaultYExtents) {
// if yMin and yMax are both positive, then yMin should be zero
if (this.yMin > 0 && this.yMax > 0) {
this.yMin = 0;
}

// if yMin and yMax are both negative, then yMax should be zero
if (this.yMin < 0 && this.yMax < 0) {
this.yMax = 0;
}
}

// save reference to y scale
this.yScale = d3.scale.linear()
.domain([0, this.yMax])
.domain([this.yMin, this.yMax])
.range([height, 0])
.nice(this.tickScale(height));

Expand Down
2 changes: 1 addition & 1 deletion src/kibana/components/vislib/vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ define(function (require) {
Vis.Super.apply(this, arguments);
this.el = $el.get ? $el.get(0) : $el;
this.ChartClass = chartTypes[config.type];
this._attr = _.defaults(config || {}, {});
this._attr = _.defaults({}, config || {}, {});
this.eventTypes = {
enabled: []
};
Expand Down
20 changes: 18 additions & 2 deletions src/kibana/components/vislib/visualizations/area_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ define(function (require) {
var color = this.handler.data.getColorFunc();
var xScale = this.handler.xAxis.xScale;
var yScale = this.handler.yAxis.yScale;
var height = yScale.range()[0];
var defaultOpacity = this._attr.defaultOpacity;

var area = d3.svg.area()
Expand All @@ -95,8 +94,9 @@ define(function (require) {
})
.y0(function (d) {
if (isOverlapping) {
return height;
return yScale(0);
}

return yScale(d.y0);
})
.y1(function (d) {
Expand Down Expand Up @@ -291,6 +291,8 @@ define(function (require) {
var margin = this._attr.margin;
var elWidth = this._attr.width = $elem.width();
var elHeight = this._attr.height = $elem.height();
var yMin = this.handler.yAxis.yMin;
var yScale = this.handler.yAxis.yScale;
var minWidth = 20;
var minHeight = 20;
var div;
Expand Down Expand Up @@ -330,6 +332,19 @@ define(function (require) {
// add path
path = self.addPath(svg, layers);

if (yMin < 0 && self._attr.mode !== 'wiggle' && self._attr.mode !== 'silhouette') {

// Draw line at yScale 0 value
svg.append('line')
.attr('class', 'zero-line')
.attr('x1', 0)
.attr('y1', yScale(0))
.attr('x2', width)
.attr('y2', yScale(0))
.style('stroke', '#ddd')
.style('stroke-width', 1);
}

// add circles
circles = self.addCircles(svg, layers);

Expand All @@ -338,6 +353,7 @@ define(function (require) {

// chart base line
var line = svg.append('line')
.attr('class', 'base-line')
.attr('x1', 0)
.attr('y1', height)
.attr('x2', width)
Expand Down
Loading

0 comments on commit 1ad2c08

Please sign in to comment.