Skip to content

Commit

Permalink
Use resize observer
Browse files Browse the repository at this point in the history
  • Loading branch information
sulemanof committed Sep 22, 2020
1 parent bce20af commit d02577f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// tgcChart__legend--small
// tgcChart__legend-isLoading

.tgcChart__container {
min-height: 0;
.tgcChart__container, .tgcChart__wrapper {
flex: 1 1 0;
display: flex;
}
Expand Down
43 changes: 29 additions & 14 deletions src/plugins/vis_type_tagcloud/public/components/tag_cloud_chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import React, { useEffect, useRef } from 'react';
import * as Rx from 'rxjs';
import React, { useCallback, useEffect, useRef } from 'react';
import { EuiResizeObserver } from '@elastic/eui';

import { TagCloudVisDependencies } from '../plugin';
import { TagCloudVisRenderValue } from '../tag_cloud_fn';
Expand All @@ -33,30 +33,45 @@ type TagCloudChartProps = TagCloudVisDependencies &
renderComplete: () => void;
};

export const TagCloudChart = ({ colors, visData, visParams, fireEvent }: TagCloudChartProps) => {
export const TagCloudChart = ({
colors,
visData,
visParams,
fireEvent,
renderComplete,
}: TagCloudChartProps) => {
const chartDiv = useRef<HTMLDivElement>(null);
const visController = useRef<any>(null);
const renderSubject = useRef(new Rx.Subject());

useEffect(() => {
visController.current = new TagCloudVisualization(chartDiv.current, colors, fireEvent);
renderSubject.current.subscribe((params: any) => {
visController.current.render(params.visData, params.visParams);
});

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

useEffect(() => {
renderSubject.current.next({
visData,
visParams,
});
}, [visData, visParams]);
if (visController.current) {
visController.current.render(visData, visParams).then(renderComplete);
}
}, [visData, visParams, renderComplete]);

const onResize = useCallback(() => {
if (visController.current) {
visController.current.render().then(renderComplete);
}
}, [renderComplete]);

return <div className="tgcChart__container" ref={chartDiv} />;
return (
<EuiResizeObserver onResize={onResize}>
{(resizeRef) => (
<div className="tgcChart__wrapper" ref={resizeRef}>
<div className="tgcChart__container" ref={chartDiv} />
</div>
)}
</EuiResizeObserver>
);
};

// default export required for React.Lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,36 @@ export class TagCloudVisualization {
}

async render(data, visParams) {
this._updateParams(visParams);
this._updateData(data);
if (data && visParams) {
this._updateParams(visParams);
this._updateData(data);
}

this._resize();

await this._renderComplete$.pipe(take(1)).toPromise();

if (data.columns.length !== 2) {
if (data && data.columns.length !== 2 && this._feedbackMessage.current) {
this._feedbackMessage.current.setState({
shouldShowTruncate: false,
shouldShowIncomplete: false,
});
return;
}

this._label.current.setState({
label: `${data.columns[0].name} - ${data.columns[1].name}`,
shouldShowLabel: visParams.showLabel,
});
this._feedbackMessage.current.setState({
shouldShowTruncate: this._truncated,
shouldShowIncomplete: this._tagCloud.getStatus() === TagCloud.STATUS.INCOMPLETE,
});
if (data && this._label.current) {
this._label.current.setState({
label: `${data.columns[0].name} - ${data.columns[1].name}`,
shouldShowLabel: visParams.showLabel,
});
}

if (this._feedbackMessage.current) {
this._feedbackMessage.current.setState({
shouldShowTruncate: this._truncated,
shouldShowIncomplete: this._tagCloud.getStatus() === TagCloud.STATUS.INCOMPLETE,
});
}
}

destroy() {
Expand Down

0 comments on commit d02577f

Please sign in to comment.