function ForceGraph({
  nodes, // an iterable of node objects 
  links // an iterable of link objects 
}, {
  nodeId = d => d.id, // given d in nodes, returns a unique identifier (string)
  nodeGroup, // given d in nodes, returns an (ordinal) value for color
  nodeGroups, // an array of ordinal values representing the node groups
  nodeTitle, // given d in nodes, a title string
  nodeFill = "currentColor", // node stroke fill 
  nodeStroke = "#fff", // node stroke color
  nodeStrokeWidth = 1.5, // node stroke width, in pixels
  nodeStrokeOpacity = 1, // node stroke opacity
  nodeRadius = 5, // node radius, in pixels
  linkSource = ({
    source
  }) => source, // given d in links, returns a node identifier string
  linkTarget = ({
    target
  }) => target, // given d in links, returns a node identifier string
  linkStroke = "#999", // link stroke color
  linkStrokeOpacity = 0.6, // link stroke opacity
  linkStrokeWidth = 1.5, // given d in links, returns a stroke width in pixels
  linkStrokeLinecap = "round", // link stroke linecap
  colors = d3.schemeTableau10, // an array of color strings, for the node groups
  width = 640, // outer width, in pixels
  height = 400, // outer height, in pixels
  invalidation // when this promise resolves, stop the simulation
} = {}) {
  // Compute values.
  const N = d3.map(nodes, nodeId).map(intern);
  const LS = d3.map(links, linkSource).map(intern);
  const LT = d3.map(links, linkTarget).map(intern);
  if (nodeTitle === undefined) nodeTitle = (_, i) => N[i];
  const T = nodeTitle == null ? null : d3.map(nodes, nodeTitle);
  const G = nodeGroup == null ? null : d3.map(nodes, nodeGroup).map(intern);
  const W = typeof linkStrokeWidth !== "function" ? null : d3.map(links, linkStrokeWidth);

  // Replace the input nodes and links with mutable objects for the simulation.
  nodes = d3.map(nodes, (_, i) => ({
    id: N[i]
  }));
  links = d3.map(links, (_, i) => ({
    source: LS[i],
    target: LT[i]
  }));

  // Compute default domains.
  if (G && nodeGroups === undefined) nodeGroups = d3.sort(G);

  // Construct the scales.
  const color = nodeGroup == null ? null : d3.scaleOrdinal(nodeGroups, colors);

   simulation = d3.forceSimulation(nodes)
    .force("link", d3.forceLink(links).id(({
      index: i
    }) => N[i]))
    .force("charge", d3.forceManyBody())
    .force("center", d3.forceCenter())
    .on("tick", ticked);

  const svg = d3.create("svg")
    .attr("width", width)
    .attr("height", height)
    .attr("viewBox", [-width / 2, -height / 2, width, height])
    .attr("style", "max-width: 100%; height: auto; height: intrinsic;");

  const link = svg.append("g")
    .attr("stroke", linkStroke)
    .attr("stroke-opacity", linkStrokeOpacity)
    .attr("stroke-width", typeof linkStrokeWidth !== "function" ? linkStrokeWidth : null)
    .attr("stroke-linecap", linkStrokeLinecap)
    .selectAll("line")
    .data(links)
    .join("line");

  const node = svg.append("g")
    .attr("fill", nodeFill)
    .attr("stroke", nodeStroke)
    .attr("stroke-opacity", nodeStrokeOpacity)
    .attr("stroke-width", nodeStrokeWidth)
    .selectAll("circle")
    .data(nodes)
    .join("circle")
    .attr("r", nodeRadius)
    .call(drag(simulation));

  if (W) link.attr("stroke-width", ({
    index: i
  }) => W[i]);
  if (G) node.attr("fill", ({
    index: i
  }) => color(G[i]));
  if (T) node.append("title").text(({
    index: i
  }) => T[i]);
  if (invalidation != null) invalidation.then(() => simulation.stop());

  function intern(value) {
    return value !== null && typeof value === "object" ? value.valueOf() : value;
  }

  function ticked() {
    link
      .attr("x1", d => d.source.x)
      .attr("y1", d => d.source.y)
      .attr("x2", d => d.target.x)
      .attr("y2", d => d.target.y);

    node
      .attr("cx", d => d.x)
      .attr("cy", d => d.y);
  }

  function drag(simulation) {
    function dragstarted(event) {
      if (!event.active) simulation.alphaTarget(0.3).restart();
      event.subject.fx = event.subject.x;
      event.subject.fy = event.subject.y;
    }

    function dragged(event) {
      event.subject.fx = event.x;
      event.subject.fy = event.y;
    }

    function dragended(event) {
      if (!event.active) simulation.alphaTarget(0);
      event.subject.fx = null;
      event.subject.fy = null;
    }

    return d3.drag()
      .on("start", dragstarted)
      .on("drag", dragged)
      .on("end", dragended);
  }

  return Object.assign(svg.node(), {
    scales: {
      color
    }
  });
}

data = {
  nodes: [{
      "relationship": 1,
      "side a": "sa_pm_test.model_cd",
      "nature": "a test set is performed on a model"
    },
    {
      "relationship": 2,
      "side a": "sa_pm_test_threshold.model_cd",
      "nature": "a test set threshold reflects model performance"
    },
    {
      "relationship": 3,
      "side a": "sa_pm_test_decile.model_cd",
      "nature": "a test set decile reflects model performance"
    },
    {
      "relationship": 4,
      "side a": "sa_pm_model_feature_considered.model_cd",
      "nature": "a model has one or more features considered for use"
    },
    {
      "relationship": 5,
      "side a": "sa_pm_model_feature_considered.feature_cd",
      "nature": "one or more features are considered for use in a model"
    },
    {
      "relationship": 6,
      "side a": "sa_pm_model_feature_correlation.model_cd",
      "nature": "a model with multiple features has correlations between the features"
    },
    {
      "relationship": 7,
      "side a": "sa_pm_model_feature_correlation.feature_1_cd",
      "nature": "each feature in a model is correlated with all other features in the model"
    },
    {
      "relationship": 8,
      "side a": "sa_pm_model_feature_correlation.feature_2_cd",
      "nature": "each feature in a model is correlated with all other features in the model"
    },
    {
      "relationship": 9,
      "side a": "sa_pm_model_feature_used.model_cd",
      "nature": "each model uses one or more features"
    },
    {
      "relationship": 10,
      "side a": "sa_pm_model_feature_used.model_feature_cd",
      "nature": "each feature can be used in one or more models"
    },
    {
      "relationship": 11,
      "side a": "sa_pm_model_feature_used_source.model_cd",
      "nature": "each model has features which can be derived from standard features"
    },
    {
      "relationship": 12,
      "side a": "sa_pm_model_feature_used_source.model_feature_cd",
      "nature": "derived model features are sourced from standard features"
    },
    {
      "relationship": 13,
      "side a": "sa_pm_model_feature_used_source.feature_cd",
      "nature": "each derived feature is sourced from one or more standard features"
    }
  ],

  links: [{
      "relationship": 1,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 2,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 3,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 4,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 5,
      "side b": "sa_pm_feature.feature_cd"
    },
    {
      "relationship": 6,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 7,
      "side b": "sa_pm_feature.feature_cd"
    },
    {
      "relationship": 8,
      "side b": "sa_pm_feature.feature_cd"
    },
    {
      "relationship": 9,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 10,
      "side b": "sa_pm_feature.feature_cd"
    },
    {
      "relationship": 11,
      "side b": "sa_pm_model.model_cd"
    },
    {
      "relationship": 12,
      "side b": "sa_pm_model_feature_used.model_feature_cd"
    },
    {
      "relationship": 13,
      "side b": "sa_pm_feature.feature_cd"
    }
  ]
}
chart = ForceGraph(data, {
    nodeId: d => d.id,
    nodeGroup: d => d.group,
    nodeTitle: d => `${d.id}\n${d.group}`,
    linkStrokeWidth: l => Math.sqrt(l.value),
    width: 200,
    height: 600,
  })

var color = d3.scaleOrdinal(d3.schemeCategory10);
drag = simulation => {
  function dragsubject(event) {
    return simulation.find(event.x, event.y);
  }

  function dragstarted(event) {
    if (!event.active) simulation.alphaTarget(0.3).restart();
    event.subject.fx = event.subject.x;
    event.subject.fy = event.subject.y;
  }

  function dragged(event) {
    event.subject.fx = event.x;
    event.subject.fy = event.y;
  }

  function dragended(event) {
    if (!event.active) simulation.alphaTarget(0);
    event.subject.fx = null;
    event.subject.fy = null;
  }

  return d3.drag()
    .subject(dragsubject)
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended);
}

 

Javascript Online Compiler

Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.

About Javascript

Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.

Key Features

  • Open-source
  • Just-in-time compiled language
  • Embedded along with HTML and makes web pages alive
  • Originally named as LiveScript.
  • Executable in both browser and server which has Javascript engines like V8(chrome), SpiderMonkey(Firefox) etc.

Syntax help

STDIN Example

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    console.log("Hello, " + line);
});

variable declaration

KeywordDescriptionScope
varVar is used to declare variables(old way of declaring variables)Function or global scope
letlet is also used to declare variables(new way)Global or block Scope
constconst is used to declare const values. Once the value is assigned, it can not be modifiedGlobal or block Scope

Backtick Strings

Interpolation

let greetings = `Hello ${name}`

Multi line Strings

const msg = `
hello
world!
`

Arrays

An array is a collection of items or values.

Syntax:

let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);

Example:

let mobiles = ["iPhone", "Samsung", "Pixel"];

// accessing an array
console.log(mobiles[0]);

// changing an array element
mobiles[3] = "Nokia";

Arrow functions

Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.

Syntax:

() => expression

Example:

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
                                    .map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);

De-structuring

Arrays

let [firstName, lastName] = ['Foo', 'Bar']

Objects

let {firstName, lastName} = {
  firstName: 'Foo',
  lastName: 'Bar'
}

rest(...) operator

 const {
    title,
    firstName,
    lastName,
    ...rest
  } = record;

Spread(...) operator

//Object spread
const post = {
  ...options,
  type: "new"
}
//array spread
const users = [
  ...adminUsers,
  ...normalUsers
]

Functions

function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
  console.log(`Hello ${name}!`);
}
 
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar

Loops

1. If:

IF is used to execute a block of code based on a condition.

Syntax

if(condition){
    // code
}

2. If-Else:

Else part is used to execute the block of code when the condition fails.

Syntax

if(condition){
    // code
} else {
    // code
}

3. Switch:

Switch is used to replace nested If-Else statements.

Syntax

switch(condition){
    case 'value1' :
        //code
        [break;]
    case 'value2' :
        //code
        [break;]
    .......
    default :
        //code
        [break;]
}

4. For

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
//code  
} 

5. While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
  // code 
}  

6. Do-While

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
  // code 
} while (condition); 

Classes

ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.

Syntax:

class className {
  constructor() { ... } //Mandatory Class method
  method1() { ... }
  method2() { ... }
  ...
}

Example:

class Mobile {
  constructor(model) {
    this.name = model;
  }
}

mbl = new Mobile("iPhone");