summaryrefslogtreecommitdiffstats
path: root/frontend-components/plotly/src/components/Dialogs/TextChartDialog.tsx
blob: d6c0f5afb634b7f32ef397e9d4eec70ce5714a8e (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import CommonDialog from "./CommonDialog";
import { useState } from "react";

const style = {
  padding: "5px 2px 2px 5px",
  margin: "2px 0",
};

export default function TextChartDialog({
  open,
  close,
  addAnnotation,
  deleteAnnotation,
  popupData,
}: {
  plotlyData: any;
  open: boolean;
  close: () => void;
  addAnnotation: (annotation: any) => void;
  updateAnnotation: (annotation: any) => void;
  deleteAnnotation: (annotation: any) => void;
  popupData: any | null;
}) {
  const defaultPopupData = {
    text: "",
    color: "#0088CC",
    size: 18,
    bordercolor: "#822661",
    yanchor: "above",
  };
  const [popUpData, setPopUpData] = useState<any>(defaultPopupData);
  const [newPopupData, setNewPopupData] = useState<any>(defaultPopupData);

  if (popupData && popupData !== popUpData) {
    if (popupData.annotation) {
      popupData.annotation = popupData?.annotation || {};
      setPopUpData(popupData);
      setNewPopupData(popupData);
    }
  }

  function onClose() {
    console.log("closing");
    setPopUpData(defaultPopupData);
    setNewPopupData(defaultPopupData);
    close();
  }

  function onChange(e: any) {
    console.log(e.target.id.replace("addtext_", ""), e.target.value);
    const name = e.target.id.replace("addtext_", "");
    const value =
      e.target.type === "checkbox"
        ? e.target.checked
          ? 1
          : 0
        : e.target.value;
    setNewPopupData({ ...newPopupData, [name]: value });
  }

  function onSubmit() {
    console.log("submitting", newPopupData);
    if (newPopupData.text !== "") {
      let above = ["above", 1].includes(newPopupData.yanchor);
      newPopupData.yanchor = above ? "above" : "below";

      if (popUpData?.annotation) {
        setNewPopupData({ ...newPopupData, annotation: popUpData.annotation });
      }
      addAnnotation(newPopupData);
      close();
    } else {
      document.getElementById("popup_textarea_warning")!.style.display =
        "block";
      document.getElementById("addtext_text")!.style.border = "1px solid red";
    }
  }
  function onDelete() {
    deleteAnnotation(popUpData);
    onClose();
  }

  return (
    <CommonDialog
      title="Add Text to Chart"
      description="Change the titles on the chart."
      open={open}
      close={onClose}
    >
      <div id="popup_title" className="popup_content">
        <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
          <div style={{ marginBottom: 20 }}>
            <label htmlFor="popup_text">
              <b>Text:</b>
              <div id="popup_textarea_warning" className="popup_warning">
                Text is required
              </div>
            </label>
            <textarea
              id="addtext_text"
              style={{
                ...style,
                width: "100%",
                maxWidth: "100%",
                maxHeight: "200px",
                marginTop: "8px",
              }}
              rows={4}
              cols={50}
              placeholder="Enter text here"
              onChange={onChange}
              defaultValue={popUpData?.annotation?.text || newPopupData?.text}
            ></textarea>
          </div>
          <div style={{ display: "flex", justifyContent: "space-between" }}>
            <div>
              <label htmlFor="addtext_color">
                <b>Font color:</b>
              </label>
              <input
                type="color"
                id="addtext_color"
                defaultValue={
                  popUpData?.annotation?.color || newPopupData?.color
                }
                onChange={onChange}
              ></input>
            </div>
            <div>
              <label htmlFor="addtext_bordercolor">
                <b>Border color:</b>
              </label>
              <input
                type="color"
                id="addtext_bordercolor"
                defaultValue={
                  popUpData?.annotation?.bordercolor ||
                  newPopupData?.bordercolor
                }
                onChange={onChange}
              ></input>
            </div>
          </div>

          <div style={{ display: "flex", justifyContent: "space-between" }}>
            <div>
              <label htmlFor="addtext_size">
                <b>Font size:</b>
              </label>
              <input
                style={{ ...style, width: "45px" }}
                type="number"
                id="addtext_size"
                onChange={onChange}
                defaultValue={popUpData?.annotation?.size || newPopupData?.size}
              ></input>
            </div>
            <div>
              <label htmlFor="addtext_yanchor">
                <b>Position (Above):</b>
              </label>
              <input
                type="checkbox"
                id="addtext_yanchor"
                name="check"
                defaultChecked={
                  newPopupData?.yanchor === "above" ? true : false
                }
                onChange={onChange}
              ></input>
            </div>
          </div>
        </div>

        <div style={{ float: "right", marginTop: 20 }}>
          <button
            className="_btn-tertiary ph-capture"
            id="title_cancel"
            onClick={onClose}
          >
            Cancel
          </button>
          <button
            className="_btn ph-capture"
            id="title_delete"
            onClick={onDelete}
          >
            Delete
          </button>
          <button
            className="_btn ph-capture"
            id="title_submit"
            onClick={onSubmit}
          >
            Submit
          </button>
        </div>
      </div>
    </CommonDialog>
  );
}