Skip to content

Instantly share code, notes, and snippets.

@lauriemerrell
Last active February 7, 2020 21:57
Show Gist options
  • Save lauriemerrell/e89123796425dc9d24700fbe8a644a17 to your computer and use it in GitHub Desktop.
Save lauriemerrell/e89123796425dc9d24700fbe8a644a17 to your computer and use it in GitHub Desktop.
CrimeInChicago_BarPlot_Starter
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style type="text/css">
svg {
border:1px solid #f0f;
}
</style>
<body>
</body>
<script>
//Define variables that do not need data first:
//Define Margins and svg here:
var margin = {top: 20, right: 30, bottom: 20, left: 30};
var width = 400;
var height = 300;
var svg = d3.select("body")
.append("div")
.append("svg");
//Define xscale, yscale, xaxis, and yaxis here:
var xScale = d3.scaleBand()
.rangeRound([0,width]);
// var yScale =
var yScale = d3.scaleLinear()
.range([0,height]);
// var xAxis =
var xAxis = d3.axisBottom(xScale);
// var yAxis =
var yAxis = d3.axisLeft(yScale);
// Reading in data here, then calling "ready" function:
d3.csv("https://raw.githubusercontent.com/molliemarie/MSIA-D3Course-2019/master/Projects%26Exercises/FirstCompleteBar/data/ChiCrime.csv", function(d) {
return {
count: +d.count,
year: +d.year,
violation: d['Primary Type']
}
}).then(ready);
// Ready Function
function ready(data) {
// filtering for 2018 data
data2018 = data.filter(function(d) { return d.year == 2018})
console.log(data2018)
// Define xScale and yScale domains here:
xScale.domain(data2018.map(function(d) { return d.violation; }));
yScale.domain(0, d3.max(data2018.count, function(d){return d.count}));
// define xAxisGroup and yAxisGroup here:
var xAxisGroup = svg.append("g")
.attr("class", "x axis")
.call(xAxis);
var yAxisGroup = svg.append("g")
.attr("class", "y axis")
.call(yAxis)
// Create bars here:
// var bars =
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment