Skip to content

Commit

Permalink
fix(slider): multiple series y
Browse files Browse the repository at this point in the history
  • Loading branch information
deletenothing committed Nov 27, 2023
1 parent 4c9eb47 commit 6cf8d24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
4 changes: 1 addition & 3 deletions __tests__/plots/static/aapl-line-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export function aaplLineBasic(): G2Spec {
y: 'close',
},
slider: {
x: {
// sparklineData: [[10, 2, 3, 4, 15, 10, 5, 0, 3, 1]]
}
x: {},
},
};
}
45 changes: 30 additions & 15 deletions src/component/slider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Slider as SliderComponent } from '@antv/component';
import { format } from 'd3-format';
import { DisplayObject } from '@antv/g';
import { isArray } from '@antv/util';
import { isTranspose } from '../utils/coordinate';
import { GuideComponentComponent as GCC, GuideComponentContext } from '../runtime';
import {
GuideComponentComponent as GCC,
GuideComponentContext,
} from '../runtime';
import { invert } from '../utils/scale';
import { isArray } from '@antv/util';

export type SliderOptions = {
orientation: 'horizontal' | 'vertical';
Expand Down Expand Up @@ -33,8 +36,13 @@ export const Slider: GCC<SliderOptions> = (options) => {
...rest
} = options;

return (content) => {
const { scales: [scale], value, theme, coordinate } = content;
return (context) => {
const {
scales: [scale],
value,
theme,
coordinate,
} = context;
const { bbox } = value;

const { width, height } = bbox;
Expand Down Expand Up @@ -62,7 +70,7 @@ export const Slider: GCC<SliderOptions> = (options) => {
const tick = invert(scale, v1, true);
return f(tick);
},
sparklineData: inferSparklineData(options, content),
sparklineData: inferSparklineData(options, context),
...style,
...rest,
}),
Expand All @@ -75,21 +83,28 @@ function markValue(markState, channels: string[]) {
.filter(([mark]) => mark.type === 'line' || mark.type === 'area')
.map(([mark]) => {
const { encode, slider } = mark;
const channel = (name) => {
const channel = encode[name];
return [name, channel ? channel.value : undefined];
};
const obj = Object.fromEntries(channels.map(channel));
return slider.x ? obj.y : obj.x;
if (slider?.x && Object.keys(slider.x).length === 0) {
const channel = (name) => {
const channel = encode[name];
return [name, channel ? channel.value : undefined];
};
return Object.fromEntries(channels.map(channel));
}
});
return value;

if (!value?.series) return value?.y;
const result = value.series.reduce((acc, curr, index) => {
acc[curr] = acc[curr] || [];
acc[curr].push(value.y[index]);
return acc;
}, {});
return Object.values(result);
}

function inferSparklineData(options, context: GuideComponentContext) {
const { markState } = context;
if (options.sparklineData && isArray(options.sparklineData))
return options.sparklineData;
return markValue(markState, ['x', 'y']);
if (isArray(options.sparklineData)) return options.sparklineData;
return markValue(markState, ['y', 'series']);
}

Slider.props = {
Expand Down

0 comments on commit 6cf8d24

Please sign in to comment.