Quandl Data with Charts.js using JSON and PHP

Brent Crude Prices Over 30 Days


The Source


//************  Brent Crude Prices  ****************
$br_json = file_get_contents('https://www.quandl.com/api/v1/datasets/CHRIS/CME_BZ1.json?auth_token=YOUR ACCESS TOKEN');
$br_obj = json_decode($br_json, true);
//Build arrays
$br_label_arr = array();
$br_value_arr = array();
$i = 0;
	foreach ($br_obj['data'] as $br_data){ //loop through data
		$br_label_arr[] = date('M j',strtotime($br_data[0])); //pull dates
		$br_value_arr[] = $br_data[6]; //pull prices
		if (++$i == 30) break;
	}
$br_labels = array_reverse($br_label_arr); //reverse the data for ASC
$br_values = array_reverse($br_value_arr); //reverse the data for ASC
$br_labels = implode('","', $br_labels); //comma sep
$br_values = implode(", ", $br_values); //comma sep

//************  Charts.js script  ****************		
var lineChartData = {
	labels : ["Apr 27","Apr 28","Apr 29","Apr 30","May 3","May 4","May 5","May 6","May 7","May 10","May 11","May 12","May 13","May 14","May 17","May 18","May 19","May 20","May 21","May 24","May 25","May 26","May 27","May 28","Jun 1","Jun 2","Jun 3","Jun 4","Jun 8","Jun 9"],
	datasets : [
	{
		label: "Brent Crude",
		fillColor : "rgba(151,187,205,0.3)",
		strokeColor : "#999",
		pointColor : "rgba(151,187,205,1)",
		pointStrokeColor : "#fff",
		pointHighlightFill : "#000",
		pointHighlightStroke : "rgba(151,187,205,1)",
		data : [66.42, 67.27, 68.56, 67.25, 67.29, 68.88, 68.96, 68.09, 68.28, 68.32, 68.55, 69.32, 67.05, 68.71, 69.46, 68.71, 66.66, 65.11, 66.44, 68.46, 68.65, 68.87, 69.46, 69.63, 69.63, 71.35, 71.31, 71.89, 72.22, 72.22]
	}
	]

}

window.onload = function(){
	var ctx = document.getElementById("canvas").getContext("2d");
	window.myLine = new Chart(ctx).Line(lineChartData, {
		responsive: true
	});
}