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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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 textR = r * 0.7;

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, textR, startDegree, finishDegree, fontColor, answers[i].text);
	svg.appendChild(svgText);

	startDegree = finishDegree;
}

(function() {
	var NS = 'http://www.w3.org/2000/svg';
	var path = document.createElementNS(NS, 'path');
	var move = 'M' + cx + ' ' + (cy - textR) + ' ' ;
	var arc = 'A' + textR + ' ' + textR + ' 0 0 1 ' + cx + ' ' + (cy + textR);
	path.setAttributeNS(null, 'd', move + arc);
	path.setAttributeNS(null, 'stroke', strokeColor);
	path.setAttributeNS(null, 'stroke-width', strokeWidth);
	path.setAttributeNS(null, 'fill', 'transparent');
	svg.appendChild(path);

	path = document.createElementNS(NS, 'path');
	move = 'M' + cx + ' ' + (cy - textR) + ' ' ;
	arc = 'A' + textR + ' ' + textR + ' 0 0 0 ' + cx + ' ' + (cy + textR);
	path.setAttributeNS(null, 'd', move + arc);
	path.setAttributeNS(null, 'stroke', strokeColor);
	path.setAttributeNS(null, 'stroke-width', strokeWidth);
	path.setAttributeNS(null, 'fill', 'transparent');
	svg.appendChild(path);
})();


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);

	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
Loading ..................

Console