D3.js transpose() Function

The transpose() function in D3.js is used to return the transpose of a 2D array. This function helps to visualize the data and making graphs and charts.
Syntax:
d3.transpose(Matrix);
Parameters: This function accept a single parameter as mentioned above and described below:
- Matrix: This parameter holds a matrix as a 2d array
Return value: It returns an array or the matrix.
Below given are a few examples of transpose function.
Example 1: Without using zip function to form a matrix.
HTML
<!DOCTYPE html> <html lang="en"> <head> Â Â <meta charset="UTF-8"> Â Â <meta name="viewport"Â Â Â Â Â Â Â Â Â content="width=device-width, initial-scale=1.0"> Â Â <title>D3.js transpose() Function</title> </head> <body> Â Â <!--Fetching from CDN of D3.js-->Â Â <script type = "text/javascript"Â Â Â </script> Â Â <script> Â Â Â Â console.log(d3.transpose([[1,2,3],[4,5,6]])) Â Â </script> </body> </html> |
Output:
Example 2: Using zip function to form a matrix.
HTML
<!DOCTYPE html> <html lang="en"> <head> Â Â <meta charset="UTF-8"> Â Â <meta name="viewport"Â Â Â Â Â Â Â Â Â content="width=device-width, initial-scale=1.0"> Â Â <title>D3 transpose() Function</title> </head> <body> Â Â Â Â Â Â <!--Fetching from CDN of D3.js-->Â Â <script type = "text/javascript"Â Â Â </script> Â Â <script> Â Â Â Â console.log("Making matrix using zip function: ") Â Â Â Â let m=d3.zip(["a","b","s"],["c","d","h"],["e","f","g"]); Â Â Â Â console.log("Original Matrix: ",m) Â Â Â Â console.log("Transpose matrix: ",d3.transpose(m)) Â Â </script> </body> </html> |
Output:
Supported Browsers:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Safari
- Opera
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!




