Advertisement
Guest User

d3

a guest
Jul 15th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var renderSvg = function(container) {
  2.     var svg = d3
  3.         .select('#svg-container')
  4.         .append('svg')
  5.         .attr({
  6.             xmlns: "http://www.w3.org/2000/svg",
  7.             xlink: "http://www.w3.org/1999/xlink",
  8.             viewBox: `0 0 ${container.clientWidth} ${container.clientHeight}`
  9.         })
  10.         .call(
  11.             d3.behavior.zoom()
  12.                 .on('zoom', function () {
  13.                     svg.attr('transform', 'translate(' + d3.event.translate + ')' + ' scale(' + d3.event.scale + ')')
  14.                 })
  15.         )
  16.         .append('g');
  17.        
  18.     return svg;
  19. };
  20.  
  21. var renderNodes = function(svg, force) {
  22.  
  23.     var group = svg.selectAll('g')
  24.         .data(force.nodes())
  25.         .enter()
  26.         .append('g');
  27.        
  28.     var defs = group
  29.         .append('defs');
  30.  
  31.     var path = defs
  32.         .append('path')
  33.         .attr({
  34.             id: n => 'path-' + n.id,
  35.             d: n => `m5,${n.radius} a${n.radius - 5},${n.radius - 5} 0 0 0 ${n.radius * 2 - 10},0`
  36.         });
  37.  
  38.     var circle = group
  39.         .append('circle')
  40.         .attr({
  41.             class: 'circle',
  42.             fill: '#ccc',
  43.             cx: n => n.radius,
  44.             cy: n => n.radius,
  45.             r: n => n.radius
  46.         });
  47.  
  48.     var text = group
  49.         .append('text')
  50.         .attr({
  51.             fill: '#333',
  52.             'font-size': '15px'
  53.         });
  54.  
  55.     var textPath = text
  56.         .append('textpath')
  57.         .text(n => n.name)
  58.         .attr('xlink:href', n => '#path-' + n.id)
  59.         .attr({
  60.             'start-offset': '0%',
  61.             'href': n => '#path-' + n.id
  62.         });
  63.  
  64.     var use = group
  65.         .append('use')
  66.         .attr('xlink:href', n => '#path-' + n.id)
  67.         .attr({
  68.             fill: '#666',
  69.             opacity: '0.1',
  70.             'href': n => '#path-' + n.id
  71.         });
  72.  
  73.     return group;
  74. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement