Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions src/area-chart/__tests__/area-chart-model-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,28 @@ describe('AreaChart computeDomainY', () => {
expect(domain).toHaveLength(0);
});

it('returns min/max domain for linear series', () => {
it('includes zero baseline in linear domain for threshold series', () => {
const domain = computeDomainY([{ type: 'threshold', y: 10, title: 'T10' }], 'linear');
expect(domain).toEqual([10, 10]);
expect(domain).toEqual([0, 10]);
});

it('returns min/max domain for log series', () => {
const domain = computeDomainY([{ type: 'threshold', y: 10, title: 'T10' }], 'linear');
expect(domain).toEqual([10, 10]);
it('includes zero baseline in linear domain when all values are positive', () => {
const domain = computeDomainY(
[{ type: 'area', data: [50, 75, 100].map((y, x) => ({ x, y })), title: 'A' }],
'linear'
);
expect(domain).toEqual([0, 100]);
});

it('includes zero baseline in linear domain when all values are negative', () => {
const domain = computeDomainY(
[{ type: 'area', data: [-50, -75, -100].map((y, x) => ({ x, y })), title: 'A' }],
'linear'
);
expect(domain).toEqual([-100, 0]);
});

it('replaces lower range bound with 1 for log scale type', () => {
it('keeps zero lower bound for linear scale type', () => {
const domain = computeDomainY(
[
{ type: 'threshold', y: 0, title: 'T0' },
Expand All @@ -106,6 +117,28 @@ describe('AreaChart computeDomainY', () => {
expect(domain).toEqual([0, 10]);
});

it('replaces zero lower bound with 1 for log scale type', () => {
const domain = computeDomainY(
[
{ type: 'threshold', y: 0, title: 'T0' },
{ type: 'threshold', y: 10, title: 'T10' },
],
'log'
);
expect(domain).toEqual([1, 10]);
});

it('keeps log domain as is when values are below 1', () => {
const domain = computeDomainY(
[
{ type: 'threshold', y: 0.1, title: 'T0.1' },
{ type: 'threshold', y: 0.5, title: 'T0.5' },
],
'log'
);
expect(domain).toEqual([0.1, 0.5]);
});

it('computes stacked min/max', () => {
const data = [1, 2, 3, 4, 5];
const domain = computeDomainY(
Expand All @@ -115,7 +148,7 @@ describe('AreaChart computeDomainY', () => {
],
'linear'
);
expect(domain).toEqual([1, 15]);
expect(domain).toEqual([0, 15]);
});

it('computes min/max with both series and threshold', () => {
Expand All @@ -128,7 +161,7 @@ describe('AreaChart computeDomainY', () => {
],
'linear'
);
expect(domain).toEqual([1, 20]);
expect(domain).toEqual([0, 20]);
});
});

Expand Down
5 changes: 5 additions & 0 deletions src/area-chart/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export function computeDomainY<T>(series: readonly AreaChartProps.Series<T>[], s
return [];
}

// Linear areas are filled from the zero baseline. This is consistent with the bar and line charts.
if (scaleType === 'linear') {
return [Math.min(min, 0), Math.max(max, 0)];
}

// Log scales can't start from 0, so, if possible, start from 1.
if (scaleType === 'log' && min === 0 && max > 1) {
return [1, max];
Expand Down
Loading