mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 01:13:23 -04:00
Merge pull request #3704 from spalger/add/yAxisFormatter
[vislib/pointSeries] support specifying a yAxisFormatter
This commit is contained in:
commit
48f6dc8cc9
5 changed files with 65 additions and 65 deletions
|
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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'));
|
||||
|
@ -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')
|
||||
})
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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'));
|
||||
|
@ -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 || {};
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -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)) {
|
||||
|
@ -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');
|
||||
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ define(function (require) {
|
|||
var YAxis;
|
||||
var Data;
|
||||
var el;
|
||||
var buildYAxis;
|
||||
var yAxis;
|
||||
var yAxisDiv;
|
||||
|
||||
|
@ -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 () {
|
||||
|
@ -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'];
|
||||
|
||||
|
@ -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%');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue