Skip to content

Commit

Permalink
fix: adjust scale range (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
El-Chiang authored Dec 20, 2021
1 parent 873c8b1 commit bc04790
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/f2/src/chart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ class Chart extends Component implements IChart, InteractionMixin {
});
}

getCoord() {
return this.coord;
}

render(): JSX.Element {
const { props, state, layout, coord } = this;
const { children, data } = props;
Expand Down
13 changes: 8 additions & 5 deletions packages/f2/src/components/interval/withInterval.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ export default (Views) => {
const { coord, shape = 'rect', animation, showLabel, labelCfg: customLabelCfg } = props;
const View = Views[shape];
const LabelView = LabelViews[shape];
const labelCfg = deepMix({
label: null,
offsetX: 0,
offsetY: 0,
}, customLabelCfg);
const labelCfg = deepMix(
{
label: null,
offsetX: 0,
offsetY: 0,
},
customLabelCfg
);

if (!View) return null;

Expand Down
25 changes: 12 additions & 13 deletions packages/f2/src/controller/scale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { each, mix, isNil, isFunction, isNumber, valuesOfKey, getRange } from '@antv/util';
import { registerTickMethod, Scale, getScale } from '@antv/scale';
import { registerTickMethod, Scale, getScale, ScaleConfig } from '@antv/scale';
import CatTick from './scale/cat-tick';
import LinearTick from './scale/linear-tick';

Expand All @@ -9,6 +9,8 @@ registerTickMethod('time-cat', CatTick);
// 覆盖linear 度量的tick算法
registerTickMethod('wilkinson-extended', LinearTick);

type ScaleOption = { type?: string } & ScaleConfig;

class ScaleController {
private data: any;
// scale 实例的配置
Expand All @@ -22,7 +24,7 @@ class ScaleController {
this.scales = {};
}

private _getType(option) {
private _getType(option: ScaleOption) {
const { type, values, field } = option;
if (type) {
return type;
Expand All @@ -36,20 +38,18 @@ class ScaleController {
return 'cat';
}

private _getOption(option) {
private _getOption(option: ScaleOption) {
const { values, field } = option;
const type = this._getType(option);

option.type = type;

// identity
if (type === 'identity') {
option.value = field;
option.field = field.toString();
option.values = [field];
return option;
}

// linear 类型
if (type === 'linear') {
// 设置默认nice
Expand All @@ -64,11 +64,10 @@ class ScaleController {
if (isNil(option.max)) {
option.max = max;
}

return option;
}
// 分类类型
if (type === 'cat') {
// 分类类型和 timeCat 类型,调整 range
if (type === 'cat' || type === 'timeCat') {
if (option.range) {
return option;
}
Expand All @@ -83,8 +82,8 @@ class ScaleController {
range = [offset, 1 - offset];
}
option.range = range;
return option;
}

return option;
}

Expand All @@ -98,7 +97,7 @@ class ScaleController {
}

// 更新或创建scale
setScale(field: string, option: any = {}) {
setScale(field: string, option?: ScaleOption) {
const { options, scales } = this;
options[field] = mix({}, options[field], option);
// 如果scale有更新,scale 也需要重新创建
Expand All @@ -107,13 +106,13 @@ class ScaleController {
}
}

create(options) {
create(options: { [k: string]: ScaleOption }) {
this.update(options);
}

update(options) {
update(options: { [k: string]: ScaleOption }) {
if (!options) return;
each(options, (option, field: string) => {
each(options, (option: ScaleOption, field: string) => {
this.setScale(field, option);
});
// 为了让外部感知到scale有变化
Expand Down
63 changes: 63 additions & 0 deletions packages/f2/test/components/interval/interval.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,67 @@ describe('Interval', () => {
const canvas = new Canvas(props);
canvas.render();
});

it('x scale 为 timeCat', () => {
const data = [
{
time: '2016-08-08 00:00:00',
tem: 10,
},
{
time: '2016-08-08 00:10:00',
tem: 22,
},
{
time: '2016-08-08 00:30:00',
tem: 20,
},
{
time: '2016-08-09 00:35:00',
tem: 26,
},
{
time: '2016-08-09 01:00:00',
tem: 20,
},
];
const context = createContext('x scale 为 timeCat');
const chartRef = { current: null };
const { offsetWidth } = document.body;
const height = offsetWidth * 0.75;
const { props } = (
<Canvas
context={context}
pixelRatio={window.devicePixelRatio}
width={offsetWidth}
height={height}
>
<Chart
ref={chartRef}
data={data}
scale={{
time: {
type: 'timeCat',
tickCount: 3,
},
tem: {
tickCount: 5,
},
}}
>
<Axis field="time" />
<Axis field="tem" />
<Interval x="time" y="tem" />
</Chart>
</Canvas>
);

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

const timeScale = chartRef.current.scale.getScale('time');
const { range } = timeScale;
expect(range[0]).toBeGreaterThan(0);
expect(range[1]).toBeLessThan(1);
});
});

0 comments on commit bc04790

Please sign in to comment.