1- import React from 'react'
2- import Plot from 'react-plotly.js'
3- import { Data , Layout } from 'plotly.js'
1+ import React , { MutableRefObject , useRef } from 'react'
2+ import Plot , { Figure } from 'react-plotly.js'
3+ import { Data , Layout , PlotData , PlotlyHTMLElement } from 'plotly.js'
44import { inject , observer } from 'mobx-react'
55import SurveyEntry from '../model/surveyEntry'
66import { injectClause , StoreProps } from '../stores/storeHelper'
7-
7+ // Define the shape of the figure argument passed by react-plotly.js
8+ /*interface PlotlyFigure {
9+ data: PlotData[];
10+ layout: Partial<Layout>;
11+ frames: any[] | null;
12+ }*/
13+
814class BoxPlot extends React . Component < StoreProps > {
915
16+
1017 defaultBoxConfig : Partial < Data > = {
1118 type : 'box' ,
1219 boxmean : 'sd' ,
1320 // boxpoints: 'all',
1421 // jitter: 0.3,
1522 // pointpos: -1.8
1623 }
24+ // Create a reference to store the plot instance
25+ //plotRef: MutableRefObject<HTMLElement | null> = useRef(null);
26+
1727
1828 render ( ) : JSX . Element {
1929 /*
@@ -32,6 +42,52 @@ class BoxPlot extends React.Component<StoreProps> {
3242 )
3343 }
3444
45+ /*
46+ render(): JSX.Element {
47+ return (
48+ <div style={{position: 'absolute', top: 0, bottom: 0, left:0, right: 0, overflow: 'auto'}}>
49+ <Plot
50+ data={this.data}
51+ layout={this.layout}
52+ style={{width: '100%', height: '100%'}}
53+ // Store reference and trigger hover when first loaded
54+ onInitialized={this.initializ}
55+ // Re-trigger hover if the plot layout updates or resizes
56+ onUpdate={this.update}
57+ // Re-trigger hover when user moves mouse away
58+ onUnhover={this.unhover}
59+
60+ />
61+ </div>
62+ )
63+ }
64+
65+ pinBoxHover(plotElement: HTMLElement | null): void {
66+ // Cast window or element to access Fx API safely in TypeScript
67+ const plotlyModule = (window as any).Plotly || (plotElement as any)?.Plotly;
68+
69+ if (plotElement && plotlyModule) {
70+ plotlyModule.Fx.hover(plotElement, [
71+ { curveNumber: 0, pointNumber: 0 }
72+ ]);
73+ }
74+
75+ };
76+
77+ initializ(figure: Readonly<Figure>, graphDiv: Readonly<HTMLElement>): void {
78+ this.plotRef.current = graphDiv;
79+ this.pinBoxHover(graphDiv);
80+ }
81+
82+ update(figure: Readonly<Figure>): void {
83+ this.pinBoxHover(this.plotRef.current);
84+ }
85+
86+ unhover(): void {
87+ this.pinBoxHover(this.plotRef.current);
88+ }*/
89+
90+
3591 private get data ( ) : Data [ ] { // TODO: Plotty Data
3692 const resultList = this . props . uiStore ! . filteredData
3793 const selectedYearStr = this . props . controlStore ! . controlState . selectedYear
@@ -47,7 +103,7 @@ class BoxPlot extends React.Component<StoreProps> {
47103 const trace : Data = {
48104 type : 'box' ,
49105 boxmean : 'sd' ,
50- name : 'Year' + selectedYearStr ,
106+ name : 'Year ' + selectedYearStr ,
51107 y : yearData . map ( ( entry : SurveyEntry ) => entry . salary ) ,
52108 } ;
53109 return [ trace ] ;
@@ -57,10 +113,14 @@ class BoxPlot extends React.Component<StoreProps> {
57113 return {
58114 autosize : true ,
59115 showlegend : false ,
116+ // 'x' mode forces plotly to show the summary statistics for the column on that X coordinate
117+ // hovermode: 'x',
118+ //hovermode: 'closest',
60119 yaxis : { fixedrange : true } ,
61120 xaxis : { fixedrange : true } ,
62121 paper_bgcolor : '#FF000000' ,
63- plot_bgcolor : '#FF000000'
122+ plot_bgcolor : '#FF000000' ,
123+ // hoverdistance: -1
64124 }
65125 }
66126}
0 commit comments