Skip to content

Commit

Permalink
fix: transposed为false时饼图交互
Browse files Browse the repository at this point in the history
  • Loading branch information
xuying.xu committed Feb 5, 2024
1 parent c26957b commit 8d05a6a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 7 deletions.
37 changes: 31 additions & 6 deletions packages/f2/src/components/geometry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,24 @@ class Geometry<
});
}

_getXSnapRecords(invertPointX, records) {
const xScale = this.getXScale();
const { field: xField } = xScale;
const xValue = xScale.invert(invertPointX);
// category
if (xScale.isCategory) {
return records.filter((record) => record[FIELD_ORIGIN][xField] === xValue);
}
// linear
return records.filter((record) => {
const rangeX = record[xField];
if (rangeX[0] <= xValue && rangeX[1] >= xValue) {
return true;
}
return false;
});
}

// 把 records 拍平
flatRecords() {
const { records } = this;
Expand All @@ -685,7 +703,7 @@ class Geometry<
getSnapRecords(point, inCoordRange?): any[] {
const { props } = this;
const { coord, adjust } = props;

console.log(point);
const invertPoint = coord.invertPoint(point);
const xScale = this.getXScale();
const yScale = this.getYScale();
Expand Down Expand Up @@ -721,11 +739,18 @@ class Geometry<
};

// 处理饼图
if (adjust === 'stack' && coord.isPolar && coord.transposed) {
// 弧度在半径范围内
if (invertPoint.x >= 0 && invertPoint.x <= 1) {
const snapRecords = this._getYSnapRecords(invertPoint.y, records);
return snapRecords;
if (adjust === 'stack' && coord.isPolar) {
if (coord.transposed) {
// 弧度在半径范围内
if (invertPoint.x >= 0 && invertPoint.x <= 1) {
const snapRecords = this._getYSnapRecords(invertPoint.y, records);
return snapRecords;
}
} else {
if (invertPoint.y >= 0 && invertPoint.y <= 1) {
const snapRecords = this._getXSnapRecords(invertPoint.x, records);
return snapRecords;
}
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 90 additions & 1 deletion packages/f2/test/components/interval/example/doughnut.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { jsx } from '../../../../src';
import { Polar, Rect } from '../../../../src/coord';
import { Canvas, Chart } from '../../../../src';
import { Interval, Axis, Legend, Tooltip, ArcGuide, TextGuide } from '../../../../src/components';
import { createContext, delay } from '../../../util';
import { createContext, delay, gestureSimulator } from '../../../util';

describe('环形图', () => {
it('基础环形图', async () => {
Expand Down Expand Up @@ -52,6 +52,15 @@ describe('环形图', () => {
field: 'name',
range: ['#FE5D4D', '#3BA4FF', '#737DDE'],
}}
selection={{
selectedStyle: (value) => {
// console.log('selectedStyle', value);
return {
// 半径放大 1.1 倍
r: value.yMax * 1.1,
};
},
}}
/>
<Legend
position="right"
Expand Down Expand Up @@ -233,4 +242,84 @@ describe('环形图', () => {
await delay(1000);
expect(context).toMatchImageSnapshot();
});

it('玫瑰环形图', async () => {
const data = [
{
name: '股票类',
percent: 10,
a: [0, 83],
},
{
name: '债券类',
percent: 40,
a: [83, 110],
},
{
name: '现金类',
percent: 80,
a: [110, 134],
},
];

const map = {};
data.forEach(function(obj) {
map[obj.name] = obj.percent + '%';
});

const context = createContext('基础环形图');
const chartRef = { current: null };
const { type, props } = (
<Canvas context={context} pixelRatio={1}>
<Chart
ref={chartRef}
data={data}
coord={{
type: 'polar',
transposed: false,
innerRadius: 0.7,
radius: 1,
}}
scale={{
a: {
type: 'linear',
nice: false,
},
}}
>
<Interval
x="a"
y="percent"
adjust="stack"
color={{
field: 'name',
range: ['#FE5D4D', '#3BA4FF', '#737DDE'],
}}
selection={{
selectedStyle: (value) => {
return {
r: value.yMax * 1.1,
};
},
}}
/>
<Legend
position="right"
itemFormatter={(value, name) => {
return map[name];
}}
valuePrefix=" "
/>
</Chart>
</Canvas>
);

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

await delay(1000);
await gestureSimulator(context.canvas, 'click', { x: 133, y: 64 });
await delay(200);
expect(context).toMatchImageSnapshot();
});
});

0 comments on commit 8d05a6a

Please sign in to comment.