<!DOCTYPE html>
<meta charset="utf-8">

<style>


.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}

text {
  font-family: sans-serif;
  font-size: 10px;
}


p {
  font-family: sans-serif;
  font-size: 12px;
}

</style>
<p><h4 align="center">How could environment design, sound and vision convey a sense of safety during a guided meditation experience?</h4></p>

<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
//create somewhere to put the force directed graph
var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");


var nodes =  [
    {"name": "Focus","ideaType": "Topic","level":"top"},
    {"name": "Medium","ideaType": "Topic","level":"top"},
    {"name": "Safety","ideaType": "Topic","level":"sub"},
    {"name": "Environment.Design","ideaType": "Topic","level":"sub"},
    {"name": "Predictable","ideaType": "Topic","level":"sub"},
    {"name": "Rhythm","ideaType": "Topic","level":"sub"},
    {"name": "Pace","ideaType": "Topic","level":"sub"},
    {"name": "Chronic.Pain","ideaType": "Topic","level":"sub"},
    {"name": "Not.Acute","ideaType": "Topic","level":"sub"},
    {"name": "Challenges","ideaType": "Topic","level":"sub"},
    {"name": "Meditation","ideaType": "Topic","level":"sub"},
    {"name": "Belief","ideaType": "Topic","level":"sub"},
    {"name": "Open.monitoring","ideaType": "Topic","level":"sub"},
    {"name": "No.targets","ideaType": "Topic","level":"sub"},
    {"name": "Attention","ideaType": "Topic","level":"sub"},
    {"name": "Motivation","ideaType": "Motivation","level":"top"},
    {"name": "Personal","ideaType": "Motivation","level":"sub"},
    {"name": "Community","ideaType": "Motivation","level":"sub"},
    {"name": "Gaps","ideaType": "Motivation","level":"sub"},
    {"name": "People.with.Pain","ideaType": "Motivation","level":"sub"},
    {"name": "XR.Technology.as.Aids","ideaType": "Motivation","level":"sub"},
    {"name": "Virtual.reality","ideaType": "Topic","level":"sub"},
    {"name": "Audio","ideaType": "Topic","level":"sub"},
    {"name": "Inquiry","ideaType": "Inquiry","level":"top"}
  ]

  //Create links data
  var links =  [
    {"source": "Focus", "target": "Safety"},
    {"source": "Focus", "target": "Chronic.Pain"},
    {"source": "Focus", "target": "Meditation"},
    {"source": "Medium", "target": "Virtual.reality"},
    {"source": "Safety", "target": "Environment.Design"},
    {"source": "Safety", "target": "Predictable"},
    {"source": "Safety", "target": "Rhythm"},
    {"source": "Safety", "target": "Pace"},
    {"source": "Safety", "target": "Belief"},
    {"source": "Chronic.Pain", "target": "Not.Acute"},
    {"source": "Chronic.Pain", "target": "Attention"},
    {"source": "Chronic.Pain", "target": "Belief"},
    {"source": "Meditation", "target": "No.targets"},
    {"source": "Meditation", "target": "Open.monitoring"},
    {"source": "Meditation", "target": "Challenges"},
    {"source": "Virtual.reality", "target": "Attention"},
    {"source": "Virtual.reality", "target": "Audio"},
    {"source": "Audio", "target": "Rhythm"},
    {"source": "Inquiry", "target": "Safety"},
    {"source": "Inquiry", "target": "Virtual.reality"},
    {"source": "Motivation", "target": "Personal"},
    {"source": "Motivation", "target": "Community"},
    {"source": "Motivation", "target": "Challenges"},
    {"source": "Community", "target": "Gaps"},
    {"source": "Personal", "target": "People.with.Pain"},
    {"source": "Personal", "target": "XR.Technology.as.Aids"}
    ]

  // based on https://bl.ocks.org/almsuarez/2f7219a4f52b572a3ca7da91a25681f9
  // built upon D3js.org from Mike Bostok

  //set up the simulation and add forces
  var simulation = d3.forceSimulation().nodes(nodes);

  var link_force =  d3.forceLink(links).id(function(d) { return d.name; });
  var charge_force = d3.forceManyBody().strength(-150);
  var center_force = d3.forceCenter(width / 2, height / 2);

  simulation
      .force("charge_force", charge_force)
      .force("center_force", center_force)
      .force("links",link_force)
   ;

   //add tick instructions:
 simulation.on("tick", tickActions );


//add encompassing group for the zoom
var g = svg.append("g")
        .attr("class", "everything");

//draw lines for the links
var link = g.append("g")
      .attr("class", "links")
      .selectAll("line")
      .data(links)
      .enter().append("line")
      .attr("stroke-width", 2)
      .style("stroke", "grey");

//draw circles for the nodes
var node = g.append("g")
        .attr("class", "nodes")
        .selectAll("circle")
        .data(nodes)
        .enter()
        .append("circle")
          .attr("r", cirlceRadius)
          .attr("fill", circleColour);

var label = g.append("g")
        .attr("class","nodes")
        .selectAll("text")
        .data(nodes)
        .enter()
        .append("text")
            .text(d => d.name)
            .style("font-size",fontStyle);




var drag_handler = d3.drag()
.on("start", drag_start)
.on("drag", drag_drag)
.on("end", drag_end);

drag_handler(node)


//add zoom capabilities
var zoom_handler = d3.zoom()
    .on("zoom", zoom_actions);

zoom_handler(svg);

function zoom_actions(){
    g.attr("transform", d3.event.transform)
}

allLinks = links;
allNodes = nodes;

function update1(){
  links = [];
  restart();
}

function update2(){

}

function update3(){

}


function update4(){

}


function resetData(){
    links = allLinks;
    nodes = allNodes;
    restart();
}

function fontStyle(d){

      if(d.level =="sub"){
          return "12px";
      } else {
          return "20px";
      }
}

//Function to choose what color circle we have
function circleColour(d){
    if(d.ideaType =="Motivation"){
        return "chocolate";
    }else if (d.ideaType=="Topic"){
        return "pink";
    } else {
      return "khaki";
    }
}

//Function to choose what size (radius) circle we have
function cirlceRadius(d){
    if(d.level =="sub"){
        return 5;
    } else {
        return 10;
    }
}





      //d is the node
      function drag_start(d) {
       if (!d3.event.active) simulation.alphaTarget(0.3).restart();
          d.fx = d.x;
          d.fy = d.y;
      }

      function drag_drag(d) {
        d.fx = d3.event.x;
        d.fy = d3.event.y;
      }


      function drag_end(d) {
        if (!d3.event.active) simulation.alphaTarget(0);
        d.fx = d.x;
        d.fy = d.y;
//          d.fx = null;
//          d.fy = null;
      }


      //Zoom functions
      function zoom_actions(){
          g.attr("transform", d3.event.transform)
      }


function tickActions() {
    //update circle positions each tick of the simulation
    node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });

    //update link positions
    //simply tells one end of the line to follow one node around
    //and the other end of the line to follow the other node around
    link
        .attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    label
        .attr("x", function(d) { return d.x+10; })
        .attr("y", function(d) { return d.y+3; });
  }

  function restart() {

    // Update and restart the simulation.
    simulation = d3.forceSimulation().nodes(nodes);

    link_force =  d3.forceLink(links).id(function(d) { return d.name; });


    simulation
        .force("charge_force", charge_force)
        .force("center_force", center_force)
        .force("links",link_force)
//        .force("collision",collision_force)
     ;

     //add tick instructions:
     simulation.on("tick", tickActions );

     simulation.restart();
  }

</script>