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: stack模式下scale设置max,min失效 #1978

Merged
merged 1 commit into from
Jun 14, 2024
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
29 changes: 2 additions & 27 deletions packages/f2/src/components/geometry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,9 @@ class Geometry<
const { chart, startOnZero = defaultStartOnZero, coord, adjust } = props;
const { isPolar, transposed } = coord;
const { y } = attrs;
const yField = y.field;

// 如果从 0 开始,只调整 y 轴 scale
if (startOnZero) {
const { y } = attrs;
chart.scale.adjustStartZero(y.scale);
}
// 饼图的scale调整,关闭nice
Expand All @@ -286,12 +285,11 @@ class Geometry<
transposed &&
(adjust === 'stack' || (adjust as AdjustProps)?.type === 'stack')
) {
const { y } = attrs;
chart.scale.adjustPieScale(y.scale);
}

if (adjust === 'stack' || (adjust as AdjustProps)?.type === 'stack') {
this._updateStackRange(yField, y.scale, this.dataArray);
chart.scale._updateStackRange(y.scale, flatten(this.dataArray));
}
}

Expand Down Expand Up @@ -391,29 +389,6 @@ class Geometry<
return adjustData;
}

_updateStackRange(field, scale, dataArray) {
const flattenArray = flatten(dataArray);
let min = Infinity;
let max = -Infinity;
for (let i = 0, len = flattenArray.length; i < len; i++) {
const obj = flattenArray[i];
const tmpMin = Math.min.apply(null, obj[field]);
const tmpMax = Math.max.apply(null, obj[field]);
if (tmpMin < min) {
min = tmpMin;
}
if (tmpMax > max) {
max = tmpMax;
}
}
if (min !== scale.min || max !== scale.max) {
scale.change({
min,
max,
});
}
}

_processData() {
const { props } = this;
const { data: originData } = props;
Expand Down
31 changes: 31 additions & 0 deletions packages/f2/src/controller/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ class ScaleController {
});
}

//堆叠下的scale调整
_updateStackRange(scale: Scale, flattenArray) {
const { options } = this;
const { field } = scale;
const option = options[field];

let dataMin = Infinity;
let dataMax = -Infinity;
for (let i = 0, len = flattenArray.length; i < len; i++) {
const obj = flattenArray[i];
const tmpMin = Math.min.apply(null, obj[field]);
const tmpMax = Math.max.apply(null, obj[field]);
if (tmpMin < dataMin) {
dataMin = tmpMin;
}
if (tmpMax > dataMax) {
dataMax = tmpMax;
}
}

// 如果有定义,则优先级更高
const min = option?.min || dataMin;
const max = option?.max || dataMax;
if (min !== scale.min || max !== scale.max) {
scale.change({
min,
max,
});
}
}

// 获取scale 在 0点对位置的值
getZeroValue(scale) {
const { min, max } = scale;
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.
110 changes: 110 additions & 0 deletions packages/f2/test/components/interval/example/column.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,116 @@ describe('柱状图示例', () => {
expect(context).toMatchImageSnapshot();
});

it('堆叠柱图-max', async () => {
const context = createContext('层叠柱图', {
height: '300px',
width: '400px',
});
const data = [
{
name: 'London',
月份: 'Jan.',
月均降雨量: 18.9,
},
{
name: 'London',
月份: 'Feb.',
月均降雨量: 28.8,
},
{
name: 'London',
月份: 'Mar.',
月均降雨量: 39.3,
},
{
name: 'London',
月份: 'Apr.',
月均降雨量: 81.4,
},
{
name: 'London',
月份: 'May.',
月均降雨量: 47,
},
{
name: 'London',
月份: 'Jun.',
月均降雨量: 20.3,
},
{
name: 'London',
月份: 'Jul.',
月均降雨量: 24,
},
{
name: 'London',
月份: 'Aug.',
月均降雨量: 35.6,
},
{
name: 'Berlin',
月份: 'Jan.',
月均降雨量: 12.4,
},
{
name: 'Berlin',
月份: 'Feb.',
月均降雨量: 23.2,
},
{
name: 'Berlin',
月份: 'Mar.',
月均降雨量: 34.5,
},
{
name: 'Berlin',
月份: 'Apr.',
月均降雨量: 99.7,
},
{
name: 'Berlin',
月份: 'May.',
月均降雨量: 52.6,
},
{
name: 'Berlin',
月份: 'Jun.',
月均降雨量: 35.5,
},
{
name: 'Berlin',
月份: 'Jul.',
月均降雨量: 37.4,
},
{
name: 'Berlin',
月份: 'Aug.',
月均降雨量: 42.4,
},
];
const { type, props } = (
<Canvas context={context} pixelRatio={1}>
<Chart
data={data}
scale={{
月均降雨量: {
max: 100,
},
}}
>
<Axis field="月份" />
<Axis field="月均降雨量" />
<Interval x="月份" y="月均降雨量" color="name" adjust="stack" />
</Chart>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();

await delay(1000);
expect(context).toMatchImageSnapshot();
});

it('百分比层叠柱图', async () => {
const context = createContext('百分比层叠柱图', {
height: '300px',
Expand Down
Loading