D3 Presentation - Sample 7 - Layouts

In this example below you will see how to do a D3 Presentation - Sample 7 - Layouts with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by JonasStattin, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright JonasStattin ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>D3 Presentation - Sample 7 - Layouts</title>
  
  
  
  
  
</head>

<body>

  <svg id="image" width="400" height="400"></svg>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/JonasStattin/d3-presentation-sample-7-layouts-pJMMmP */
var svg = d3.select('#image'),
    data = [
      {
        label: 'A',
        value: 10
      },
      {
        label: 'B',
        value: 7
      },
      {
        label: 'C',
        value: 13
      }
    ],
    colors = ['#3498db', '#e74c3c', '#1abc9c', '#f1c40f', '#34495e'],
    width = svg.attr('width'),
    height = svg.attr('height'),
    radius = width / 2;

var colorScale = d3.scale.ordinal()
  .range(colors)
  .domain(data.map(function(d, i, j) {
    return d.label;
  }));

var arcHelper = d3.svg.arc()
  .outerRadius(radius)
  .innerRadius(0);

var pieLayout = d3.layout.pie()
  .value(function(d, i, j) { 
    return d.value; 
  });

var wrapper = svg.append('g')
  .attr('transform', 'translate('+radius+', '+radius+')');

var arc = wrapper.selectAll('.arc')
  .data(pieLayout(data))
  .enter().append('g')
    .attr('class', 'arc');

arc.append('path')
  .attr('d', arcHelper)
  .style('fill', function(d, i, j) { 
    return colorScale(d.data.label); 
  });

Comments