Blame view

datalets/graph-datalet/graph-datalet.html 14.7 KB
ce4b5fb1   Luigi Serra   graph-datalet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  <!--

  @license

      The MIT License (MIT)

  

      Copyright (c) 2015 Dipartimento di Informatica - Università di Salerno - Italy

  

      Permission is hereby granted, free of charge, to any person obtaining a copy

      of this software and associated documentation files (the "Software"), to deal

      in the Software without restriction, including without limitation the rights

      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

      copies of the Software, and to permit persons to whom the Software is

      furnished to do so, subject to the following conditions:

  

      The above copyright notice and this permission notice shall be included in

      all copies or substantial portions of the Software.

  

      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

      THE SOFTWARE.

  -->

  

  <!--

  * Developed by :

  * ROUTE-TO-PA Project - grant No 645860. - www.routetopa.eu

  *

  -->

  

  <link rel="import" href="../base-datalet/base-datalet.html">

  <link rel="import" href="../../bower_components/iron-flex-layout/iron-flex-layout.html">

33ee61fd   Luigi Serra   graph updates
34
  <link rel="import" href="../../bower_components/paper-dialog/paper-dialog.html">

4ef93b8d   Luigi Serra   graph updates
35
  <link rel="import" href="../../bower_components/paper-fab/paper-fab.html">

ce4b5fb1   Luigi Serra   graph-datalet
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  

  <!--

  `preview-datalet` is a datalet that allow user to preview the content of a web page. It creates a thumbnail of the site using the data-url attribute passed as input.

  

  Example:

  

      <preview-datalet data-url="http://spod.routetopa.eu"

      </preview-datalet>

  

  

  @element preview-datalet

  @status beta

  @homepage

  @group datalets

  -->

  

  <dom-module id="graph-datalet">

      <template>

  

          <link rel="stylesheet" href="static/css/graphStyle.css">

  

          <style is="custom-style">

ce4b5fb1   Luigi Serra   graph-datalet
58
  

33ee61fd   Luigi Serra   graph updates
59
              #dialog{

4ef93b8d   Luigi Serra   graph updates
60
                  position: relative;

33ee61fd   Luigi Serra   graph updates
61
                  padding: 20px;

4ef93b8d   Luigi Serra   graph updates
62
63
64
65
66
67
68
69
70
71
72
73
74
                  right:5%;

              }

  

              #close{

                  position: absolute;

                  top: -20px;

                  right: 3px;

                  --iron-icon-height: 20px;

                  --iron-icon-width: 20px;

                  width: 24px;

                  height: 24px;

                  --paper-fab-background:#9e9e9e;

                  z-index: 1001;

33ee61fd   Luigi Serra   graph updates
75
76
              }

  

ce4b5fb1   Luigi Serra   graph-datalet
77
78
          </style>

  

01a6d2e5   Luigi Serra   graph updates
79
          <div style="align-content: center;overflow: visible" id="graph_content">

c49910f8   Luigi Serra   graph updates
80
81
              <svg id="sbiricuda"></svg>

          </div>

ce4b5fb1   Luigi Serra   graph-datalet
82
  

33ee61fd   Luigi Serra   graph updates
83
          <paper-dialog id="dialog">

4ef93b8d   Luigi Serra   graph updates
84
              <paper-fab id="close" mini icon="close" on-click="_onCloseClick"></paper-fab>

33ee61fd   Luigi Serra   graph updates
85
              <h2 id="dialog_title"></h2>

4ef93b8d   Luigi Serra   graph updates
86
              <paper-dialog-scrollable id="dialog_content">cos</paper-dialog-scrollable>

33ee61fd   Luigi Serra   graph updates
87
88
          </paper-dialog>

  

ce4b5fb1   Luigi Serra   graph-datalet
89
90
91
      </template>

  

      <script src="static/js/d3.v3.js"></script>

ce4b5fb1   Luigi Serra   graph-datalet
92
93
94
  

      <script>

  

c49910f8   Luigi Serra   graph updates
95
96
97
98
99
100
101
          gnodes = [];

          glinks = [];

  

          width  = undefined;

          height = undefined;

          svg    = undefined;

          _this  = null;

33ee61fd   Luigi Serra   graph updates
102
          prev_selected_node = null;

4f0953cd   Luigi Serra   graph datalet
103
  

ce4b5fb1   Luigi Serra   graph-datalet
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
          Polymer({

              is : 'graph-datalet',

  

              properties: {

                  /**

                   * It's the url for the preview

                   *

                   * @attribute url

                   * @type Strig

                   * @default ''

                   */

                  data : {

                      type : Object,

                      value : undefined

                  },

  

                  width: {

                      type: Number,

01a6d2e5   Luigi Serra   graph updates
122
                      value: undefined

ce4b5fb1   Luigi Serra   graph-datalet
123
124
125
126
127
128
129
130
131
132
                  },

  

                  height : {

                      type : Number,

                      type: undefined

                  },

  

                  svg: {

                      type: Object,

                      value: undefined

4ef93b8d   Luigi Serra   graph updates
133
134
135
136
                  },

                  feelings:{

                      type: Array,

                      values: ["Agree", "Neutral", "Not agree"]

c49910f8   Luigi Serra   graph updates
137
138
                  }

              },

4f0953cd   Luigi Serra   graph datalet
139
  

c49910f8   Luigi Serra   graph updates
140
              buildGraph: function (t){

4f0953cd   Luigi Serra   graph datalet
141
  

c49910f8   Luigi Serra   graph updates
142
143
144
145
                  this.svg = this.svg.append("g")

                                     .call(d3.behavior.zoom().scaleExtent([.25, 20]).on("zoom", this.zoom))

                                     .append("g");

  

01a6d2e5   Luigi Serra   graph updates
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                  //set initial zoom

                  scale = (0.8);

                  //translate = [(width-scale*width)/2, ((height-scale*height)/4)];

                  translate = [0, 0];

                  console.log("w: " + width + " h:" + height);

                  this.svg.transition()

                          .duration(750)

                          .attr("transform", "translate(" + translate + ")scale(" + scale +    ")")

                          .each("end", function () {

                              d3.behavior.zoom()

                                      .scale(scale)

                                      .translate(translate);

                          });

                  //end set initial zoom

  

c49910f8   Luigi Serra   graph updates
161
162
163
164
                  this.svg.append("rect")

                          .attr("fill", "white")

                          .attr("width", width)

                          .attr("height", height);

ce4b5fb1   Luigi Serra   graph-datalet
165
  

0f6424fe   root   update new data-s...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                  //pezzotto

                  this.svg.append("filter")

                              .attr("id","filter1")

                              .attr("x","0%")

                              .attr("y","0%")

                              .attr("width","100%")

                              .attr("height","100%")

                          .append("feImage")

                              .attr("xlink:href","http://icons.iconarchive.com/icons/hopstarter/soft-scraps/256/User-Executive-Green-icon.png");

  

                  /*this.svg.append("defs")

                          .append("pattern")

                              .attr("id","urlimage")

                              //.attr("patternUnits","userSpaceOnUse")

                              .attr("width","100%")

                              .attr("height","100%")

                          .append("image")

                              .attr("xlink:href","http://lorempixel.com/400/200")

                              //.attr("patternUnits","userSpaceOnUse")

                              .attr("x","0%")

                              .attr("y","0%")

                              .attr("width","100%")

                              .attr("height","100%");*/

                  //end pezzotto

  

  

ce4b5fb1   Luigi Serra   graph-datalet
192
193
194
195
196
197
198
199
200
201
                  var graph = {};

  

                  graph.nodes = t.nodes;

                  graph.links = t.links;

  

                  force = d3.layout.force().nodes(graph.nodes).links(graph.links).size([this.width, this.height]).charge(-1e3).friction(.7).linkDistance(function (t)

                  {

                      return t.value ? t.value : 80

                  }).on("tick", this.tick).start();

  

c49910f8   Luigi Serra   graph updates
202
203
                  glinks = this.svg.selectAll(".link").data(graph.links).enter().append("line").attr("class", "link");

  

01a6d2e5   Luigi Serra   graph updates
204
205
206
207
                  this.svg.selectAll(".link").attr("style", function(t){

                      return "stroke:" + t.color

                  });

  

c49910f8   Luigi Serra   graph updates
208
209
210
211
212
213
214
215
                  gnodes = this.svg.selectAll(".node").data(graph.nodes).enter().append("g").attr("class", function (t) {

                                                                                                  return t.fixed ? "node fixed" : "node"

                                                                                              }).attr("name", function (t) {

                                                                                                  return t.name ? t.name.split(" ").join("_").toLowerCase() : ""

                                                                                              }).on("mouseover", this.mouseover)

                                                                                                .on("mouseout", this.mouseout)

                                                                                                .on("click", this.active)

                                                                                              .append("circle")

33ee61fd   Luigi Serra   graph updates
216
217
218
                                                                                                  .attr("id", function(t){

                                                                                                     return t.id;

                                                                                                  })

c49910f8   Luigi Serra   graph updates
219
220
221
222
                                                                                                  .attr("class", function (t) {

                                                                                                      return t.fixed ? "" : "drag"

                                                                                                  }).attr("r", function (t) {

                                                                                                      return t.r ? t.r : 15

0f6424fe   root   update new data-s...
223
224
                                                                                                  })

                                                                                                  .attr("style", function (t) {

01a6d2e5   Luigi Serra   graph updates
225
                                                                                                      return t.color ? "fill:" + t.color : !1 + "; stroke:white"

c49910f8   Luigi Serra   graph updates
226
                                                                                                  });

ce4b5fb1   Luigi Serra   graph-datalet
227
  

0f6424fe   root   update new data-s...
228
229
                  d3.selectAll('circle').attr("filter", function(t){return  t.image ? 'url(#filter1)' : ""});

  

ce4b5fb1   Luigi Serra   graph-datalet
230
231
232
                  //d3.selectAll(".drag").call(force.drag), svg.selectAll("g.fixed").call(text);

                  d3.selectAll(".drag").call(force.drag), this.svg.selectAll("g.node").call(this.text);

  

c49910f8   Luigi Serra   graph updates
233
234
235
236
237
238
                  svg = this.svg;

  

              },

  

              zoom: function() {

                  svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");

01a6d2e5   Luigi Serra   graph updates
239
                  console.log(d3.event.scale);

ce4b5fb1   Luigi Serra   graph-datalet
240
              },

c49910f8   Luigi Serra   graph updates
241
  

ce4b5fb1   Luigi Serra   graph-datalet
242
243
244
245
246
247
248
249
250
251
252
253
254
255
              text: function (t)

              {

                  var e = t.append("svg:foreignObject").attr("width", 120).attr("height", 30);

                  e.attr("style", function (t) {

                      return "color:" + (t.color ? t.color : "#000")

                  }).append("xhtml:div").html(function (t) {

                      //return t.fixed ? t.name : null

                      return t.name;

                  })

              },

  

              tick : function () {

                  // d3.select("svg").attr("style", "transform:translate(8%)");

                  d3.selectAll("g foreignObject").attr("x", function (t) {

01a6d2e5   Luigi Serra   graph updates
256
                      return t.x + (t.r ? 0.8 * t.r : 15)

ce4b5fb1   Luigi Serra   graph-datalet
257
                  }).attr("y", function (t) {

01a6d2e5   Luigi Serra   graph updates
258
                      return t.y - 20

ce4b5fb1   Luigi Serra   graph-datalet
259
260
261
262
263
264
265
266
                  });

  

                  d3.selectAll("#logo text").attr("x", function (t) {

                      return t.x + .7 * (t.r ? t.r : 15)

                  }).attr("y", function (t) {

                      return t.y

                  });

  

4f0953cd   Luigi Serra   graph datalet
267
  

c49910f8   Luigi Serra   graph updates
268
269
                  gnodes.attr("cx", function (t) {

                      return t.x = Math.max(25, Math.min(width - 50, t.x))

ce4b5fb1   Luigi Serra   graph-datalet
270
271
                  }).attr("cy", function (t) {

                      return t.y = Math.max(8, Math.min(600, t.y))

4f0953cd   Luigi Serra   graph datalet
272
273
                  });

  

c49910f8   Luigi Serra   graph updates
274
                  glinks.attr("x1", function (t) {

ce4b5fb1   Luigi Serra   graph-datalet
275
276
277
278
279
280
281
282
283
284
285
                      return t.source.x

                  }).attr("y1", function (t) {

                      return t.source.y

                  }).attr("x2", function (t) {

                      return t.target.x

                  }).attr("y2", function (t) {

                      return t.target.y

                  })

              },

              mouseover : function (t) {

                  d3.select(this).selectAll("circle").transition().duration(600).ease("elastic").attr("r", function (t) {

33ee61fd   Luigi Serra   graph updates
286
                      //return 1 == t.fixed ? 1.4 * t.r : 15

0f6424fe   root   update new data-s...
287
                      return 1 == t.fixed ? 1.4 * t.r : t.r + 10;

ce4b5fb1   Luigi Serra   graph-datalet
288
                  });

ce4b5fb1   Luigi Serra   graph-datalet
289
290
291
292
293
              },

  

              mouseout : function () {

                  d3.select(this).selectAll("text").style("visibility", "hidden"), d3.select(this).selectAll("circle").transition().duration(400).attr("r", function (t) {

                      return t.r ? t.r : 15

4ef93b8d   Luigi Serra   graph updates
294
                  });

ce4b5fb1   Luigi Serra   graph-datalet
295
296
297
              },

  

              active : function (t) {

c49910f8   Luigi Serra   graph updates
298
                 _this.fire('graph-datalet_node-clicked', {node : t});

33ee61fd   Luigi Serra   graph updates
299
300
  

                  if(prev_selected_node != null){

01a6d2e5   Luigi Serra   graph updates
301
302
                      prev_selected_node.style.fill = prev_selected_node.style.stroke;

                      prev_selected_node.style.stroke = "#FFFFFF";

33ee61fd   Luigi Serra   graph updates
303
304
305
                  }

  

                  prev_selected_node = document.getElementById("" + t.id);

01a6d2e5   Luigi Serra   graph updates
306
307
                  prev_selected_node.style.fill   = "#FFFFFF"

                  prev_selected_node.style.stroke = t.color;

33ee61fd   Luigi Serra   graph updates
308
  

4ef93b8d   Luigi Serra   graph updates
309
310
311
                  _this.$.dialog.close();

                  _this.$.dialog_title.innerHTML   = t.name;

                  _this.$.dialog_content.innerHTML = t.content;

4ef93b8d   Luigi Serra   graph updates
312
313
314
  

                 _this.$.dialog.open();

              },

33ee61fd   Luigi Serra   graph updates
315
  

4ef93b8d   Luigi Serra   graph updates
316
317
              _onCloseClick : function(){

                  _this.$.dialog.close();

ce4b5fb1   Luigi Serra   graph-datalet
318
319
              },

  

ce4b5fb1   Luigi Serra   graph-datalet
320
321
322
323
324
325
326
327
328
329
330
              /**

               * It is called after the element’s template has been stamped and all elements inside the element’s local

               * DOM have been configured (with values bound from parents, deserialized attributes, or else default values)

               * and had their ready method called.

               *

               * Extract the dataset domain from the entire URL and set the text content of the datalet footer.

               *

               * @method ready

               *

               */

              ready: function(){

ce4b5fb1   Luigi Serra   graph-datalet
331
  

c49910f8   Luigi Serra   graph updates
332
                  _this = this;

4f0953cd   Luigi Serra   graph datalet
333
  

01a6d2e5   Luigi Serra   graph updates
334
335
                  //this.height = 9 * this.width / 16;

                  //this.height = this.width;

4f0953cd   Luigi Serra   graph datalet
336
  

c49910f8   Luigi Serra   graph updates
337
                  width  = this.width;

ce4b5fb1   Luigi Serra   graph-datalet
338
339
                  height = this.height;

  

01a6d2e5   Luigi Serra   graph updates
340
                  //this.$.graph_content.style.height = height + "px";

c49910f8   Luigi Serra   graph updates
341
342
  

                  this.svg = d3.select("svg#sbiricuda").attr("class", "svg").attr({

ce4b5fb1   Luigi Serra   graph-datalet
343
344
                      width: "100%",

                      height: "100%"

c49910f8   Luigi Serra   graph updates
345
                  }).attr("viewBox", "0 0 " + this.width + " " + this.height).attr("pointer-events", "all").style("position", "absolute").attr("style", "transform:translate(0px)");

4f0953cd   Luigi Serra   graph datalet
346
  

a3eb2652   Luigi Serra   graph datalet upd...
347
348
349
350
351
352
                  /*var g = JSON.parse(this.data);

                  g.nodes[0].x = this.width / 2;

                  g.nodes[0].y = this.height / 2;

                  this.buildGraph(g);*/

  

                  var g = this.data;

4f0953cd   Luigi Serra   graph datalet
353
                  g.nodes[0].x = this.width / 2;

01a6d2e5   Luigi Serra   graph updates
354
                  g.nodes[0].y = this.height / 8;

c49910f8   Luigi Serra   graph updates
355
                  this.buildGraph(g);

01a6d2e5   Luigi Serra   graph updates
356
                  console.log(g.nodes.length);

ce4b5fb1   Luigi Serra   graph-datalet
357
358
359
360
              }

          });

      </script>

  </dom-module>