Published
Edited
Jun 28, 2022
2 forks
2 stars
Insert cell
# Sankey diagram of citation flows
Insert cell
// viewof ts_or_not = select({
// title: "Timeseries or not",
// description: "Please indicate whether you want it to be dynamic or not. If True, it will show year by year; if False, it will show the aggregated result.",
// options: ["True", "False"],
// value: "True"
// })
Insert cell
viewof timeseries = Inputs.select(['True', 'False'], {label: "Timeseries"})
Insert cell
Insert cell
viewof source = Inputs.radio(new Map([["Cited papers", "REF"], ["VIS", "VIS"]]), {value: "REF", label: "Source of citation"})
Insert cell
// viewof valuecutoff = Inputs.range([0, 500], {step: 1, label: "Appear at least n times", value: 100})
Insert cell
viewof rankCutoff = Inputs.range([1, 150], {step: 1, label: "Top N pairs :", value: 100})
Insert cell
Insert cell
Insert cell
Insert cell
viewof playSpeedSankey = Inputs.radio(new Map([["Very slow", 0], ["Slow", 1], ["Medium", 2], ["Fast", 3], ["Very fast", 4]]), {value: 3, label: "Adjust play speed"})
Insert cell
viewof yearRange = rangeSlider({
min: 1990,
max: 2021,
step: 1,
title: 'Adjust the year range as you need.'
})
Insert cell
// viewof yearRange = Inputs.range([1990, 2021], {step: 1, value: 1990, label: "Year range", width:500})
Insert cell
// viewof currYear = Scrubber(_.range(yearRange[0], yearRange[1]+1, 1), {
// autoplay: false,
// loop: false,
// alternate: false,
// initial: 31,
// // format: date => date.toLocaleString("ko-KR", {year: "numeric", month: "numeric", day: "numeric"}),
// delay: 5000 - playSpeedSankey * 1000,
// })
Insert cell
viewof currYear = Scrubber(_.range(1990, 2022, 1), {
autoplay: false,
loop: false,
alternate: false,
// format: date => date.toLocaleString("ko-KR", {year: "numeric", month: "numeric", day: "numeric"}),
delay: 5000 - playSpeedSankey * 1000,
})
Insert cell
displayText = source == "REF" ? "From cited paper to VIS" : "From VIS to citing papers"
Insert cell
fromWhere = md`<h2>${displayText}</h2>`
Insert cell
year = md`<h2>Year displayed: ${currYear}</h2>`
Insert cell
tsDisaplayText = timeseries === "True" ? "Below is time-series data:" : "Below is aggregated data for VIS 1990-2021:"

// tsDisaplayText = ts_or_not === "True" ? "Below is time-series data:" : "Below is aggregated data for VIS 1990-2021:"
Insert cell
tsDisplay = md`${tsDisaplayText}`
Insert cell
chartSankey = SankeyChart({
links: data
}, {
nodeGroup: d => d.id.split(/\W/)[0], // take first word for color
nodeAlign, // e.g., d3.sankeyJustify; set by input above
linkColor, // e.g., "source" or "target"; set by input above
format: (f => d => `${f(d)} citations`)(d3.format(",.1~f")),
width: `${frameWidth}`,
height: 600
})
Insert cell
// data = (ts_or_not === "True")? sankeyTS.filter(
// d => d.year == currYear && d['source name'] == source && d.level == level && d.value >= valuecutoff) :
// sankeyAggregated.filter(d => d['source name'] == source && d.level == level && d.value >= valuecutoff)
Insert cell
data = (timeseries === "True")? sankeyTS.filter(
d => d.year == currYear && d['source name'] == source && d.level == level && d.rank <= rankCutoff) :
sankeyAggregated.filter(d => d['source name'] == source && d.level == level && d.rank <= rankCutoff)

// data = (ts_or_not === "True")? sankeyTS.filter(
// d => d.year == currYear && d['source name'] == source && d.level == level && d.rank <= rankCutoff) :
// sankeyAggregated.filter(d => d['source name'] == source && d.level == level && d.rank <= rankCutoff)
Insert cell
sankeyTS = d3.csv("https://raw.githubusercontent.com/hongtaoh/32visNEW/master/data/plots/sankey/sankey_ts_df.csv")
Insert cell
sankeyAggregated = d3.csv("https://raw.githubusercontent.com/hongtaoh/32visNEW/master/data/plots/sankey/sankey_aggregated_df.csv")
Insert cell
Insert cell
d3Sankey = require.alias({"d3-array": d3, "d3-shape": d3, "d3-sankey": "d3-sankey@0.12.3/dist/d3-sankey.min.js"})("d3-sankey")
Insert cell
import {select, radio, slider, checkbox} from "@jashkenas/inputs"
Insert cell
import {swatches} from "@d3/color-legend"
Insert cell
import { rangeSlider as rangeSlider } from '@mootari/range-slider'
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
import {howto} from "@d3/example-components"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more