The sixth of a series of data visualizations on basic coding concepts. A is a matrix of rows(i) and columns (j). Use indexing to select elements of the matrix!
Last active
December 10, 2015 21:09
-
-
Save mtaptich/8f9787aa99fecc58aa77 to your computer and use it in GitHub Desktop.
Indexing
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> | |
| <meta charset="utf-8"> | |
| <style> | |
| body { | |
| width: 1024px; | |
| margin-top: 0; | |
| margin: auto; | |
| font-family: "Lato", "PT Serif", serif; | |
| color: #222222; | |
| padding: 0; | |
| font-weight: 300; | |
| line-height: 33px; | |
| -webkit-font-smoothing: antialiased; | |
| } | |
| text { | |
| pointer-events: none; | |
| } | |
| .tile { | |
| stroke: #000; | |
| stroke-width: 2px; | |
| fill: #ecf0f1; | |
| -webkit-font-smoothing: antialiased; | |
| } | |
| </style> | |
| <body> | |
| <script src="//d3js.org/d3.v3.min.js"></script> | |
| <script> | |
| var width = 960, | |
| height =1850, | |
| translate_speed = 1000; | |
| var svg = d3.select('body').append('svg') | |
| .attr('width', width) | |
| .attr('height', height); | |
| var data = d3.range(0,15).map(function(d){ | |
| return [[d,0], [d,1], [d,2], [d,3], [d,4], [d,5]] | |
| }) | |
| var tiles = svg.selectAll('.tiles') | |
| .data(data).enter().append('g') | |
| .selectAll('.tile') | |
| .data(function(d){ return d}).enter() | |
| tiles.append('rect') | |
| .attr('transform', function(d){ return 'translate('+ ( d[1] * 110 + 150 ) +','+ (d[0] * 110 + 200 ) +')' }) | |
| .attr('width', 100) | |
| .attr('height', 100) | |
| .attr('rx', 3) | |
| .attr('ry', 3) | |
| .attr('class', function(d){ return 'tile_'+d[0]+'_'+d[1]}) | |
| .style('fill', '#ecf0f1') | |
| .on('mouseover', function(d){ | |
| d3.select(this).style('fill', '#eee') | |
| }) | |
| .on('mouseout', function(d){ | |
| d3.select(this).style('fill', '#ecf0f1') | |
| }); | |
| tiles.append('text') | |
| .attr('transform', function(d){ return 'translate('+ ( d[1] * 110 + 150 ) +','+ (d[0] * 110 + 200 ) +')' }) | |
| .attr('x', 50) | |
| .attr('y', 55) | |
| .attr('class', function(d){ return 'tile_'+d[0]+'_'+d[1]}) | |
| .text(function(d){ return d}) | |
| .style('text-anchor', 'middle') | |
| .style('font-size', '24'); | |
| svg.append('text') | |
| .attr('x', width/2) | |
| .attr('y', 55) | |
| .attr('class', 'status') | |
| .text('A is a Matrix.') | |
| .style('text-anchor', 'middle') | |
| .style('font-size', '50') | |
| function bring_to_top(row, col){ | |
| d3.select('.status').text('Call: A['+row+','+col+']') | |
| d3.selectAll('.tile_'+row+'_'+col) | |
| .transition().duration(translate_speed) | |
| .attr('transform', function(){ | |
| var t = d3.select(this).attr('transform').split(','); | |
| return "translate("+ (10)+","+t[1]; | |
| }) | |
| .transition().duration(translate_speed) | |
| .attr('transform', 'translate(10,90)') | |
| .transition().duration(translate_speed) | |
| .attr('transform', 'translate(430,90)') | |
| .transition().duration(translate_speed) | |
| .attr('transform', 'translate(430,80)') | |
| .style('fill', '#fdb03f') | |
| .transition().duration(200) | |
| .attr('transform', 'translate(430,90)') | |
| .style('fill', '#ecf0f1') | |
| .transition().duration(translate_speed) | |
| .attr('transform', 'translate(840,90)') | |
| .transition().duration(translate_speed) | |
| .attr('transform', function(d){ return 'translate('+ ( 840 ) +','+ (d[0] * 110 + 200 ) +')' }) | |
| .transition().duration(translate_speed) | |
| .attr('transform', function(d){ return 'translate('+ ( d[1] * 110 + 150 ) +','+ (d[0] * 110 + 200 ) +')' }) | |
| } | |
| function run(){ | |
| var row = Math.floor(Math.random()*15), | |
| col = Math.floor(Math.random()*6) | |
| bring_to_top(row, col) | |
| } | |
| setInterval(run, 1850) | |
| d3.select(self.frameElement).style("height", (height+20) + "px"); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment