File size: 9,901 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//@ts-nocheck
import {
  Annotations,
  PlotMouseEvent,
  PlotlyHTMLElement,
} from "plotly.js-dist-min";
import { Figure } from "react-plotly.js";

type PopupData = {
  x: number;
  y: number;
  yref: string;
  text: string;
  yshift: number;
  yanchor: string;
  bordercolor: string;
  color: string;
  size: number;
  bgcolor?: string;
  arrowcolor?: string;
  arrowsize?: number;
  arrowwidth?: number;
  annotation?: any;
  high?: number;
  low?: number;
};

export function add_annotation({
  plotData,
  popup_data,
  current_text,
}: {
  plotData: Figure;
  popup_data: PopupData;
  current_text?: string;
}) {
  const x = popup_data.x;
  let y = popup_data.y;
  const yref = popup_data.yref;
  const annotations = plotData?.layout?.annotations || [];
  let index = -1;

  for (let i = 0; i < annotations.length; i++) {
    if (
      annotations[i].x === x &&
      annotations[i].y === y &&
      annotations[i].text === current_text
    ) {
      index = i;
      break;
    }
  }

  if (popup_data.high !== undefined) {
    y = popup_data.yanchor === "above" ? popup_data.high : popup_data.low;
  }
  if (index === -1) {
    const annotation: Annotations = {
      x: x,
      y: y,
      xref: "x",
      yref: yref,
      xanchor: "center",
      text: popup_data.text,
      showarrow: true,
      arrowhead: 2,
      arrowsize: popup_data.arrowsize || 1,
      arrowwidth: popup_data.arrowwidth || 2,
      ax: x,
      ay: y + popup_data.yshift,
      ayref: yref,
      axref: "x",
      bordercolor: popup_data.bordercolor,
      bgcolor: popup_data.bgcolor || "#000000",
      arrowcolor: popup_data.arrowcolor || popup_data.bordercolor,
      borderwidth: 2,
      borderpad: 4,
      opacity: 0.8,
      font: {
        color: popup_data.color,
        size: popup_data.size,
      },
      clicktoshow: "onoff",
      captureevents: true,
      high: popup_data.high || undefined,
      low: popup_data.low || undefined,
    };
    annotations.push(annotation);
  } else {
    annotations[index].y = y;
    annotations[index].text = popup_data.text;
    annotations[index].font.color = popup_data.color;
    annotations[index].font.size = popup_data.size;
    annotations[index].ay = y + popup_data.yshift;
    annotations[index].bordercolor = popup_data.bordercolor;
    annotations[index].bgcolor = popup_data.bgcolor || "#000000";
    annotations[index].arrowcolor = popup_data.arrowcolor || popup_data.bordercolor;
    annotations[index].arrowsize = popup_data.arrowsize || 1;
    annotations[index].arrowwidth = popup_data.arrowwidth || 2;
    annotations[index].high = popup_data.high || undefined;
    annotations[index].low = popup_data.low || undefined;
  }
  return { annotations: annotations, annotation: annotations[index] };
}

export function plot_text({
  plotData,
  popup_data,
  current_text,
}: {
  plotData: Figure;
  popup_data: PopupData;
  current_text?: string;
}) {
  // Plots text on the chart based on the popup_data
  // If current_text is not null, it will be replaced with the new text
  // If current_text is null, a new annotation will be added
  // popup_data is the data from the popup
  // data is the data from the chart

  console.log("plot_text: current_text", current_text);
  let output = undefined;
  const yaxis = popup_data.yref.replace("y", "yaxis");
  const yrange = plotData.layout[yaxis].range;
  let yshift = (yrange[1] - yrange[0]) * 0.2;

  if (popup_data.yanchor === "below") {
    yshift = -yshift;
  }
  popup_data.yshift = yshift;

  output = add_annotation({ plotData, popup_data, current_text });

  const to_update = { annotations: output.annotations, dragmode: "pan" };
  to_update[`${yaxis}.type`] = "linear";
  return { update: to_update, annotation: output.annotation };
}

export function init_annotation({
  plotData,
  popupData,
  setPlotData,
  setModal,
  setOnAnnotationClick,
  setAnnotations,
  onAnnotationClick,
  ohlcAnnotation,
  setOhlcAnnotation,
  annotations,
  plotDiv,
}: {
  plotData: Figure;
  popupData: Partial<PopupData>;
  setPlotData: (plotData: Partial<Figure>) => void;
  setModal: (modal: { name: string; data?: any }) => void;
  onAnnotationClick: any;
  setOnAnnotationClick: (onAnnotationClick: any) => void;
  setAnnotations: (annotations: Partial<Annotations>[]) => void;
  ohlcAnnotation: any;
  setOhlcAnnotation: (ohlcAnnotation: any) => void;
  annotations: Annotations[];
  plotDiv: PlotlyHTMLElement;
}) {
  if (popupData.text !== undefined && popupData.text !== "") {
    popupData.text = popupData.text.replace(/\n/g, "<br>");
    let popup_data: Partial<PopupData>;
    let inOhlc = false;

    if (popupData.annotation) {
      console.log("data", popupData);
      popup_data = {
        x: popupData.annotation.x,
        y: popupData.annotation.y,
        yref: popupData.annotation.yref,
        yanchor:
          popupData.annotation.y < popupData.annotation.ay ? "above" : "below",
        ...popupData,
      };
      if (popupData.annotation.high !== undefined) {
        inOhlc = true;
      }
      console.log("popup_data", popup_data);
      const to_update = plot_text({
        plotData,
        popup_data: popup_data as PopupData,
        current_text: popupData.annotation.text,
      });

      if (inOhlc) {
        // we update the ohlcAnnotation
        const ohlcAnnotationIndex = ohlcAnnotation.findIndex(
          (a) =>
            a.x === popupData.annotation.x &&
            a.y === popupData.annotation.y &&
            a.yref === popupData.annotation.yref,
        );
        console.log("ohlcAnnotationIndex", ohlcAnnotationIndex);
        if (ohlcAnnotationIndex === -1) {
          // we add the annotation to the ohlcAnnotation array
          setOhlcAnnotation([...ohlcAnnotation, to_update.annotation]);
        } else {
          // we replace the annotation in the ohlcAnnotation array
          ohlcAnnotation[ohlcAnnotationIndex] = to_update.annotation;
          setOhlcAnnotation(ohlcAnnotation);
        }
      }

      setAnnotations(
        [...annotations, to_update.annotation].filter((a) => a !== undefined),
      );
      plotData.layout.dragmode = "pan";
      setPlotData({ ...plotData, ...to_update.update });
      setOnAnnotationClick({});

      return;
    }

    // First remove any existing click handlers to avoid duplicates
    plotDiv.removeAllListeners("plotly_clickannotation");
    plotDiv.removeAllListeners("plotly_click");

    // Add handler for clicking on existing annotations
    plotDiv.on("plotly_clickannotation", (eventData) => {
      console.log("plotly_clickannotation", eventData);
      const annotation = eventData.annotation;

      if (annotation.text === undefined) {
        console.log("annotation.text is undefined");
        return;
      }
      console.log("annotation.text", annotation.text);
      // we replace <br> with \n so that the textarea can display the text properly
      annotation.text = annotation.text.replace(/<br>/g, "\n");

      const popup_data = {
        x: annotation.x,
        y: annotation.y,
        high: annotation?.high ?? undefined,
        low: annotation?.low ?? undefined,
        yanchor: annotation.y < annotation.ay ? "above" : "below",
        text: annotation.text,
        color: annotation.font.color,
        size: annotation.font.size,
        bordercolor: annotation.bordercolor,
        annotation: annotation,
      };

      console.log("popup_data_clickannotation", popup_data);
      setOnAnnotationClick(popup_data);
      setModal({ name: "textDialog", data: popup_data });
      setOnAnnotationClick({});
    });

    // Add handler for adding a new annotation on click
    function clickHandler(eventData: PlotMouseEvent) {
      console.log("plotly_click", eventData);
      const x = eventData.points[0].x;
      const yaxis = eventData.points[0].fullData.yaxis;
      let y = 0;
      let high;
      let low;

      // We need to check if the trace is a candlestick or not
      // this is because the y value is stored in the high or low
      if (eventData.points[0].y !== undefined) {
        y = eventData.points[0].y;
      } else if (eventData.points[0].low !== undefined) {
        high = eventData.points[0].high;
        low = eventData.points[0].low;
        if (popup_data?.yanchor === "below") {
          y = eventData.points[0].low;
        } else {
          y = eventData.points[0].high;
        }
      }

      popup_data = {
        x: onAnnotationClick?.annotation?.x ?? x,
        y: onAnnotationClick?.annotation?.y ?? y,
        yref: onAnnotationClick?.annotation?.yref ?? yaxis,
        high: onAnnotationClick?.annotation?.high ?? high,
        low: onAnnotationClick?.annotation?.low ?? low,
        ...popupData,
      };

      if (high !== undefined) {
        // save the annotation to use later
        ohlcAnnotation.push(popup_data);
        setOhlcAnnotation(ohlcAnnotation);
        console.log("ohlcAnnotation", ohlcAnnotation);
      }

      const to_update = plot_text({
        plotData,
        popup_data: popup_data as PopupData,
        current_text: onAnnotationClick?.annotation?.text,
      });

      setAnnotations(
        [...annotations, to_update.annotation].filter((a) => a !== undefined),
      );

      // Important: update plotData with the new annotations to make them visible
      plotData.layout.dragmode = "pan";
      setPlotData({ ...plotData, ...to_update.update });

      // Force a relayout to ensure annotations appear
      Plotly.relayout(plotDiv, {'annotations': to_update.update.annotations, dragmode: "pan"});

      // Remove click handler after creating the annotation
      plotDiv.removeAllListeners("plotly_click");
    }

    // Set up dragmode and add the click handler
    plotData.layout.dragmode = "select";
    setPlotData({ ...plotData });

    // Ensure we add the click handler
    plotDiv.on("plotly_click", clickHandler);
  }
}