Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions d3-graph/src/App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logo from './logo.svg';
import './App.css';
import { client, useConfig, useElementData } from '@sigmacomputing/plugin';
import * as d3 from 'd3';
import {client, useConfig, useElementData} from '@sigmacomputing/plugin';
import { useEffect, useRef } from 'react';
import './App.css';

client.config.configureEditorPanel([
{ name: "vertices", type: "element" },
Expand All @@ -16,54 +15,54 @@ client.config.configureEditorPanel([
const width = 600;
const height = 600;
function renderGraph(data, ref) {
if (!ref.current) return null;
if (!ref.current) return 'null';
d3.selectAll('svg > *').remove();
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));

const svg = d3.select(ref.current)
.attr("width", "100vw")
.attr("height", "100vh")
.attr("preserveAspectRatio", "xMidYMid meet")
.attr("viewBox", [0, 0, width, height]);
.attr("width", "100vw")
.attr("height", "100vh")
.attr("preserveAspectRatio", "xMidYMid meet")
.attr("viewBox", [0, 0, width, height]);

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
.attr("stroke-width", d => Math.sqrt(d.value));

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", color)
.call(drag(simulation));
.attr("r", 5)
.attr("fill", color)
.call(drag(simulation));

node.append("title")
.text(d => d.id);
.text(d => d.id);
node.append("text").text(d => d.id);

simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});

return simulation.stop;
Expand All @@ -75,32 +74,32 @@ const color = () => {
}

const drag = simulation => {

function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}

function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}

function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}

return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}

function transform(config, vertices, edges) {
const {id, to, from, value} = config;
const { id, to, from, value } = config;
console.log(arguments);
if (!id || !to || !from || !value || !vertices?.[id]?.length || !edges?.[to]?.length || !edges[value]?.length) return { nodes: [], links: [] };
const transformed = {
Expand Down
59 changes: 29 additions & 30 deletions orgchart/src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { client, useConfig, useElementColumns, useElementData } from '@sigmacomputing/plugin';
import * as d3 from 'd3';
import { OrgChart } from 'd3-org-chart';
import { useEffect } from 'react';
import './App.css';
import default_face from "./default.png";
import root_face from "./sigma.jpg";
import {client, useConfig, useElementData, useElementColumns} from '@sigmacomputing/plugin';
import { useEffect, useRef, useMemo } from 'react';
import { OrgChart } from 'd3-org-chart';
import * as d3 from 'd3';

// d.data.positionName
// d.data.area
Expand All @@ -26,25 +26,24 @@ client.config.configureEditorPanel([
function graph(data) {
d3.selectAll('.container > *').remove();
new OrgChart()
.container('.container')
.data(data)
.nodeHeight((d) => 85 + 25)
.nodeWidth((d) => 220 + 2)
.childrenMargin(d => 50)
.compactMarginBetween(d => 35)
.compactMarginPair(d => 30)
.neightbourMargin((a, b) => 20)
.buttonContent(({ node, state }) => {
return `<div style="border-radius:3px;padding:3px;font-size:10px;margin:auto auto;background-color:lightgray"> <span style="font-size:9px">${
node.children
? `<i class="fas fa-chevron-up"></i>`
: `<i class="fas fa-chevron-down"></i>`
.container('.container')
.data(data)
.nodeHeight((d) => 85 + 25)
.nodeWidth((d) => 220 + 2)
.childrenMargin(d => 50)
.compactMarginBetween(d => 35)
.compactMarginPair(d => 30)
.neightbourMargin((a, b) => 20)
.buttonContent(({ node, state }) => {
return `<div style="border-radius:3px;padding:3px;font-size:10px;margin:auto auto;background-color:lightgray"> <span style="font-size:9px">${node.children
? `<i class="fas fa-chevron-up"></i>`
: `<i class="fas fa-chevron-down"></i>`
}</span> ${node.data._directSubordinates} </div>`;
})
.nodeContent(function (d, i, arr, state) {
const color = '#FFFFFF';
const imageDiffVert = 25 + 2;
return `
})
.nodeContent(function (d, i, arr, state) {
const color = '#FFFFFF';
const imageDiffVert = 25 + 3;
return `
<div className="parent" style='width:${d.width}px;height:${d.height}px;padding-top:${imageDiffVert - 2}px;padding-left:1px;padding-right:1px;'>

<div className="person-component" style="font-family: 'Inter', sans-serif;background-color:${color}; margin-left:-1px;width:${d.width - 2}px;height:${d.height - imageDiffVert}px;border-radius:10px;border: 1px solid #E4E2E9; display:flex; justify-content:center; align-items:center; flex-direction:column; position: relative">
Expand Down Expand Up @@ -73,8 +72,8 @@ function graph(data) {
</div>
</div>
`;
})
.render();
})
.render();
}
function transform(config, columns, sigmaData) {
const name = config.name;
Expand All @@ -100,12 +99,12 @@ function transform(config, columns, sigmaData) {
"id": sigmaData[id][idx],
"parentId": sigmaData[parentId][idx]
});
if(!sigmaData[parentId][idx]) {
if (!sigmaData[parentId][idx]) {
roots.push(idx);
}
}
if(roots.length > 1) {
for(let j=0; j < roots.length; ++j) {
if (roots.length > 1) {
for (let j = 0; j < roots.length; ++j) {
const idx = roots[j];
data[idx]['parentId'] = "Root";
}
Expand Down Expand Up @@ -142,9 +141,9 @@ function App() {
}, [config, columns, sigmaData]);

return (
<div className="App">
<div className="container"/>
</div>
<div className="App">
<div className="container" />
</div>
);
};

Expand Down
Loading