Skip to content

Commit

Permalink
[Reporting] Missing vega vis is when generating report from sample data
Browse files Browse the repository at this point in the history
Closes: #124886
  • Loading branch information
alexwizp committed Feb 16, 2022
1 parent cf25ac3 commit cf72e84
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/plugins/vis_types/vega/public/components/vega_vis.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
flex: 1 1 100%;
position: relative;
// flex-direction determined by js

// Fix Firefox incorrectly showing vertical/horizontal scrollbars for canvas element
canvas {
display: block;
}
}

.vgaVis__view {
Expand Down
45 changes: 32 additions & 13 deletions src/plugins/vis_types/vega/public/components/vega_vis_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import React, { useEffect, useCallback, useRef } from 'react';
import { EuiResizeObserver } from '@elastic/eui';
import React, { useEffect, useRef, useMemo, useCallback } from 'react';
import { EuiResizeObserver, EuiResizeObserverProps } from '@elastic/eui';
import { throttle } from 'lodash';

import type { IInterpreterRenderHandlers, RenderMode } from 'src/plugins/expressions';
Expand All @@ -27,6 +27,8 @@ interface VegaVisComponentProps {

type VegaVisController = InstanceType<ReturnType<typeof createVegaVisualization>>;

const THROTTLE_INTERVAL = 300;

export const VegaVisComponent = ({
visData,
fireEvent,
Expand All @@ -35,38 +37,55 @@ export const VegaVisComponent = ({
renderMode,
}: VegaVisComponentProps) => {
const chartDiv = useRef<HTMLDivElement>(null);
const renderCompleted = useRef(false);
const visController = useRef<VegaVisController | null>(null);

useEffect(() => {
if (chartDiv.current) {
const VegaVis = createVegaVisualization(deps, renderMode);
visController.current = new VegaVis(chartDiv.current, fireEvent);
}

return () => {
visController.current?.destroy();
visController.current = null;
};
}, [deps, fireEvent, renderMode]);

useEffect(() => {
const asyncRender = async (visCtrl: VegaVisController) => {
await visCtrl.render(visData);
renderCompleted.current = true;
renderComplete();
};

if (visController.current) {
visController.current.render(visData).then(renderComplete);
asyncRender(visController.current);
}
}, [visData, renderComplete]);
}, [renderComplete, visData]);

const resizeChart = useMemo(
() =>
throttle(
(dimensions) => {
visController.current?.resize(dimensions);
},
THROTTLE_INTERVAL,
{ leading: false, trailing: true }
),
[]
);

/* eslint-disable-next-line react-hooks/exhaustive-deps */
const updateChartSize = useCallback(
throttle(() => {
if (visController.current) {
visController.current.render(visData).then(renderComplete);
const onContainerResize: EuiResizeObserverProps['onResize'] = useCallback(
(dimensions) => {
if (renderCompleted.current) {
resizeChart(dimensions);
}
}, 300),
[renderComplete, visData]
},
[resizeChart]
);

return (
<EuiResizeObserver onResize={updateChartSize}>
<EuiResizeObserver onResize={onContainerResize}>
{(resizeRef) => (
<div className="vgaVis__wrapper" ref={resizeRef}>
<div ref={chartDiv} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class VegaBaseView {
_addDestroyHandler(handler: Function): void;

destroy(): Promise<void>;
resize(dimensions?: { height: number; width: number }): Promise<void>;

_$container: any;
_$controls: any;
Expand Down
15 changes: 9 additions & 6 deletions src/plugins/vis_types/vega/public/vega_view/vega_base_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,19 @@ export class VegaBaseView {
}
}

resize() {
async resize(dimensions) {
if (this._parser.useResize && this._view) {
this.updateVegaSize(this._view);
return this._view.runAsync();
this.updateVegaSize(this._view, dimensions);
await this._view.runAsync();

// The derived class should create this method
this.onViewContainerResize?.();
}
}

updateVegaSize(view) {
const width = Math.floor(Math.max(0, this._$container.width()));
const height = Math.floor(Math.max(0, this._$container.height()));
updateVegaSize(view, dimensions) {
const width = Math.floor(Math.max(0, dimensions?.width ?? this._$container.width()));
const height = Math.floor(Math.max(0, dimensions?.height ?? this._$container.height()));

if (view.width() !== width || view.height() !== height) {
view.width(width).height(height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ async function updateVegaView(mapBoxInstance: Map, vegaView: View) {
}

export class VegaMapView extends VegaBaseView {
private mapBoxInstance?: Map;

private get shouldShowZoomControl() {
return Boolean(this._parser.mapConfig.zoomControl);
}
Expand Down Expand Up @@ -139,6 +141,7 @@ export class VegaMapView extends VegaBaseView {
};

mapBoxInstance.once('load', initMapComponents);
this.mapBoxInstance = mapBoxInstance;
});
}

Expand Down Expand Up @@ -193,4 +196,8 @@ export class VegaMapView extends VegaBaseView {

await this.initMapContainer(vegaView);
}

protected async onViewContainerResize() {
this.mapBoxInstance?.resize();
}
}
1 change: 0 additions & 1 deletion src/plugins/vis_types/vega/public/vega_vis_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const getVegaVisRenderer: (
handlers.onDestroy(() => {
unmountComponentAtNode(domNode);
});

render(
<KibanaThemeProvider theme$={deps.core.theme.theme$}>
<VisualizationContainer handlers={handlers}>
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/vis_types/vega/public/vega_visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createVegaStateRestorer } from './lib/vega_state_restorer';

type VegaVisType = new (el: HTMLDivElement, fireEvent: IInterpreterRenderHandlers['event']) => {
render(visData: VegaParser): Promise<void>;
resize(dimensions?: { height: number; width: number }): Promise<void>;
destroy(): void;
};

Expand Down Expand Up @@ -97,6 +98,10 @@ export const createVegaVisualization = (
}
}

async resize(dimensions?: { height: number; width: number }) {
return this.vegaView?.resize(dimensions);
}

destroy() {
this.vegaStateRestorer.clear();
this.vegaView?.destroy();
Expand Down

0 comments on commit cf72e84

Please sign in to comment.