mirror of
https://github.com/getredash/redash.git
synced 2026-03-22 10:00:17 -04:00
* Allow touch action on dashboard grid * Deactivate touch when resizing widgets * Disable touch interactions on Plotly * Update Plotly and use dragmode: false * Remove autoFocus from ItemsList search * Fix spacing for queries and dashboard favorites * Make sure admin pages don't go over 100% width
77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
import { isArray, isObject } from "lodash";
|
|
import React, { useState, useEffect, useContext } from "react";
|
|
import useMedia from "use-media";
|
|
import { ErrorBoundaryContext } from "@/components/ErrorBoundary";
|
|
import { RendererPropTypes } from "@/visualizations/prop-types";
|
|
import resizeObserver from "@/services/resizeObserver";
|
|
|
|
import getChartData from "../getChartData";
|
|
import { Plotly, prepareData, prepareLayout, updateData, applyLayoutFixes } from "../plotly";
|
|
|
|
function catchErrors(func, errorHandler) {
|
|
return (...args) => {
|
|
try {
|
|
return func(...args);
|
|
} catch (error) {
|
|
errorHandler.handleError(error);
|
|
}
|
|
};
|
|
}
|
|
|
|
export default function PlotlyChart({ options, data }) {
|
|
const [container, setContainer] = useState(null);
|
|
const errorHandler = useContext(ErrorBoundaryContext);
|
|
const isMobile = useMedia({ maxWidth: 768 });
|
|
|
|
useEffect(
|
|
catchErrors(() => {
|
|
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), dragmode: !isMobile ? "zoom" : false };
|
|
|
|
// It will auto-purge previous graph
|
|
Plotly.newPlot(container, plotlyData, plotlyLayout, plotlyOptions).then(
|
|
catchErrors(() => {
|
|
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u));
|
|
}, errorHandler)
|
|
);
|
|
|
|
container.on(
|
|
"plotly_restyle",
|
|
catchErrors(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);
|
|
}
|
|
}, errorHandler)
|
|
);
|
|
|
|
const unwatch = resizeObserver(
|
|
container,
|
|
catchErrors(() => {
|
|
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u));
|
|
}, errorHandler)
|
|
);
|
|
return unwatch;
|
|
}
|
|
}, errorHandler),
|
|
[options, data, container, isMobile]
|
|
);
|
|
|
|
// Cleanup when component destroyed
|
|
useEffect(() => {
|
|
if (container) {
|
|
return () => Plotly.purge(container);
|
|
}
|
|
}, [container]);
|
|
|
|
return <div className="chart-visualization-container" ref={setContainer} />;
|
|
}
|
|
|
|
PlotlyChart.propTypes = RendererPropTypes;
|