|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<meta http-equiv="X-UA-Compatible" content="ie=edge"> |
|
<title>Sankey</title> |
|
<style> |
|
div#main { |
|
margin: 0 auto; |
|
padding-top: 30px; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<div id="main" style="width:964px; height:600px;"></div> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> |
|
<script src="nodesAndLinks.js" lang="babel" type="text/babel"></script> |
|
<script lang="babel" type="text/babel"> |
|
const log = console.log; |
|
|
|
const url = "https://gist.githubusercontent.com/mikelotis/" + |
|
"98000626c7332958b47fe4f4fc0cf4fc/raw/0b08cdabf738b1dc41de1e04072e57cfdfe214ea/"+ |
|
"Traffic_Disruptions(July-23-2018).csv"; |
|
|
|
const myChart = echarts.init(document.getElementById('main')); |
|
|
|
d3.csv(url) |
|
.then(makeSankey); |
|
|
|
function makeSankey(csv) { |
|
//-----------------Data prep |
|
// use only required attributes |
|
const data = csv.map(element => { |
|
return { |
|
"Disruptions": "Disruptions", |
|
"Status": element["Status"], |
|
"Impact": element["Impact"], |
|
"Activity Type" : element["Activity Type"], |
|
"Traffic District": element["Traffic District"], |
|
"Infrastructure": element["Infrastructure"] |
|
}; |
|
}); |
|
//-----------------Data prep |
|
|
|
//---------------------------Data Viz |
|
// sankey flow order |
|
// Disruptions → Status → Impact → Activity Type → Traffic District → Infrastructure |
|
const order = [ |
|
"Disruptions", "Status", "Impact", |
|
"Activity Type", "Traffic District", "Infrastructure" |
|
]; |
|
|
|
//Chart margin, data for sankey series, and option for myChart |
|
const margin = {top: 30, right: 30, bottom: 30, left: 70}; |
|
const sankeyData = nodesAndLinks(data, order); |
|
const option = { |
|
title: { |
|
text: "Edmonton Traffic Disruptions", |
|
x: "center" |
|
}, |
|
tooltip: {}, |
|
series: [ |
|
{ |
|
type: "sankey", |
|
top: margin.top, |
|
right: margin.right, |
|
bottom: margin.bottom, |
|
left: margin.left, |
|
width: 964 - margin.left - margin.right, |
|
height: 600 - margin.top - margin.bottom, |
|
nodeWidth: 15, |
|
nodeGap: 30, |
|
layoutIterations: 100, |
|
nodes: sankeyData.nodes, |
|
links: sankeyData.links, |
|
draggable: false, |
|
label: { |
|
position: "left" |
|
}, |
|
itemStyle: { |
|
normal: { |
|
borderWidth: 1, |
|
borderColor: "#aaa" |
|
} |
|
} |
|
} |
|
] |
|
}; |
|
//---------------------------Data Viz |
|
|
|
myChart.setOption(option); |
|
}; |
|
</script> |
|
</body> |
|
</html> |