Skip to content

Commit

Permalink
fix(scale): group scale by name and index
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Nov 20, 2023
1 parent 291f0af commit 71d01ba
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ If all goes well, you can get the following lovely bar chart!
- [ant-design-charts](https://github.com/ant-design/ant-design-charts) - The React chart library, based on [G2](https://github.com/antvis/G2), [G6](https://github.com/antvis/G6), [X6](https://github.com/antvis/X6), [L7Plot](https://github.com/antvis/L7Plot).
- [More...](https://github.com/antvis/G2/discussions/5772)



## 📮 Contributing

- [Issues](https://github.com/antvis/g2/issues) - report bugs or request features
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
1 change: 1 addition & 0 deletions __tests__/plots/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ export { stateAgesIntervalCustomStyle } from './stateages-interval-custom-style'
export { mockTooltipClosest } from './mock-tooltip-closest';
export { stateAgesIntervalScrollbar } from './stateages-interval-scrollbar';
export { aaplLineOverflow } from './aapl-line-overflow';
export { moviesIntervalScaleKeyScrollbar } from './movies-interval-scale-key-scrollbar';
25 changes: 25 additions & 0 deletions __tests__/plots/tooltip/movies-interval-scale-key-scrollbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { G2Spec } from '../../../src';
import { seriesTooltipSteps } from './utils';

export async function moviesIntervalScaleKeyScrollbar(): Promise<G2Spec> {
return {
type: 'line',
data: {
type: 'fetch',
value: 'data/movies.csv',
},
encode: {
x: 'Major Genre',
y: 'Worldwide Gross',
series: () => 'Worldwide Gross',
color: () => 'Worldwide Gross',
},
transform: [{ type: 'groupX', y: 'sum' }],
scale: { y: { key: 'left' } },
axis: { y: { labelFormatter: '~s' }, x: { labelTransform: 'rotate(90)' } },
tooltip: { items: [{ channel: 'y', valueFormatter: '~s' }] },
slider: { x: true },
};
}

moviesIntervalScaleKeyScrollbar.steps = seriesTooltipSteps([100, 300]);
2 changes: 1 addition & 1 deletion src/component/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Slider: GCC<SliderOptions> = (options) => {

const { width, height } = bbox;
const { slider: sliderTheme = {} } = theme;
const defaultFormatter = scale.getFormatter?.() || ((v) => v.toString());
const defaultFormatter = scale.getFormatter?.() || ((v) => v + '');
const formatter =
typeof labelFormatter === 'string'
? format(labelFormatter)
Expand Down
27 changes: 11 additions & 16 deletions src/runtime/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,17 @@ export function assignScale(
): Record<string, Scale> {
const keys = Object.keys(target);
for (const scale of Object.values(source)) {
const { name, key } = scale.getOptions();
if (typeof key === 'string') {
if (!(key in target)) target[key] = scale;
} else {
// For scale.key = Symbol('independent')
if (!(name in target)) target[name] = scale;
else {
const I = keys
.filter((d) => d.startsWith(name))
// Reg is for extract `1` from `x1`;
.map((d) => +(d.replace(name, '') || 0));
const index = max(I) + 1;
const newKey = `${name}${index}`;
target[newKey] = scale;
scale.getOptions().key = newKey;
}
const { name } = scale.getOptions();
if (!(name in target)) target[name] = scale;
else {
const I = keys
.filter((d) => d.startsWith(name))
// Reg is for extract `1` from `x1`;
.map((d) => +(d.replace(name, '') || 0));
const index = max(I) + 1;
const newKey = `${name}${index}`;
target[newKey] = scale;
scale.getOptions().key = newKey;
}
}
return target;
Expand Down

0 comments on commit 71d01ba

Please sign in to comment.