Skip to content

Commit 89dd5a5

Browse files
author
xuying.xu
committed
fix: stack scale
1 parent c58dd31 commit 89dd5a5

File tree

5 files changed

+143
-27
lines changed

5 files changed

+143
-27
lines changed

packages/f2/src/components/geometry/index.tsx

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,9 @@ class Geometry<
274274
const { chart, startOnZero = defaultStartOnZero, coord, adjust } = props;
275275
const { isPolar, transposed } = coord;
276276
const { y } = attrs;
277-
const yField = y.field;
277+
278278
// 如果从 0 开始,只调整 y 轴 scale
279279
if (startOnZero) {
280-
const { y } = attrs;
281280
chart.scale.adjustStartZero(y.scale);
282281
}
283282
// 饼图的scale调整,关闭nice
@@ -286,12 +285,11 @@ class Geometry<
286285
transposed &&
287286
(adjust === 'stack' || (adjust as AdjustProps)?.type === 'stack')
288287
) {
289-
const { y } = attrs;
290288
chart.scale.adjustPieScale(y.scale);
291289
}
292290

293291
if (adjust === 'stack' || (adjust as AdjustProps)?.type === 'stack') {
294-
this._updateStackRange(yField, y.scale, this.dataArray);
292+
chart.scale._updateStackRange(y.scale, flatten(this.dataArray));
295293
}
296294
}
297295

@@ -391,29 +389,6 @@ class Geometry<
391389
return adjustData;
392390
}
393391

394-
_updateStackRange(field, scale, dataArray) {
395-
const flattenArray = flatten(dataArray);
396-
let min = Infinity;
397-
let max = -Infinity;
398-
for (let i = 0, len = flattenArray.length; i < len; i++) {
399-
const obj = flattenArray[i];
400-
const tmpMin = Math.min.apply(null, obj[field]);
401-
const tmpMax = Math.max.apply(null, obj[field]);
402-
if (tmpMin < min) {
403-
min = tmpMin;
404-
}
405-
if (tmpMax > max) {
406-
max = tmpMax;
407-
}
408-
}
409-
if (min !== scale.min || max !== scale.max) {
410-
scale.change({
411-
min,
412-
max,
413-
});
414-
}
415-
}
416-
417392
_processData() {
418393
const { props } = this;
419394
const { data: originData } = props;

packages/f2/src/controller/scale.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,37 @@ class ScaleController {
241241
});
242242
}
243243

244+
//堆叠下的scale调整
245+
_updateStackRange(scale: Scale, flattenArray) {
246+
const { options } = this;
247+
const { field } = scale;
248+
const option = options[field];
249+
250+
let dataMin = Infinity;
251+
let dataMax = -Infinity;
252+
for (let i = 0, len = flattenArray.length; i < len; i++) {
253+
const obj = flattenArray[i];
254+
const tmpMin = Math.min.apply(null, obj[field]);
255+
const tmpMax = Math.max.apply(null, obj[field]);
256+
if (tmpMin < dataMin) {
257+
dataMin = tmpMin;
258+
}
259+
if (tmpMax > dataMax) {
260+
dataMax = tmpMax;
261+
}
262+
}
263+
264+
// 如果有定义,则优先级更高
265+
const min = option?.min || dataMin;
266+
const max = option?.max || dataMax;
267+
if (min !== scale.min || max !== scale.max) {
268+
scale.change({
269+
min,
270+
max,
271+
});
272+
}
273+
}
274+
244275
// 获取scale 在 0点对位置的值
245276
getZeroValue(scale) {
246277
const { min, max } = scale;

packages/f2/test/components/interval/example/column.test.tsx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,116 @@ describe('柱状图示例', () => {
442442
expect(context).toMatchImageSnapshot();
443443
});
444444

445+
it('堆叠柱图-max', async () => {
446+
const context = createContext('层叠柱图', {
447+
height: '300px',
448+
width: '400px',
449+
});
450+
const data = [
451+
{
452+
name: 'London',
453+
月份: 'Jan.',
454+
月均降雨量: 18.9,
455+
},
456+
{
457+
name: 'London',
458+
月份: 'Feb.',
459+
月均降雨量: 28.8,
460+
},
461+
{
462+
name: 'London',
463+
月份: 'Mar.',
464+
月均降雨量: 39.3,
465+
},
466+
{
467+
name: 'London',
468+
月份: 'Apr.',
469+
月均降雨量: 81.4,
470+
},
471+
{
472+
name: 'London',
473+
月份: 'May.',
474+
月均降雨量: 47,
475+
},
476+
{
477+
name: 'London',
478+
月份: 'Jun.',
479+
月均降雨量: 20.3,
480+
},
481+
{
482+
name: 'London',
483+
月份: 'Jul.',
484+
月均降雨量: 24,
485+
},
486+
{
487+
name: 'London',
488+
月份: 'Aug.',
489+
月均降雨量: 35.6,
490+
},
491+
{
492+
name: 'Berlin',
493+
月份: 'Jan.',
494+
月均降雨量: 12.4,
495+
},
496+
{
497+
name: 'Berlin',
498+
月份: 'Feb.',
499+
月均降雨量: 23.2,
500+
},
501+
{
502+
name: 'Berlin',
503+
月份: 'Mar.',
504+
月均降雨量: 34.5,
505+
},
506+
{
507+
name: 'Berlin',
508+
月份: 'Apr.',
509+
月均降雨量: 99.7,
510+
},
511+
{
512+
name: 'Berlin',
513+
月份: 'May.',
514+
月均降雨量: 52.6,
515+
},
516+
{
517+
name: 'Berlin',
518+
月份: 'Jun.',
519+
月均降雨量: 35.5,
520+
},
521+
{
522+
name: 'Berlin',
523+
月份: 'Jul.',
524+
月均降雨量: 37.4,
525+
},
526+
{
527+
name: 'Berlin',
528+
月份: 'Aug.',
529+
月均降雨量: 42.4,
530+
},
531+
];
532+
const { type, props } = (
533+
<Canvas context={context} pixelRatio={1}>
534+
<Chart
535+
data={data}
536+
scale={{
537+
月均降雨量: {
538+
max: 100,
539+
},
540+
}}
541+
>
542+
<Axis field="月份" />
543+
<Axis field="月均降雨量" />
544+
<Interval x="月份" y="月均降雨量" color="name" adjust="stack" />
545+
</Chart>
546+
</Canvas>
547+
);
548+
const canvas = new Canvas(props);
549+
await canvas.render();
550+
551+
await delay(1000);
552+
expect(context).toMatchImageSnapshot();
553+
});
554+
445555
it('百分比层叠柱图', async () => {
446556
const context = createContext('百分比层叠柱图', {
447557
height: '300px',

0 commit comments

Comments
 (0)