	
	$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{}
		}
	}

	$graphvar = new Dygraph(
		document.getElementById("$divname"),
		$dataname,	
		{
			labels: [ "Date","Lower" , "Mean", "Upper", "Scatter"],
			labelsDiv: document.getElementById("$labeldiv"),
			labelsSeparateLines: true,
			legend: "always",
			title: '$title',
			xlabel: 'Date',
			ylabel: '$ylabel',
			showRangeSelector: true,
			rangeSelectorHeight: 20,
			rangeSelectorPlotFillColor: "rgba(0,0,0,0)",
			rangeSelectorPlotFillGradientColor: "rgba(0,0,0,0)",
			rangeSelectorPlotStrokeColor: "rgba(0,0,0,0)",
			highlightSeriesOpts: { highlightCircleSize : 5 },
			interactionModel: Dygraph.defaultInteractionModel,
			includeZero: true,	
			displayAnnotations: true,
			drawHighlightPointCallback: Dygraph.Circles.HEXAGON,
			axisLabelFontSize: 10,
			titleHeight: 25,
			xLabelHeight: 15,
			yLabelWidth: 15,
			legendFormatter: legendFormatter,
			axes: {
				x: {	
					valueFormatter: function me(d,opt,se,dy,row,col) {
						var text = [], 
						data = $dataname[row],
						run = $runname[row],
						sample = $samplename[row];
						
						text = "<b>Run Name: </b>".fontcolor("#800000") + run[col] 
								+ "<br/><b>Sample Name (y-value): </b>".fontcolor("#008080") + "<font size='-1.5'>" + sample + "</font>" 
								+ "<br><b>Date:</b> ".fontcolor("#800080") + moment(d).format("DD/MM/YYYY") 
								+ "<br/><b> Mean: </b>".fontcolor("#006400") + data[2]   
								+ "<br/><b> 2SD: </b>".fontcolor("#ff0000") + data[1] + ' - ' + data[3]  
								+ "<br/><b> $ylabel: </b>".fontcolor("#000080") + data[4] 
								+ '</font>'
						
						
						if (run[1].length == 0){return text}
						else if (run[1].length != 0) {
							note = "<br/><b>Notes: </b><br/>" + run[1] 
							text = 	text + note.fontcolor("red") 
							return text
						}
						else {console.log("Error in labels")}
					},
				},	
			},
			series: {
				"Lower" : {
					strokePattern: Dygraph.DASHED_LINE ,
					color: "#ff0000",
					strokeWidth: 2,
				},
				"Mean" : {
					drawPoints: false,
					color: "#006400",
					strokeWidth: 2,
				},
				"Upper": {	
					strokePattern: Dygraph.DASHED_LINE ,
					color: "#ff0000",
					strokeWidth: 2, 
				},
				"Scatter" : {
					drawPoints: true,
					color: "#0080ff",
					pointSize: 3,
					strokeWidth: 0,
				}
			},
			underlayCallback: function(canvas, area, g) {		
				
				ctx = g.hidden_ctx_;
				ctx.strokeStyle = "#dd4b39";
				
				for (i = 0; i < $dataname.length; i++) { 
								
					var point = $dataname[i][4],
					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)
				makeQuery(run,date)
			},
		}
	);

	document.getElementById("$restorebutton").onclick 	= function() { restorePositioning($graphvar); };
	document.getElementById("$yearbutton").onclick 	= 	function() 	{$graphvar.updateOptions({dateWindow: [Date.parse('$startdate'),Date.parse('$enddate')]});};
	gs.push($graphvar);
	