├── LICENSE ├── README.md ├── createDummyData.sql ├── getPositions.php └── index.htm /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Bernhard Zuba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # D3.js Organizational chart 2 | 3 | ## Demo 4 | Want to see this in action? Visit: https://blog.zubasoft.at/Examples/D3.js/OrgChart/index.htm 5 | 6 | ## Description 7 | Goal of this project was to develop a simple org chart with the [D3 library](https://d3js.org/). You can use this as a starting point in your own projects (Licensed under MIT). You can also contribute this project and file a bug or make a pull request. 8 | 9 | The Organizational chart comes with two layouts. A more modern variant: 10 | ![Modern style](https://blog.zubasoft.at/wp-content/uploads/2014/09/organigramm_modern1.png) 11 | 12 | And the classical view: 13 | ![Classic style](https://blog.zubasoft.at/wp-content/uploads/2014/09/organigramm_klassisch1.png) 14 | -------------------------------------------------------------------------------- /createDummyData.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `position` ( 2 | `id` int(11) NOT NULL, 3 | `parent_id` int(11) NOT NULL, 4 | `desc` varchar(50) NOT NULL 5 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ; 6 | 7 | INSERT INTO `position` (`id`, `parent_id`, `desc`) VALUES 8 | (1, 0, 'Executive office'), 9 | (2, 1, 'Sales'), 10 | (3, 1, 'Marketing'), 11 | (4, 1, 'Development'), 12 | (8, 2, 'Salesman 1'), 13 | (9, 2, 'Salesman 2'), 14 | (10, 3, 'Person M1'), 15 | (11, 4, 'Team 1'), 16 | (12, 4, 'Team 2'), 17 | (13, 4, 'Team 3'), 18 | (14, 11, 'Developer Team1 1'), 19 | (15, 11, 'Developer Team1 2'), 20 | (16, 12, 'Developer Team2 1'), 21 | (17, 12, 'Developer Team2 2'), 22 | (18, 12, 'Developer Team2 3'), 23 | (19, 12, 'Developer Team2 4'), 24 | (20, 13, 'Developer Team3 1'), 25 | (21, 13, 'Developer Team3 2'), 26 | (22, 13, 'Developer Team3 3'); 27 | 28 | ALTER TABLE `position` 29 | ADD PRIMARY KEY (`id`); 30 | 31 | ALTER TABLE `position` 32 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=23; 33 | -------------------------------------------------------------------------------- /getPositions.php: -------------------------------------------------------------------------------- 1 | function db() { 2 | // TODO: Replace these variables 3 | $dsn = 'mysql:host=localhost;dbname=ORGANIGRAMM'; 4 | $username = 'ORGANIGRAMM_USER'; 5 | $password = 'PLEASE_FILL_ME_IN'; 6 | $options = array( 7 | PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', 8 | ); 9 | return new PDO($dsn, $username, $password, $options); 10 | } 11 | 12 | function getChilds($dbconn, $id) { 13 | $stmt = $dbconn->prepare("SELECT `id`, `desc` FROM `position` WHERE `parent_id`=?"); 14 | $stmt->execute(array($id)); 15 | return $stmt; 16 | } 17 | 18 | $dbconn = db(); 19 | 20 | $id = filter_input(INPUT_GET,"id", FILTER_VALIDATE_INT); 21 | $stmt = getChilds($dbconn, $id); 22 | 23 | if($id == 0) { 24 | // Initial load: Get the first children too 25 | $row = $stmt->fetch(PDO::FETCH_ASSOC); 26 | 27 | $stmt2 = getChilds($dbconn, $row['id']); 28 | $i_j = 0; 29 | $childs = array(); 30 | while($childrow = $stmt2->fetch(PDO::FETCH_ASSOC)) { 31 | $stmt3 = getChilds($dbconn, $childrow['id']); 32 | $hasChildRow = $stmt3->rowCount(); 33 | if($hasChildRow > 0) { 34 | $hasChild = true; 35 | } else { 36 | $hasChild = false; 37 | } 38 | 39 | $childs[$i_j] = array("id" => $childrow['id'], "desc" => $childrow['desc'], "hasChild" => $hasChild); 40 | $i_j++; 41 | } 42 | 43 | echo json_encode(array("id" => $row['id'], "desc" => $row['desc'], "children" => $childs)); 44 | } else { 45 | // Just check if there are children 46 | $i_i = 0; 47 | $childs = array(); 48 | 49 | while($childrow = $stmt->fetch(PDO::FETCH_ASSOC)) { 50 | $stmt3 = getChilds($dbconn, $childrow['id']); 51 | $hasChildRow = $stmt3->rowCount(); 52 | if($hasChildRow > 0) { 53 | $hasChild = true; 54 | } else { 55 | $hasChild = false; 56 | } 57 | 58 | $childs[$i_i] = array("id" => $childrow['id'], "desc" => $childrow['desc'], "hasChild" => $hasChild); 59 | $i_i++; 60 | } 61 | 62 | echo json_encode(array("result" => $childs)); 63 | } -------------------------------------------------------------------------------- /index.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 34 | 35 | 36 | 400 | 401 | 402 | 403 | 404 |
405 | 406 | 407 | 408 | 409 | 425 | 426 | 427 | --------------------------------------------------------------------------------