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: selection change & legend click & style #1639

Merged
merged 2 commits into from
Nov 8, 2022
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: 2 additions & 2 deletions packages/f2/src/components/area/areaView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (props) => {
clip: {
type: 'sector',
property: ['endAngle'],
attrs: {
style: {
x: center.x,
y: center.y,
startAngle,
Expand All @@ -34,7 +34,7 @@ export default (props) => {
clip: {
type: 'rect',
property: ['width'],
attrs: {
style: {
x: left,
y: top,
height: height,
Expand Down
1 change: 1 addition & 0 deletions packages/f2/src/components/axis/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface TickLine extends LineStyleProps {

interface Text extends TextStyleProps {
align?: 'left' | 'right' | 'start' | 'center' | 'end' | 'between';
text?: string;
}

// 仅在 bottom 下新增了 align 支持 `between`
Expand Down
20 changes: 18 additions & 2 deletions packages/f2/src/components/geometry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,19 @@ class Geometry<

willReceiveProps(nextProps) {
const { props: lastProps, attrController, justifyContent } = this;
const { data: nextData, adjust: nextAdjust, zoomRange: nextZoomRange, coord } = nextProps;
const { data: lastData, adjust: lastAdjust, zoomRange: lastZoomRange } = lastProps;
const {
data: nextData,
adjust: nextAdjust,
zoomRange: nextZoomRange,
coord,
selection,
} = nextProps;
const {
data: lastData,
adjust: lastAdjust,
zoomRange: lastZoomRange,
selection: lastSelection,
} = lastProps;

const justifyContentCenter = !coord.isCyclic() || justifyContent;

Expand All @@ -90,6 +101,11 @@ class Geometry<
if (!isEqual(nextZoomRange, lastZoomRange)) {
this.records = null;
}

// selection 发生变化
if (!isEqual(selection, lastSelection)) {
super.willReceiveProps(nextProps);
}
}

willMount() {
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/src/components/guide/views/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default (props: TextGuideProps, context) => {
return (
<text
attrs={{
text: content,
text: `${content}`,
x: posX,
y: posY,
...style,
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/src/components/interval/view/polar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default (props) => {
clip: {
type: 'sector',
property: ['endAngle'],
attrs: {
style: {
x: center.x,
y: center.y,
startAngle,
Expand Down
3 changes: 3 additions & 0 deletions packages/f2/src/components/legend/legendView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default (props) => {
nameStyle,
valueStyle,
valuePrefix,
onClick,
} = props;

const formatValue = (value, valuePrefix = ': ') => {
Expand All @@ -54,6 +55,7 @@ export default (props) => {
{items.map((item) => {
const { color, name, value, filtered, tickValue } = item;
const valueText = isFunction(itemFormatter) ? itemFormatter(value, tickValue) : value;

return (
<group
className="legend-item"
Expand All @@ -67,6 +69,7 @@ export default (props) => {
padding: ['6px', '6px', '6px', 0],
}}
data-item={item}
onClick={onClick}
>
<Marker color={filtered ? '#bfbfbf' : color} type={marker} />
<text
Expand Down
78 changes: 29 additions & 49 deletions packages/f2/src/components/legend/withLegend.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { jsx } from '../../index';
import { Component, computeLayout, GroupStyleProps, TextStyleProps } from '@antv/f-engine';
import Chart from '../../chart';
import { find, isFunction } from '@antv/util';
import { isInBBox } from '../../util';
import { isFunction } from '@antv/util';

interface LegendItem {
/**
Expand Down Expand Up @@ -216,7 +215,7 @@ export default (View) => {
}

didMount() {
this._initEvent();
// this._initEvent();
}

willUpdate(): void {
Expand All @@ -225,56 +224,36 @@ export default (View) => {
this.updateCoord();
}

_initEvent() {
const { context, props, container } = this;
_onclick = (item) => {
const { props } = this;
const { chart, clickable = true, onClick } = props;

if (!clickable) return;
const clickItem = item.currentTarget;
if (!clickItem) {
return;
}
// @ts-ignore
const dataItem = clickItem.config['data-item'];
if (!dataItem) {
return;
}
if (isFunction(onClick)) {
onClick(dataItem);
}
const { field, tickValue } = dataItem;

// item 点击事件
context.gesture.on('click', (ev) => {
const { x, y } = ev;

const point = { x, y };
const bbox = container.getBBox();
if (!isInBBox(bbox, point)) {
return;
}

const legendItems = container.getElementsByClassName('legend-item');
if (!legendItems.length) {
return;
}
const clickItem = find(legendItems, (item) => {
const itemBBox = item.getBBox();
return isInBBox(itemBBox, point);
});
if (!clickItem) {
return;
}
// @ts-ignore
const dataItem = clickItem.config['data-item'];
if (!dataItem) {
return;
}
if (isFunction(onClick)) {
onClick(dataItem);
}
const { field, tickValue } = dataItem;

const { filtered: prevFiltered } = this.state;
const filtered = {
...prevFiltered,
[tickValue]: !prevFiltered[tickValue],
};
this.setState({
filtered,
});
chart.filter(field, (value) => {
return !filtered[value];
});
const { filtered: prevFiltered } = this.state;
const filtered = {
...prevFiltered,
[tickValue]: !prevFiltered[tickValue],
};
this.setState({
filtered,
});
}
chart.filter(field, (value) => {
return !filtered[value];
});
};

render() {
const { props, itemWidth, legendStyle } = this;
Expand All @@ -292,6 +271,7 @@ export default (View) => {
...legendStyle,
...props.style,
}}
onClick={this._onclick}
/>
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/f2/src/components/line/lineView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default (props: LineViewProps) => {
clip: {
type: 'sector',
property: ['endAngle'],
attrs: {
style: {
x: center.x,
y: center.y,
startAngle,
Expand All @@ -101,7 +101,7 @@ export default (props: LineViewProps) => {
clip: {
type: 'rect',
property: ['width'],
attrs: {
style: {
x: left,
y: top,
height: height,
Expand Down Expand Up @@ -132,7 +132,7 @@ export default (props: LineViewProps) => {
return (
<polyline
key={key}
attrs={{
style={{
points: fliterPoints.map((point) => {
return [point.x, point.y];
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/f2/src/components/point/pointView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default (props) => {
return (
<circle
key={key}
attrs={{
style={{
cx: x,
cy: y,
fill: shapeName === 'circle' ? color : null,
Expand All @@ -69,7 +69,7 @@ export default (props) => {
update: {
easing: 'linear',
duration: 450,
property: ['x', 'y', 'r', 'fill'],
property: ['cx', 'cy', 'r', 'fill'],
},
},
animation
Expand Down
5 changes: 3 additions & 2 deletions packages/f2/src/components/tooltip/tooltipView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ export default class TooltipView extends Component {
Math.max(x - coordLeft - halfWidth, -arrowWidth - radius),
coordWidth - width + arrowWidth + radius
);

// 因为默认是从 coord 的范围内显示的,所以要往上移,移出 coord,避免挡住 geometry
const offset = Math.min(y, height + arrowWidth); // 因为不能超出 canvas 画布区域,所以最大只能是 y
rect.translate(offsetX, -offset);

rect.setLocalPosition(offsetX, -offset);

arrowRef.current.translate(0, height - offset);
}
didMount() {
Expand Down
30 changes: 0 additions & 30 deletions packages/f2/test/base/equal.test.tsx

This file was deleted.

1 change: 1 addition & 0 deletions packages/f2/test/canvas/lifecycle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe('Canvas', () => {
['componentRender'],
['componentDidUpdate'],
['containerDidUpdate'],
['componentShouldUpdate'],
['componentWillUpdate'],
['componentRender'],
['componentDidUpdate'],
Expand Down
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.
5 changes: 3 additions & 2 deletions packages/f2/test/components/canvas/animation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ describe('Canvas', () => {
);

const canvas = new Canvas(props);
const testComponent = canvas.children;
await delay(100);
canvas.render();
await canvas.render();
await delay(0);

// @ts-ignore
const rect = canvas.children.component.container.children[0];

expect(rect.getAttribute('width')).toBe(10);
Expand Down
1 change: 1 addition & 0 deletions packages/f2/test/components/canvas/canvas.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Canvas', () => {

expect(testComponent).toBe(Test);

// @ts-ignore
const rect = canvas.children.component.container.children[0];

expect(rect.config.type).toBe('rect');
Expand Down
1 change: 1 addition & 0 deletions packages/f2/test/components/canvas/px2hd.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('Canvas', () => {

expect(testComponent).toBe(Test);

// @ts-ignore
const rect = canvas.children.component.container.children[0];
expect(rect.config.type).toBe('rect');
expect(rect.getAttribute('fill')).toBe('red');
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/test/components/legend/pie.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('图例 - position', () => {
);

const canvas = new Canvas(props);
canvas.render();
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/f2/test/components/line/line.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ describe('折线图', () => {
align: 'center',
textAlign: 'start',
textBaseline: 'middle',
rotate: Math.PI / 2,
transform: 'rotate(90deg)',
},
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/f2/test/components/line/radar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('雷达图', () => {
);

const canvas = new Canvas(props);
canvas.render();
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion packages/f2/test/components/tooltip/tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ describe('tooltip', () => {

const canvas = new Canvas(props);
await canvas.render();
await delay(200);

const newChart = (
<Chart data={data}>
Expand All @@ -216,7 +217,7 @@ describe('tooltip', () => {
expect(context).toMatchImageSnapshot();
});

it('Tooltip 默认展示更新(新增图形元素导致的坐标变动)', async () => {
it.skip('Tooltip 默认展示更新(新增图形元素导致的坐标变动)', async () => {
const context = createContext('Tooltip 默认展示更新(新增图形元素导致的坐标变动)');
const { props } = (
<Canvas context={context} pixelRatio={1}>
Expand All @@ -229,6 +230,7 @@ describe('tooltip', () => {

const canvas = new Canvas(props);
await canvas.render();
await delay(1000);
const newChart = (
<Chart data={data}>
<Interval x="genre" y="sold" color="genre" />
Expand Down
Loading