UNPKG

orgchart

Version:

Simple and direct organization chart(tree-like hierarchy) library based on pure DOM.

73 lines (70 loc) 2.71 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>OrgChart Center on Selected Node Demo</title> <link rel="icon" href="img/logo.png"> <link rel="stylesheet" href="css/orgchart.css"> <link rel="stylesheet" href="css/style.css"> <style type="text/css"> .orgchart { background: white; } </style> </head> <body> <section class="demo-notes"> <h1>Center on Selected Node Demo</h1> <p>This demo keeps the selected node centered in the viewport so large charts are easier to navigate.</p> </section> <div id="chart-container"></div> <script type="text/javascript" src="js/orgchart.js"></script> <script type="text/javascript"> document.addEventListener('DOMContentLoaded', () => { const datasource = { 'name': 'Lao Lao', 'title': 'general manager', 'children': [ { 'name': 'Bo Miao', 'title': 'department manager' }, { 'name': 'Su Miao', 'title': 'department manager', 'children': [ { 'name': 'Tie Hua', 'title': 'senior engineer' }, { 'name': 'Hei Hei', 'title': 'senior engineer', 'children': [ { 'name': 'Pang Pang', 'title': 'engineer' }, { 'name': 'Dan Zai', 'title': 'UE engineer', 'children': [ { 'name': 'Er Dan Zai', 'title': 'Intern' } ] } ] } ] }, { 'name': 'Hong Miao', 'title': 'department manager' }, { 'name': 'Chun Miao', 'title': 'department manager' } ] }; const oc = new OrgChart({ chartContainer: '#chart-container', 'visibleLevel': 2, 'pan': true, 'data' : datasource, 'nodeContent': 'title', 'createNode': function(node) { node.addEventListener('click', (event) => { if (!event.target.closest('.edge, .toggleBtn')) { const chart = node.closest('.orgchart'); const chartRect = chart.getBoundingClientRect(); const nodeRect = node.getBoundingClientRect(); const newX = window.parseInt((chartRect.width / 2) - (nodeRect.left - chartRect.left) - (nodeRect.width / 2)); const newY = window.parseInt((chartRect.height / 2) - (nodeRect.top - chartRect.top) - (nodeRect.height / 2)); chart.style.transform = 'matrix(1, 0, 0, 1, ' + newX + ', ' + newY + ')'; } }); } }); }); </script> </body> </html>