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: 为interval添加最小高度配置 #5715

Merged
merged 10 commits into from
Nov 7, 2023
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions __tests__/plots/static/alphanbet-interval-min-height-transposed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalMinHeightTransposed(): G2Spec {
return {
type: 'interval',
coordinate: { transform: [{ type: 'transpose' }] },
data: [
{ genre: 'Sports', sold: 0 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: {
x: "genre",
y: "sold",
color: "genre"
},
axis: {
x: { animate: false },
y: { animate: false }
},
style: {
draggable: true,
droppable: true,
minHeight: 50
}
};
}
28 changes: 28 additions & 0 deletions __tests__/plots/static/alphanbet-interval-min-height.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalMinHeight(): G2Spec {
return {
type: 'interval',
data: [
{ genre: 'Sports', sold: 0 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: {
x: "genre",
y: "sold",
color: "genre"
},
axis: {
x: { animate: false },
y: { animate: false }
},
style: {
draggable: true,
droppable: true,
minHeight: 50
}
};
}
2 changes: 2 additions & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export { alphabetIntervalMaxWidth } from './alphabet-interval-max-width';
export { alphabetIntervalMinWidth } from './alphabet-interval-min-width';
export { alphabetIntervalMaxWidthTransposed } from './alphabet-interval-max-width-transposed';
export { alphabetIntervalMinWidthTransposed } from './alphabet-interval-min-width-transposed';
export { alphabetIntervalMinHeight } from './alphanbet-interval-min-height'
export { alphabetIntervalMinHeightTransposed } from './alphanbet-interval-min-height-transposed'
export { alphabetIntervalTitle } from './alphabet-interval-title';
export { alphabetIntervalLabelOverflowHide } from './alphabet-interval-label-overflow-hide';
export { alphabetIntervalLabelContrastReverse } from './alphabet-interval-label-contrast-reverse';
Expand Down
1 change: 1 addition & 0 deletions site/docs/spec/mark/interval.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ chart.render();
| ---------------------- | ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | --------- |
| minWidth | 柱子的最小宽度,单位为像素 | `number` | `-Infinity` |
| maxWidth | 柱子的最大宽度,单位为像素 | `number` | `Infinity` |
| minHeight | 柱子的最小高度,单位为像素 | `number` | `-Infinity` |
| radius | 外层矩形的四个圆角大小 | `number` \| `Function<number>` | 0 |
| radiusTopLeft | 外层左上角的圆角 | `number` \| `Function<number>` | 0 |
| radiusTopRight | 外层右上角的圆角 | `number` \| `Function<number>` | 0 |
Expand Down
20 changes: 20 additions & 0 deletions site/examples/general/interval/demo/column-minHeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Chart } from '@antv/g2';

const chart = new Chart({ container: 'container' });
chart.data([
{ genre: 'Sports', sold: 0 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
]);

chart
.interval()
.encode('x', 'genre')
.encode('y', 'sold')
.encode('color', 'genre')
.axis({ x: { animate: false }, y: { animate: false } })
.style('draggable', true)
.style('droppable', true)
.style('minHeight', 50);
18 changes: 14 additions & 4 deletions src/shape/interval/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export type ColorOptions = {
* Maximum width of each interval.
*/
maxWidth?: number;

/**
* Minimum height of each interval.
*/
minHeight?: number;

[key: string]: any;
};

Expand All @@ -40,6 +46,7 @@ export function rect(
radiusTopRight = radius,
minWidth = -Infinity,
maxWidth = Infinity,
minHeight = -Infinity,
...rest
} = style;
if (!isPolar(coordinate) && !isHelix(coordinate)) {
Expand All @@ -51,6 +58,7 @@ export function rect(
// Deal with width or height is negative.
const absX = width > 0 ? x : x + width;
const absY = height > 0 ? y : y + height;

const absWidth = Math.abs(width);
const absHeight = Math.abs(height);
const finalX = absX + insetLeft;
Expand All @@ -59,13 +67,13 @@ export function rect(
const finalHeight = absHeight - (insetTop + insetBottom);

const clampWidth = tpShape
? finalWidth
? clamp(finalWidth, minHeight, Infinity)
: clamp(finalWidth, minWidth, maxWidth);
const clampHeight = tpShape
? clamp(finalHeight, minWidth, maxWidth)
: finalHeight;
const clampX = finalX - (clampWidth - finalWidth) / 2;
const clampY = finalY - (clampHeight - finalHeight) / 2;
: clamp(finalHeight, minHeight, Infinity);
const clampX = tpShape ? finalX : finalX - (clampWidth - finalWidth) / 2;
const clampY = tpShape ? finalY - (clampHeight - finalHeight ) / 2 : finalY - (clampHeight - finalHeight);

return select(document.createElement('rect', {}))
.style('x', clampX)
Expand Down Expand Up @@ -145,6 +153,7 @@ export const Color: SC<ColorOptions> = (options, context) => {
insetTop = inset,
minWidth,
maxWidth,
minHeight,
...rest
} = style;
const { color = defaultColor, opacity } = value;
Expand Down Expand Up @@ -179,6 +188,7 @@ export const Color: SC<ColorOptions> = (options, context) => {
insetTop,
minWidth,
maxWidth,
minHeight
};

return (
Expand Down
10 changes: 10 additions & 0 deletions src/shape/interval/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export type RectOptions = {
* Maximum width of each interval.
*/
maxWidth?: number;

/**
* Minimum height of each interval.
*/
minHeight?: number;

/**
* Minimum height of each interval.
*/
maxHeight?: number;
pearmini marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
Loading