Merge pull request #765 from influxdata/get-range

Get range
pull/767/head
Andrew Watkins 2017-01-12 14:51:13 -08:00 committed by GitHub
commit 03c1311e15
3 changed files with 100 additions and 57 deletions

View File

@ -0,0 +1,43 @@
import getRange from 'shared/parsing/getRangeForDygraph';
describe('getRangeForDygraphSpec', () => {
it('gets the range for one timeSeries', () => {
const timeSeries = [[new Date(1000), 1], [new Date(2000), 2], [new Date(3000), 3]];
const actual = getRange(timeSeries);
const expected = [1, 3];
expect(actual).to.deep.equal(expected);
});
it('does not get range when a range is provided', () => {
const timeSeries = [[new Date(1000), 1], [new Date(2000), 2], [new Date(3000), 3]];
const providedRange = [0, 4];
const actual = getRange(timeSeries, providedRange);
expect(actual).to.deep.equal(providedRange);
});
it('gets the range for multiple timeSeries', () => {
const timeSeries = [
[new Date(1000), null, 1],
[new Date(1000), 100, 1],
[new Date(2000), null, 2],
[new Date(3000), 200, 3],
];
const actual = getRange(timeSeries);
const expected = [1, 200];
expect(actual).to.deep.equal(expected);
});
it('returns a null array of two elements when min and max are equal', () => {
const timeSeries = [[new Date(1000), 1], [new Date(2000), 1], [new Date(3000), 1]];
const actual = getRange(timeSeries);
const expected = [null, null];
expect(actual).to.deep.equal(expected);
});
});

View File

@ -1,6 +1,7 @@
/* eslint-disable no-magic-numbers */
import React, {PropTypes} from 'react';
import Dygraph from '../../external/dygraph';
import getRange from 'src/shared/parsing/getRangeForDygraph';
import _ from 'lodash';
const {
@ -179,60 +180,3 @@ export default React.createClass({
);
},
});
const PADDING_FACTOR = 0.1;
function getRange(timeSeries, override, value = null, rangeValue = null) {
if (override) {
return override;
}
const subtractPadding = (val) => +val - val * PADDING_FACTOR;
const addPadding = (val) => +val + val * PADDING_FACTOR;
const pad = (val, side) => {
if (val === null || val === '') {
return null;
}
if (val < 0) {
return side === "top" ? subtractPadding(val) : addPadding(val);
}
return side === "top" ? addPadding(val) : subtractPadding(val);
};
const points = [
...timeSeries,
[null, pad(value)],
[null, pad(rangeValue, "top")],
];
const range = points.reduce(([min, max], series) => {
for (let i = 1; i < series.length; i++) {
const val = series[i];
if (max === null) {
max = val;
}
if (min === null) {
min = val;
}
if (typeof val === "number") {
min = Math.min(min, val);
max = Math.max(max, val);
}
return [min, max];
}
}, [null, null]);
// Dygraph will not reliably plot X / Y axis labels if min and max are both 0
if (range[0] === 0 && range[1] === 0) {
return [null, null];
}
return range;
}

View File

@ -0,0 +1,56 @@
const PADDING_FACTOR = 0.1;
export default function getRange(timeSeries, override, value = null, rangeValue = null) {
if (override) {
return override;
}
const subtractPadding = (val) => +val - val * PADDING_FACTOR;
const addPadding = (val) => +val + val * PADDING_FACTOR;
const pad = (val, side) => {
if (val === null || val === '') {
return null;
}
if (val < 0) {
return side === "top" ? subtractPadding(val) : addPadding(val);
}
return side === "top" ? addPadding(val) : subtractPadding(val);
};
const points = [
...timeSeries,
[null, pad(value)],
[null, pad(rangeValue, "top")],
];
const range = points.reduce(([min, max] = [], series) => {
for (let i = 1; i < series.length; i++) {
const val = series[i];
if (max === null) {
max = val;
}
if (min === null) {
min = val;
}
if (typeof val === "number") {
min = Math.min(min, val);
max = Math.max(max, val);
}
}
return [min, max];
}, [null, null]);
// If time series is such that min and max are equal use Dygraph defaults
if (range[0] === range[1]) {
return [null, null];
}
return range;
}