| OLD | NEW |
| (Empty) | |
| 1 <!-- The <cluster-sk> custom element declaration. |
| 2 |
| 3 Displays digests in a force layout, i.e. as svg circles as if they were |
| 4 attached via springs that were proportional to the distance between the |
| 5 digest images. |
| 6 |
| 7 Attributes: |
| 8 None. |
| 9 |
| 10 Events: |
| 11 digest-select - Event generated when a digest is selected. The event |
| 12 detail is the digest. |
| 13 |
| 14 Methods: |
| 15 setData(data) - Data describing digests in a d3 compatible format. |
| 16 |
| 17 { |
| 18 "nodes": [ |
| 19 {"name":"a3801be6d11aa0c9316daaa0b22ca3ac"}, |
| 20 {"name":"0ae9d1ee00a9f6149a61dd1c2c372fc3"} |
| 21 ], |
| 22 "links":[ |
| 23 {"source":0, "target":0, "value":0}, |
| 24 {"source":0, "target":1, "value":5.0775}, |
| 25 {"source":0, "target":2, "value":5.0725}, |
| 26 ] |
| 27 } |
| 28 |
| 29 newTriageStatus(digests, status) - Set the new triage status for the array |
| 30 of given digests. |
| 31 --> |
| 32 <polymer-element name="cluster-sk"> |
| 33 <template> |
| 34 <style type="text/css" media="screen"> |
| 35 .link { |
| 36 stroke: #ccc; |
| 37 } |
| 38 |
| 39 .node { |
| 40 cursor: move; |
| 41 fill: #ccc; |
| 42 stroke: #000; |
| 43 stroke-width: 1.5px; |
| 44 } |
| 45 |
| 46 .node.positive { |
| 47 fill: #1B9E77; |
| 48 } |
| 49 |
| 50 .node.negative { |
| 51 fill: #E7298A; |
| 52 } |
| 53 |
| 54 .node.untriaged { |
| 55 fill: #A6761D; |
| 56 } |
| 57 |
| 58 :host { |
| 59 display: block; |
| 60 position: relative; |
| 61 width: 100%; |
| 62 height: 100%; |
| 63 } |
| 64 |
| 65 svg { |
| 66 position: relative; |
| 67 width: 100%; |
| 68 height: 100%; |
| 69 } |
| 70 </style> |
| 71 </template> |
| 72 <script> |
| 73 Polymer({ |
| 74 publish: { |
| 75 data: { |
| 76 value: {}, |
| 77 reflect: false, |
| 78 }, |
| 79 }, |
| 80 |
| 81 // Force everything into the light DOM. |
| 82 parseDeclaration: function(elementElement) { |
| 83 var template = this.fetchTemplate(elementElement); |
| 84 if (template != null) { |
| 85 this.lightFromTemplate(template); |
| 86 }; |
| 87 }, |
| 88 |
| 89 ready: function() { |
| 90 // Controls the link distance multiplier between nodes. |
| 91 this.scale = 10; |
| 92 |
| 93 // Controls the electrostatic charge between nodes, used in the |
| 94 // Barnes-Hut layout. |
| 95 this.charge = -100; |
| 96 |
| 97 this.width = 960; |
| 98 this.height = 500; |
| 99 |
| 100 // d3 force layout object. |
| 101 this.force = null; |
| 102 |
| 103 this.svg = d3.select(this).append("svg") |
| 104 .attr("width", this.width) |
| 105 .attr("height", this.height); |
| 106 |
| 107 document.body.addEventListener('keydown', this.keyboard.bind(this)); |
| 108 window.setInterval(this.resizeWatcher.bind(this), 300); |
| 109 }, |
| 110 |
| 111 keyboard: function(e) { |
| 112 var c = String.fromCharCode(e.keyCode); |
| 113 switch (c) { |
| 114 case "A": |
| 115 this.scale = this.scale*2; |
| 116 this.force.start(); |
| 117 break; |
| 118 case "Z": |
| 119 this.scale = this.scale/2; |
| 120 this.force.start(); |
| 121 break; |
| 122 case "S": |
| 123 this.charge = this.charge*2; |
| 124 this.force.start(); |
| 125 break; |
| 126 case "X": |
| 127 this.charge = this.charge/2; |
| 128 this.force.start(); |
| 129 break; |
| 130 } |
| 131 console.log(this.charge); |
| 132 }, |
| 133 |
| 134 dataChanged: function() { |
| 135 this.reloadSVG(); |
| 136 }, |
| 137 |
| 138 dragstart: function(d) { |
| 139 this.dispatchEvent(new CustomEvent('digest-select', {detail: d.name, bub
bles: true})); |
| 140 }, |
| 141 |
| 142 reloadSVG: function() { |
| 143 this.svg.selectAll(".link,.node").remove(); |
| 144 |
| 145 this.force = d3.layout.force() |
| 146 .gravity(.05) |
| 147 .linkDistance(function(d) { return d.value*this.scale; }.bind(this)) |
| 148 .charge(function() { return this.charge; }.bind(this)) |
| 149 .size([this.width, this.height]) |
| 150 .nodes(this.data.nodes) |
| 151 .links(this.data.links) |
| 152 .start(); |
| 153 |
| 154 this.force.drag().on("dragstart", this.dragstart.bind(this)); |
| 155 |
| 156 var link = this.svg.selectAll(".link") |
| 157 .data(this.data.links) |
| 158 .enter().append("line") |
| 159 .attr("class", "link"); |
| 160 |
| 161 var node = this.svg.selectAll(".node") |
| 162 .data(this.data.nodes) |
| 163 .enter() |
| 164 .append("circle") |
| 165 .attr("class", function(d) { return "node " + d.status }) |
| 166 .attr("r", 12) |
| 167 .attr("data-name", function(d) { return d.name }) |
| 168 .attr("id", function(d) { return "x" + d.name }) |
| 169 .call(this.force.drag); |
| 170 |
| 171 this.force.on("tick", function() { |
| 172 link |
| 173 .attr("x1", function(d) { return d.source.x; }) |
| 174 .attr("y1", function(d) { return d.source.y; }) |
| 175 .attr("x2", function(d) { return d.target.x; }) |
| 176 .attr("y2", function(d) { return d.target.y; }); |
| 177 |
| 178 node.attr("transform", function(d) { return "translate(" + d.x + "," +
d.y + ")"; }); |
| 179 }); |
| 180 }, |
| 181 |
| 182 // Watch the size of the svg parents element and when it changes then |
| 183 // then resize the svg element to match. |
| 184 resizeWatcher: function() { |
| 185 if (!this.force) { |
| 186 return |
| 187 } |
| 188 var w = +window.getComputedStyle(this, null).getPropertyValue('width').s
lice(0, -2); |
| 189 var h = +window.getComputedStyle(this, null).getPropertyValue('height').
slice(0, -2); |
| 190 if (w != this.width || h != this.height) { |
| 191 this.width = w; |
| 192 this.height = h; |
| 193 this.svg.attr("width", this.width) |
| 194 .attr("height", this.height); |
| 195 this.force.size([w, h]).start(); |
| 196 } |
| 197 }, |
| 198 |
| 199 newTriageStatus: function(digests, status) { |
| 200 digests.forEach(function(digest) { |
| 201 $$$("#x"+digest).setAttribute("class", "node " + status); |
| 202 }.bind(this)); |
| 203 |
| 204 /* |
| 205 this.data.nodes.forEach(function(e) { |
| 206 if (digests.indexOf(e.name) != -1) { |
| 207 e.status = status; |
| 208 } |
| 209 }.bind(this)); |
| 210 */ |
| 211 }, |
| 212 |
| 213 }); |
| 214 </script> |
| 215 </polymer-element> |
| OLD | NEW |