1- import { ValueStatus } from "mendix" ;
2- import { useEffect , useMemo , useState } from "react" ;
1+ import { ValueStatus , ObjectItem } from "mendix" ;
2+ import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
33import { ensure } from "@mendix/pluggable-widgets-tools" ;
44import { HeatMapContainerProps } from "../../typings/HeatMapProps" ;
55import { ChartWidgetProps , compareAttrValuesAsc } from "@mendix/shared-charts/main" ;
66import { executeAction } from "@mendix/widget-plugin-platform/framework/execute-action" ;
77import Big from "big.js" ;
8+ import { PlotDatum } from "plotly.js-dist-min" ;
89
910type HeatMapDataSeriesHooks = Pick <
1011 HeatMapContainerProps ,
@@ -22,6 +23,7 @@ type HeatMapDataSeriesHooks = Pick<
2223 | "verticalAxisAttribute"
2324 | "verticalSortAttribute"
2425 | "verticalSortOrder"
26+ | "seriesItemSelection"
2527> ;
2628
2729type AttributeValue = string | number | Date | undefined ;
@@ -33,6 +35,7 @@ type LocalHeatMapData = {
3335 verticalAxisValue : AttributeValue ;
3436 horizontalSortValue : string | Big | Date | undefined ;
3537 verticalSortValue : string | Big | Date | undefined ;
38+ id : string ;
3639} ;
3740
3841function getUniqueValues < T > ( values : T [ ] ) : T [ ] {
@@ -64,20 +67,28 @@ export const useHeatMapDataSeries = ({
6467 tooltipHoverText,
6568 verticalAxisAttribute,
6669 verticalSortAttribute,
67- verticalSortOrder
70+ verticalSortOrder,
71+ seriesItemSelection
6872} : HeatMapDataSeriesHooks ) : HeatMapHookData => {
6973 const [ heatmapChartData , setHeatMapData ] = useState < LocalHeatMapData [ ] > ( [ ] ) ;
74+ const objectMap = useRef < Map < string , ObjectItem > > ( new Map ( ) ) ;
7075
7176 useEffect ( ( ) => {
7277 if ( seriesDataSource . status === ValueStatus . Available && seriesDataSource . items ) {
73- const dataSourceItems = seriesDataSource . items . map ( dataSourceItem => ( {
74- value : ensure ( seriesValueAttribute ) . get ( dataSourceItem ) . value ?. toNumber ( ) ,
75- hoverText : tooltipHoverText ?. get ( dataSourceItem ) . value ,
76- horizontalAxisValue : formatValueAttribute ( horizontalAxisAttribute ?. get ( dataSourceItem ) . value ) ,
77- horizontalSortValue : horizontalSortAttribute ?. get ( dataSourceItem ) . value ,
78- verticalAxisValue : formatValueAttribute ( verticalAxisAttribute ?. get ( dataSourceItem ) . value ) ,
79- verticalSortValue : verticalSortAttribute ?. get ( dataSourceItem ) . value
80- } ) ) ;
78+ objectMap . current = new Map ( ) ;
79+ const dataSourceItems = seriesDataSource . items . map ( dataSourceItem => {
80+ objectMap . current . set ( dataSourceItem . id , dataSourceItem ) ;
81+ const item = {
82+ value : ensure ( seriesValueAttribute ) . get ( dataSourceItem ) . value ?. toNumber ( ) ,
83+ hoverText : tooltipHoverText ?. get ( dataSourceItem ) . value ,
84+ horizontalAxisValue : formatValueAttribute ( horizontalAxisAttribute ?. get ( dataSourceItem ) . value ) ,
85+ horizontalSortValue : horizontalSortAttribute ?. get ( dataSourceItem ) . value ,
86+ verticalAxisValue : formatValueAttribute ( verticalAxisAttribute ?. get ( dataSourceItem ) . value ) ,
87+ verticalSortValue : verticalSortAttribute ?. get ( dataSourceItem ) . value ,
88+ id : dataSourceItem . id
89+ } ;
90+ return item ;
91+ } ) ;
8192 setHeatMapData ( dataSourceItems ) ;
8293 }
8394 } , [
@@ -90,7 +101,32 @@ export const useHeatMapDataSeries = ({
90101 verticalSortAttribute
91102 ] ) ;
92103
93- const onClick = useMemo ( ( ) => ( onClickAction ? ( ) => executeAction ( onClickAction ) : undefined ) , [ onClickAction ] ) ;
104+ const onClick = useCallback (
105+ ( item : ObjectItem , data : PlotDatum ) => {
106+ let selectedObjectItem : ObjectItem | undefined = item ;
107+ if ( selectedObjectItem === null || selectedObjectItem === undefined ) {
108+ const selectedLocalHeatmapData = heatmapChartData . values ( ) . find ( heatMapPointData => {
109+ return (
110+ heatMapPointData . horizontalAxisValue === data . x &&
111+ heatMapPointData . verticalAxisValue === data . y &&
112+ heatMapPointData . value === data . z
113+ ) ;
114+ } ) ;
115+
116+ if ( selectedLocalHeatmapData ) {
117+ selectedObjectItem = objectMap . current . get ( selectedLocalHeatmapData . id ) ;
118+ }
119+ }
120+
121+ if ( selectedObjectItem ) {
122+ executeAction ( onClickAction ?. get ( selectedObjectItem ) ) ;
123+ if ( seriesItemSelection && seriesItemSelection . type === "Single" ) {
124+ seriesItemSelection . setSelection ( selectedObjectItem ) ;
125+ }
126+ }
127+ } ,
128+ [ onClickAction , heatmapChartData , seriesItemSelection ]
129+ ) ;
94130
95131 return useMemo < HeatMapHookData > ( ( ) => {
96132 // `Array.reverse` mutates, so we make a copy.
0 commit comments