block by timelyportfolio 3007024

ripoff d3.js dji-area example but use direct yahoo finance feed

Full Screen

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;

// Scales. Note the inverted range for the y-scale

var x = d3.time.scale()
    .range([20, width - 20]);

var y = d3.scale.linear()
    .range([height - 20, 

20]);

// Axes.
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
    yAxis = d3.svg.axis().scale(y).orient

("right");

// An area generator.
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);
//use this line if running outside of bl.ocks.org
//d3.json("//axysreporting.com/proxy-json.php?ticker=^rut&month=1&day=1&year=1900",  function(data) {
//to work in blocks I have pasted the json into this gist
d3.json("rut-json.json", function(data) {



  // Parse dates and numbers.
  data.reverse().forEach(function(d) {
    d.Date = parse(d.Date);
    d.Close = +d.Close;
  });

  

// Compute the minimum and maximum date, and the maximum price.
  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
/*
 * Converts CSV to JSON
 * Example uses Google Spreadsheet CSV feed
 * csvToArray function I think I found on php.net
 */

header('Content-type: application/json');

// Set your CSV feed
    $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';

// Arrays we'll use later
$keys = array();
$newArray = array();

// Function to convert CSV into associative 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; 
} 

// Do it
$data = csvToArray($feed, ',');

// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;
  
//Use first row for names  
$labels = array_shift($data);  

foreach ($labels as $label) {
  $keys[] = $label;
}

// Add Ids, just in case we want them later
$keys[] = 'id';

for ($i = 0; $i < $count; $i++) {
  $data[$i][] = $i;
}
  
// Bring it all together
for ($j = 0; $j < $count; $j++) {
  $d = array_combine($keys, $data[$j]);
  $newArray[$j] = $d;
}

// Print it out as JSON
echo json_encode($newArray);

?>