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
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
// GLOBAL VARIABLES //
// Set dimensions 
const width = 360

let dimensions = {
    width: width,
    height: width,
    margin: {
        top: 20, 
        right: 20,
        bottom: 100,
        left: 100,
    },
}

dimensions.boundedWidth = dimensions.width
    - dimensions.margin.left
    - dimensions.margin.right

dimensions.boundedHeight = dimensions.height
    - dimensions.margin.top
    - dimensions.margin.bottom

// Create scales 
const xScale = d3.scaleLinear()
    .range([0, dimensions.boundedWidth])

const yScale = d3.scaleLinear()
    .range([dimensions.boundedHeight, 0])

const xAxisGenerator = d3.axisBottom()
    .scale(xScale)

const yAxisGenerator = d3.axisLeft()
    .scale(yScale)


//to draw circles
const xAccessor = d => (parseFloat(d.xfrom) + parseFloat(d.xto)) / 2 + 0.3 * Math.random() //hack to spread points a wee bit 
const yAccessor = d => (parseFloat(d.yfrom) + parseFloat(d.yto)) / 2 + 0.3 * Math.random() //hack to spread points a wee bit that overlap exactly

//to draw rects 
const xfromAccessor = d => parseFloat(d.xfrom) + 0.05
const yfromAccessor = d => parseFloat(d.yfrom) + 0.05
const xtoAccessor = d => parseFloat(d.xto) - 0.05
const ytoAccessor = d => parseFloat(d.yto) - 0.05

// to draw text
const imgTitle = d => d.name
const imgAuthor = d => d.author
const srcURL = d => d.url
const imgURL = d => d.imgURL
const domainAccessor = d => d.primary_domain
const secondaryDomainAccessor = d => d.secondary_domain

const tooltip = d3.select("#tooltip")
const imgTitleTT = d3.select("#name-title")
const imgAuthorTT = d3.select("#author-title")
const srcURLTT = d3.select("#link-title")
const domainAccessorTT = d3.select("#primary-domain-title")
const secondDomainAccessorTT = d3.select("#secondary-domain-title")
const imgPreview = d3.select("#preview-img")

// CMT rationale variables
const structuralAccessor = d => d.structural_details
const ontologicalAccessor = d => d.ontological_details
const orientationalAccessor = d => d.orientational_details
const imagisticAccessor = d => d.imagistic_details
const textGraphicMetaphorAcc = d => d.text_support_graphical_metaphors
const textGraphicMetaphorDetailsAcc = d => d.type_text_graphical_metaphor

const structTT = d3.select("#structural-details")
const ontoTT =  d3.select("#ontological-details")
const orientTT = d3.select("#orientational-details")
const imagTT = d3.select("#imagistic-details")
const textMetTT = d3.select("#text-support-graphical-metaphors")
const textMetTypeTT = d3.select("#text-support-graphical-metaphors-details")


function domainTick(whichDomain) {
    if (whichDomain == "Biomedicine") {
        //const yTick = [10, 20, 30, 40, 50, 60, 70, 80, 90]
        const yTickStr = [
            'molecules',
            'viruses',
            'cells',
            'bacteria',
            'tissues',
            'organs',
            'organ systems',
            'organism',
            'population',

        ]
        //const xTick = [10, 20, 30, 40, 50, 60, 70, 80, 90]
        const xTickStr = [
            "nanosec", 
            "sec",
            "min",
            "hour",
            "day",
            "week",
            "month",
            "year"
        ]
        let domainColor = "#009488"
        return [xTickStr, yTickStr, domainColor]

    } else if (whichDomain == "Climate") {
        //const yTick = [10, 20, 30]
        const yTickStr = [
            "local",
            "regional",
            "global"
        ]
        //const xTick = [10, 20, 30, 40, 50]
        const xTickStr = [
            "<1 year",
            "decade",
            "century",
            "thousand",
            "" //HANNA check this 
        ]
        let domainColor = "#3ECCBF"
        return [xTickStr, yTickStr, domainColor]

    } else if (whichDomain == "Space") {
        //const yTick = [10, 20, 30, 40, 50, 60]
        const yTickStr = [
            "objects in space",
            "planet",
            "star",
            "black holes",
            "galaxy",
            "universe",
        ]
        //const xTick = [10, 20, 30, 40]
        const xTickStr = [
            "<1 yr",
            "decade",
            "million",
            "billion"
        ]
        let domainColor = "#7ACCC4"
        return [xTickStr, yTickStr, domainColor]

    } else if (whichDomain == "Anthropology") {
        //const yTick = [10, 20, 30, 40, 50]
        const yTickStr = [
            "local",
            "regional",
            "continental",
            "intercontinental",
            "global"
        ]
        //const xTick = [10, 20, 30, 40, 50, 60, 70, 80]
        const xTickStr = [
            "<1 yr",
            "decade",
            "century",
            "quincent.",
            "thousand",
            "centamill.",
            "million",
            "billion"
        ]
        let domainColor = "#A7C7C4"
        return [xTickStr, yTickStr, domainColor]
    }

}

function onMouseEnter(e, datum) {
    
    const barLengths = [
        {"metaphorType" : "Str", "count" : parseInt(datum.cnt_structural)},
        {"metaphorType" : "Ont", "count" : parseInt(datum.cnt_ontological)},
        {"metaphorType" : "Ori", "count" : parseInt(datum.cnt_orientational)},
        {"metaphorType" : "Img", "count" : parseInt(datum.cnt_imagistic)},
    ]

    updateBarPlot(barLengths)

    //CMT rationale
    structTT.text(structuralAccessor(datum))
    ontoTT.text(ontologicalAccessor(datum))
    orientTT.text(orientationalAccessor(datum))
    imagTT.text(imagisticAccessor(datum))
    textMetTT.text(textGraphicMetaphorAcc(datum))
    textMetTypeTT.text(textGraphicMetaphorDetailsAcc(datum))

    // Other image metadata to populate based on mouseHover:
    imgTitleTT.text(imgTitle(datum))
    imgAuthorTT.text(imgAuthor(datum))
    srcURLTT.attr("href", srcURL(datum)).text(srcURL(datum))
    domainAccessorTT.text(domainAccessor(datum))
    secondDomainAccessorTT.text(secondaryDomainAccessor(datum))

    imgPreview.attr("src", "./images/placeholder.png")
    imgPreview.attr("src", imgURL(datum))

    let whichDomain = domainAccessor(datum)
    console.log(whichDomain)
    
    this.parentNode.appendChild(this) //move hovered item to top

    d3.select(this)
        .transition().ease(d3.easeLinear)
        .duration(400)
        .attr("fill", "black") // BRIGHT TURQ #5afaed
        .attr("fill", domainTick(whichDomain)[2])
        .attr("opacity", 1)
        //.attr("r", 10)


    
    hoveredRect = d3.selectAll("rect")
        .filter(d => d==datum)
    
    hoveredRect
        .attr("opacity", 1)
        //.attr("fill", "black")
        .attr("fill", domainTick(whichDomain)[2])


}

function onMouseLeave(e, datum) {
    let whichDomain = domainAccessor(datum)

    tooltip.style("opacity", 0)

    d3.select(this)
        .transition().ease(d3.easeLinear)
        .duration(400)
        //.attr("fill", "gray")
        .attr("fill", domainTick(whichDomain)[2])
        .attr("opacity", 0.5)
        .attr("r", 5)
    
    d3.selectAll(".scatterRects")
        .attr("opacity", 0.2)
        //.attr("fill", "lightgray")
        .attr("fill", domainTick(whichDomain)[2])

    //imgPreview.attr("src", "./images/placeholder.png")
}

function setTickMarks(dataset) {
    let whichDomain = domainAccessor(dataset[0])
    console.log(dataset[0])

    let ticks = domainTick(whichDomain)
    console.log(ticks)

    xAxisGenerator
        .ticks(ticks[0].length)
        .tickFormat(d => ticks[0][d-1]) //bring to 0 since lowest value in dataset is 1 

    yAxisGenerator
        .ticks(ticks[1].length)
        .tickFormat(d => ticks[1][d-1])
}

function styleAxes(dataset, scatterBounds) {        
    //redraw x axis
    xScale.domain([d3.extent(dataset, xfromAccessor)[0], d3.extent(dataset, xtoAccessor)[1]])
    scatterBounds.selectAll(".myXaxis").transition()
        .duration(750)
        .call(xAxisGenerator)
        .selectAll("text")  
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", ".15em")
        .attr("transform", "rotate(-30)")

    //redraw y axis 
    yScale.domain([d3.extent(dataset, yfromAccessor)[0], d3.extent(dataset, ytoAccessor)[1]]) 
        scatterBounds.selectAll(".myYaxis").transition()
            .duration(750)
            .call(yAxisGenerator)
}

async function drawScatterPlot(dataset) {

    // Initial draw canvas 
    const scatterWrapper = d3.select("#scatterplot")
        .append("svg")
        .attr("width", dimensions.width)
        .attr("height", dimensions.height)
    
    const scatterBounds = scatterWrapper.append("g")
        .attr("id", "scatter-bounds")
        .style("transform", `translate(${
            dimensions.margin.left
        }px, ${
            dimensions.margin.top
        }px)`)
    

    // Access data 
    //const dataset= await d3.csv(dataCSV)
    //console.table(dataset[0])

    setTickMarks(dataset)

    const xAxis = scatterBounds.append("g")
        .call(xAxisGenerator)
        .style("transform", `translateY(${dimensions.boundedHeight}px)`)
        .attr("class", "myXaxis")
    
    // scatterBounds.selectAll(".myXaxis")
    //     .selectAll("text")  
    //     .style("text-anchor", "end")
    //     .attr("dx", "-.8em")
    //     .attr("dy", ".15em")
    //     .attr("transform", "rotate(-30)")

    const yAxis = scatterBounds.append("g")
        .call(yAxisGenerator)
        .attr("class", "myYaxis")
    

    styleAxes(dataset, scatterBounds)


    scatterBounds.append("text")
        .attr("class", "y label")
        .attr("text-anchor", "end")
        .attr("y", -dimensions.margin.left + 15)
        .attr("x", -dimensions.margin.top)
        .attr("fill", "black")
        //.style("font-size", "1.2em")
        .attr("transform", "rotate(-90)")
        .text("SPATIAL COVERAGE")

    scatterBounds.append("text")
        .attr("class", "x label")
        .attr("text-anchor", "end")
        .attr("x", dimensions.boundedWidth)
        .attr("y", dimensions.boundedHeight + 55)
        .text("TEMPORAL COVERAGE");

    let whichDomain = domainAccessor(dataset[0])
    console.log(domainTick(whichDomain)[2])
    
    const sqs = scatterBounds.selectAll("rect")
        .data(dataset)
    sqs
        .join("rect")
        .attr("class", "scatterRects")
        .attr("x", d => xScale(xfromAccessor(d)))
        .attr("y", d => yScale(ytoAccessor(d)))
        .attr("width", d => xScale(xtoAccessor(d))-xScale(xfromAccessor(d)))
        .attr("height", d => yScale(yfromAccessor(d))-yScale(ytoAccessor(d)))
        .attr("rx", 15)
        .attr("opacity", 0.2)
        //.attr("fill", "lightgray") //domainTick(whichDomain)[2])
        .attr("fill", domainTick(whichDomain)[2])


    //create update selection to bind new data
    const dots = scatterBounds.selectAll("circle").data(dataset)

    dots
        .join("circle")
        //.attr('class', 'circle-base')
        .transition().duration(750)
        .attr("cx", d => xScale(xAccessor(d)))
        .attr("cy", d => yScale(yAccessor(d)))
        //.attr("fill", "gray") 
        .attr("fill", domainTick(whichDomain)[2])
        .attr("opacity", 0.5)
        .attr("r", 5)


    // INTERACTIONS 
    scatterBounds.selectAll("circle").on("mouseenter", function(e, d){
        console.log(e.target)
    })

    scatterBounds.selectAll("circle")
        .on("mouseenter", onMouseEnter)
        .on("mouseleave", onMouseLeave)                         
}


async function updateScatterPlot(dataset) {
    //const dataset= await d3.csv(dataCSV)
    console.log("this is my new data")
    console.table(dataset[1])
    let whichDomain = domainAccessor(dataset[0])
    console.log(domainTick(whichDomain)[2])

    const scatterBounds = d3.select("#scatter-bounds")

    // redraw X and Y axes 
    setTickMarks(dataset)
    styleAxes(dataset, scatterBounds)

    //redraw squares in new position
    scatterBounds.selectAll("rect")
        .data(dataset)
        .join("rect")
        .transition().duration(1000)
        .attr("x", d => xScale(xfromAccessor(d)))
        .attr("y", d => yScale(ytoAccessor(d)))
        .attr("width", d => xScale(xtoAccessor(d))-xScale(xfromAccessor(d)))
        .attr("height", d => yScale(yfromAccessor(d))-yScale(ytoAccessor(d)))
        .attr("fill", domainTick(whichDomain)[2])

    // redraw dots in new position
    scatterBounds.selectAll("circle")
        .data(dataset)
        .join("circle")
        .transition().duration(1000)
        .attr("cx", d => xScale(xAccessor(d)))
        .attr("cy", d => yScale(yAccessor(d)))
        .attr("fill", domainTick(whichDomain)[2])

}