PageSourceSearch

https://cse442-17s.github.io/Your-Corner-of-the-News-Ecosystem/scripts/gravityTools.js

js cse442-17s.github.io collected 2026-10-03 08:50:28 UTC 2,742 bytes, 73 lines download raw bytes

1/**
2 * Created by bking on 5/30/17.
3 */
4
5const DEFAULT_GRAVITY_FIELD_PARAMS =  {
6    "linkForceStrength" : 6,
7    "chargeForceStrength" : -250,
8    "gravityForceStrength": .3
9};
10
11// function for generating gravitational field from data
12function build_gravitational_field(selection, keyFunction, asLinear, width, height, defaultWells) {
13    var usingArgumentWells = (defaultWells !== undefined);
14    var wells = (usingArgumentWells) ? defaultWells : {};
15    if (!usingArgumentWells) {
16        var data = selection.data();
17        for (var i = 0; i < data.length; i++) {
18            var key = keyFunction(data[i]);
19            if (key in wells) {
20                wells[key] += 1; //will add x, y when all keys are known
21            } else {
22                wells[key] = 1;
23            }
24        }
25    }
26    var result =  {
27        gravityWells : (usingArgumentWells) ? wells : place_wells(wells, asLinear, width, height),
28        defaultParams: DEFAULT_GRAVITY_FIELD_PARAMS,
29        keyFunction : keyFunction,
30        updateGravityPosition  : function (d) {
31            var well = d.id;
32            this.gravityWells[well].x = d.x;
33            this.gravityWells[well].y = d.y;
34        },
35        getGravityWellPosition : function (d) {
36            var well = this.keyFunction(d);
37            d.well = well; // give node a well
38            return this.gravityWells[well]; // dynamically return x position of gravity well
39        }
40    }
41    return result;
42}
43
44function place_wells(wells, asLinear, width, height) {
45    var size =  Object.keys(wells).length;
46    var keys = Object.keys(wells).sort();
47    if (asLinear) {
48        var i = 1.0;
49        for (var k = 0; k < keys.length; k++) {
50            wells[keys[k]] = {'x': width * (i / (size + 1)), 'y': height * .5, 'id': keys[k]};
51            i++;
52        }
53    } else {
54        i = 0;
55        // run a false, hidden simulation in a 100 by 100 box, run it a few iterations, and use these positions as well centers
56        var nodes = d3.range(size).map(function(i) { return {index: i}; });
57        var simulation = d3.forceSimulation()
58            .force("charge", d3.forceManyBody().strength(-10))
59            .force("center", d3.forceCenter(50, 50))
60            .nodes(nodes)
61            .stop();
62
63        // See https://github.com/d3/d3-force/blob/master/README.md#simulation_tick
64        for (var j = 0, n = Math.ceil(Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay())); j < n; ++j) {
65            simulation.tick();
66        }
67        for (var key in wells) {
68            wells[key] = {'x': width * Math.max(.1, Math.min(.9, nodes[i].x / 100)), 'y': height * Math.max(.1, Math.min(.9, nodes[i].y / 100)), 'id': key};
69            i++;
70        }
71    }
72    return wells;
73}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.