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(slider): index scale by scaleKey #5291

Merged
merged 3 commits into from
Jul 12, 2023
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
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.
13 changes: 13 additions & 0 deletions __tests__/plots/interaction/countries-annotation-slider-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ export function countriesAnnotationSliderFilter(): G2Spec {
],
},
},
{
type: 'text',
style: {
text: 'Population',
x: '100%',
y: '100%',
dx: -10,
dy: -10,
textAlign: 'end',
fontSize: 50,
textBaseline: 'bottom',
},
},
],
};
}
Expand Down
18 changes: 8 additions & 10 deletions src/runtime/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { documentOf, useLibrary } from './library';
import { initializeMark } from './mark';
import {
applyScale,
assignScale,
collectScales,
inferScale,
syncFacetsScales,
Expand Down Expand Up @@ -535,6 +536,7 @@ async function initializeMarks(
(total, { scale }) => deepMix(total, scale),
{},
);
const { scaleKey } = channels[0];

// Use the fields of the first channel as the title.
const { values: FV } = channels[0];
Expand All @@ -550,14 +552,10 @@ async function initializeMarks(
// Use the name of the first channel as the scale name.
const { name } = channels[0];
const values = channels.flatMap(({ values }) => values.map((d) => d.value));
const scale = inferScale(
name,
values,
options,
coordinates,
theme,
library,
);
const scale = {
...inferScale(name, values, options, coordinates, theme, library),
key: scaleKey,
};
channels.forEach((channel) => (channel.scale = scale));
}

Expand Down Expand Up @@ -622,7 +620,7 @@ function initializeState(
const { name } = descriptor;
const scale = useRelationScale(descriptor, library);
scales.push(scale);
scaleInstance[name] = scale;
assignScale(scaleInstance, { [name]: scale });
}
component.scaleInstances = scales;
}
Expand Down Expand Up @@ -651,7 +649,7 @@ function initializeState(
const markScaleInstance = mapObject(scale, (options) => {
return useRelationScale(options, library);
});
Object.assign(scaleInstance, markScaleInstance);
assignScale(scaleInstance, markScaleInstance);
const value = applyScale(channels, markScaleInstance);

// Calc points and transformation for each data,
Expand Down
29 changes: 28 additions & 1 deletion src/runtime/scale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Linear, createInterpolateValue } from '@antv/scale';
import { extent } from 'd3-array';
import { extent, max } from 'd3-array';
import * as d3ScaleChromatic from 'd3-scale-chromatic';
import { deepMix, omit, upperFirst } from '@antv/util';
import { firstOf, lastOf, unique } from '../utils/array';
Expand Down Expand Up @@ -161,6 +161,33 @@ export function useRelation(
return [conditionalize, deconditionalize];
}

export function assignScale(
target: Record<string, Scale>,
source: Record<string, Scale>,
): Record<string, Scale> {
const keys = Object.keys(target);
for (const scale of Object.values(source)) {
const { name, key } = scale.getOptions();
if (typeof key === 'string') {
pearmini marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}
return target;
}

export function useRelationScale(
options: Record<string, any>,
library: G2Library,
Expand Down