Skip to content

Commit

Permalink
Merge pull request #3704 from spalger/add/yAxisFormatter
Browse files Browse the repository at this point in the history
[vislib/pointSeries] support specifying a yAxisFormatter
  • Loading branch information
spalger committed May 1, 2015
2 parents b1fe261 + ff3e5bc commit 48f6dc8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 65 deletions.
15 changes: 3 additions & 12 deletions src/kibana/components/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,9 @@ define(function (require) {
* @param thing {String} Data object key
* @returns {*} Data object value
*/
Data.prototype.get = function (thing) {
var data;

if (this.data.rows) {
data = this.data.rows;
} else if (this.data.columns) {
data = this.data.columns;
} else {
data = [this.data];
}

return _.pluck(data, thing)[0];
Data.prototype.get = function (thing, def) {
var source = (this.data.rows || this.data.columns || [this.data])[0];
return _.get(source, thing, def);
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
define(function (require) {
return function ColumnHandler(d3, Private) {
var $ = require('jquery');

var injectZeros = Private(require('components/vislib/components/zero_injection/inject_zeros'));
var Handler = Private(require('components/vislib/lib/handler/handler'));
var Data = Private(require('components/vislib/lib/data'));
Expand Down Expand Up @@ -45,7 +43,8 @@ define(function (require) {
el : vis.el,
yMin : data.getYMin(),
yMax : data.getYMax(),
_attr: vis._attr
_attr: vis._attr,
tickFormat: data.get('yAxisFormatter')
})
});
};
Expand Down
31 changes: 7 additions & 24 deletions src/kibana/components/vislib/lib/y_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ define(function (require) {
return function YAxisFactory(d3, Private) {
var _ = require('lodash');
var $ = require('jquery');
var numeral = require('numeral');
var errors = require('errors');

var ErrorHandler = Private(require('components/vislib/lib/_error_handler'));
Expand All @@ -18,6 +17,7 @@ define(function (require) {
this.el = args.el;
this.yMin = args.yMin;
this.yMax = args.yMax;
this.yAxisFormatter = args.yAxisFormatter;
this._attr = args._attr || {};
}

Expand Down Expand Up @@ -103,18 +103,11 @@ define(function (require) {
.nice();
};

/**
* By default, d3.format('s') returns billion values
* with a `G` instead of a `B`. @method formatAxisLabel returns
* billion values with a B instead of a G. Else, it defaults
* to the d3.format('s') value.
*
* @method formatAxisLabel
* @param d {Number}
* @returns {*}
*/
YAxis.prototype.formatAxisLabel = function (d) {
return numeral(d).format('0.[0]a');
YAxis.prototype.tickFormat = function () {
var isPercentage = this._attr.mode === 'percentage';
if (isPercentage) return d3.format('%');
if (this.yAxisFormatter) return this.yAxisFormatter;
return d3.format('n');
};

/**
Expand All @@ -126,16 +119,6 @@ define(function (require) {
*/
YAxis.prototype.getYAxis = function (height) {
var yScale = this.getYScale(height);
var isPercentage = (this._attr.mode === 'percentage');
var tickFormat;

if (isPercentage) {
tickFormat = d3.format('%');
} else if (this.yMax <= 100 && this.yMin >= -100 && !isPercentage) {
tickFormat = d3.format('n');
} else {
tickFormat = this.formatAxisLabel;
}

// y scale should never be `NaN`
if (!yScale || _.isNaN(yScale)) {
Expand All @@ -145,7 +128,7 @@ define(function (require) {
// Create the d3 yAxis function
this.yAxis = d3.svg.axis()
.scale(yScale)
.tickFormat(tickFormat)
.tickFormat(this.tickFormat())
.ticks(this.tickScale(height))
.orient('left');

Expand Down
13 changes: 10 additions & 3 deletions src/kibana/utils/_mixins_chainable.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,18 @@ define(function (require) {
},

/**
* Shortcut for the simple version of _.deepGet
* Shortcut for the simple version of _.deepGet with support for default
* values added
*
* @param {obj} any - the value to read from
* @param {string|array} path - the location of the value to return as
* a dot-notated string or array of keys.
* @param {any} def - when the value is null or undefined return this instead
* @return {any}
*/
get: function (obj, path) {
return _.deepGet(obj, path);
get: function (obj, path, def) {
var val = _.deepGet(obj, path);
return (val == null && def != null) ? def : val;
},

/**
Expand Down
66 changes: 43 additions & 23 deletions test/unit/specs/vislib/lib/y_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define(function (require) {
var YAxis;
var Data;
var el;
var buildYAxis;
var yAxis;
var yAxisDiv;

Expand Down Expand Up @@ -70,14 +71,18 @@ define(function (require) {
defaultYMin: true
});

yAxis = new YAxis({
el: node,
yMin: dataObj.getYMin(),
yMax: dataObj.getYMax(),
_attr: {
margin: { top: 0, right: 0, bottom: 0, left: 0 }
}
});
buildYAxis = function (params) {
return new YAxis(_.merge({}, params, {
el: node,
yMin: dataObj.getYMin(),
yMax: dataObj.getYMax(),
_attr: {
margin: { top: 0, right: 0, bottom: 0, left: 0 }
}
}));
};

yAxis = buildYAxis();
}

describe('Vislib yAxis Class Test Suite', function () {
Expand Down Expand Up @@ -206,20 +211,6 @@ define(function (require) {
});
});

describe('formatAxisLabel method', function () {
var num = 1e9;
var val;

beforeEach(function () {
createData(defaultGraphData);
val = yAxis.formatAxisLabel(num);
});

it('should return a string with suffix B', function () {
expect(val).to.be('1b');
});
});

describe('getScaleType method', function () {
var fnNames = ['linear', 'log', 'square root'];

Expand Down Expand Up @@ -360,5 +351,34 @@ define(function (require) {
expect(yAxis.tickScale(20)).to.be(0);
});
});

describe('#tickFormat()', function () {
var formatter = function () {};

it('returns a basic number formatter by default', function () {
var yAxis = buildYAxis();
expect(yAxis.tickFormat()).to.not.be(formatter);
expect(yAxis.tickFormat()(1)).to.be('1');
});

it('returns the yAxisFormatter when passed', function () {
var yAxis = buildYAxis({
yAxisFormatter: formatter
});
expect(yAxis.tickFormat()).to.be(formatter);
});

it('returns a percentage formatter when the vis is in percentage mode', function () {
var yAxis = buildYAxis({
yAxisFormatter: formatter,
_attr: {
mode: 'percentage'
}
});

expect(yAxis.tickFormat()).to.not.be(formatter);
expect(yAxis.tickFormat()(1)).to.be('100%');
});
});
});
});
});

0 comments on commit 48f6dc8

Please sign in to comment.