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(slider): auto layout #5274

Merged
merged 1 commit into from
Jul 6, 2023
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
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.
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.
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.
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.
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified __tests__/integration/snapshots/static/aaplLineSlider.png
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.
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.
Binary file modified __tests__/integration/snapshots/static/weatherLineMultiSlider.png
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/alphabet-interval-auto-padding-slider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalAutoPaddingSlider(): G2Spec {
return {
type: 'interval',
padding: 'auto',
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
axis: {
y: { labelFormatter: (d) => d + '0000' },
x: { labelFormatter: (d) => d + '0000' },
},
slider: { x: true, y: true },
viewStyle: {
viewFill: '#4e79a7',
plotFill: '#f28e2c',
mainFill: '#e15759',
contentFill: '#76b7b2',
},
};
}
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,4 @@ export { mockIntervalFacetRectPolar } from './mock-interval-facet-rect-polar';
export { alphabetIntervalAutoRotate } from './alphabet-interval-auto-padding-rotate';
export { mockLineSmallInterval } from './mock-line-small-interval';
export { unemploymentAreaStackedLegendSize } from './unemployment-area-stacked-legend-size';
export { alphabetIntervalAutoPaddingSlider } from './alphabet-interval-auto-padding-slider';
28 changes: 22 additions & 6 deletions src/component/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,32 @@ export type SliderOptions = {
[key: string]: any;
};

function inferPosition(bbox, position, trackSize) {
const { x, y, width, height } = bbox;
if (position === 'left') return [x + width - trackSize, y];
if (position === 'right') return [x, y];
if (position === 'bottom') return [x, y];
if (position === 'top') return [x, y + height - trackSize];
}

/**
* Slider component.
*/
export const Slider: GCC<SliderOptions> = (options) => {
// do not pass size.
const { orientation, labelFormatter, size, style, position, ...rest } =
options;
const {
orientation,
labelFormatter,
size,
style = {},
position,
...rest
} = options;

return ({ scales: [scale], value, theme, coordinate }) => {
const { bbox } = value;
const { x, y, width, height } = bbox;

const { width, height } = bbox;
const { slider: sliderTheme = {} } = theme;
const defaultFormatter = scale.getFormatter?.() || ((v) => v.toString());
const formatter =
Expand All @@ -29,12 +44,13 @@ export const Slider: GCC<SliderOptions> = (options) => {

const isHorizontal = orientation === 'horizontal';
const reverse = isTranspose(coordinate) && isHorizontal;

const { trackSize = sliderTheme.trackSize } = style;
const [x0, y0] = inferPosition(bbox, position, trackSize);
return new SliderComponent({
className: 'slider',
style: Object.assign({}, sliderTheme, {
x,
y,
x: x0,
y: y0,
trackLength: isHorizontal ? width : height,
orientation,
formatter: (v) => {
Expand Down
11 changes: 9 additions & 2 deletions src/interaction/sliderFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export function SliderFilter({
if (!sliders.length) return () => {};

let filtering = false;
const { scale, coordinate } = view;
const { scale, coordinate, layout } = view;
const { paddingLeft, paddingTop, paddingBottom, paddingRight } = layout;
const { x: scaleX, y: scaleY } = scale;
const transposed = isTranspose(coordinate);

Expand Down Expand Up @@ -159,7 +160,13 @@ export function SliderFilter({
});
}

await update(newOptions);
await update({
...newOptions,
paddingLeft,
paddingTop,
paddingBottom,
paddingRight,
});
filtering = false;
},
wait,
Expand Down
18 changes: 18 additions & 0 deletions src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ export function computeComponentSize(
if (t.startsWith('group')) return computeGroupSize;
if (t.startsWith('legendContinuous')) return computeContinuousLegendSize;
if (t === 'legendCategory') return computeCategoryLegendSize;
if (t.startsWith('slider')) return computeSliderSize;
return () => {};
};
return createCompute()(
Expand Down Expand Up @@ -604,6 +605,23 @@ function computeGroupSize(
children.forEach((d) => (d.size = maxSize));
}

function computeSliderSize(
component: G2GuideComponentOptions,
crossSize: number,
crossPadding: [number, number],
position: GCP,
theme: G2Theme,
library: G2Library,
) {
const styleOf = () => {
const { slider } = theme;
return deepMix({}, slider, component.style);
};
const { trackSize, handleIconSize } = styleOf();
const size = Math.max(trackSize, handleIconSize * 2.4);
component.size = size;
}

function computeAxisSize(
component: G2GuideComponentOptions,
crossSize: number,
Expand Down