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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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import * as d3 from "d3";
import * as d3Contour from "d3-contour";
import { Backdrop, CircularProgress, Box, Button } from "@mui/material";
import Typography from '@mui/material/Typography';
import RepeatIcon from '@mui/icons-material/Repeat';
import React, { useEffect, useRef, useState, useContext } from "react";
import TextContainer, { topicsSizeFraction }  from "./TextContainer";
import { TopicsContext } from "./UploadFileContext";
import QueryView from "./QueryView";
import HelpIcon from '@mui/icons-material/Help';
import { HtmlTooltip } from "./Map";

const bunkaDocs = "bunka_bourdieu_docs.json";
const bunkaTopics = "bunka_bourdieu_topics.json";
const bunkaQuery = "bunka_bourdieu_query.json";
const { REACT_APP_API_ENDPOINT } = process.env;

function Bourdieu() {
  const [selectedDocument, setSelectedDocument] = useState(null);
  const [mapLoading, setMapLoading] = useState(false);
  const [topicsCentroids, setTopicsCentroids] = useState([])

  const { bourdieuData: apiData, isLoading: isFileProcessing } = useContext(TopicsContext);

  const svgRef = useRef(null);
  const scatterPlotContainerRef = useRef(null);
  // Set the SVG height to match your map's desired height
  const svgHeight = window.innerHeight - document.getElementById("top-banner").clientHeight - 50;
  const svgWidth = window.innerWidth * 0.70; // Set the svg container height to match the layout
  
  const createScatterPlot = (docsData, topicsData, queryData) => {
    const margin = {
      top: 20,
      right: 20,
      bottom: 50,
      left: 50,
    };
    const plotWidth = svgWidth;
    const plotHeight = svgHeight;

    d3.select(svgRef.current).selectAll("*").remove();

    const svg = d3
      .select(svgRef.current)
      .attr("width", "100%")
      .attr("height", svgHeight);

    /**
     * SVG canvas group on which transforms apply.
     */
    const g = svg.append("g").classed("canvas", true);
    
    /**
     * Setup Zoom.
     */
    const zoom = d3.zoom()
      .scaleExtent([1, 3])
      .translateExtent([[0,0], [plotWidth, plotHeight]])
      .on("zoom", function ({ transform }) {
        g.attr(
          "transform",
          `translate(${transform.x ?? 0}, ${transform.y ?? 0}) scale(${transform.k ?? 1})`
        );
        // props.setTransform?.({
        //   x: transform.x,
        //   y: transform.y,
        //   k: transform.k
        // })
      });
    
    /**
     * Initial zoom.
    */
    svg.call(zoom);
    // const defaultTransform = { k: 1 };
    // const initialTransform = defaultTransform?.k != null
    //   ? new ZoomTransform(
    //     defaultTransform.k ?? 1,
    //     defaultTransform.x ?? 0,
    //     defaultTransform.y ?? 0
    //   )
    //   : d3.zoomIdentity;
    // svg.call(zoom.transform, initialTransform);

    // Axes
    const dimensionX = { idLeft: queryData.x_left_words[0], idRight: queryData.x_right_words[0] };
    const dimensionY = { idLeft: queryData.y_bottom_words[0], idRight: queryData.y_top_words[0] };

    const xMin = d3.min(docsData, (d) => d.x);
    const xMax = d3.max(docsData, (d) => d.x);
    const yMin = d3.min(docsData, (d) => d.y);
    const yMax = d3.max(docsData, (d) => d.y);
    const maxDomainValue = Math.max(xMax, -xMin, yMax, -yMin);

    var xScale = d3.scaleLinear()
      .domain([-maxDomainValue, maxDomainValue])
      .range([ 0, plotWidth ]);
    var yScale = d3.scaleLinear()
      .domain([-maxDomainValue, maxDomainValue])
      .range([ plotHeight, 0 ]);

    const axes = d3.create("svg:g").classed("axes", true);
    svg
      .append('defs')
      .append('marker')
      .attr('id', 'arrowhead-right')
      .attr('refX', 5)
      .attr('refY', 5)
      .attr('markerWidth', 10)
      .attr('markerHeight', 10)
      .append('path')
      .attr('d', 'M 0 0 L 5 5 L 0 10')
      .attr('stroke', 'grey')
      .attr('stroke-width', 1)
      .attr('fill', 'none');
    svg
      .append('defs')
      .append('marker')
      .attr('id', 'arrowhead-left')
      .attr('refX', 0)
      .attr('refY', 5)
      .attr('markerWidth', 10)
      .attr('markerHeight', 10)
      .append('path')
      .attr('d', 'M 5 0 L 0 5 L 5 10')
      .attr('stroke', 'grey')
      .attr('stroke-width', 1)
      .attr('fill', 'none');
    svg
      .append('defs')
      .append('marker')
      .attr('id', 'arrowhead-top')
      .attr('refX', 5)
      .attr('refY', 0)
      .attr('markerWidth', 10)
      .attr('markerHeight', 10)
      .append('path')
      .attr('d', 'M 0 5 L 5 0 L 10 5')
      .attr('stroke', 'grey')
      .attr('stroke-width', 1)
      .attr('fill', 'none');
    svg
      .append('defs')
      .append('marker')
      .attr('id', 'arrowhead-bottom')
      .attr('refX', 5)
      .attr('refY', 5)
      .attr('markerWidth', 10)
      .attr('markerHeight', 10)
      .append('path')
      .attr('d', 'M 0 0 L 5 5 L 10 0')
      .attr('stroke', 'grey')
      .attr('stroke-width', 1)
      .attr('fill', 'none');
    // X axis
    axes.append("g")
      .attr("transform", `translate(0,${plotHeight / 2})`)
      .call(
        d3.axisBottom(xScale)
          .tickSizeInner(0)
          .tickSizeOuter(0)
          .tickPadding(10)
      )
        .attr("class", "axis xAxis")
        .datum({ dimension: dimensionX })
        .select('path.domain')
          .attr("marker-start", "url(#arrowhead-left)")
          .attr("marker-end", "url(#arrowhead-right)");
    // Y axis
    axes.append("g")
      .attr("transform", `translate(${plotWidth / 2},0)`)
      .call(
        d3.axisRight(yScale)
          .tickSizeInner(0)
          .tickSizeOuter(0)
          .tickPadding(10)
      )
        .attr("class", "axis yAxis")
        .datum({ dimension: dimensionY })
        .select('path.domain')
          .attr("marker-end", "url(#arrowhead-top)")
          .attr("marker-start", "url(#arrowhead-bottom)");
    // Style the tick texts
    axes.selectAll(".tick text")
      .style("fill", "blue") // Color of the text
      .style("font-weight", "bold");

    // Show only first and last ticks
    axes.selectAll(".xAxis .tick text")
      .style('text-anchor', "middle")
      .attr('transform', (d, i, nodes) => (i === 0 || i === nodes.length - 1) ? "rotate(-90)" : "")
      .attr("visibility", (d, i, nodes) => (i === 0 || i === nodes.length - 1) ? "visible" : "hidden");
    axes.selectAll(".yAxis .tick text")
      .style('text-anchor', "start")
      .attr("visibility", (d, i, nodes) => (i === 0 || i === nodes.length - 1) ? "visible" : "hidden");
    axes.selectAll(".xAxis .tick text")
      .text((d, i, nodes) => {
        if (i === 0) {
          return dimensionX.idLeft; // Custom text for the first tick
        } else if (i === nodes.length - 1) {
          return dimensionX.idRight;  // Custom text for the last tick
        }
        return d; // Default text for all other ticks
      });
    axes.selectAll(".yAxis .tick text")
      .text((d, i, nodes) => {
        if (i === 0) {
          return dimensionY.idLeft; // Custom text for the first tick
        } else if (i === nodes.length - 1) {
          return dimensionY.idRight;;  // Custom text for the last tick
        }
        return d; // Default text for all other ticks
      });
    /**
     * Draw Bourdieu map contents
     */
    const contourData = d3Contour
      .contourDensity()
      .x((d) => xScale(-d.x))
      .y((d) => yScale(d.y))
      .size([plotWidth, plotHeight])
      .bandwidth(30)(docsData);

    const contourLineColor = "rgb(94, 163, 252)";

    g
      .selectAll("path.contour")
      .data(contourData)
      .enter()
      .append("path")
      .attr("class", "contour")
      .attr("d", d3.geoPath())
      .style("fill", "none")
      .style("stroke", contourLineColor)
      .style("stroke-width", 1);

    const centroids = topicsData.filter((d) => d.x_centroid && d.y_centroid);
    setTopicsCentroids(centroids);

    g
      .selectAll("circle.topic-centroid")
      .data(centroids)
      .enter()
      .append("circle")
      .attr("class", "topic-centroid")
      .attr("cx", (d) => xScale(-d.x_centroid))
      .attr("cy", (d) => yScale(d.y_centroid))
      .attr("r", 8)
      .style("fill", "red")
      .style("stroke", "black")
      .style("stroke-width", 2)
      .on("click", (event, d) => {
        setSelectedDocument(d);
      });

    g
      .selectAll("text.topic-label")
      .data(centroids)
      .enter()
      .append("text")
      .attr("class", "topic-label")
      .attr("x", (d) => xScale(-d.x_centroid))
      .attr("y", (d) => yScale(d.y_centroid) - 12)
      .text((d) => d.name)
      .style("text-anchor", "middle");

    const convexHullData = topicsData.filter((d) => d.convex_hull);
    for (const d of convexHullData) {
      const hull = d.convex_hull;
      if (hull) {
        const hullPoints = hull.x_coordinates.map((x, i) => [xScale(-x), yScale(hull.y_coordinates[i])]);

        g
          .append("path")
          .datum(d3.polygonHull(hullPoints))
          .attr("class", "convex-hull-polygon")
          .attr("d", (dAttr) => `M${dAttr.join("L")}Z`)
          .style("fill", "none")
          .style("stroke", "rgba(255, 255, 255, 0.5)")
          .style("stroke-width", 2);
      }
    }
    const xGreaterThanZeroAndYGreaterThanZero = docsData.filter((d) => d.x > 0 && d.y > 0).length;
    const xLessThanZeroAndYGreaterThanZero = docsData.filter((d) => d.x < 0 && d.y > 0).length;
    const xGreaterThanZeroAndYLessThanZero = docsData.filter((d) => d.x > 0 && d.y < 0).length;
    const xLessThanZeroAndYLessThanZero = docsData.filter((d) => d.x < 0 && d.y < 0).length;

    // Calculate the total number of documents
    const totalDocuments = docsData.length;

    // Calculate the percentages
    const percentageXGreaterThanZeroAndYGreaterThanZero = (xGreaterThanZeroAndYGreaterThanZero / totalDocuments) * 100;
    const percentageXLessThanZeroAndYGreaterThanZero = (xLessThanZeroAndYGreaterThanZero / totalDocuments) * 100;
    const percentageXGreaterThanZeroAndYLessThanZero = (xGreaterThanZeroAndYLessThanZero / totalDocuments) * 100;
    const percentageXLessThanZeroAndYLessThanZero = (xLessThanZeroAndYLessThanZero / totalDocuments) * 100;

    // Add labels to display percentages in the squares
    // const squareSize = 300; // Adjust this based on your map's layout
    // const labelOffsetX = 10; // Adjust these offsets as needed
    // const labelOffsetY = 20;

    // Calculate the maximum X and Y coordinates

    // Calculate the midpoints for the squares
    const xMid = -d3.max(docsData, (d) => d.x) / 2;
    const yMid = d3.max(docsData, (d) => d.y) / 2;

    // Labels for X > 0 and Y > 0 square
    g
      .append("text")
      .attr("x", xScale(xMid))
      .attr("y", yScale(yMid))
      .text(`${percentageXGreaterThanZeroAndYGreaterThanZero.toFixed(0)}%`) // Remove the prefix
      .style("text-anchor", "middle")
      .style("fill", "dark") // Change the text color to blue
      .style("font-size", "100px") // Adjust the font size
      .style("opacity", 0.1); // Adjust the opacity (0.7 means slightly transparent)

    // Labels for X < 0 and Y > 0 square
    g
      .append("text")
      .attr("x", xScale(-xMid))
      .attr("y", yScale(yMid))
      .text(`${percentageXLessThanZeroAndYGreaterThanZero.toFixed(0)}%`) // Remove the prefix
      .style("text-anchor", "middle")
      .style("fill", "dark") // Change the text color to light blue
      .style("font-size", "100px") // Adjust the font size
      .style("opacity", 0.1); // Adjust the opacity (0.05 means slightly transparent)

    // Labels for X > 0 and Y < 0 square
    g
      .append("text")
      .attr("x", xScale(xMid))
      .attr("y", yScale(-yMid))
      .text(`${percentageXGreaterThanZeroAndYLessThanZero.toFixed(0)}%`) // Remove the prefix
      .style("text-anchor", "middle")
      .style("fill", "dark") // Change the text color to light blue
      .style("font-size", "100px") // Adjust the font size
      .style("opacity", 0.1); // Adjust the opacity (0.05 means slightly transparent)

    // Labels for X > 0 and Y < 0 square
    g
      .append("text")
      .attr("x", xScale(-xMid))
      .attr("y", yScale(-yMid))
      .text(`${percentageXLessThanZeroAndYLessThanZero.toFixed(0)}%`) // Remove the prefix
      .style("text-anchor", "middle")
      .style("fill", "dark") // Change the text color to light blue
      .style("font-size", "100px") // Adjust the font size
      .style("opacity", 0.1); // Adjust the opacity (0.05 means slightly transparent)

    const topicsPolygons = g
      .selectAll("polygon.topic-polygon")
      .data(centroids)
      .enter()
      .append("polygon")
      .attr("class", "topic-polygon")
      .attr("points", (d) => {
        const hull = d.convex_hull;
        if (hull) {
          const hullPoints = hull.x_coordinates.map((x, i) => [xScale(-x), yScale(hull.y_coordinates[i])]);
          return hullPoints.map((point) => point.join(",")).join(" ");
        }
      })
      .style("fill", "transparent")
      .style("stroke", "transparent")
      .style("stroke-width", 2);

    let currentlyClickedPolygon = null;

    /**
     * Render Axes
     */
    g.append(() => axes.node())

    topicsPolygons.on("click", (event, d) => {
      // Reset the fill color of the previously clicked polygon to transparent light grey
      if (currentlyClickedPolygon !== null) {
        currentlyClickedPolygon.style("fill", "transparent");
        currentlyClickedPolygon.style("stroke", "transparent");
      }

      // Set the fill color of the clicked polygon to transparent light grey and add a red border
      const clickedPolygon = d3.select(event.target);
      clickedPolygon.style("fill", "rgba(200, 200, 200, 0.4)");
      clickedPolygon.style("stroke", "red");

      currentlyClickedPolygon = clickedPolygon;
      if (d.top_doc_content) {
        // Render the TextContainer component with topic details
        setSelectedDocument(d);
      }
    });
  };

  useEffect(() => {
    if (REACT_APP_API_ENDPOINT === "local" || apiData === undefined) {
      setMapLoading(true);
      // Fetch the JSON data locally
      fetch(`/${bunkaDocs}`)
        .then((response) => response.json())
        .then((docsData) => {
          // Fetch the local topics data and merge it with the existing data
          fetch(`/${bunkaTopics}`)
            .then((response) => response.json())
            .then((topicsData) => {
              fetch(`/${bunkaQuery}`)
                .then((response) => response.json())
                .then((queryData) => {
                  // Call the function to create the scatter plot after data is loaded
                  createScatterPlot(docsData, topicsData, queryData);
                })
                .catch((error) => {
                  console.error("Error fetching bourdieu query data:", error);
                })
                .finally(() => {
                  setMapLoading(false);  
                });
            })
            .catch((error) => {
              console.error("Error fetching topics data:", error);
            })
            .finally(() => {
              setMapLoading(false);  
            });
        })
        .catch((error) => {
          console.error("Error fetching documents data:", error);
        })
        .finally(() => {
          setMapLoading(false);  
        });
    } else {
      // Call the function to create the scatter plot with the data provided by TopicsContext
      createScatterPlot(apiData.docs, apiData.topics, apiData.query);
    }
  }, [apiData]);
   
  const mapDescription = "This map is generated by projecting documents onto a two-dimensional space, where the axes are defined by the user. Two documents are positioned close to each other if they share a similar relationship with the axes. The documents themselves are not directly represented on the map; rather, they are aggregated into clusters. Each cluster represents a group of documents that exhibit similarities.";
   
  return (
    <div className="json-display">
      {(isFileProcessing || mapLoading) ? (
        <Backdrop open={isFileProcessing || mapLoading} style={{ zIndex: 9999 }}>
          <CircularProgress color="primary" />
        </Backdrop>
      ) : (
        <div className="scatter-plot-and-text-container">
          <div className="scatter-plot-container" ref={scatterPlotContainerRef}>
          <HtmlTooltip
            title={
              <React.Fragment>
                <Typography color="inherit">{mapDescription}</Typography>
              </React.Fragment>
            }
            followCursor
            >
              <HelpIcon style={{
                position: "relative",
                top: 10,
                left: 40,
                border: "none"
              }}/>
            </HtmlTooltip>
            <svg ref={svgRef} />
          </div>
          
          <div className="text-container">
          {selectedDocument !== null ? (
            <>
              <Box sx={{ marginBottom: "1em" }}>
                <Button sx={{ width: "100%" }} component="label" variant="outlined" startIcon={<RepeatIcon />} onClick={() => setSelectedDocument(null)}>
                  Upload another CSV file
                </Button>
              </Box>
              <TextContainer topicName={selectedDocument.name} topicSizeFraction={topicsSizeFraction(topicsCentroids, selectedDocument.size)} content={selectedDocument.top_doc_content} />
            </>
            ) : <QueryView />}
          </div>
        </div>
      )}
    </div>
  );
}

export default Bourdieu;