Built with blockbuilder.org
Created
April 3, 2018 23:12
-
-
Save GerardoFurtado/40c4a36d48e6dfaa30f0325f2973098f to your computer and use it in GitHub Desktop.
fresh block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Market | Food | Deliciousness | |
|---|---|---|---|
| A | Apples | 9 | |
| A | Green Beans | 5 | |
| A | Bananas | 5 | |
| B | Apples | 4 | |
| B | Green Beans | 8 | |
| B | Bananas | 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <meta charset="utf-8"> | |
| <title>D3: Loading data from a CSV file</title> | |
| </head> | |
| <body> | |
| <div class="chart-container"> | |
| <form class="controls"> | |
| Scale: | |
| <label> | |
| <label><input type="radio" name="market" value="A" checked> | |
| A</label> | |
| <label><input type="radio" name="market" value="B"> B</label> | |
| </label> | |
| </form> | |
| <svg class="chart js-chart"></svg> | |
| </div> | |
| <script type="text/javascript"> | |
| var margin = { | |
| top: 20, | |
| right: 20, | |
| bottom: 30, | |
| left: 40 | |
| }, | |
| w = 600 - margin.left - margin.right, | |
| h = 300 - margin.top - margin.bottom; | |
| var svg = d3.select("body") | |
| .append("svg") | |
| .attr("width", w + margin.left + margin.right) | |
| .attr("height", h + margin.top + margin.bottom) | |
| .append("g") | |
| .attr("transform", "translate(" + margin.left + "," + | |
| margin.top + ")"); | |
| var xScale = d3.scaleBand() | |
| .range([0, w]) | |
| .paddingInner(0.2); | |
| var yScale = d3.scaleLinear() | |
| .rangeRound([h, 0]); | |
| d3.csv('food.csv', function(error, data) { | |
| if (error) throw error; | |
| // format the data | |
| data.forEach(function(d) { | |
| d.Deliciousness = +d.Deliciousness | |
| }); | |
| redraw(data.filter(d => (d.Market == "A"))) | |
| //radio button | |
| d3.selectAll(("input[name='market']")).on("change", function() { | |
| var data_new = data.filter(d => (d.Market == this.value)); | |
| redraw(data_new); | |
| }); | |
| function redraw(data) { | |
| xScale.domain(data.map(function(d) { | |
| return d.Food; | |
| })); | |
| var xAxis = d3.axisBottom() | |
| .scale(xScale) | |
| .ticks(5); | |
| yScale.domain([0, d3.max(data, d => d.Deliciousness)]); | |
| var yAxis = d3.axisLeft() | |
| .scale(yScale) | |
| .ticks(5); | |
| var rect = svg.selectAll('rect') | |
| .data(data); | |
| var rectEnter = rect.enter() | |
| .append('rect') | |
| .merge(rect) | |
| .attr('x', (d) => xScale(d.Food)) | |
| .attr('y', d => yScale(d.Deliciousness)) | |
| .attr('width', xScale.bandwidth()) | |
| .attr('height', d => h - yScale(d.Deliciousness)) | |
| .attr('fill', "blue"); | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment