
	$dataname = $data
	$runname = $run_name
	$samplename = $sample_name

	for (j = 0; j < $dataname.length; j++){
		$dataname[j][0] = new Date($dataname[j][0])
	}
	for (j = 0; j < $runname.length; j++){
		for (i = 0; i < obj.length; i++){
			var ann = obj[i]
			if(ann.Run == $runname[j][0]){
				$runname[j][1]=ann.Annotation
			}else{}
		}
	}

	var $id_perSample = "$id_perSample_bool";

	$graphvar = new Dygraph(
    	document.getElementById("$divname"),
		$dataname, 
		{
			labels: [ "Date","Threshold","Threshold","$ylabel"],
			labelsDiv: document.getElementById("$labeldiv"),
			labelsSeparateLines: true,
			showRangeSelector: true,
			rangeSelectorHeight: 20,
			includeZero: true,
			rangeSelectorPlotFillColor: "rgba(0,0,0,0)",
			rangeSelectorPlotFillGradientColor: "rgba(0,0,0,0)",
			rangeSelectorPlotStrokeColor: "rgba(0,0,0,0)",
			interactionModel: Dygraph.defaultInteractionModel,
			highlightSeriesOpts: { highlightCircleSize : 5 },
			legend: "always",
			title: '$title',
			xlabel: 'Date',
			ylabel: '$ylabel',
			axisLabelFontSize: 10,
			titleHeight: 25,
			xLabelHeight: 15,
			yLabelWidth: 15,
			legendFormatter: legendFormatter,
			axes: {
				x: {
					valueFormatter: function (d,opt,se,dy,row,col) {
						var text = [], 
						data = $dataname[row],
						run = $runname[row],
						sample = $samplename[row];

						if ($id_perSample == "False"){
							text = "<b>Run ID: </b>".fontcolor("#000") + run[0] 
									+ "<br/><b>Sample ID (y-value): </b>".fontcolor("#000") + "<font size='-1.5'>" + sample + "</font>" 
									+ "<br><b>Date:</b> ".fontcolor("#000") + moment(d).format("DD/MM/YYYY")
						}else{
							text = "<b>Sample ID: </b>".fontcolor("#000") + run[0] 
									+ "<br/><b>Run ID: </b>".fontcolor("#000") + "<font size='-1.5'>" + sample + "</font>" 
									+ "<br><b>Date:</b> ".fontcolor("#000") + moment(d).format("DD/MM/YYYY")
						}
						
						if (data[1]==null){
							text = text + "<br/><b>Threshold: </b>".fontcolor("#ff0000") + data[2]
						}
						else{
							text = text + "<br/><b>Upper Threshold: </b>".fontcolor("#ff0000") + data[1]
									+ "<br/><b>Lower Threshold: </b>".fontcolor("#ff0000") + data[2]
						}
						
						 text = text + "<br/><b>$ylabel: </b>".fontcolor("#0080ff") + data[3] 
						
						if (run[1].length != 0){
							note = "<br/><b>Notes: </b><br/>" + run[1]
							text = text + note.fontcolor("red");
							return text
						}
					
						return text
						
					},					
				},
			},
			series: {
				"Threshold" : {
					strokePattern: Dygraph.DASHED_LINE ,
					color: "#ff0000",
					strokeWidth: 2, 
				}, 
				"$ylabel" : {
					strokeWidth: 0,
					drawPoints: true,
					pointSize: 3,
					color: "#0080ff"
				}
			},	
			underlayCallback: function(canvas, area, g) {		
				
				ctx = g.hidden_ctx_;
				ctx.strokeStyle = "#dd4b39";
				
				for (i = 0; i < $dataname.length; i++) { 
								
					var point = $dataname[i][3],
						note = $runname[i][1],
						date = $dataname[i][0];
					
					if ($runname[i][0] == null){}
					else if(note.length != 0){
						mutated = g.toDomCoords(date, point)
						ctx.beginPath();
						ctx.arc(mutated[0], mutated[1],5,0,Math.PI*2,true);
						ctx.closePath();
						ctx.stroke();
					}
				}
				
				ctx.fillStyle = "#0080ff";
				ctx.strokeStyle = "#0080ff";
				var [bottom,top] = g.yAxisRange();
					for(i = 0; i < dateobj.length; i++) {
						date = dateobj[i];
						position = g.toDomCoords(date.Annotated_date,bottom);
						dateobj[i].key3 = position;
						canvas.fillText(dateobj[i].Notes,dateobj[i].key3[0]+2,dateobj[i].key3[1]-15);
						canvas.strokeRect(dateobj[i].key3[0],dateobj[i].key3[1]-25,dateobj[i].Notes.length*7,15)
						canvas.beginPath();
						canvas.moveTo(dateobj[i].key3[0],dateobj[i].key3[1]);
						canvas.lineTo(dateobj[i].key3[0],dateobj[i].key3[1]-10);
						canvas.closePath();
						canvas.stroke();
						
					}
				
			},
			pointClickCallback: function(e, point) {
				run = $runname[point.idx][0]
				date = $dataname[point.idx][0]
				date =  moment(date).format("DD-MM-YYYY")
				//copyToClipboard(run)
				chartid = "$chart_id"
				makeQuery(run,date, chartid)
			},
		}
	);
	var upper=1, lower =1;
	for(i = 1; i < $dataname.length; i++){
		if($dataname[i][1]==null){upper += 1;}
		if($dataname[i][2]==null){lower += 1;}
	}
	if(upper==$dataname.length && lower==$dataname.length){$graphvar.updateOptions({labels: ["Date","","","$ylabel"]});}
	else if (lower==$dataname.length){$graphvar.updateOptions({labels: ["Date","Threshold","","$ylabel"]});}
	else if (upper ==$dataname.length){$graphvar.updateOptions({labels: ["Date","","Threshold","$ylabel"]});}
	
	document.getElementById("$restorebutton").onclick 	= function() { restorePositioning($graphvar); };
	document.getElementById("$yearbutton").onclick 	= 	function() 	{$graphvar.updateOptions({dateWindow: [Date.parse('$startdate'),Date.parse('$enddate')]});};
	gs.push($graphvar);
	