chord 0.1.6

Rust wrapper around d3-chord
Documentation

Example Image

Introduction

In a chord diagram (or radial network), entities are arranged radially as segments with their relationships visualised by arcs that connect them. The size of the segments illustrates the numerical proportions, whilst the size of the arc illustrates the significance of the relationships1.

Chord diagrams are useful when trying to convey relationships between different entities, and they can be beautiful and eye-catching.

The Chord Crate

I wasn't able to find any Rust crates for plotting chord diagrams, so I ported my own from Python to Rust.

You can get the package either from crates.io or from the GitHub repository. With your processed data, you should be able to plot something beautiful with just a single line, Chord{ matrix : matrix, names : names, .. Chord::default() }.show()

The primary support is for Jupyter Lab (not the older Jupyter Notebook).

Installation

Available on crates.io.

:dep chord = {Version = "0.1.1"}
use chord::{Chord, Plot};

Examples

You can see the actual interactive examples on this page. The below examples are screenshots.

The Dataset

The focus for this section will be the demonstration of the chord package. To keep it simple, we will use synthetic data that illustrates the co-occurrences between movie genres within the same movie.

let matrix: Vec<Vec<f64>> = vec![
    vec![0., 5., 6., 4., 7., 4.],
    vec![5., 0., 5., 4., 6., 5.],
    vec![6., 5., 0., 4., 5., 5.],
    vec![4., 4., 4., 0., 5., 5.],
    vec![7., 6., 5., 5., 0., 4.],
    vec![4., 5., 5., 5., 4., 0.],
];

let names: Vec<String> = vec![
    "Action",
    "Adventure",
    "Comedy",
    "Drama",
    "Fantasy",
    "Thriller",
]
.into_iter()
.map(String::from)
.collect();

Default Settings

Let's see what the Chord defaults produce when we invoke the show() method.

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    ..Chord::default()
}
.show();

Example Image

You can also save it to a HTML file.

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    ..Chord::default()
}
.to_html();

Different Colours

The defaults are nice, but what if we want different colours? You can pass in almost anything from d3-scale-chromatic, or you could pass in a list of hexadecimal colour codes.

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    colors: "d3.schemeSet2".to_string(),
    ..Chord::default()
}
.show();

Example Image

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    colors: format!("d3.schemeGnBu[{:?}]",names.len()).to_string(),
    ..Chord::default()
}
.show();

Example Image

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    colors: "d3.schemeSet3".to_string(),
    ..Chord::default()
}
.show();

Example Image

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    colors: format!("d3.schemePuRd[{:?}]",names.len()).to_string(),
    ..Chord::default()
}
.show();

Example Image

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    colors: format!("d3.schemeYlGnBu[{:?}]",names.len()).to_string(),
    ..Chord::default()
}
.show();

Example Image

let hex_colours : Vec<String> = vec!["#222222", "#333333", "#4c4c4c", "#666666", "#848484", "#9a9a9a"].into_iter()
.map(String::from)
.collect();

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    colors: format!("{:?}",hex_colours),
    ..Chord::default()
}
.show();

Example Image

Label Styling

We can disable the wrapped labels, and even change the colour.

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    wrap_labels: false,
    label_color:"#4c40bf".to_string(),
    ..Chord::default()
}
.show();

Example Image

Opacity

We can also change the default opacity of the relationships.

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    opacity: 0.1,
    ..Chord::default()
}
.show();

Example Image

Diagram Size

We can change the maximum diagram size by specifying a width.

Chord {
    matrix: matrix.clone(),
    names: names.clone(),
    width: 400.0,
    ..Chord::default()
}
.show()

Credits

  • d3-chord, Mike Bostock.
  • d3-chord gradient fills, Nadieh Bremer.
  • chord (Python), Shahin Rostami.
  • chord (Rust), Shahin Rostami.

Example Image