Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
<style>
#left {
  float: left;
  width: 250px;
  overflow: hidden;
  margin-right:130px;
}

h1,h2,h3,h4 {
  font-weight: 400;
  letter-spacing: 0.3rem;
  text-transform: uppercase;
  color: #777;
}


.buttons{
	margin-left:250px;
	margin-bottom:70px;
	margin-top:80px;

   width:805px;
}
#chart {  overflow: hidden;
        height: 360px;
        margin: 0 auto;                                               /* NEW */
        position: relative;
       
      }
      .tooltip {
        background: #eee;
        box-shadow: 0 0 5px #999999;
        color: #333;
        display: none;
        font-size: 12px;
        left: 130px;
        padding: 10px;
        position: Absolute;
        text-align: center;
        top: 95px;
        width: 80px;
        z-index: 10;
      }
      .legend {
        font-size: 12px;
      }
      rect {
        cursor: pointer;                                              /* NEW */
        stroke-width: 2;
      }
      rect.disabled {                                                 /* NEW */
        fill: transparent !important;                                 /* NEW */
      }                                                               /* NEW */
      h1 {                                                            /* NEW */
        font-size: 14px;                                              /* NEW */
        text-align: center;                                           /* NEW */
      } 
             
        .return{
          font-size: 3.8rem;
        }
        .tooltip{
          text-align: left;
        }

.container {
	width: 1200px;
	height: 500px;
	margin: 25px auto 25px auto;
	padding: 50px 50px 50px 50px;
	background-color: white;
	box-shadow: 0 0 20px #ccc;
font-family: 'Source Sans Pro', sans-serif;
}
p{
	 color: #777;
  font-size: 16px;
  line-height: 28px;
  word-spacing: 1px;
  letter-spacing: 1px;

}

</style>
		
		
		<!-- Custom CSS styles -->
		
	</head>
	<body>
	
  	  <script src="../d3.js"></script>
		<div class="container">
		<div id="left">
		<h2> Description </h2>
		<p> This Donut Diagram shows you the number of enrolled students in 2018 in each Major and Specialization
		just click on your desired major and hover on each piece of donut for more information!</p>
		<h2> Category:</h2>
		<p>
		Academic Data</p>
		</div>
    <div id="chart"></div>	
      
			
	<div class='buttons'>
      <button id="tortoise">Culture / Media
</button>
      <button id="mix">Engineering</button>
      <button id="hare">Mathematics and Natural Science</button>
      <button id="socialStudies">Social Studies</button>
      <button id="law">Economy and law </button>
        <button id="med">Medicine </button>
    </div>


		</div> 
		

		<!-- JS Libraries -->
		<!--<script src="d3.js" charset="utf-8"></script>-->
		<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>

		<!-- Custom JS code -->
  <script src="pie-test.js"></script>

		
	</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                (function(d3) {
        'use strict';
        
        var transitionSpeed = 2000;
  
        var hareReturn = "";
        var mixReturn = "";
        var tortoiseReturn = "";
    
        var currentData = [];
        var dataset = [
             {"label": "Mathematics", "count": 16500},
             {"label": "Computer Science", "count": 58000},
              {"label": "Physics", "count": 35000},
              {"label": "Chemistry", "count": 12700},
              {"label": "Biology", "count": 13500},
              ];
          	
	
	
	

          var tortoiseData = [
             {"label": "English / American Studies", "count": 5800},
             {"label": "History", "count": 7900},
            {"label": "German", "count": 11000},
            {"label": "", "count": 0},
          {"label": "", "count": 0}
          ];

        var mixData = [
             {"label": "Electro technology", "count": 17500},
             {"label": "Civil Engineering", "count": 13700},
            {"label": "Architecture", "count": 8850},
            {"label": "Mechanical Engineering / Process Engineering", "count": 37000},
            {"label": "", "count":0 },
            {"label": "", "count": 0},
            {"label": "", "count": 0},
          ];
                   
	
	 var socialData = [
             {"label": "Social Studies", "count": 25400},
             {"label": "Social Sciences", "count": 8200},
            {"label": "Pedagogic", "count": 10500},
            {"label": "Psychology", "count":10800 },
            {"label": "", "count":0 }
            
          ];
       var lawData = [
             {"label": "Economics", "count": 102000},
             {"label": "Business Engineering", "count": 19900},
            {"label": "Jurisprudence", "count": 32000},
            {"label": "Administration & Public Management", "count":20000 },
            {"label": "", "count":0 },
            {"label": "", "count": 0},
            {"label": "", "count": 0},
          ];         	
	 var MedData = [
             {"label": "Medicine", "count": 16500},
             {"label": "Pharmacy", "count": 15100},
            {"label": "Dentist", "count": 14800},
            {"label": "", "count":0 },
            {"label": "", "count": 0}]; 

		
	
	


	


        var width = 360;
        var height = 360;
        var radius = Math.min(width, height) / 2;
        var donutWidth = 75;
        var legendRectSize = 18;
        var legendSpacing = 4;

        var color = d3.scale.category20b();

        

       
        
        
        
        
        
        var svg = d3.select('#chart')
          .append('svg')
          .attr('width', width)
          .attr('height', height)
          .append('g')
          .attr('transform', 'translate(' + (width / 2) + 
            ',' + (height / 2) + ')');

        var arc = d3.svg.arc()
          .innerRadius(radius - donutWidth)
          .outerRadius(radius);

        var pie = d3.layout.pie()
          .value(function(d) { return d.count; })
          .sort(null);

        var tooltip = d3.select('#chart')
          .append('div')
          .attr('class', 'tooltip');
        
        tooltip.append('div')
          .attr('class', 'label');

        tooltip.append('div')
          .attr('class', 'count');

      
        
         /* dataset.forEach(function(d) {
            d.count = +d.count;
            d.enabled = true;                                         // NEW
          }); */
          //prepPieArray(dataset);
          //prepPieArray(tortoiseData);

          currentData = dataset;

          var path = svg.selectAll('path')
            .data(pie(currentData))
            .enter()
            .append('path')
            .attr('d', arc)
            .attr('fill', function(d, i) { 
              return color(d.data.label); 
            })                                                        // UPDATED (removed semicolon)
            .each(function(d) { this._current = d; });                // NEW

          path.on('mouseover', function(d) {
            var total = d3.sum(currentData.map(function(d) {
              return d.count;                       // UPDATED
            }));
            var percent = Math.round(transitionSpeed * d.data.count / total) / 10;
            tooltip.select('.label').html(d.data.label);
            tooltip.select('.count').html(d.data.count); 
             
            tooltip.style('display', 'block');
          });
          
          path.on('mouseout', function() {
            tooltip.style('display', 'none');
          });

        svg.append("text")
        .attr("class", "return")
        .text(hareReturn + "%")
        .attr("y", ".3em")
        .attr("text-anchor", "middle")
	 .style('fill','white')


 var tortoise = d3.select("#tortoise")
        .on("click", function(e){
          //alert("tortoise");
           d3.select( ".return" )
            .transition()
            .duration(transitionSpeed) 
            .tween( 'text', textTween( tortoiseReturn ) );
            
          currentData = tortoiseData;  
          path = path.data(pie(currentData));   
          path.transition()                                       // NEW
              .duration(transitionSpeed)                                        // NEW
              .attrTween('d', arcTween);           
            
        });
        
        var mix = d3.select("#mix")
        .on("click", function(e){
          //alert("tortoise");
           d3.select( ".return" )
            .transition()
            .duration(transitionSpeed) 
            .tween( 'text', textTween( mixReturn ) );
            
          path = path.data(pie(mixData));   
          path.transition()                                       // NEW
              .duration(transitionSpeed)                                        // NEW
              .attrTween('d', arcTween);          
        })
        
        var hare = d3.select("#hare")
        .on("click", function(e){
          //alert("tortoise");
           d3.select( ".return" )
            .transition()
            .duration(transitionSpeed) 
            .tween( 'text', textTween( hareReturn ) );
            
          path = path.data(pie(dataset));   
          path.transition()                                       // NEW
              .duration(transitionSpeed)                                        // NEW
              .attrTween('d', arcTween);          
        })
var social = d3.select("#socialStudies")
        .on("click", function(e){
          //alert("tortoise");
           d3.select( ".return" )
            .transition()
            .duration(transitionSpeed) 
            .tween( 'text', textTween( mixReturn ) );
            
          path = path.data(pie(socialData));   
          path.transition()                                       // NEW
              .duration(transitionSpeed)                                        // NEW
              .attrTween('d', arcTween);          
        })
var ecolaw = d3.select("#law")
        .on("click", function(e){
          //alert("tortoise");
           d3.select( ".return" )
            .transition()
            .duration(transitionSpeed);
            
          path = path.data(pie(lawData));   
          path.transition()                                       // NEW
              .duration(transitionSpeed)                                        // NEW
              .attrTween('d', arcTween);          
        })
 
var Med = d3.select("#med")
        .on("click", function(e){
          //alert("tortoise");
           d3.select( ".return" )
            .transition()
            .duration(transitionSpeed);
            
          path = path.data(pie(MedData));   
          path.transition()                                       // NEW
              .duration(transitionSpeed)                                        // NEW
              .attrTween('d', arcTween);          
        })
      

  function textTween( newValue ) {
    return function() {
      // get current value as starting point for tween animation
      var currentValue = +this.textContent;
      // create interpolator and do not show nasty floating numbers
      var i = d3.interpolate( this.textContent, newValue );

      return function(t) {
        
        this.textContent = d3.round( i(t) , 2) + "%" ;
      };
    }
  }

// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(d) {
 var interpolate = d3.interpolate(this._current, d); // NEW
  this._current = interpolate(0);                     // NEW
  return function(t) {                                // NEW
    return arc(interpolate(t));                       // NEW
  };     
}

d3.interpolators.push(function(a, b) {
  var re = /^(\d\d\.\d\d)%$/, ma, mb, f = d3.format('05.2f');
  
  if ((ma = re.exec(a)) && (mb = re.exec(b))) {
    
    a = parseFloat(ma[1]);
    b = parseFloat(mb[1]) - a;
    
   
    
    return function(t) {
      return f(a + b * t) + '%';
    };
  }
});



      })(window.d3);
              
            
!
999px

Console