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" ;
3
3
import { ensure } from "@mendix/pluggable-widgets-tools" ;
4
4
import { HeatMapContainerProps } from "../../typings/HeatMapProps" ;
5
5
import { ChartWidgetProps , compareAttrValuesAsc } from "@mendix/shared-charts/main" ;
6
6
import { executeAction } from "@mendix/widget-plugin-platform/framework/execute-action" ;
7
7
import Big from "big.js" ;
8
+ import { PlotDatum } from "plotly.js-dist-min" ;
8
9
9
10
type HeatMapDataSeriesHooks = Pick <
10
11
HeatMapContainerProps ,
@@ -22,6 +23,7 @@ type HeatMapDataSeriesHooks = Pick<
22
23
| "verticalAxisAttribute"
23
24
| "verticalSortAttribute"
24
25
| "verticalSortOrder"
26
+ | "seriesItemSelection"
25
27
> ;
26
28
27
29
type AttributeValue = string | number | Date | undefined ;
@@ -33,6 +35,7 @@ type LocalHeatMapData = {
33
35
verticalAxisValue : AttributeValue ;
34
36
horizontalSortValue : string | Big | Date | undefined ;
35
37
verticalSortValue : string | Big | Date | undefined ;
38
+ id : string ;
36
39
} ;
37
40
38
41
function getUniqueValues < T > ( values : T [ ] ) : T [ ] {
@@ -64,20 +67,28 @@ export const useHeatMapDataSeries = ({
64
67
tooltipHoverText,
65
68
verticalAxisAttribute,
66
69
verticalSortAttribute,
67
- verticalSortOrder
70
+ verticalSortOrder,
71
+ seriesItemSelection
68
72
} : HeatMapDataSeriesHooks ) : HeatMapHookData => {
69
73
const [ heatmapChartData , setHeatMapData ] = useState < LocalHeatMapData [ ] > ( [ ] ) ;
74
+ const objectMap = useRef < Map < string , ObjectItem > > ( new Map ( ) ) ;
70
75
71
76
useEffect ( ( ) => {
72
77
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
+ } ) ;
81
92
setHeatMapData ( dataSourceItems ) ;
82
93
}
83
94
} , [
@@ -90,7 +101,32 @@ export const useHeatMapDataSeries = ({
90
101
verticalSortAttribute
91
102
] ) ;
92
103
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
+ ) ;
94
130
95
131
return useMemo < HeatMapHookData > ( ( ) => {
96
132
// `Array.reverse` mutates, so we make a copy.
0 commit comments