Creating a BalkanGraph OrgChart with Spfx

An explanation of how to create a BalkanGraph OrgChart with Spfx (Including Export Buttons)

Open a command prompt. Create a directory for SPFx solution.
md spfx-React-BalkanGraphChart
 
Navigate to the above-created directory.
cd spfx-React-BalkanGraphChart
 
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
 
Solution Name
Hit Enter for the default name (spfx-pnp-DropDown in this case,) or type in any other name for your solution.
Selected Choice - Hit Enter
 
Target for the Component
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected Choice - SharePoint Online only (latest)
 
File Location
We may choose to use the same folder or create a subfolder for our solution.
Selected Choice - Same folder
 
Deployment Option
Selecting Y will allow the app to be deployed instantly to all sites and accessible everywhere.
Selected Choice: N (install on each site explicitly)
 
Permissions to access Web APIs
Choose if the components in the solution require permissions to access web APIs which are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)
 
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected Choice - web part
 
Web part name
Hit Enter to select the default name or type in any other name.
Selected Choice - BalkanOrgChart
 
Web part description
Hit Enter to select the default description or type in any other value.
 
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected Choice - React
 
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
npm shrinkwrap
 
In the command prompt, type below command to open the solution in the code editor of your choice.
code .
In BalkanOrgChart.tsx
  1. import * as React from 'react';
  2. import { IBalkanOrgChartProps } from './IBalkanOrgChartProps';
  3. declare var OrgChart:any;
  4. var chart:any;
  5. export default class BalkanOrgChart extends React.Component<IBalkanOrgChartProps> {
  6. public componentDidMount () {
  7. var my_script = this.new_script('https://balkangraph.com/js/latest/OrgChart.js');
  8. my_script.then(()=> {
  9. chart = new OrgChart(document.getElementById("OrgChart"), {
  10. template: "olivia",
  11. layout: OrgChart.mixed,
  12. mouseScrool: OrgChart.none,
  13. menu: {
  14. pdfPreview: {
  15. text: "PDF Preview",
  16. icon: OrgChart.icon.pdf(24,24, '#7A7A7A'),
  17. onClick: preview
  18. },
  19. pdf: { text: "Export PDF" },
  20. png: { text: "Export PNG" },
  21. svg: { text: "Export SVG" },
  22. csv: { text: "Export CSV" }
  23. },
  24. nodeMenu: {
  25. pdf: { text: "Export PDF" },
  26. png: { text: "Export PNG" },
  27. svg: { text: "Export SVG" }
  28. },
  29. nodeBinding: {
  30. field_0: "name",
  31. field_1: "title",
  32. img_0: "img"
  33. },
  34. nodes: [
  35. { id: "1", name: "Jack Hill", title: "Chairman and CEO", email: "amber@domain.com", img: "https://balkangraph.com/js/img/1.jpg" },
  36. { id: "2", pid: "1", name: "Lexie Cole", title: "QA Lead", email: "ava@domain.com", img: "https://balkangraph.com/js/img/2.jpg" },
  37. { id: "3", pid: "1", name: "Janae Barrett", title: "Technical Director", img: "https://balkangraph.com/js/img/3.jpg" },
  38. { id: "4", pid: "1", name: "Aaliyah Webb", title: "Manager", email: "jay@domain.com", img: "https://balkangraph.com/js/img/4.jpg" },
  39. { id: "5", pid: "2", name: "Elliot Ross", title: "QA", img: "https://balkangraph.com/js/img/5.jpg" },
  40. { id: "6", pid: "2", name: "Anahi Gordon", title: "QA", img: "https://balkangraph.com/js/img/6.jpg" },
  41. { id: "7", pid: "2", name: "Knox Macias", title: "QA", img: "https://balkangraph.com/js/img/7.jpg" },
  42. { id: "8", pid: "3", name: "Nash Ingram", title: ".NET Team Lead", email: "kohen@domain.com", img: "https://balkangraph.com/js/img/8.jpg" },
  43. { id: "9", pid: "3", name: "Sage Barnett", title: "JS Team Lead", img: "https://balkangraph.com/js/img/9.jpg" },
  44. { id: "10", pid: "8", name: "Alice Gray", title: "Programmer", img: "https://balkangraph.com/js/img/10.jpg" },
  45. { id: "11", pid: "8", name: "Anne Ewing", title: "Programmer", img: "https://balkangraph.com/js/img/11.jpg" },
  46. { id: "12", pid: "9", name: "Reuben Mcleod", title: "Programmer", img: "https://balkangraph.com/js/img/12.jpg" },
  47. { id: "13", pid: "9", name: "Ariel Wiley", title: "Programmer", img: "https://balkangraph.com/js/img/13.jpg" },
  48. { id: "14", pid: "4", name: "Lucas West", title: "Marketer", img: "https://balkangraph.com/js/img/14.jpg" },
  49. { id: "15", pid: "4", name: "Adan Travis", title: "Designer", img: "https://balkangraph.com/js/img/15.jpg" },
  50. { id: "16", pid: "4", name: "Alex Snider", title: "Sales Manager", img: "https://balkangraph.com/js/img/16.jpg" }
  51. ]
  52. });
  53. }).catch(()=> {
  54. });
  55. }
  56. private new_script(src:any) {
  57. return new Promise((resolve, reject)=>{
  58. var script = document.createElement('script');
  59. script.src = src;
  60. script.addEventListener('load', ()=> {
  61. resolve();
  62. });
  63. script.addEventListener('error', (e)=> {
  64. reject(e);
  65. });
  66. document.body.appendChild(script);
  67. });
  68. }
  69. public render(): React.ReactElement<IBalkanOrgChartProps> {
  70. return (
  71. <div id='OrgChart'>
  72. </div>
  73. );
  74. }
  75. }
  76. const preview = () => {
  77. return OrgChart.pdfPrevUI.show(chart, {
  78. format: 'A4'
  79. });
  80. };
  81. /*var preview= function(){
  82. OrgChart.pdfPrevUI.show(chart, {
  83. format: 'A4'
  84. });
  85. }*/
System requirements
 
OrgChart JS will run on any server that supports HTML. You can even run OrgChart.js locally from a filesystem.
 
OrgChart JS does not depend on any third-party JavaScript library.
 
OrgChart JS works in all modern browsers and uses SVG for the graphics rendering.
 
Where...
 
nodes is the data source. The 'id' property is mandatory.
pid is the parent id, this represents a connection between two nodes.
nodeBinding is the 'name' property form, the data source will be bound to 'field_0' UI element from the template.
Sample Output
 
 
 
For Details Visit.
Happy Coding :)