Skip to content

Commit

Permalink
[Lens ] Preview metric (#43755)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Sep 4, 2019
1 parent 426d9d7 commit b443d3e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ $lnsSuggestionWidth: 150px;
.lnsSuggestionChartWrapper {
height: $lnsSuggestionHeight - $euiSize;
pointer-events: none;
margin: 0 $euiSizeS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ describe('AutoScale', () => {
expect(computeScale(mockElement(2000, 2000), mockElement(1000, 1000))).toBe(1);
});

it('is never under 0.3', () => {
it('is never under 0.3 in default case', () => {
expect(computeScale(mockElement(2000, 1000), mockElement(1000, 10000))).toBe(0.3);
});

it('is never under specified min scale if specified', () => {
expect(computeScale(mockElement(2000, 1000), mockElement(1000, 10000), 0.1)).toBe(0.1);
});

it('is the lesser of the x or y scale', () => {
expect(computeScale(mockElement(2000, 2000), mockElement(3000, 5000))).toBe(0.4);
expect(computeScale(mockElement(2000, 3000), mockElement(4000, 3200))).toBe(0.5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EuiResizeObserver } from '@elastic/eui';

interface Props extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode | React.ReactNode[];
minScale?: number;
}

interface State {
Expand All @@ -25,7 +26,7 @@ export class AutoScale extends React.Component<Props, State> {
super(props);

this.scale = _.throttle(() => {
const scale = computeScale(this.parent, this.child);
const scale = computeScale(this.parent, this.child, this.props.minScale);

// Prevent an infinite render loop
if (this.state.scale !== scale) {
Expand Down Expand Up @@ -54,15 +55,15 @@ export class AutoScale extends React.Component<Props, State> {
};

render() {
const { children } = this.props;
const { children, minScale, ...rest } = this.props;
const { scale } = this.state;
const style = this.props.style || {};

return (
<EuiResizeObserver onResize={this.scale}>
{resizeRef => (
<div
{...this.props}
{...rest}
ref={el => {
this.setParent(el);
resizeRef(el);
Expand Down Expand Up @@ -97,23 +98,24 @@ interface ClientDimensionable {
clientHeight: number;
}

const MAX_SCALE = 1;
const MIN_SCALE = 0.3;

/**
* computeScale computes the ratio by which the child needs to shrink in order
* to fit into the parent. This function is only exported for testing purposes.
*/
export function computeScale(
parent: ClientDimensionable | null,
child: ClientDimensionable | null
child: ClientDimensionable | null,
minScale: number = MIN_SCALE
) {
const MAX_SCALE = 1;
const MIN_SCALE = 0.3;

if (!parent || !child) {
return 1;
}

const scaleX = parent.clientWidth / child.clientWidth;
const scaleY = parent.clientHeight / child.clientHeight;

return Math.max(Math.min(MAX_SCALE, Math.min(scaleX, scaleY)), MIN_SCALE);
return Math.max(Math.min(MAX_SCALE, Math.min(scaleX, scaleY)), minScale);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function sampleArgs() {
accessor: 'a',
layerId: 'l1',
title: 'My fanci metric chart',
mode: 'full',
};

return { data, args };
Expand All @@ -50,6 +51,52 @@ describe('metric_expression', () => {

expect(shallow(<MetricChart data={data} args={args} formatFactory={x => x} />))
.toMatchInlineSnapshot(`
<div
style={
Object {
"alignItems": "center",
"display": "flex",
"flexDirection": "column",
"justifyContent": "center",
"maxHeight": "100%",
"maxWidth": "100%",
"textAlign": "center",
}
}
>
<AutoScale>
<div
style={
Object {
"fontSize": "60pt",
"fontWeight": 600,
}
}
>
10110
</div>
<div
style={
Object {
"fontSize": "24pt",
}
}
>
My fanci metric chart
</div>
</AutoScale>
</div>
`);
});

test('it does not render title in reduced mode', () => {
const { data, args } = sampleArgs();

expect(
shallow(
<MetricChart data={data} args={{ ...args, mode: 'reduced' }} formatFactory={x => x} />
)
).toMatchInlineSnapshot(`
<div
style={
Object {
Expand All @@ -63,7 +110,9 @@ describe('metric_expression', () => {
}
}
>
<AutoScale>
<AutoScale
minScale={0}
>
<div
style={
Object {
Expand All @@ -74,15 +123,6 @@ describe('metric_expression', () => {
>
10110
</div>
<div
style={
Object {
"fontSize": "24pt",
}
}
>
My fanci metric chart
</div>
</AutoScale>
</div>
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export const metricChart: ExpressionFunction<
types: ['string'],
help: 'The column whose value is being displayed',
},
mode: {
types: ['string'],
options: ['reduced', 'full'],
default: 'full',
help:
'The display mode of the chart - reduced will only show the metric itself without min size',
},
},
context: {
types: ['lens_multitable'],
Expand Down Expand Up @@ -82,7 +89,7 @@ export function MetricChart({
args,
formatFactory,
}: MetricChartProps & { formatFactory: FormatFactory }) {
const { title, accessor } = args;
const { title, accessor, mode } = args;
let value = '-';
const firstTable = Object.values(data.tables)[0];

Expand All @@ -109,9 +116,9 @@ export function MetricChart({
textAlign: 'center',
}}
>
<AutoScale>
<AutoScale minScale={mode === 'reduced' ? 0 : undefined}>
<div style={{ fontSize: '60pt', fontWeight: 600 }}>{value}</div>
<div style={{ fontSize: '24pt' }}>{title}</div>
{mode === 'full' && <div style={{ fontSize: '24pt' }}>{title}</div>}
</AutoScale>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ describe('metric_suggestions', () => {
"accessor": Array [
"bytes",
],
"mode": Array [
"reduced",
],
"title": Array [
"",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function getSuggestion(table: TableSuggestion): VisualizationSuggestion<State> {
arguments: {
title: [''],
accessor: [col.columnId],
mode: ['reduced'],
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface State {

export interface MetricConfig extends State {
title: string;
mode: 'reduced' | 'full';
}

export type PersistableState = State;

0 comments on commit b443d3e

Please sign in to comment.