Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break D3 draw function into multiple functions #139

Merged
merged 1 commit into from
Apr 2, 2020
Merged
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
121 changes: 67 additions & 54 deletions src/components/flowchart/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,15 @@ import { curveBasis, line } from 'd3-shape';
import icon from './icon';

/**
* Render chart to the DOM with D3
* Render node icons and name labels
*/
const draw = function() {
const { nodes, edges, centralNode, linkedNodes, textLabels } = this.props;

// Create selections
this.el.edges = this.el.edgeGroup
.selectAll('.edge')
.data(edges, edge => edge.id);
const drawNodes = function() {
const { nodes, centralNode, linkedNodes, textLabels } = this.props;

this.el.nodes = this.el.nodeGroup
.selectAll('.node')
.data(nodes, node => node.id);

// Set up line shape function
const lineShape = line()
.x(d => d.x)
.y(d => d.y)
.curve(curveBasis);

// Create edges
const enterEdges = this.el.edges
.enter()
.append('g')
.attr('class', 'edge')
.attr('opacity', 0);

enterEdges.append('path').attr('marker-end', d => `url(#arrowhead)`);

this.el.edges
.exit()
.transition('exit-edges')
.duration(this.DURATION)
.attr('opacity', 0)
.remove();

this.el.edges = this.el.edges.merge(enterEdges);

this.el.edges
.attr('data-id', edge => edge.id)
.classed(
'edge--faded',
({ source, target }) =>
centralNode && (!linkedNodes[source] || !linkedNodes[target])
)
.transition('show-edges')
.duration(this.DURATION)
.attr('opacity', 1);

this.el.edges
.select('path')
.transition('update-edges')
.duration(this.DURATION)
.attrTween('d', function(edge) {
const current = edge.points && lineShape(edge.points);
const previous = select(this).attr('d') || current;
return interpolatePath(previous, current);
});

// Create nodes
const enterNodes = this.el.nodes
.enter()
.append('g')
Expand Down Expand Up @@ -141,4 +90,68 @@ const draw = function() {
.attr('y', node => node.iconSize / -2);
};

/**
* Render edge lines
*/
const drawEdges = function() {
const { edges, centralNode, linkedNodes } = this.props;

this.el.edges = this.el.edgeGroup
.selectAll('.edge')
.data(edges, edge => edge.id);

// Set up line shape function
const lineShape = line()
.x(d => d.x)
.y(d => d.y)
.curve(curveBasis);

// Create edges
const enterEdges = this.el.edges
.enter()
.append('g')
.attr('class', 'edge')
.attr('opacity', 0);

enterEdges.append('path').attr('marker-end', d => `url(#arrowhead)`);

this.el.edges
.exit()
.transition('exit-edges')
.duration(this.DURATION)
.attr('opacity', 0)
.remove();

this.el.edges = this.el.edges.merge(enterEdges);

this.el.edges
.attr('data-id', edge => edge.id)
.classed(
'edge--faded',
({ source, target }) =>
centralNode && (!linkedNodes[source] || !linkedNodes[target])
)
.transition('show-edges')
.duration(this.DURATION)
.attr('opacity', 1);

this.el.edges
.select('path')
.transition('update-edges')
.duration(this.DURATION)
.attrTween('d', function(edge) {
const current = edge.points && lineShape(edge.points);
const previous = select(this).attr('d') || current;
return interpolatePath(previous, current);
});
};

/**
* Render chart to the DOM with D3
*/
const draw = function() {
drawEdges.call(this);
drawNodes.call(this);
};

export default draw;