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

[7.x] [Lens] Add styling options for x and y axes on the settings popover (#71829) #74782

Merged
merged 1 commit into from
Aug 12, 2020
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion x-pack/plugins/lens/public/xy_visualization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ExpressionsSetup } from '../../../../../src/plugins/expressions/public'
import { UI_SETTINGS } from '../../../../../src/plugins/data/public';
import { xyVisualization } from './xy_visualization';
import { xyChart, getXyChartRenderer } from './xy_expression';
import { legendConfig, layerConfig, yAxisConfig } from './types';
import { legendConfig, layerConfig, yAxisConfig, tickLabelsConfig, gridlinesConfig } from './types';
import { EditorFrameSetup, FormatFactory } from '../types';
import { ChartsPluginSetup } from '../../../../../src/plugins/charts/public';

Expand Down Expand Up @@ -39,6 +39,8 @@ export class XyVisualization {
) {
expressions.registerFunction(() => legendConfig);
expressions.registerFunction(() => yAxisConfig);
expressions.registerFunction(() => tickLabelsConfig);
expressions.registerFunction(() => gridlinesConfig);
expressions.registerFunction(() => layerConfig);
expressions.registerFunction(() => xyChart);

Expand Down
77 changes: 75 additions & 2 deletions x-pack/plugins/lens/public/xy_visualization/to_expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ describe('#toExpression', () => {
legend: { position: Position.Bottom, isVisible: true },
preferredSeriesType: 'bar',
fittingFunction: 'Carry',
tickLabelsVisibilitySettings: { x: false, y: true },
gridlinesVisibilitySettings: { x: false, y: true },
layers: [
{
layerId: 'first',
Expand Down Expand Up @@ -77,6 +79,27 @@ describe('#toExpression', () => {
).toEqual('None');
});

it('should default the showXAxisTitle and showYAxisTitle to true', () => {
const expression = xyVisualization.toExpression(
{
legend: { position: Position.Bottom, isVisible: true },
preferredSeriesType: 'bar',
layers: [
{
layerId: 'first',
seriesType: 'area',
splitAccessor: 'd',
xAccessor: 'a',
accessors: ['b', 'c'],
},
],
},
frame
) as Ast;
expect(expression.chain[0].arguments.showXAxisTitle[0]).toBe(true);
expect(expression.chain[0].arguments.showYAxisTitle[0]).toBe(true);
});

it('should not generate an expression when missing x', () => {
expect(
xyVisualization.toExpression(
Expand Down Expand Up @@ -140,8 +163,8 @@ describe('#toExpression', () => {
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('b');
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('c');
expect(mockDatasource.publicAPIMock.getOperationForColumnId).toHaveBeenCalledWith('d');
expect(expression.chain[0].arguments.xTitle).toEqual(['col_a']);
expect(expression.chain[0].arguments.yTitle).toEqual(['col_b']);
expect(expression.chain[0].arguments.xTitle).toEqual(['']);
expect(expression.chain[0].arguments.yTitle).toEqual(['']);
expect(
(expression.chain[0].arguments.layers[0] as Ast).chain[0].arguments.columnToLabel
).toEqual([
Expand All @@ -152,4 +175,54 @@ describe('#toExpression', () => {
}),
]);
});

it('should default the tick labels visibility settings to true', () => {
const expression = xyVisualization.toExpression(
{
legend: { position: Position.Bottom, isVisible: true },
preferredSeriesType: 'bar',
layers: [
{
layerId: 'first',
seriesType: 'area',
splitAccessor: 'd',
xAccessor: 'a',
accessors: ['b', 'c'],
},
],
},
frame
) as Ast;
expect(
(expression.chain[0].arguments.tickLabelsVisibilitySettings[0] as Ast).chain[0].arguments
).toEqual({
x: [true],
y: [true],
});
});

it('should default the gridlines visibility settings to true', () => {
const expression = xyVisualization.toExpression(
{
legend: { position: Position.Bottom, isVisible: true },
preferredSeriesType: 'bar',
layers: [
{
layerId: 'first',
seriesType: 'area',
splitAccessor: 'd',
xAccessor: 'a',
accessors: ['b', 'c'],
},
],
},
frame
) as Ast;
expect(
(expression.chain[0].arguments.gridlinesVisibilitySettings[0] as Ast).chain[0].arguments
).toEqual({
x: [true],
y: [true],
});
});
});
63 changes: 36 additions & 27 deletions x-pack/plugins/lens/public/xy_visualization/to_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,6 @@ interface ValidLayer extends LayerConfig {
xAccessor: NonNullable<LayerConfig['xAccessor']>;
}

function xyTitles(layer: LayerConfig, frame: FramePublicAPI) {
const defaults = {
xTitle: 'x',
yTitle: 'y',
};

if (!layer || !layer.accessors.length) {
return defaults;
}
const datasource = frame.datasourceLayers[layer.layerId];
if (!datasource) {
return defaults;
}
const x = layer.xAccessor ? datasource.getOperationForColumnId(layer.xAccessor) : null;
const y = layer.accessors[0] ? datasource.getOperationForColumnId(layer.accessors[0]) : null;

return {
xTitle: x ? x.label : defaults.xTitle,
yTitle: y ? y.label : defaults.yTitle,
};
}

export const toExpression = (state: State, frame: FramePublicAPI): Ast | null => {
if (!state || !state.layers.length) {
return null;
Expand All @@ -52,7 +30,7 @@ export const toExpression = (state: State, frame: FramePublicAPI): Ast | null =>
});
});

return buildExpression(state, metadata, frame, xyTitles(state.layers[0], frame));
return buildExpression(state, metadata, frame);
};

export function toPreviewExpression(state: State, frame: FramePublicAPI) {
Expand Down Expand Up @@ -99,8 +77,7 @@ export function getScaleType(metadata: OperationMetadata | null, defaultScale: S
export const buildExpression = (
state: State,
metadata: Record<string, Record<string, OperationMetadata | null>>,
frame?: FramePublicAPI,
{ xTitle, yTitle }: { xTitle: string; yTitle: string } = { xTitle: '', yTitle: '' }
frame?: FramePublicAPI
): Ast | null => {
const validLayers = state.layers.filter((layer): layer is ValidLayer =>
Boolean(layer.xAccessor && layer.accessors.length)
Expand All @@ -116,8 +93,8 @@ export const buildExpression = (
type: 'function',
function: 'lens_xy_chart',
arguments: {
xTitle: [xTitle],
yTitle: [yTitle],
xTitle: [state.xTitle || ''],
yTitle: [state.yTitle || ''],
legend: [
{
type: 'expression',
Expand All @@ -137,6 +114,38 @@ export const buildExpression = (
},
],
fittingFunction: [state.fittingFunction || 'None'],
showXAxisTitle: [state.showXAxisTitle ?? true],
showYAxisTitle: [state.showYAxisTitle ?? true],
tickLabelsVisibilitySettings: [
{
type: 'expression',
chain: [
{
type: 'function',
function: 'lens_xy_tickLabelsConfig',
arguments: {
x: [state?.tickLabelsVisibilitySettings?.x ?? true],
y: [state?.tickLabelsVisibilitySettings?.y ?? true],
},
},
],
},
],
gridlinesVisibilitySettings: [
{
type: 'expression',
chain: [
{
type: 'function',
function: 'lens_xy_gridlinesConfig',
arguments: {
x: [state?.gridlinesVisibilitySettings?.x ?? true],
y: [state?.gridlinesVisibilitySettings?.y ?? true],
},
},
],
},
],
layers: validLayers.map((layer) => {
const columnToLabel: Record<string, string> = {};

Expand Down
Loading