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

feat: add minInterval option for custom xDomain #240

Merged
merged 12 commits into from
Jun 19, 2019
Merged
37 changes: 37 additions & 0 deletions src/lib/series/domains/x_domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,41 @@ describe('X Domain', () => {
const errorMessage = 'xDomain for ordinal scale should be an array of values, not a DomainRange object';
expect(attemptToMerge).toThrowError(errorMessage);
});

describe('should account for custom minInterval', () => {
const xValues = new Set([1, 2, 3, 4, 5]);
const specs: Pick<BasicSeriesSpec, 'seriesType' | 'xScaleType'>[] = [
{ seriesType: 'bar', xScaleType: ScaleType.Linear },
];

test('with valid minInterval', () => {
const xDomain = { minInterval: 0.5 };
const mergedDomain = mergeXDomain(specs, xValues, xDomain);
expect(mergedDomain.minInterval).toEqual(0.5);
});

test('with valid minInterval greater than computed minInterval for single datum set', () => {
const xDomain = { minInterval: 10 };
const mergedDomain = mergeXDomain(specs, new Set([5]), xDomain);
expect(mergedDomain.minInterval).toEqual(10);
});

test('with invalid minInterval greater than computed minInterval for multi data set', () => {
const invalidXDomain = { minInterval: 10 };
const attemptToMerge = () => {
mergeXDomain(specs, xValues, invalidXDomain);
};
const expectedError = 'custom xDomain is invalid, custom minInterval is greater than computed minInterval';
expect(attemptToMerge).toThrowError(expectedError);
});

test('with invalid minInterval less than 0', () => {
const invalidXDomain = { minInterval: -1 };
const attemptToMerge = () => {
mergeXDomain(specs, xValues, invalidXDomain);
};
const expectedError = 'custom xDomain is invalid, custom minInterval is less than 0';
expect(attemptToMerge).toThrowError(expectedError);
});
});
});
27 changes: 23 additions & 4 deletions src/lib/series/domains/x_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function mergeXDomain(
const values = [...xValues.values()];
let seriesXComputedDomains;
let minInterval = 0;

if (mainXScaleType.scaleType === ScaleType.Ordinal) {
seriesXComputedDomains = computeOrdinalDataDomain(values, identity, false, true);
if (xDomain) {
Expand All @@ -40,8 +41,16 @@ export function mergeXDomain(
}
} else {
seriesXComputedDomains = computeContinuousDataDomain(values, identity, true);
let customMinInterval: undefined | number;

if (xDomain) {
if (!Array.isArray(xDomain)) {
if (Array.isArray(xDomain)) {
throw new Error('xDomain for continuous scale should be a DomainRange object, not an array');
}

customMinInterval = xDomain.minInterval;

if (xDomain) {
const [computedDomainMin, computedDomainMax] = seriesXComputedDomains;

if (isCompleteBound(xDomain)) {
Expand All @@ -63,11 +72,21 @@ export function mergeXDomain(

seriesXComputedDomains = [computedDomainMin, xDomain.max];
}
} else {
throw new Error('xDomain for continuous scale should be a DomainRange object, not an array');
}
}
minInterval = findMinInterval(values);

const computedMinInterval = findMinInterval(values);
if (customMinInterval != null) {
// Allow greater custom min iff xValues has 1 member.
if (xValues.size > 1 && customMinInterval > computedMinInterval) {
throw new Error('custom xDomain is invalid, custom minInterval is greater than computed minInterval');
}
if (customMinInterval < 0) {
throw new Error('custom xDomain is invalid, custom minInterval is less than 0');
}
}

minInterval = customMinInterval || computedMinInterval;
}

return {
Expand Down
28 changes: 21 additions & 7 deletions src/lib/series/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,34 @@ export type Datum = any;
export type Rotation = 0 | 90 | -90 | 180;
export type Rendering = 'canvas' | 'svg';

export interface LowerBoundedDomain {
min: number;
interface DomainMinInterval {
/** Custom minInterval for the domain which will affect data bucket size.
* The minInterval cannot be greater than the computed minimum interval between any two adjacent data points.
* Further, if you specify a custom numeric minInterval for a timeseries, please note that due to the restriction
* above, the specified numeric minInterval will be interpreted as a fixed interval.
* This means that, for example, if you have yearly timeseries data that ranges from 2016 to 2019 and you manually
* compute the interval between 2016 and 2017, you'll have 366 days due to 2016 being a leap year. This will not
* be a valid interval because it is greater than the computed minInterval of 365 days betwen the other years.
*/
minInterval?: number;
}

export interface UpperBoundedDomain {
max: number;
interface LowerBound {
/** Lower bound of domain range */
min: number;
}

export interface CompleteBoundedDomain {
min: number;
interface UpperBound {
/** Upper bound of domain range */
max: number;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add the jsdocs to documents the meaning of minInterval? It's important to stress that this should be the minimum interval available between data. If for example we are visualizing yearly data from 2016 to 2019 and we the consumer compute the first interval an error will occur: 2016 is a leap year this the interval is greater then the minComputedInterval of 365 days.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a datum with monthly aggregation they should provide 28 days as the minInterval.
It's worth also having a look at the current data_histogram intervals https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html because with time intervals there is always a distinction between calendar intervals and fixed intervals.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also suggest to check if there is a better way to specify that interval. In particular, for Time domains.
I'd like to avoid pushing the consumer to compute that minInterval in millis manually and rather accept also some data_histogram intervals values like 1D or 1M or similar. This facilitate the use of the library and enables the consumer to just specify a simple interval.
Keep also an eye on this deprecation: elastic/kibana#27410

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markov00

See comment explaining minInterval with warnings about certain kinds of timeseries intervals here: 9de618a

It would certainly be possible to extend the type of minInterval to accept either a raw number which would match the same units as the raw datum or a shorthand for intervals specific to a certain unit that we internally use to compute the actual minInterval of a domain. We could do something like:

interface DomainMinInterval {
  minInterval?: number | TimeInterval;
}

And then TimeInterval could be defined either as just a string that we parse ourselves or an interface with explicit unit: string and length: number where valid units are restricted to a certain set of strings.

However, this seems to potentially duplicate efforts because on the Kibana side, we are already using @elastic/datemath and parseInterval (note that parseInterval is not a part of @elastic/datemath and is instead a function defined within Kibana) to transform the returned interval from Elasticsearch (a separate issue is that it seems the Discover query still uses the deprecated interval field instead of calendar_interval or fixed_interval). That is, in the Discover data, we get back chartData.ordered.interval which is a moment duration which requires no additional parsing or transformation and is passed directly in as the value of minInterval:

    // Note: this is using the deprecated `interval` field; see above
    const xInterval = chartData.ordered.interval; 

    const xDomain = {
      domainRange: {
        min: domainMin,
        max: domainMax,
      },
      minInterval: xInterval,
    };

So for developer users of elastic-charts in Kibana, they should already have the ability to directly pass in correctly formatted custom minInterval values given the tools that exist within Kibana.

If we want to support more custom time interval shorthands within elastic-charts itself, that seems to me to be an additional enhancement to consider outside of this PR (the functionality exposed by this PR is needed for the Discover replacement, which does not need support for interval shorthands; and any other app within Kibana should be able to use the parseInterval helper to get the milliseconds value) because we may want to consider how to avoid duplication of efforts between our own internal parser and Kibana's. (One small difference that may be an issue is that Kibana is using moment while elastic-charts using luxon which uses slightly different strings to define patterns.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @emmacunningham for the clarification. Yes let's keep it like it is for the moment


export type DomainRange = LowerBoundedDomain | UpperBoundedDomain | CompleteBoundedDomain;
export type LowerBoundedDomain = DomainMinInterval & LowerBound;
export type UpperBoundedDomain = DomainMinInterval & UpperBound;
export type CompleteBoundedDomain = DomainMinInterval & LowerBound & UpperBound;
export type UnboundedDomainWithInterval = DomainMinInterval;

export type DomainRange = LowerBoundedDomain | UpperBoundedDomain | CompleteBoundedDomain | UnboundedDomainWithInterval;

export interface DisplayValueSpec {
/** Show value label in chart element */
Expand Down
Loading