Skip to content

Instantly share code, notes, and snippets.

@bengolder
Forked from lqb2/5feb2014.csv
Last active August 29, 2015 13:56
Show Gist options
  • Save bengolder/9219344 to your computer and use it in GitHub Desktop.
Save bengolder/9219344 to your computer and use it in GitHub Desktop.
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://code.shutterstock.com/rickshaw/vendor/d3.layout.min.js"></script>
<script src="http://code.shutterstock.com/rickshaw/rickshaw.min.js"></script>
<script src="tosci_ben.js" charset="utf-8"></script>
<div id="chart"></div>
<script>
var data = [ { x: 0, y: 40 }, { x: 1, y: 49 }, { x: 2, y: 17 }, { x: 3, y: 42 } ];
var graph = new Rickshaw.Graph( {
element: document.querySelector("#chart"),
width: 580,
height: 250,
series: [ {
color: 'steelblue',
data: data,
//data: coffeeData
} ]
} );
graph.render();
</script>
</body>
</html>
function parseRow(d) {
var dateTimeJoiner = d3.time.format("%x %X"); // combines date and time into a Date object
var dateTime = dateTimeJoiner.parse(d.Date + " " + d["Time"]) // grabs the date and time
var gross_sales_conversion = +d["Gross Sales"].substr(1) //strips the $ sign
var total_sales = gross_sales_conversion * 1.07 //add tax
return {
date: dateTime,
category: d.Category,
item: d.Item,
quantity: +d.Quantity,
pricepoint: d["Price Point Name"],
gross_sales: gross_sales_conversion,
total_sale: +d3.format(".2f")(total_sales), // rounds to 2 decimal places
device_name: d["Device Name"]
};
}
function prepRowForRickshaw( d ) {
return {
x: d.date,
y: d.total_sale,
};
}
function handleRows( error, rows ) {
// this function is only called once all the rows are loaded
// In this function you can now access the loaded and parsed rows
rows.forEach( handleRow );
//console.log("dataset after loading csv:", dataset)
console.log("coffeeData after full loading:", coffeeData);
}
function handleRow( row, i, rows ) {
//console.log("item #", i, row.date, "adding to dataset");
if (row.category == "Coffee") { // for all items that are coffee...
//console.log("Adding", row.item, "to dataset");
//console.log("This item was sold in the hour of", d3.time.hour.floor(row.date));
//if the hour of sale of this item exists as a key in our sales dictionary
if (!(d3.time.hour.floor(row.date) in coffeeData)) {
//add the total_sale to that hour
coffeeData[d3.time.hour.floor(row.date)] = row.total_sale
//console.log("Adding", row.total_sale, "of", row.item, "to new hour:", d3.time.hour.floor(row.date))
}
//otherwise, add that hour to the dictionary and initialize it with this item's total_sale
else {
coffeeData[d3.time.hour.floor(row.date)] += row.total_sale
//console.log("Adding", row.total_sale, "of", row.item, "to existing hour", d3.time.hour.floor(row.date));
}
}
dataset.push(row);
}
// d3.csv( csv_file_path, parsing_function, callback_function)
d3.csv("5feb2014.csv", parseRow, handleRows);
var dataset = [];
var coffeeData = [];
//var coffeeMap = d3.map(coffeeData);
//console.log("dataset before loading csv:", dataset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment