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

[Lens] allow drag and drop reorder on xyChart for y dimension #84640

Merged
merged 10 commits into from
Dec 3, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ export function LayerPanel(
const isFromTheSameGroup =
isDraggedOperation(dragging) &&
dragging.groupId === group.groupId &&
dragging.columnId !== accessor &&
dragging.groupId !== 'y'; // TODO: remove this line when https://github.com/elastic/elastic-charts/issues/868 is fixed
dragging.columnId !== accessor;

const isDroppable = isDraggedOperation(dragging)
? dragType === 'reorder'
Expand Down
15 changes: 8 additions & 7 deletions x-pack/plugins/lens/public/xy_visualization/color_assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ColorAssignments = Record<
string,
{
totalSeriesCount: number;
getRank(layer: LayerColorConfig, seriesKey: string, yAccessor: string): number;
getRank(sortedLayer: LayerColorConfig, seriesKey: string, yAccessor: string): number;
}
>;

Expand Down Expand Up @@ -72,8 +72,8 @@ export function getColorAssignments(
);
return {
totalSeriesCount,
getRank(layer: LayerColorConfig, seriesKey: string, yAccessor: string) {
const layerIndex = paletteLayers.indexOf(layer);
getRank(sortedLayer: LayerColorConfig, seriesKey: string, yAccessor: string) {
const layerIndex = paletteLayers.findIndex((l) => sortedLayer.layerId === l.layerId);
const currentSeriesPerLayer = seriesPerLayer[layerIndex];
const splitRank = currentSeriesPerLayer.splits.indexOf(seriesKey);
return (
Expand All @@ -82,8 +82,10 @@ export function getColorAssignments(
: seriesPerLayer
.slice(0, layerIndex)
.reduce((sum, perLayer) => sum + perLayer.numberOfSeries, 0)) +
(layer.splitAccessor && splitRank !== -1 ? splitRank * layer.accessors.length : 0) +
layer.accessors.indexOf(yAccessor)
(sortedLayer.splitAccessor && splitRank !== -1
? splitRank * sortedLayer.accessors.length
: 0) +
sortedLayer.accessors.indexOf(yAccessor)
);
},
};
Expand All @@ -94,13 +96,12 @@ export function getAccessorColorConfig(
colorAssignments: ColorAssignments,
frame: FramePublicAPI,
layer: LayerConfig,
sortedAccessors: string[],
paletteService: PaletteRegistry
): AccessorConfig[] {
const layerContainsSplits = Boolean(layer.splitAccessor);
const currentPalette: PaletteOutput = layer.palette || { type: 'palette', name: 'default' };
const totalSeriesCount = colorAssignments[currentPalette.name].totalSeriesCount;
return sortedAccessors.map((accessor) => {
return layer.accessors.map((accessor) => {
const currentYConfig = layer.yConfig?.find((yConfig) => yConfig.forAccessor === accessor);
if (layerContainsSplits) {
return {
Expand Down
21 changes: 21 additions & 0 deletions x-pack/plugins/lens/public/xy_visualization/visualization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,27 @@ describe('xy_visualization', () => {
const accessorConfig = breakdownConfig!.accessors[0];
expect(typeof accessorConfig !== 'string' && accessorConfig.palette).toEqual(customColors);
});

it('should respect the order of accessors coming from datasource', () => {
mbondyra marked this conversation as resolved.
Show resolved Hide resolved
const colorAssignment = require('./color_assignment'); // eslint-disable-line @typescript-eslint/no-var-requires
const getAccessorColorConfigSpy = jest.spyOn(colorAssignment, 'getAccessorColorConfig');
mockDatasource.publicAPIMock.getTableSpec.mockReturnValue([
{ columnId: 'c' },
{ columnId: 'b' },
]);
callConfigForYConfigs({});
expect(getAccessorColorConfigSpy).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
{
accessors: ['c', 'b'],
layerId: 'first',
seriesType: 'area',
xAccessor: 'a',
},
expect.anything()
);
});
});
});

Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/lens/public/xy_visualization/visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ export const getXyVisualization = ({
mappedAccessors = getAccessorColorConfig(
colorAssignments,
frame,
layer,
sortedAccessors,
{
...layer,
accessors: sortedAccessors.filter((sorted) => layer.accessors.includes(sorted)),
},
paletteService
);
}
Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/lens/public/xy_visualization/xy_config_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { TooltipWrapper } from './tooltip_wrapper';
import { getAxesConfiguration } from './axes_configuration';
import { PalettePicker } from '../shared_components';
import { getAccessorColorConfig, getColorAssignments } from './color_assignment';
import { getSortedAccessors } from './to_expression';

type UnwrapArray<T> = T extends Array<infer P> ? P : T;
type AxesSettingsConfigKeys = keyof AxesSettingsConfig;
Expand Down Expand Up @@ -579,6 +580,9 @@ const ColorPicker = ({
const currentColor = useMemo(() => {
if (overwriteColor || !frame.activeData) return overwriteColor;

const datasource = frame.datasourceLayers[layer.layerId];
const sortedAccessors: string[] = getSortedAccessors(datasource, layer);

const colorAssignments = getColorAssignments(
state.layers,
{ tables: frame.activeData },
Expand All @@ -587,11 +591,14 @@ const ColorPicker = ({
const mappedAccessors = getAccessorColorConfig(
colorAssignments,
frame,
layer,
[accessor],
{
...layer,
accessors: sortedAccessors.filter((sorted) => layer.accessors.includes(sorted)),
},
paletteService
);
return mappedAccessors[0].color;

return mappedAccessors.find((a) => a.columnId === accessor)?.color || null;
}, [overwriteColor, frame, paletteService, state.layers, accessor, formatFactory, layer]);

const [color, setColor] = useState(currentColor);
Expand Down