cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <p>神はいると思う?</p>
<svg id="pie-chart" xmlns="http://www.w3.org/2000/svg" width="400px" height="400px"></svg>
            
          
!
            
              (function() {

var answers = [{text: 'インターネットで見た', percent: 82}, {text: 'いない', percent: 13}, {text: 'わからない', percent: 5}];

var cx = 200; //中心点のx座標;
var cy = 200; //中心点のy座標;
var r = 196; //半径;

var startDegree = 0;
var finishDegree = 0;
var backColor = '#fdf7e2';
var fontColor = '#6d5450';
var strokeColor = '#6d5450';
var strokeWidth = 2;

var svg = document.getElementById('pie-chart');
for(var i = 0; i < answers.length; i++) {
	//扇形の終わりの角度を導く;
	finishDegree = startDegree + 360 * answers[i].percent / 100;
	//扇形を描く;
	var path = createFanShape(cx, cy, r, startDegree, finishDegree, backColor, strokeColor, strokeWidth);
	svg.appendChild(path);
	//扇形の中にテキストを配置する;
	var svgText = createPieChartText(cx, cy, r * 0.7, startDegree, finishDegree, fontColor, answers[i].text);
	svg.appendChild(svgText);

	//次の扇形の始まりの角度を現在の扇形の終わりの角度にする;
	startDegree = finishDegree;
}


function createFanShape(cx, cy, r, startDegree, finishDegree, backColor, strokeColor, strokeWidth) {
	//円弧の始まりの座標;
	var startX = cx + r * Math.sin(startDegree / 180 * Math.PI);
	var startY = cy - r * Math.cos(startDegree / 180 * Math.PI);
	//円弧の終わり座標;
	var finishX = cx + r * Math.sin(finishDegree / 180 * Math.PI);
	var finishY = cy - r * Math.cos(finishDegree / 180 * Math.PI);
	//扇形の角度が180度を超えているか;
	var largeArcFlag = (finishDegree - startDegree <= 180) ? 0 : 1;

	//扇形の中心点へ移動するコマンド;
	var move = 'M' + cx + ' ' + cy + ' ';
	//扇形の中心点から円弧の始まりまで線を結ぶコマンド;
	var line = 'L' + startX + ' ' + startY + ' ';
	//円弧を描くコマンド;
	var arc = 'A' + r + ' ' + r + ' ' + 0 + ' ' + largeArcFlag + ' ' + 1 + ' ' + finishX + ' ' + finishY + ' ';
	//円弧の終おわりから中心点を結ぶコマンド;
	var close = 'Z';

	//path要素を作成;
	var NS = 'http://www.w3.org/2000/svg';
	var path = document.createElementNS(NS, 'path');
	path.setAttributeNS(null, 'd', move + line + arc + close);
	path.setAttributeNS(null, 'fill', backColor);
	path.setAttributeNS(null, 'stroke', strokeColor);
	path.setAttributeNS(null, 'stroke-width', strokeWidth);

	return path;
}

function createPieChartText(cx, cy, r, startDegree, finishDegree, fontColor, text) {
	//textを表示する角度(扇形の真ん中);
	var degree = (startDegree + finishDegree) / 2;
	//textの座標;
	var textX = cx + r * Math.sin(degree / 180 * Math.PI);
	var textY = cy - r * Math.cos(degree / 180 * Math.PI);

	//text要素を作成;
	var NS = 'http://www.w3.org/2000/svg';
	var svgText = document.createElementNS(NS, 'text');
	svgText.textContent = text;
	svgText.setAttributeNS(null, 'x', textX);
	svgText.setAttributeNS(null, 'y', textY);
	svgText.setAttributeNS(null, 'fill', fontColor);
	svgText.setAttributeNS(null, 'dominant-baseline', 'middle');
	svgText.setAttributeNS(null, 'text-anchor', 'middle');

	return svgText;
}

})();
            
          
!
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