|
@@ -44,6 +44,8 @@ import {
|
|
|
useModule,
|
|
|
} from "../../utils/hooks";
|
|
|
import { darkThemeTemplate } from "../../themes/darkThemeTemplate";
|
|
|
+import { Figure } from "react-plotly.js";
|
|
|
+import { ligthenPayload } from "../../context/wsUtils";
|
|
|
|
|
|
const Plot = lazy(() => import("react-plotly.js"));
|
|
|
|
|
@@ -66,6 +68,7 @@ interface ChartProp extends TaipyActiveProps, TaipyChangeProps {
|
|
|
template_Light_?: string;
|
|
|
//[key: `selected_${number}`]: number[];
|
|
|
figure?: Array<Record<string, unknown>>;
|
|
|
+ onClick?: string;
|
|
|
}
|
|
|
|
|
|
interface ChartConfig {
|
|
@@ -175,6 +178,23 @@ const MARKER_TO_COL = ["color", "size", "symbol", "opacity", "colors"];
|
|
|
|
|
|
const isOnClick = (types: string[]) => (types?.length ? types.every((t) => t === "pie") : false);
|
|
|
|
|
|
+interface Axis {
|
|
|
+ p2c: () => number;
|
|
|
+ p2d: (a: number) => number;
|
|
|
+}
|
|
|
+interface PlotlyMap {
|
|
|
+ _subplot?: { xaxis: Axis; yaxis: Axis };
|
|
|
+}
|
|
|
+interface PlotlyDiv extends HTMLDivElement {
|
|
|
+ _fullLayout?: {
|
|
|
+ map?: PlotlyMap;
|
|
|
+ geo?: PlotlyMap;
|
|
|
+ mapbox?: PlotlyMap;
|
|
|
+ xaxis?: Axis;
|
|
|
+ yaxis?: Axis;
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
interface WithpointNumbers {
|
|
|
pointNumbers: number[];
|
|
|
}
|
|
@@ -263,6 +283,7 @@ const Chart = (props: ChartProp) => {
|
|
|
data = emptyData,
|
|
|
onRangeChange,
|
|
|
propagate = true,
|
|
|
+ onClick,
|
|
|
} = props;
|
|
|
const dispatch = useDispatch();
|
|
|
const [selected, setSelected] = useState<number[][]>([]);
|
|
@@ -575,9 +596,45 @@ const Chart = (props: ChartProp) => {
|
|
|
]
|
|
|
);
|
|
|
|
|
|
- const onAfterPlot = useCallback(() => {
|
|
|
- // Manage loading Animation ... One day
|
|
|
- }, []);
|
|
|
+ const clickHandler = useCallback(
|
|
|
+ (evt?: MouseEvent) => {
|
|
|
+ const map =
|
|
|
+ (evt?.currentTarget as PlotlyDiv)?._fullLayout?.map ||
|
|
|
+ (evt?.currentTarget as PlotlyDiv)?._fullLayout?.geo ||
|
|
|
+ (evt?.currentTarget as PlotlyDiv)?._fullLayout?.mapbox;
|
|
|
+ const xaxis = map ? map._subplot?.xaxis : (evt?.currentTarget as PlotlyDiv)?._fullLayout?.xaxis;
|
|
|
+ const yaxis = map ? map._subplot?.xaxis : (evt?.currentTarget as PlotlyDiv)?._fullLayout?.yaxis;
|
|
|
+ if (!xaxis || !yaxis) {
|
|
|
+ console.info("clickHandler: Plotly div does not have an xaxis object", evt);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const transform = (axis: Axis, delta: keyof DOMRect) => {
|
|
|
+ const bb = (evt?.target as HTMLDivElement).getBoundingClientRect();
|
|
|
+ return (pos?: number) => axis.p2d((pos || 0) - (bb[delta] as number));
|
|
|
+ };
|
|
|
+ dispatch(
|
|
|
+ createSendActionNameAction(
|
|
|
+ id,
|
|
|
+ module,
|
|
|
+ ligthenPayload({
|
|
|
+ action: onClick,
|
|
|
+ lat: map ? yaxis.p2c() : undefined,
|
|
|
+ y: map ? undefined : transform(yaxis, "top")(evt?.clientY),
|
|
|
+ lon: map ? xaxis.p2c() : undefined,
|
|
|
+ x: map ? undefined : transform(xaxis, "left")(evt?.clientX),
|
|
|
+ })
|
|
|
+ )
|
|
|
+ );
|
|
|
+ },
|
|
|
+ [dispatch, module, id, onClick]
|
|
|
+ );
|
|
|
+
|
|
|
+ const onInitialized = useCallback(
|
|
|
+ (figure: Readonly<Figure>, graphDiv: Readonly<HTMLElement>) => {
|
|
|
+ onClick && graphDiv.addEventListener("click", clickHandler);
|
|
|
+ },
|
|
|
+ [onClick, clickHandler]
|
|
|
+ );
|
|
|
|
|
|
const getRealIndex = useCallback(
|
|
|
(index?: number) =>
|
|
@@ -585,8 +642,8 @@ const Chart = (props: ChartProp) => {
|
|
|
? props.figure
|
|
|
? index
|
|
|
: data[dataKey].tp_index
|
|
|
- ? (data[dataKey].tp_index[index] as number)
|
|
|
- : index
|
|
|
+ ? (data[dataKey].tp_index[index] as number)
|
|
|
+ : index
|
|
|
: 0,
|
|
|
[data, dataKey, props.figure]
|
|
|
);
|
|
@@ -631,11 +688,11 @@ const Chart = (props: ChartProp) => {
|
|
|
layout={layout}
|
|
|
style={style}
|
|
|
onRelayout={onRelayout}
|
|
|
- onAfterPlot={onAfterPlot}
|
|
|
onSelected={onSelect}
|
|
|
onDeselect={onSelect}
|
|
|
config={plotConfig}
|
|
|
useResizeHandler
|
|
|
+ onInitialized={onInitialized}
|
|
|
/>
|
|
|
) : (
|
|
|
<Plot
|
|
@@ -643,12 +700,12 @@ const Chart = (props: ChartProp) => {
|
|
|
layout={layout}
|
|
|
style={style}
|
|
|
onRelayout={onRelayout}
|
|
|
- onAfterPlot={onAfterPlot}
|
|
|
onSelected={isOnClick(config.types) ? undefined : onSelect}
|
|
|
onDeselect={isOnClick(config.types) ? undefined : onSelect}
|
|
|
onClick={isOnClick(config.types) ? onSelect : undefined}
|
|
|
config={plotConfig}
|
|
|
useResizeHandler
|
|
|
+ onInitialized={onInitialized}
|
|
|
/>
|
|
|
)}
|
|
|
</Suspense>
|