This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
fix(forcedirected.js): make force directed graph zoomable and pannable #393
Open
IndexOutOfRange
wants to merge
2
commits into
apache-superset:master
Choose a base branch
from
IndexOutOfRange:force_graph_zoomable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,18 @@ const propTypes = { | |
/* Modified from http://bl.ocks.org/d3noob/5141278 */ | ||
function ForceDirected(element, props) { | ||
const { data, width, height, linkLength = 200, charge = -500 } = props; | ||
|
||
let w = window.innerWidth; | ||
let h = window.innerHeight; | ||
const minZoom = 0.1; | ||
const maxZoom = 7; | ||
const div = d3.select(element); | ||
const zoom = d3.behavior.zoom().scaleExtent([minZoom, maxZoom]); | ||
div.classed('superset-legacy-chart-force-directed', true); | ||
|
||
const links = data; | ||
const nodes = {}; | ||
|
||
// Compute the distinct nodes from the links. | ||
links.forEach(link => { | ||
link.source = | ||
|
@@ -82,6 +89,9 @@ function ForceDirected(element, props) { | |
nodes[targetName].total += link.value; | ||
}); | ||
|
||
div.selectAll('*').remove(); | ||
const svg = div.append('svg'); | ||
const g = svg.append('g'); | ||
/* eslint-disable no-use-before-define */ | ||
// add the curvy lines | ||
function tick() { | ||
|
@@ -107,15 +117,22 @@ function ForceDirected(element, props) { | |
.on('tick', tick) | ||
.start(); | ||
|
||
div.selectAll('*').remove(); | ||
const svg = div | ||
.append('svg') | ||
.attr('width', width) | ||
.attr('height', height); | ||
function resize() { | ||
const { innerWidth, innerHeight } = window; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
svg.attr('width', innerWidth).attr('height', innerHeight); | ||
|
||
force | ||
.size([ | ||
force.size()[0] + (innerWidth - w) / zoom.scale(), | ||
force.size()[1] + (innerHeight - h) / zoom.scale(), | ||
]) | ||
.resume(); | ||
w = innerWidth; | ||
h = innerHeight; | ||
} | ||
|
||
// build the arrow. | ||
svg | ||
.append('svg:defs') | ||
g.append('svg:defs') | ||
.selectAll('marker') | ||
.data(['end']) // Different link/path types can be defined here | ||
.enter() | ||
|
@@ -132,7 +149,7 @@ function ForceDirected(element, props) { | |
|
||
const edgeScale = d3.scale.linear().range([0.1, 0.5]); | ||
// add the links and the arrows | ||
const path = svg | ||
const path = g | ||
.append('svg:g') | ||
.selectAll('path') | ||
.data(force.links()) | ||
|
@@ -143,7 +160,7 @@ function ForceDirected(element, props) { | |
.attr('marker-end', 'url(#end)'); | ||
|
||
// define the nodes | ||
const node = svg | ||
const node = g | ||
.selectAll('.node') | ||
.data(force.nodes()) | ||
.enter() | ||
|
@@ -170,6 +187,9 @@ function ForceDirected(element, props) { | |
.transition() | ||
.style('font-size', 12); | ||
}) | ||
.on('mousedown', function() { | ||
d3.event.stopPropagation(); | ||
}) | ||
.call(force.drag); | ||
|
||
// add the nodes | ||
|
@@ -187,6 +207,11 @@ function ForceDirected(element, props) { | |
.attr('x', 6) | ||
.attr('dy', '.35em') | ||
.text(d => d.name); | ||
zoom.on('zoom', function() { | ||
g.attr('transform', `translate(${d3.event.translate})scale(${d3.event.scale})`); | ||
}); | ||
svg.call(zoom); | ||
resize(); | ||
} | ||
|
||
ForceDirected.displayName = 'ForceDirected'; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The chart does not always have
width
andheight
being 100% of the windowwidth
andheight
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the input. I commited a version where width/height are inherited.