File size: 4,991 Bytes
ccefd0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import CommonDialog, { styleDialog } from "../Dialogs/CommonDialog";
import { useState } from "react";

export default function TitleChartDialog({
  plotlyData,
  open,
  close,
  defaultTitle,
  updateTitle,
  updateAxesTitles,
}: {
  plotlyData?: any;
  open: boolean;
  close: () => void;
  defaultTitle: string;
  updateTitle: (title: string) => void;
  updateAxesTitles: (axesTitles: any) => void;
}) {
  const [title, setTitle] = useState(defaultTitle);

  const yAxes = Object.keys(plotlyData.layout || {}).filter(
    (k) => k.startsWith("yaxis") && plotlyData.layout[k].range != undefined
  );
  const xAxes = Object.keys(plotlyData.layout || {}).filter(
    (k) =>
      k.startsWith("xaxis") &&
      plotlyData.layout[k].showticklabels != undefined &&
      plotlyData.layout[k]?.anchor
  );

  const [axesTitles, setAxesTitles] = useState<any>({});

  return (
    <CommonDialog
      title="Chart Titles"
      description="Change the titles on the chart."
      open={open}
      close={close}
    >
      <div id="popup_title" className="popup_content">
        <div style={{ display: "flex", flexDirection: "column", gap: 0 }}>
          <div>
            <label htmlFor="title_text">
              <b>Title:</b>
            </label>
            <textarea
              id="title_text"
              style={{
                ...styleDialog,
                width: "100%",
                maxWidth: "100%",
                maxHeight: "200px",
                marginTop: "8px",
                marginLeft: "0px",
              }}
              rows={2}
              cols={20}
              value={title}
              onChange={(e) => setTitle(e.target.value)}
            ></textarea>
          </div>
          <div
            id="xaxis_div"
            className="csv_column_container"
            style={{ marginTop: 5, marginBottom: -5 }}
          >
            {xAxes.map((x, i) => (
              <div key={x} style={{ marginTop: 5, marginBottom: 5 }}>
                <label htmlFor={`title_${x}`}>
                  {i === 0 ? <b>X axis:</b> : <b>X axis {i + 1}:</b>}
                </label>
                <input
                  id={`title_${x}`}
                  style={{ marginLeft: "0px", padding: "5px 2px 2px 5px" }}
                  type="text"
                  defaultValue={plotlyData?.layout[x]?.title?.text || ""}
                  onChange={(e) => {
                    setAxesTitles({
                      ...axesTitles,
                      [x]: e.target.value,
                    });
                  }}
                />
              </div>
            ))}
          </div>
          <div
            id="yaxis_div"
            className="csv_column_container"
            style={{ marginTop: 5, marginBottom: 5 }}
          >
            {yAxes.map((y, i) => (
              <div key={y} style={{ marginTop: 10 }}>
                <label htmlFor={`title_${y}`}>
                  {i === 0 ? <b>Y axis:</b> : <b>Y axis {i + 1}:</b>}
                </label>
                <input
                  id={`title_${y}`}
                  style={{ marginLeft: "0px", padding: "5px 2px 2px 5px" }}
                  type="text"
                  defaultValue={plotlyData?.layout[y]?.title?.text || ""}
                  onChange={(e) => {
                    setAxesTitles({
                      ...axesTitles,
                      [y]: e.target.value,
                    });
                  }}
                />
              </div>
            ))}
          </div>
        </div>

        <div style={{ float: "right", marginTop: 20 }}>
          <button
            className="_btn-tertiary ph-capture"
            id="title_cancel"
            onClick={close}
          >
            Cancel
          </button>
          <button
            className="_btn ph-capture"
            id="title_submit"
            onClick={() => {
              // Update parent state - this will trigger the useEffect in Chart.tsx
              updateTitle(title);
              updateAxesTitles(axesTitles);

              // Force an immediate update to the plotly chart directly
              if (window.Plotly && document.getElementById('plotlyChart')) {
                const chart = document.getElementById('plotlyChart');

                // Only update axis titles, not the main chart title
                const updateObj: { [key: string]: string } = {};

                // Add all axis title changes
                Object.entries(axesTitles).forEach(([axis, text]) => {
                  updateObj[`${axis}.title.text`] = String(text);
                });

                if (Object.keys(updateObj).length > 0) {
                  console.log("Applying immediate axis title updates:", updateObj);
                  window.Plotly.relayout(chart, updateObj);
                }
              }

              close();
            }}
          >
            Submit
          </button>
        </div>
      </div>
    </CommonDialog>
  );
}