Advertisement
dhshin

vert_bar_fin

Mar 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width">
  6.   <title>JS Bin</title>
  7. </head>
  8. <body>
  9.   <svg height="1000" width="1000">
  10.   </svg>
  11.   <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
  12.   <script>
  13.    
  14.     let students = [
  15.       {"name": "Jane", "value": 80},
  16.       {"name": "John", "value": 90},
  17.       {"name": "Bob", "value": 60}
  18.     ];
  19.    
  20.     let svg = d3.select('svg');
  21.     let maxScore = 100
  22.     let maxHeight = 100
  23.     let barWidth = 30
  24.     let margin = 10
  25.    
  26.     svg.selectAll('rect')
  27.       .data(students)
  28.       .enter()
  29.       .append('rect')
  30.       .attr('width', barWidth)
  31.       .attr('height', d => d.value / maxScore * maxHeight)
  32.       .attr('x', (d, i) => (barWidth + margin) * i)
  33.       .attr('y', function() {
  34.         return maxHeight - d3.select(this).attr('height');
  35.       });
  36.      
  37.    
  38.   </script>
  39. </body>
  40. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement