var i, pi, rmax;
var xlMedium, xlNone, xlThin, xlAutomatic, xlXYScatter;
var xlCircle, xlValue, xlCategory;

pi = 3.141592653589793;
ExcelConstants();

var objXL = WScript.CreateObject("Excel.Application");

objXL.Visible = true;
objXL.WorkBooks.Add();					//新規ブック

// 1/4円を描くためのデータ ４列目がx、５列目がy
//本当はワークシート関数を使ったほうが速い
for(i = 0; i <= 50; i++) {
	objXL.Cells(i + 1, 4).Value = Math.sin(pi * i / 100);
	objXL.Cells(i + 1, 5).Value = Math.cos(pi * i / 100);
}

//座標値が0〜1の値を取る２次元の点が
//半径１の円の中にいくつ入ったかで円周率を求める
rmax = 1000;
with(objXL) {
	Range(Cells(1, 3), Cells(rmax, 3)).FormulaR1C1
							= "=if(rc1*rc1+rc2*rc2<1,1,0)";
	Range(Cells(1, 1), Cells(rmax, 2)).FormulaR1C1
							= "=rand()";
}
objXL.Cells(1, 7).FormulaR1C1						//結果
				= "=average(r1c3:r" + rmax + "c3)*4";
objXL.Cells(1, 7).Borders.Weight = xlMedium;

//疑似乱数によって生成された点と1/4円をグラフとして描く
with(objXL) {
    ActiveSheet.ChartObjects.Add(60, 30, 330, 300).Select
    with(ActiveSheet.ChartObjects(1).Chart) {
		Type = xlXYScatter;
		HasLegend = false;
		
		// 1/4の円
		SeriesCollection.Add(objXL.Range("E1:E51"));
		with(SeriesCollection(1)) {
			XValues = objXL.Range("D1:D51");
			MarkerStyle = xlNone;
			Border.LineStyle = xlAutomatic;
		}
		
		//疑似乱数によって生成された点
		SeriesCollection.Add(objXL.Range("B1:B" + rmax));
		with(SeriesCollection(2)) {
			XValues = objXL.Range("A1:A" + rmax);
			MarkerStyle = xlCircle;
			MarkerBackgroundColorIndex = 1;
			MarkerForegroundColorIndex = 1;
			Border.LineStyle = xlNone;
		}
		
		//グラフのフォーマット
    	with(Axes(xlValue)) {
	        MinimumScale = 0;
        	MaximumScale = 1;
        	MajorTickMark = xlNone;
        	TickLabelPosition = xlNone;
        	MajorUnit = 1
        }
	    with(Axes(xlCategory)) {
        	MinimumScale = 0
        	MaximumScale = 1
        	MajorTickMark = xlNone
        	TickLabelPosition = xlNone
        }
    	with(PlotArea) {
        	Border.LineStyle = xlAutomatic
    		Interior.ColorIndex = xlNone
    	}
	}
}

//Excelの定数
function ExcelConstants() {
	xlMedium = -4138;
	xlNone = -4142;
	xlThin = 2;
	xlAutomatic = -4105;
	xlXYScatter = -4169;
	xlCircle = 8;
	xlValue = 2;
	xlCategory = 1;
}
