/* MultiPlot macro
reads tab-delimited numbers into results table
and creates a stacked plot.
N.Vischer 01.04.15 2:37
*/




var kk = 2,
bigFont = 28;

macro "Load Tab-delimited Results"{
	run("Results... ", "open=");
	selectWindow("Results");

}

macro "Set Magnification ..."{
	kk = getNumber("Set Maginification (1 or 2)", 2);
}

macro "MultiPlots-Fig 6 [F6]"{
	titleLine = String.getResultsHeadings;
	titles =   split(titleLine, "\t");
	titles =   split("zapA,zapB,FtsZ,PBP3,FtsN,PBP5,bodipy", ",");
	shortTitles =   titles;
	colors = split("black,red,orange,green,blue,magenta,gray,red,orange,green,blue,magenta,gray",",");
	makePlots(titles, colors, shortTitles);
}

macro "MultiPlots-Fig S1 [F1]"{
	titleLine = String.getResultsHeadings;
	titles =   split(titleLine, "\t");
	titles =   split("Eosine,minC,minD", ", ");
	shortTitles =   titles;
	colors = split("black,red,orange,green,blue,magenta,gray,red,orange,green,blue,magenta,gray",",");
	makePlots(titles, colors, shortTitles);
}




macro "Vertical text"{

	newImage("Text", "RGB white", 400 * kk, 400 * kk, 1);
	setJustification("left");
	setFont("SansSerif" , bigFont*kk, "antialiased");
	setColor(0, 0, 0);
	drawString("Relative concentration", 0, 100);
	drawString("Concentration", 0, 200);
	run("Rotate 90 Degrees Left");

}




function makePlots(titles, colors, shortTitles){
	numResults = nResults;//20
	if(numResults < 2) exit("No results available");
	maxPlots = titles.length;
	close("*");
	ww = 600 *kk;
	hh = 100 *kk;
	newImage("ManyPlots", "RGB white", ww +60 + 18, hh + 15 + 40, maxPlots+1);

	for (plot = 0; plot < maxPlots; plot++){
		title = titles[plot];
		Plot.create("Simple Plot", "age", titles[plot]);
		Plot.setLimits(0, 100, 70, 130);
		Plot.setFrameSize(ww, hh);
		xx = newArray(numResults);
		yy = newArray(numResults);

		for (jj = 0; jj < numResults; jj++){
			val = getResult("age", jj);
			xx[jj] = val;
			val = getResult(title, jj);
			yy[jj] = val;
		}
		Plot.setLineWidth(4*kk);
		Plot.setColor(colors[plot]);
		Plot.add("line", xx, yy);
		//Plot.add("circles", xx, yy);
		Plot.setLineWidth(1 *kk);
		Plot.show;
	
		setFont("SansSerif" , bigFont*kk,"antialiased");
		setJustification("right");

		setColor(0, 0, 0);
		drawString(shortTitles[plot], ww + 60 , 44*kk);


		makeRectangle(60, 15, ww, hh);
		changeValues(0xffffff, 0xffffff, 0xf0f0f0);

		run("Select All");
		run("Copy");
		close;
		selectImage("ManyPlots");
		setSlice(plot+1);
		run("Paste");

	}
	//debug;
	vSpace= 17;
	makeRectangle(0,0, getWidth, hh + vSpace);
	run("Crop");
	run("Make Montage...", "columns=1 rows=" + (maxPlots + 1) +" scale=1 increment=1 border=0 font=12");
	makeRectangle(0,0, 60, getHeight);

	run("Clear");
	
	setJustification("Center");// === draw age
	setFont("SansSerif" , 18*kk,"antialiased");
	for (age = 10; age <=90; age +=10){
		drawString(""+ age, 60+ww*age/100, getHeight - hh + kk * 12);
	}
	setJustification("left"); // === draw bottom legend
	setFont("SansSerif" , bigFont*kk,"antialiased");
	drawString("Cell division cycle age [%]", 260, getHeight - kk * 40);
	newWidth = getWidth + 50;
	height = getHeight;
	run("Canvas Size...", "width=&newWidth  position=Top-Right");
	setFont("SansSerif" , 18*kk,"antialiased");// === draw 70-130%
	setJustification("right");
	drawString("70%", 110, getHeight - (hh + vSpace) + kk*12);
	drawString("100%", 110, getHeight - (hh * 1.5 + vSpace) + kk*12);
	drawString("130%", 110, getHeight - (hh * 2 + vSpace) + kk*12);


