index.html
<!DOCTYPE html>
<html>
<head>
<title>d3.js with Direct Yahoo Finance</title>
<script type="text/javascript" src="//mbostock.github.com/d3/d3.js?2.3.0"></script>
<style type="text/css">
body {
font: 10px sans-serif;
}
rect {
fill: #ddd;
}
path.area {
fill: #000;
fill-opacity: .75;
}
.axis, .grid {
shape-rendering: crispEdges;
}
.grid line {
stroke:
#fff;
}
.grid line.minor {
stroke-opacity: .5;
}
.grid text {
display: none;
}
.axis line {
stroke: #000;
}
.grid path,
.axis path {
display: none;
}
</style>
</head>
<body>
<script type="text/javascript">
var margin = {top:
10, right: 50, bottom: 20, left: 10},
width = 960 - margin.right - margin.left,
height = 500 - margin.top -
margin.bottom,
parse = d3.time.format("%Y-%m-%d").parse;
var x = d3.time.scale()
.range([20, width - 20]);
var y = d3.scale.linear()
.range([height - 20,
20]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient
("right");
var area = d3.svg.area()
.x(function(d) { return x(d.Date); })
.y0(y(0))
.y1(function(d) { return y(d.Close); });
var svg = d3.select("body").append("svg")
.attr("width", width +
margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr
("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("rect")
.attr("width", width)
.attr("height", height);
d3.json("rut-json.json", function(data) {
data.reverse().forEach(function(d) {
d.Date = parse(d.Date);
d.Close = +d.Close;
});
x.domain([data[0].Date, data[data.length -
1].Date]);
y.domain([0, d3.max(data, function(d) { return d.Close; })]);
svg.append("g")
.attr("class", "x grid")
.attr("transform", "translate(0," + height + ")")
.call(xAxis.tickSubdivide(1).tickSize(-
height));
svg.append("g")
.attr("class", "y grid")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis.tickSubdivide(1).tickSize(-width));
svg.append("g")
.attr("class", "x axis")
.attr
("transform", "translate(0," + height + ")")
.call(xAxis.tickSubdivide(0).tickSize(6));
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + width + ",0)")
.call(yAxis.tickSubdivide
(0).tickSize(6));
svg.append("path")
.attr("class", "area")
.attr("d", area(data));
});
</script>
</body>
</html>
proxy-json.php
<?php
header('Content-type: application/json');
$ticker = $_GET["ticker"];
$month = intval($_GET["month"]) - 1;
$day = intval($_GET["day"]);
$year = intval($_GET["year"]);
$feed = 'http://ichart.finance.yahoo.com/table.csv?s=' . $ticker . '&a=' . $month . '&b=' . $day . '&c=' . $year . '&g=d&ignore=.csv';
$keys = array();
$newArray = array();
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
$data = csvToArray($feed, ',');
$count = count($data) - 1;
$labels = array_shift($data);
foreach ($labels as $label) {
$keys[] = $label;
}
$keys[] = 'id';
for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
echo json_encode($newArray);
?>