css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="chart-funding"></div>
            
          
!
            
              body {
  padding: 3em;
}
            
          
!
            
              /**
 * Main.js
 *
 */

(function (global) {
	'use strict';

	/**
	 * Create a SVG Pie Chart
	 * Arguments:
	 *   data: an array of numbers to chart, one for each wedge of the pie.
	 *   width: width of SVG graphic [px]
	 *   height: height of SVG graphic [px]
	 *   cx, cy, r: the center and radius of the pie
	 *   colors: array of HTML color strings, one for each wedge
	 *   labels: array of labels to appear in the legend, one for each wedge
	 *   lx, ly: upper-left corner of the chart legend
	 * Returns:
	 *    <svg> element that holds the pie chart.
	 *    The caller must insert the returned element into the document.
	 */

	var pieChart = function (data, width, height, cx, cy, r, colors, labels, lx, ly) {
		var svgns = 'http://www.w3.org/2000/svg';
		var chart = document.createElementNS(svgns, 'svg:svg');
		var total = 0;
		var angles = [];
		var startangle = 0;
		var i = 0;

		chart.setAttribute('width', width);
		chart.setAttribute('height', height);
		chart.setAttribute('viewBox', '0 0 ' + width + ' ' + height);

		// Add up the data values so we know how big the pie is
		for (i = 0; i < data.length; i++) {
			total += data[i];
		};

		// Now figure out how big each slice of pie is. Angles in radians.
		for (i = 0; i < data.length; i++) {
			angles[i] = data[i] / total * Math.PI * 2;
		};

		// Loop through each slice of pie.
		for (i = 0; i < data.length; i++) {
			var path = document.createElementNS(svgns, 'path');
			var animate = document.createElementNS(svgns, 'animate');
			var labelColor = document.createElementNS(svgns, 'rect');
			var label = document.createElementNS(svgns, 'text');
			var endangle = startangle + angles[i];
			var x1 = cx + r * Math.sin(startangle);
			var y1 = cy - r * Math.cos(startangle);
			var x2 = cx + r * Math.sin(endangle);
			var y2 = cy - r * Math.cos(endangle);
			var big = 0;
			var dStart, dEnd;
			var labelShare = Math.round(((data[i] / total) * 100)) + '%: ';
			var duration = 1;

			if (endangle - startangle > Math.PI) {
				big = 1;
			}

			dStart = 'M ' + cx + ',' + cy + ' L ' + x1 + ',' + y1 + ' A ' + r + ',' + r + ' 0 ' + big + ' 1 ' + x1 + ',' + y1 + ' Z';
			dEnd = 'M ' + cx + ',' + cy + ' L ' + x1 + ',' + y1 + ' A ' + r + ',' + r + ' 0 ' + big + ' 1 ' + x2 + ',' + y2 + ' Z'; 

			// Now set attributes on the <svg:path> element
			path.setAttribute('d', dStart);
			path.setAttribute('fill', colors[i]);
      
			path.appendChild(animate);
			chart.appendChild(path);
      
			animate.setAttribute('dur', duration + 's');
			animate.setAttribute('repeatCount', 1);
			animate.setAttribute('attributeName', 'd');
			animate.setAttribute('fill', 'freeze');
			animate.setAttribute('begin', ( duration * i ) + 's');
			animate.setAttribute('values', dStart + ';' + dEnd + ';');
      

			// The next wedge begins where this one ends
			startangle = endangle;

			// Now draw a little matching square for the key
			labelColor.setAttribute('x', lx);
			labelColor.setAttribute('y', ly + 30*i);
			labelColor.setAttribute('width', 20);
			labelColor.setAttribute('height', 20);
			labelColor.setAttribute('fill', colors[i]);
			chart.appendChild(labelColor);

			// Add label to the right of the rectangle
			label.setAttribute('x', lx + 30);
			label.setAttribute('y', ly + 30*i + 18);
			label.setAttribute('font-family', '"jaf-bernino-sans", sans-serif');
			label.setAttribute('font-size', '16');
			// Add DOM node for the text to the <svg:text> element
			label.appendChild(document.createTextNode(labelShare + labels[i]));
			chart.appendChild(label);
		}

		return chart;
	};



	// fundingChart
	var fundingChartWrapper = document.querySelector('#chart-funding');
	var fundingChartData = [60, 250]; // funded, wanted
	var fundingChartColors = ['rgb(0, 178, 202)', '#EEEEEE'];
	var fundingChartLabels = ['Raised monthly money', 'still needed'];
	var fundingChart = pieChart(fundingChartData, 450, 200, 100, 100, 100, fundingChartColors, fundingChartLabels, 220, 50);

  

	if (fundingChartWrapper) {
		fundingChartWrapper.appendChild(fundingChart);
		fundingChartWrapper.classList.add('has-svg');
	};
}(window));


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console