-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd706eb
commit 52b86d8
Showing
8 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.chord svg #circle circle { | ||
fill: none; | ||
pointer-events: all; | ||
} | ||
|
||
.chord svg .group path { | ||
fill-opacity: .6; | ||
} | ||
|
||
.chord svg path.chord { | ||
stroke: #000; | ||
stroke-width: .25px; | ||
} | ||
|
||
.chord svg #circle:hover path.fade { | ||
opacity: 0.2; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import d3 from 'd3'; | ||
import { category21 } from '../javascripts/modules/colors'; | ||
import './chord.css'; | ||
|
||
function chordViz(slice, json) { | ||
slice.container.html(''); | ||
|
||
const div = d3.select(slice.selector); | ||
const nodes = json.data.nodes; | ||
const fd = slice.formData; | ||
const f = d3.format(fd.y_axis_format); | ||
|
||
const width = slice.width(); | ||
const height = slice.height(); | ||
|
||
const outerRadius = Math.min(width, height) / 2 - 10; | ||
const innerRadius = outerRadius - 24; | ||
|
||
let chord; | ||
|
||
const arc = d3.svg.arc() | ||
.innerRadius(innerRadius) | ||
.outerRadius(outerRadius); | ||
|
||
const layout = d3.layout.chord() | ||
.padding(0.04) | ||
.sortSubgroups(d3.descending) | ||
.sortChords(d3.descending); | ||
|
||
const path = d3.svg.chord() | ||
.radius(innerRadius); | ||
|
||
const svg = div.append('svg') | ||
.attr('width', width) | ||
.attr('height', height) | ||
.on('mouseout', () => chord.classed('fade', false)) | ||
.append('g') | ||
.attr('id', 'circle') | ||
.attr('transform', `translate(${width / 2}, ${height / 2})`); | ||
|
||
svg.append('circle') | ||
.attr('r', outerRadius); | ||
|
||
// Compute the chord layout. | ||
layout.matrix(json.data.matrix); | ||
|
||
const group = svg.selectAll('.group') | ||
.data(layout.groups) | ||
.enter().append('g') | ||
.attr('class', 'group') | ||
.on('mouseover', (d, i) => { | ||
chord.classed('fade', p => p.source.index !== i && p.target.index !== i); | ||
}); | ||
|
||
// Add a mouseover title. | ||
group.append('title').text((d, i) => `${nodes[i]}: ${f(d.value)}`); | ||
|
||
// Add the group arc. | ||
const groupPath = group.append('path') | ||
.attr('id', (d, i) => 'group' + i) | ||
.attr('d', arc) | ||
.style('fill', (d, i) => category21(nodes[i])); | ||
|
||
// Add a text label. | ||
const groupText = group.append('text') | ||
.attr('x', 6) | ||
.attr('dy', 15); | ||
|
||
groupText.append('textPath') | ||
.attr('xlink:href', (d, i) => `#group${i}`) | ||
.text((d, i) => nodes[i]); | ||
// Remove the labels that don't fit. :( | ||
groupText.filter(function (d, i) { | ||
return groupPath[0][i].getTotalLength() / 2 - 16 < this.getComputedTextLength(); | ||
}) | ||
.remove(); | ||
|
||
// Add the chords. | ||
chord = svg.selectAll('.chord') | ||
.data(layout.chords) | ||
.enter().append('path') | ||
.attr('class', 'chord') | ||
.on('mouseover', (d) => { | ||
chord.classed('fade', p => p !== d); | ||
}) | ||
.style('fill', d => category21(nodes[d.source.index])) | ||
.attr('d', path); | ||
|
||
// Add an elaborate mouseover title for each chord. | ||
chord.append('title').text(function (d) { | ||
return nodes[d.source.index] | ||
+ ' → ' + nodes[d.target.index] | ||
+ ': ' + f(d.source.value) | ||
+ '\n' + nodes[d.target.index] | ||
+ ' → ' + nodes[d.source.index] | ||
+ ': ' + f(d.target.value); | ||
}); | ||
} | ||
|
||
module.exports = chordViz; |
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
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