Drop down button to filter a d3.js graph

2019-07-09 14:01发布

I have a graph that works fine and even filters the data on button click. Now I am trying to use a drop down button instead of a regular button. When a user chooses Amount as either 1000 or 2000 from the drop down , nodes and links should get filtered based on the property

d.total_amt> 1000 or d.total_amt > 2000

HTML

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

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

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

  div.tooltip {
    position: absolute;
    background-color: white;
    max-width: 200px;
    height: auto;
    padding: 1px;
    border-style: solid;
    border-radius: 4px;
    border-width: 1px;
    box-shadow: 3px 3px 10px rgba(0, 0, 0, .5);
    pointer-events: none;
  }

</style>
<svg id="Network_graph" width="400" height="350"></svg>


  <label for="input ID">Amount</label>
  <select id="select_ID" name="select_ID">
    <option value="1000">1000</option>
    <option value="2000">2000</option>
   </select>
   <button id="full_data">
   full data
   </button>

JSON

    var IDData = JSON.stringify([
  ["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264, "1374903"],
  ["node/1061", "node/21373542", "Agent", "Customer", "530848.0", 1, 3000, "529502"],
  ["node/10750", "node/59648369", "Agent", "Customer", "1454228.0", 1, 120, "1454118"],
  ["node/10750", "node/78569210", "Agent", "Customer", "1425251.0", 1, 234, "1421416"],
  ["node/10750", "node/96726118", "Agent", "Customer", "1376239.0", 1, 434, "1376152"],
  ["node/10946829", "node/11190", "Customer", "Agent", "1409620.0", 20, 3380, "1406665"],
  ["node/10946829", "node/57774036", "Customer", "Customer", "1460029.0", 3, 960, "1459731"],
  ["node/109947", "node/97911872", "Agent", "Customer", "1323025.0", 1, 600, "1315582"],..])

Below is the piece of code that will make this data in a format suitable to render the graph:

var galData = JSON.parse(IDData);
var startnodes = [];
var endnodes = [];
var startnodetype = [];
var endnodetype = [];
var PayTime = [];
var TXN_COUNT = [];
var Total_Amt = [];
var SendTime = [];
galData.map(function(e, i) {
  startnodes.push(e[0]);
  endnodes.push(e[1]);
  startnodetype.push(e[2]);
  endnodetype.push(e[3]);
  PayTime.push(e[4]);
  TXN_COUNT.push(e[5]);
  Total_Amt.push(e[6]);
  SendTime.push(e[7]);
});
var final_data = createNodes(startnodes, endnodes, startnodetype, endnodetype, PayTime, TXN_COUNT, Total_Amt, SendTime);
makeGraph("#Network_graph", final_data);

I have made a force directed graph using this data. Jsfiddle is the link to the working graph.

Now I am trying to add drop down for data filtering. This is the section of code that holds on to the selection from the drop down

    d3.select(this)  //right way to hold on to the selection or need some jquery?
.selectAll("option")
.filter(function(d, i) {
  return this.selected;
});

This piece of code uses the filter function to filter the nodes and links.

    function isUnique(id, nodes) {
      for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].id == id) {
          return false;
        }
      }
      return true;
    }

  var filtered_data = [];
  var nodes = [];
  var links = [];
  d3.selectAll("line").filter(function(d, i) {
    if (d.total_amt > this.value) {
      if (isUnique(d.source.id, nodes)) {
        nodes.push(d.source);
      }

      if (isUnique(d.target.id, nodes)) {
        nodes.push(d.target);
      }
      links.push(d);
    }
  });
  filtered_data.links = links;
  filtered_data.nodes = nodes;
  filtered_data.nodetype = final_data.nodetype;

  d3.select('#Network_graph').selectAll("*").remove();
  makeGraph("#Network_graph", filtered_data); 

Does the above approach make sense? So , far this has not worked. I don't know what is the missing piece. I am open to other ideas too that may work better for a drop down. Still finding my way around d3.js /javascript. Looking forward for some help.

1条回答
【Aperson】
2楼-- · 2019-07-09 14:28

OK

Here's the code you need:

var elem = document.getElementById('select_ID');
elem.addEventListener("change", onSelectChange);

function onSelectChange(){
  var value = this.value;
  var fdata = filteredData(value);
  d3.select('#Network_graph').selectAll("*").remove();
  makeGraph("#Network_graph", fdata);
}

In order to do something when your select option changes. Select the element and attach an event listener (change in this case) to it.

var elem = document.getElementById('select_ID');
elem.addEventListener("change", onSelectChange);

And inside this change event get the value with this.value as you've already said and make the filtering.

Here's the fiddle: https://jsfiddle.net/tgv6s5cd/14/

查看更多
登录 后发表回答