Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation fix #2

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
78d5d84
initial commit
stormpython Jan 5, 2015
05b66fe
Merge branch 'master' into fix/#1962
stormpython Jan 5, 2015
8d99647
changing things
stormpython Jan 12, 2015
1030987
changes
stormpython Jan 13, 2015
3c4b9e3
Merge branch 'master' into fix/#1962
stormpython Jan 13, 2015
bd0a11d
it's alive, got line chart working with negative values
stormpython Jan 13, 2015
23d047d
getting stacked and possibly grouped bar charts to work on negative axes
stormpython Jan 14, 2015
d64a224
Merge branch 'master' into fix/#1962
stormpython Jan 14, 2015
4b3936f
adding almost finishing touches
stormpython Jan 15, 2015
85df0f2
returning defaultYMin value to true
stormpython Jan 15, 2015
b695748
Merge branch 'master' into fix/#1962
stormpython Jan 15, 2015
057c378
small changes
stormpython Jan 16, 2015
f5a50e2
revised addSTackedBars in columnchart to use yMin
Jan 16, 2015
39b41fa
Merge branch 'master' into fix/#1962
Jan 16, 2015
278e509
Merge branch 'fix/#1962' of https://github.com/jthomassie/kibana into…
stormpython Jan 16, 2015
4f35360
fixing overlap for area chart with negative axes and a bug in stacked…
stormpython Jan 16, 2015
8350df0
space deletion
stormpython Jan 16, 2015
5043f9a
Merge branch 'master' into fix/#1962
stormpython Jan 16, 2015
e6856e5
adding Y min function to data.js, which fixes problems with line char…
stormpython Jan 19, 2015
efb5858
fixed y_axis failing test and set defaultYMin to true
stormpython Jan 19, 2015
2f32b8b
fix updateParams call, fix config side effects
w33ble Jan 20, 2015
7e8c9f1
use a real node for el
w33ble Jan 20, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 118 additions & 2 deletions src/kibana/components/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ define(function (require) {
return new Data(data, attr);
}

var self = this;
var offset;

if (attr.mode === 'stacked') {
Expand Down Expand Up @@ -220,7 +221,65 @@ 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' || this._attr.defaultYMin) {
return 0;
}

// User defined y axis min value
if (this._attr.userDefinedYMin) {
return this.validateUserDefinedYMin(this._attr.userDefinedYMin);
}

// 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 +300,17 @@ define(function (require) {
return 1;
}

// User defined y axis min value
if (this._attr.userDefinedYMin) {
return this.validateUserDefinedYMin(this._attr.userDefinedYMin);
}

// 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 +334,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 +380,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
19 changes: 18 additions & 1 deletion src/kibana/components/vislib/lib/y_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ define(function (require) {
*/
function YAxis(args) {
this.el = args.el;
this.yMin = args.yMin;
this.yMax = args.yMax;
this._attr = _.defaults(args._attr || {}, {});
}
Expand All @@ -39,9 +40,25 @@ 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;
}
}

// 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
4 changes: 3 additions & 1 deletion src/kibana/components/vislib/vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ 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 || {}, {
defaultYMin: true
});
this.eventTypes = {
enabled: []
};
Expand Down
18 changes: 18 additions & 0 deletions src/kibana/components/vislib/visualizations/area_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ define(function (require) {
})
.y0(function (d) {
if (isOverlapping) {
if (!self._attr.defaultYMin) {
return yScale(0);
}
return height;
}

return yScale(d.y0);
})
.y1(function (d) {
Expand Down Expand Up @@ -291,6 +295,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 +336,18 @@ 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('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 Down
46 changes: 46 additions & 0 deletions src/kibana/components/vislib/visualizations/column_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ define(function (require) {
var data = this.chartData;
var xScale = this.handler.xAxis.xScale;
var yScale = this.handler.yAxis.yScale;
var height = yScale.range()[0];
var yMin = this.handler.yAxis.yScale.domain()[0];
var self = this;

// update
bars
Expand All @@ -155,9 +158,23 @@ define(function (require) {
return xScale.rangeBand();
})
.attr('y', function (d) {
if (d.y < 0 && !self._attr.defaultYMin) {
return yScale(d.y0);
}

return yScale(d.y0 + d.y);
})
.attr('height', function (d) {
if (d.y < 0 && !self._attr.defaultYMin) {
return Math.abs(yScale(d.y0 + d.y) - yScale(d.y0));
}

// for split bars or for one series,
// last series will have d.y0 = 0
if (d.y0 === 0 && yMin > 0) {
return yScale(yMin) - yScale(d.y);
}

return yScale(d.y0) - yScale(d.y0 + d.y);
});

Expand All @@ -174,12 +191,14 @@ define(function (require) {
ColumnChart.prototype.addGroupedBars = function (bars) {
var xScale = this.handler.xAxis.xScale;
var yScale = this.handler.yAxis.yScale;
var yMin = this.handler.yAxis.yMin;
var data = this.chartData;
var n = data.series.length;
var height = yScale.range()[0];
var groupSpacingPercentage = 0.15;
var isTimeScale = (data.ordered && data.ordered.date);
var minWidth = 1;
var self = this;
var barWidth;

// update
Expand Down Expand Up @@ -207,9 +226,22 @@ define(function (require) {
return xScale.rangeBand() / n;
})
.attr('y', function (d) {
if (d.y < 0 && !self._attr.defaultYMin) {
return yScale(0);
}

return yScale(d.y);
})
.attr('height', function (d) {
if (d.y < 0 && !self._attr.defaultYMin) {
return Math.abs(yScale(0) - yScale(d.y));
}

// if there is a negative yMin value, use yScale(0) instead of height
if (yMin < 0) {
return yScale(0) - yScale(d.y);
}

return height - yScale(d.y);
});

Expand Down Expand Up @@ -253,6 +285,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 @@ -294,6 +328,18 @@ define(function (require) {
.style('stroke', '#ddd')
.style('stroke-width', 1);

if (yMin < 0) {

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

return svg;
});
};
Expand Down
Loading