Built with blockbuilder.org
Last active
January 11, 2018 13:12
-
-
Save mukhtyar/182862bdc1cce6f3c19cf5bc657c209c to your computer and use it in GitHub Desktop.
D3 - Data Binding 2
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
| <!DOCTYPE html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <style> | |
| body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
| #chart { | |
| margin: 1em 3em; | |
| } | |
| .note { | |
| margin-top: 1em; | |
| font-size: 1.2em; | |
| background-color: #D8D8D8; | |
| } | |
| .bar { | |
| background-color: blue; | |
| height: 20px; | |
| margin-top: 2px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="note"> | |
| <p>You need 4 properties to create a SVG rect: x, y, width, height</p> | |
| <p>More at <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/rect">MDN Docs</a></p> | |
| </div> | |
| <svg id="chart"> | |
| <rect/> | |
| <rect/> | |
| <rect/> | |
| <rect/> | |
| <rect/> | |
| </svg> | |
| <script> | |
| var rectWidth = 50; | |
| var chartHeight = 400; | |
| var chartWidth = 700; | |
| var data = [200, 60, 100, 80, 10]; | |
| var svg = d3.select('#chart') | |
| .attr('height', chartHeight) | |
| .attr('width', chartWidth); | |
| svg.selectAll('rect') | |
| .data(data) | |
| .attr('x', function(d, i){ | |
| return i * rectWidth; | |
| }) | |
| .attr('y', function(d){ | |
| return chartHeight - d; | |
| }) | |
| .attr('width', rectWidth) | |
| .attr('height', function(d){ | |
| return d; | |
| }) | |
| .attr('fill', 'blue') | |
| .attr('stroke', '#FF00FF') | |
| .attr('stroke-width', '2'); | |
| </script> | |
| </body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment