Built with blockbuilder.org
forked from molliemarie's block: CrimeInChicago_BarPlot_Starter
forked from mscheriger's block: CrimeInChicago_BarPlot_Starter
| license: mit |
Built with blockbuilder.org
forked from molliemarie's block: CrimeInChicago_BarPlot_Starter
forked from mscheriger's block: CrimeInChicago_BarPlot_Starter
| <!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> | |
| <div><h1>Title Y'all</h1></div> | |
| </body> | |
| <script> | |
| //Define variables that do not need data first: | |
| //Define Margins and svg here: | |
| var margin = {top:20, right:30, bottom:150, left:50}; | |
| var width = 600 - margin.left - margin.right | |
| var height = 500 - margin.top - margin.bottom | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width + margin.left + margin.right) | |
| .attr("height", height + margin.top + margin.bottom) | |
| .append("g") | |
| .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
| //Define xscale, yscale, xaxis, and yaxis here: | |
| var xScale = d3.scaleBand() | |
| .padding([.1]) | |
| .rangeRound([0, width]); | |
| var yScale = d3.scaleLinear() | |
| .range([height, 0]) | |
| var xAxis = d3.axisBottom(xScale) | |
| 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: | |
| yScale.domain([0,d3.max(data2018,function(d) { return d.count})]); | |
| xScale.domain(data2018.map(function(d) { return d.violation; })); | |
| // define xAxisGroup and yAxisGroup here: | |
| var xAxisGroup = svg.append("g") | |
| .attr("class", "x axis") | |
| .call(xAxis) | |
| .attr("transform", "translate(0," + height + ")") | |
| .selectAll('text') | |
| .attr('transform', 'rotate(45) translate(8,1)') | |
| .style('text-anchor', 'start'); | |
| var yAxisGroup = svg.append("g") | |
| .attr("class", "y axis") | |
| .call(yAxis); | |
| // Create bars here: | |
| svg.selectAll(".bar") | |
| .data(data) | |
| .enter().append("rect") | |
| .attr("class", "bar") | |
| .attr("x", function(d) { return xScale(d.violation); }) | |
| .attr("y", function(d) { return yScale(d.count); }) | |
| .attr("width", xScale.bandwidth()) | |
| .attr("height", function(d) { return height - yScale(d.count); }) | |
| .style("fill", "red") //assign a color of red. | |
| // var bars = | |
| } | |
| </script> |