mirror of
https://github.com/getredash/redash.git
synced 2026-03-22 10:00:17 -04:00
* Prettier all the JS files * Add GitHub Action to autoformat code pushed to master * Fix eslint violation due to formatting. * Remove GitHub actions for styling * Add restyled.io config
52 lines
1.8 KiB
JavaScript
52 lines
1.8 KiB
JavaScript
import { isArray, isObject } from "lodash";
|
|
import React, { useState, useEffect } from "react";
|
|
import { RendererPropTypes } from "@/visualizations";
|
|
import resizeObserver from "@/services/resizeObserver";
|
|
|
|
import getChartData from "../getChartData";
|
|
import { Plotly, prepareData, prepareLayout, updateData, applyLayoutFixes } from "../plotly";
|
|
|
|
export default function PlotlyChart({ options, data }) {
|
|
const [container, setContainer] = useState(null);
|
|
|
|
useEffect(() => {
|
|
if (container) {
|
|
const plotlyOptions = { showLink: false, displaylogo: false };
|
|
|
|
const chartData = getChartData(data.rows, options);
|
|
const plotlyData = prepareData(chartData, options);
|
|
const plotlyLayout = prepareLayout(container, options, plotlyData);
|
|
|
|
// It will auto-purge previous graph
|
|
Plotly.newPlot(container, plotlyData, plotlyLayout, plotlyOptions).then(() => {
|
|
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u));
|
|
});
|
|
|
|
container.on("plotly_restyle", updates => {
|
|
// This event is triggered if some plotly data/layout has changed.
|
|
// We need to catch only changes of traces visibility to update stacking
|
|
if (isArray(updates) && isObject(updates[0]) && updates[0].visible) {
|
|
updateData(plotlyData, options);
|
|
Plotly.relayout(container, plotlyLayout);
|
|
}
|
|
});
|
|
|
|
const unwatch = resizeObserver(container, () => {
|
|
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u));
|
|
});
|
|
return unwatch;
|
|
}
|
|
}, [options, data, container]);
|
|
|
|
// Cleanup when component destroyed
|
|
useEffect(() => {
|
|
if (container) {
|
|
return () => Plotly.purge(container);
|
|
}
|
|
}, [container]);
|
|
|
|
return <div className="chart-visualization-container" ref={setContainer} />;
|
|
}
|
|
|
|
PlotlyChart.propTypes = RendererPropTypes;
|