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

fix(grid,xychart): account for scale bandwidth in offsets #1181

Merged
merged 3 commits into from
Apr 22, 2021
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
4 changes: 3 additions & 1 deletion packages/visx-grid/src/grids/GridColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Group } from '@visx/group';
import { Point } from '@visx/point';
import { getTicks, ScaleInput, coerceNumber } from '@visx/scale';
import { CommonGridProps, GridScale } from '../types';
import getScaleBandwidth from '../utils/getScaleBandwidth';

export type GridColumnsProps<Scale extends GridScale> = CommonGridProps & {
/** `@visx/scale` or `d3-scale` object used to convert value to position. */
Expand Down Expand Up @@ -41,8 +42,9 @@ export default function GridColumns<Scale extends GridScale>({
...restProps
}: AllGridColumnsProps<Scale>) {
const ticks = tickValues ?? getTicks(scale, numTicks);
const scaleOffset = (offset ?? 0) + getScaleBandwidth(scale) / 2;
const tickLines = ticks.map(d => {
const x = offset ? (coerceNumber(scale(d)) || 0) + offset : coerceNumber(scale(d)) || 0;
const x = (coerceNumber(scale(d)) ?? 0) + scaleOffset;
return {
from: new Point({
x,
Expand Down
4 changes: 3 additions & 1 deletion packages/visx-grid/src/grids/GridRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Group } from '@visx/group';
import { Point } from '@visx/point';
import { getTicks, ScaleInput, coerceNumber } from '@visx/scale';
import { CommonGridProps, GridScale } from '../types';
import getScaleBandwidth from '../utils/getScaleBandwidth';

export type GridRowsProps<Scale extends GridScale> = CommonGridProps & {
/** `@visx/scale` or `d3-scale` object used to convert value to position. */
Expand Down Expand Up @@ -41,8 +42,9 @@ export default function GridRows<Scale extends GridScale>({
...restProps
}: AllGridRowsProps<Scale>) {
const ticks = tickValues ?? getTicks(scale, numTicks);
const scaleOffset = (offset ?? 0) + getScaleBandwidth(scale) / 2;
const tickLines = ticks.map(d => {
const y = offset ? (coerceNumber(scale(d)) || 0) + offset : coerceNumber(scale(d)) || 0;
const y = (coerceNumber(scale(d)) ?? 0) + scaleOffset;
return {
from: new Point({
x: 0,
Expand Down
5 changes: 5 additions & 0 deletions packages/visx-grid/src/utils/getScaleBandwidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { GridScale } from '../types';

export default function getScaleBandwidth(scale: GridScale) {
return 'bandwidth' in scale ? scale.bandwidth() : 0;
}
29 changes: 28 additions & 1 deletion packages/visx-grid/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { scaleLinear, scaleBand } from '@visx/scale';
import polarToCartesian from '../src/utils/polarToCartesian';
import getScaleBandwidth from '../src/utils/getScaleBandwidth';

describe('GridUtils', () => {
describe('grid utils', () => {
describe('polarToCartesian', () => {
const config = {
radius: 20,
Expand All @@ -14,4 +16,29 @@ describe('GridUtils', () => {
expect(polarToCartesian(config)).toEqual(expected);
});
});

describe('getScaleBandwidth', () => {
it('should return 0 for non-band scales', () => {
expect(
getScaleBandwidth(
scaleLinear({
range: [0, 90],
domain: [0, 100],
}),
),
).toBe(0);
});

it('should return the size of the band for band scales', () => {
expect(
getScaleBandwidth(
scaleBand({
range: [0, 90],
domain: ['a', 'b', 'c'],
padding: 0,
}),
),
).toBe(30);
});
});
});