Skip to content

Commit

Permalink
fix minInterval error on date histogram values
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Dec 17, 2020
1 parent 8f9b440 commit 6417be1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
23 changes: 15 additions & 8 deletions src/plugins/charts/public/static/components/endzones.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,32 @@ const getIntervalInMs = (
}
};

/**
* Returns the adjusted interval based on the data
*
* @param xValues sorted and unquie x values
* @param esValue
* @param esUnit
* @param timeZone
*/
export const getAdjustedInterval = (
xValues: number[],
esValue: number,
esUnit: string,
esUnit: unitOfTime.Base,
timeZone: string
): number => {
return xValues.reduce((minInterval, currentXvalue, index) => {
const newInterval = xValues.reduce((minInterval, currentXvalue, index) => {
let currentDiff = minInterval;

if (index > 0) {
currentDiff = Math.abs(xValues[index - 1] - currentXvalue);
}
const singleUnitInterval = getIntervalInMs(
currentXvalue,
esValue,
esUnit as unitOfTime.Base,
timeZone
);

const singleUnitInterval = getIntervalInMs(currentXvalue, esValue, esUnit, timeZone);
return Math.min(minInterval, singleUnitInterval, currentDiff);
}, Number.MAX_SAFE_INTEGER);

return newInterval > 0 ? newInterval : moment.duration(esValue, esUnit).asMilliseconds();
};

const partialDataText = i18n.translate('charts.partialData.bucketTooltipText', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import moment from 'moment-timezone';
import moment, { unitOfTime } from 'moment-timezone';
import React, { Component } from 'react';
import PropTypes from 'prop-types';

Expand Down Expand Up @@ -153,7 +153,12 @@ export class DiscoverHistogram extends Component<DiscoverHistogramProps, Discove
const xDomain = {
min: domainMin,
max: domainMax,
minInterval: getAdjustedInterval(xValues, intervalESValue, intervalESUnit, timeZone),
minInterval: getAdjustedInterval(
xValues,
intervalESValue,
intervalESUnit as unitOfTime.Base,
timeZone
),
};
const tooltipProps = {
headerFormatter: renderEndzoneTooltip(xInterval, domainStart, domainEnd, this.formatXValue),
Expand Down
35 changes: 26 additions & 9 deletions src/plugins/vis_type_xy/public/utils/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* under the License.
*/

import { uniq } from 'lodash';
import { unitOfTime } from 'moment';

import { DomainRange } from '@elastic/charts';

import { getAdjustedInterval } from '../../../charts/public';
import { Datatable } from '../../../expressions/public';

import { getTimefilter } from '../services';
import { Aspect, DateHistogramParams, HistogramParams } from '../types';
Expand All @@ -45,24 +49,35 @@ export const getXDomain = (params: Aspect['params']): DomainRange => {
};

export const getAdjustedDomain = (
data: any[],
data: Datatable['rows'],
{ accessor, params }: Aspect,
timeZone: string,
domain?: DomainRange,
hasBars?: boolean
): DomainRange => {
const { interval, intervalESValue, intervalESUnit } = params as DateHistogramParams;

if (accessor && domain && 'min' in domain && 'max' in domain) {
const xValues = data.map((d) => d[accessor]).sort();
if (
accessor &&
domain &&
'min' in domain &&
'max' in domain &&
'intervalESValue' in params &&
'intervalESUnit' in params
) {
const { interval, intervalESValue, intervalESUnit } = params;
const xValues = uniq(data.map((d) => d[accessor]).sort());

const [firstXValue] = xValues;
const lastXValue = xValues[xValues.length - 1];

const domainMin = Math.min(firstXValue, domain.min);
const domainMaxValue = hasBars ? domain.max - interval : lastXValue + interval;
const domainMax = Math.max(domainMaxValue, lastXValue);
const minInterval = getAdjustedInterval(xValues, intervalESValue, intervalESUnit, timeZone);
const minInterval = getAdjustedInterval(
xValues,
intervalESValue,
intervalESUnit as unitOfTime.Base,
timeZone
);

return {
min: domainMin,
Expand All @@ -71,7 +86,9 @@ export const getAdjustedDomain = (
};
}

return {
minInterval: interval,
};
return 'interval' in params
? {
minInterval: params.interval,
}
: {};
};

0 comments on commit 6417be1

Please sign in to comment.