Skip to content

Commit

Permalink
fix(slider): index scale by scaleKey
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Jul 11, 2023
1 parent 873bec7 commit 3ae3032
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
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
28 changes: 27 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,32 @@ 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') {
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))
.map((d) => +(/[^\d]+(\d*)$/.exec(d)?.[1] || 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

0 comments on commit 3ae3032

Please sign in to comment.