Skip to content

Commit

Permalink
feat(interaction): add elementSelect, elementSelectByX, elementSelect…
Browse files Browse the repository at this point in the history
…ByColor (#4219)
  • Loading branch information
pearmini committed Oct 25, 2022
1 parent 488e34b commit a53bba9
Show file tree
Hide file tree
Showing 37 changed files with 444 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { G2Spec, ELEMENT_CLASS_NAME } from '../../../src';
import { step } from './utils';

export function alphabetIntervalSelectSingle(): G2Spec {
return {
type: 'interval',
padding: 0,
transform: [{ type: 'sortX', by: 'y', reverse: true }],
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: false,
legend: false,
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
interaction: [
{
type: 'elementSelect',
selectedFill: 'red',
unselectedOpacity: 0.6,
single: true,
},
],
};
}

alphabetIntervalSelectSingle.steps = ({ canvas }) => {
const { document } = canvas;
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
const [e, e1] = elements;
return [step(e, 'click'), step(e1, 'click'), step(e1, 'click')];
};
42 changes: 42 additions & 0 deletions __tests__/integration/interactions/alphabet-interval-select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
G2Spec,
ELEMENT_CLASS_NAME,
MAIN_LAYER_CLASS_NAME,
} from '../../../src';
import { step } from './utils';

export function alphabetIntervalSelect(): G2Spec {
return {
type: 'interval',
padding: 0,
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: false,
legend: false,
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
interaction: [
{ type: 'elementSelect', selectedFill: 'red', unselectedOpacity: 0.6 },
],
};
}

alphabetIntervalSelect.steps = ({ canvas }) => {
const { document } = canvas;
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
const [mainLayer] = document.getElementsByClassName(MAIN_LAYER_CLASS_NAME);
const [e, e1] = elements;
return [
step(e, 'click'),
step(e1, 'click'),
step(e1, 'click'),
step(e, 'click'),
step(e, 'click'),
step(mainLayer, 'click'), // Reset the canvas.
];
};
5 changes: 5 additions & 0 deletions __tests__/integration/interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export { alphabetIntervalActiveInactive } from './alphabet-interval-active-inact
export { stateAgesIntervalActiveByColorLinked } from './stateages-interval-active-by-color-linked';
export { stateAgesIntervalActiveByColor } from './stateages-interval-active-by-color';
export { stateAgesIntervalActiveByX } from './stateages-interval-active-by-x';
export { alphabetIntervalSelect } from './alphabet-interval-select';
export { alphabetIntervalSelectSingle } from './alphabet-interval-select-single';
export { stateAgesIntervalSelectByColor } from './stateages-interval-select-by-color';
export { stateAgesIntervalSelectByColorSingle } from './stateages-interval-select-by-color-single';
export { stateAgesIntervalSelectByX } from './stateages-interval-select-by-x';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
G2Spec,
ELEMENT_CLASS_NAME,
MAIN_LAYER_CLASS_NAME,
} from '../../../src';
import { step } from './utils';

export function stateAgesIntervalSelectByColorSingle(): G2Spec {
return {
type: 'interval',
padding: 0,
axis: false,
legend: false,
width: 800,
transform: [
{ type: 'stackY' },
{ type: 'sortX', by: 'y', reverse: true, slice: 5 },
],
data: {
type: 'fetch',
value: 'data/stateages.csv',
format: 'csv',
},
encode: {
x: 'state',
y: 'population',
color: 'age',
},
scale: {
x: { paddingInner: 0.2 },
},
interaction: [
{
type: 'elementSelectByColor',
link: true,
linkFill: (d) => (d.state === 'CA' ? 'red' : undefined),
selectedStroke: '#000',
selectedStrokeWidth: 1,
unselectedOpacity: 0.6,
linkFillOpacity: 0.5,
single: true,
},
],
};
}

stateAgesIntervalSelectByColorSingle.steps = ({ canvas }) => {
const { document } = canvas;
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
const [mainLayer] = document.getElementsByClassName(MAIN_LAYER_CLASS_NAME);
const [e1] = elements;
const e2 = elements[elements.length - 1];
return [
step(e1, 'click'),
step(e1, 'click'),
step(e1, 'click'),
step(e2, 'click'),
step(mainLayer, 'click'),
];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { G2Spec, ELEMENT_CLASS_NAME } from '../../../src';
import { step } from './utils';

export function stateAgesIntervalSelectByColor(): G2Spec {
return {
type: 'interval',
padding: 0,
axis: false,
legend: false,
width: 800,
transform: [
{ type: 'stackY' },
{ type: 'sortX', by: 'y', reverse: true, slice: 5 },
],
data: {
type: 'fetch',
value: 'data/stateages.csv',
format: 'csv',
},
encode: {
x: 'state',
y: 'population',
color: 'age',
},
scale: {
x: { paddingInner: 0.2 },
},
interaction: [
{
type: 'elementSelectByColor',
link: true,
linkFill: (d) => (d.state === 'CA' ? 'red' : undefined),
selectedStroke: '#000',
selectedStrokeWidth: 1,
unselectedOpacity: 0.6,
linkFillOpacity: 0.5,
},
],
};
}

stateAgesIntervalSelectByColor.steps = ({ canvas }) => {
const { document } = canvas;
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
const [e1] = elements;
const e2 = elements[elements.length - 1];
return [
step(e1, 'click'),
step(e2, 'click'),
step(e1, 'click'),
step(e2, 'click'),
];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { G2Spec, ELEMENT_CLASS_NAME } from '../../../src';
import { step } from './utils';

export function stateAgesIntervalSelectByX(): G2Spec {
return {
type: 'interval',
padding: 0,
transform: [
{ type: 'sortX', by: 'y', reverse: true, reducer: 'sum', slice: 6 },
{ type: 'dodgeX' },
],
data: {
type: 'fetch',
value: 'data/stateages.csv',
},
axis: false,
legend: false,
encode: {
x: 'state',
y: 'population',
color: 'age',
},
interaction: [
{
type: 'elementSelectByX',
selectedFill: 'red',
unselectedOpacity: 0.6,
},
],
};
}

stateAgesIntervalSelectByX.steps = ({ canvas }) => {
const { document } = canvas;
const elements = document.getElementsByClassName(ELEMENT_CLASS_NAME);
const [e1] = elements;
return [step(e1, 'click')];
};
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.
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.
6 changes: 6 additions & 0 deletions __tests__/unit/stdlib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ import {
ElementActive,
ElementActiveByColor,
ElementActiveByX,
ElementSelect,
ElementSelectByColor,
ElementSelectByX,
// ElementSelected,
// Tooltip,
// Fisheye as FisheyeInteraction,
Expand Down Expand Up @@ -418,6 +421,9 @@ describe('stdlib', () => {
'interaction.elementActive': ElementActive,
'interaction.elementActiveByX': ElementActiveByX,
'interaction.elementActiveByColor': ElementActiveByColor,
'interaction.elementSelect': ElementSelect,
'interaction.elementSelectByX': ElementSelectByX,
'interaction.elementSelectByColor': ElementSelectByColor,
// 'interaction.elementSelected': ElementSelected,
// 'interaction.elementHighlight': ElementHighlight,
// 'interaction.elementHighlightByX': ElementHighlightByX,
Expand Down
5 changes: 4 additions & 1 deletion src/interaction/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// export { createInteraction } from './create';
// export { createInteractionLibrary } from './stdlib';

export { ElementActive } from './native/elementActive';
export { ElementActiveByX } from './native/elementActiveByX';
export { ElementActiveByColor } from './native/elementActiveByColor';
export { ElementSelect } from './native/elementSelect';
export { ElementSelectByX } from './native/elementSelectByX';
export { ElementSelectByColor } from './native/elementSelectByColor';

// export { ElementSelected } from './builtin/elementSelected';
// export { Tooltip } from './builtin/tooltip';
// export { Fisheye } from './builtin/fisheye';
Expand Down
2 changes: 0 additions & 2 deletions src/interaction/native/elementActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
applyDefaultsActiveStyle,
} from './utils';

export const LINK_CLASS_NAME = 'element-link';

/**
* Active a group of elements.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/interaction/native/elementActiveByColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
applyDefaultsActiveStyle,
createDatumof,
selectG2Elements,
createColorKey,
} from './utils';
import { elementActive } from './elementActive';

Expand All @@ -16,7 +17,7 @@ export function ElementActiveByColor(options) {
...applyDefaultsActiveStyle(options),
elements: selectG2Elements,
datum: createDatumof(view),
groupKey: (element) => element.__data__.color,
groupKey: createColorKey(view),
});
};
}
7 changes: 2 additions & 5 deletions src/interaction/native/elementActiveByX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
applyDefaultsActiveStyle,
createDatumof,
selectG2Elements,
createXKey,
} from './utils';
import { elementActive } from './elementActive';

Expand All @@ -14,15 +15,11 @@ export function ElementActiveByX(
) {
return (context) => {
const { container, view } = context;
const { x: scaleX } = context.view.scale;
return elementActive(container, {
...applyDefaultsActiveStyle(options),
elements: selectG2Elements,
datum: createDatumof(view),
groupKey: (element) => {
const { x } = element.__data__;
return scaleX.invert(x);
},
groupKey: createXKey(view),
});
};
}
Loading

0 comments on commit a53bba9

Please sign in to comment.