-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathsankey.js
279 lines (259 loc) · 9 KB
/
sankey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*Copyright 2021 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
import { CirclePath, makeCurve } from "./circlepath.js"
import { spec } from "./factory.js"
import { colorList, iconSize, getColorMaps, renderNode, imageViewBox } from "./graph.js"
import { sheetHash, sheetWidth, sheetHeight } from "./icon.js"
import { one } from "./rational.js"
import { graphClickHandler, graphMouseOverHandler, graphMouseLeaveHandler } from "./visualize.js"
import * as d3sankey from "./d3-sankey/index.js"
const nodePadding = 36
const sankeyNodeMargin = 2
const columnWidth = 200
const maxNodeHeight = 175
function selfPath(d) {
let x0 = d.source.x1
let y0 = d.y0
let x1 = d.source.x1
let y1 = d.source.y1 + d.width/2 + 10
let r1 = (y1 - y0) / 2
let x2 = d.target.x0
let y2 = d.target.y1 + d.width/2 + 10
let x3 = d.target.x0
let y3 = d.y1
let r2 = (y3 - y2) / 2
return new CirclePath(1, 0, [
{x: x0, y: y0},
{x: x1, y: y1},
{x: x2, y: y2},
{x: x3, y: y3},
])
}
function backwardPath(d) {
// start point
let x0 = d.source.x1
let y0 = d.y0
// end point
let x3 = d.target.x0
let y3 = d.y1
let y2a = d.source.y0 - d.width/2 - 10
let y2b = d.source.y1 + d.width/2 + 10
let y3a = d.target.y0 - d.width/2 - 10
let y3b = d.target.y1 + d.width/2 + 10
let points = [{x: x0, y: y0}]
let starty
let endy
if (y2b < y3a) {
// draw start arc down, end arc up
starty = y2b
endy = y3a
} else if (y2a > y3b) {
// draw start arc up, end arc down
starty = y2a
endy = y3b
} else {
// draw both arcs down
starty = y2b
endy = y3b
}
let curve = makeCurve(-1, 0, x0, starty, x3, endy)
for (let {x, y} of curve.points) {
points.push({x, y})
}
points.push({x: x3, y: y3})
return new CirclePath(1, 0, points)
}
function linkPath(d) {
if (d.direction === "self") {
return selfPath(d)
} else if (d.direction === "backward") {
return backwardPath(d)
}
let x0 = d.source.x1
let y0 = d.y0
let x1 = d.target.x0
let y1 = d.y1
return makeCurve(1, 0, x0, y0, x1, y1, d.width)
}
export function renderSankey(data, direction, ignore) {
let maxNodeWidth = 0
let testSVG = d3.select("body").append("svg")
.classed("sankey test", true)
let text = testSVG.append("text")
for (let node of data.nodes) {
let nodeWidth = node.labelWidth(text, sankeyNodeMargin)
if (nodeWidth > maxNodeWidth) {
maxNodeWidth = nodeWidth
}
node.width = nodeWidth
}
text.remove()
testSVG.remove()
let nw, np
if (direction === "down") {
nw = nodePadding
np = maxNodeWidth
} else if (direction === "right") {
nw = maxNodeWidth
np = nodePadding
}
let sankey = d3sankey.sankey()
.nodeWidth(nw)
.nodePadding(np)
.nodeAlign(d3sankey.sankeyRight)
.maxNodeHeight(maxNodeHeight)
.linkLength(columnWidth)
let {nodes, links} = sankey(data)
let [itemColors, recipeColors] = getColorMaps(nodes, links)
for (let link of links) {
link.curve = linkPath(link)
if (direction === "down") {
link.curve = link.curve.transpose()
}
let belts = []
if (link.beltCount !== null) {
let dy = link.width / link.beltCount.toFloat()
// Only render belts if there are at least three pixels per belt.
if (dy > 3) {
for (let i = one; i.less(link.beltCount); i = i.add(one)) {
let offset = i.toFloat() * dy - link.width/2
let beltCurve = link.curve.offset(offset)
belts.push({item: link.item, curve: beltCurve})
}
}
}
link.belts = belts
}
if (direction === "down") {
for (let node of nodes) {
[node.x0, node.y0] = [node.y0, node.x0];
[node.x1, node.y1] = [node.y1, node.x1];
}
}
let svg = d3.select("svg#graph")
.classed("sankey", true)
svg.selectAll("g").remove()
// Node rects
let rects = svg.append("g")
.classed("nodes", true)
.selectAll("g")
.data(nodes)
.join("g")
.classed("node", true)
let nodeJust = "left"
if (direction === "down") {
nodeJust = "center"
}
renderNode(rects, sankeyNodeMargin, nodeJust, recipeColors, ignore)
// Link paths
let link = svg.append("g")
.classed("links", true)
.selectAll("g")
.data(links)
.join("g")
.classed("link", true)
.each(function(d) { d.elements.push(this) })
//.style("mix-blend-mode", "multiply")
link.append("path")
.attr("fill", "none")
.attr("stroke-opacity", 0.3)
.attr("d", d => d.curve.path())
.attr("stroke", d => colorList[itemColors.get(d.item) % colorList.length])
.attr("stroke-width", d => Math.max(1, d.width))
link.append("g")
.selectAll("path")
.data(d => [
d.curve.offset(-d.width/2),
d.curve.offset(d.width/2),
])
.join("path")
.classed("highlighter", true)
.attr("fill", "none")
.attr("d", d => d.path())
.attr("stroke", "none")
.attr("stroke-width", 1)
link.append("g")
.classed("belts", true)
.selectAll("path")
.data(d => d.belts)
.join("path")
.classed("belt", true)
.attr("fill", "none")
.attr("stroke-opacity", 0.3)
.attr("d", d => d.curve.path())
.attr("stroke", d => colorList[itemColors.get(d.item) % colorList.length])
.attr("stroke-width", 1)
link.append("title")
.text(d => `${d.source.name} \u2192 ${d.target.name}\n${spec.format.rate(d.rate)}`)
let linkIcon = link.filter(d => d.extra)
.append("svg")
.attr("viewBox", d => imageViewBox(d.item))
.attr("x", d => d.source.x1 + 2.25)
.attr("y", d => d.y0 - iconSize/4 + 0.25)
.attr("width", iconSize/2)
.attr("height", iconSize/2)
linkIcon.append("image")
.attr("xlink:href", "images/sprite-sheet-" + sheetHash + ".png")
.attr("width", sheetWidth)
.attr("height", sheetHeight)
if (direction === "down") {
linkIcon
.attr("x", d => d.y0 - iconSize/4 + 0.25)
.attr("y", d => d.source.y1 + 2.25)
}
let linkLabel = link.append("text")
.attr("x", d => d.source.x1 + 2 + (d.extra ? iconSize/2 : 0))
.attr("y", d => d.y0)
.attr("dy", "0.35em")
.attr("text-anchor", "start")
.text(d => (d.extra ? "\u00d7 " : "") + spec.format.rate(d.rate) + "/" + spec.format.rateName)
if (direction === "down") {
linkLabel
.attr("x", null)
.attr("y", null)
.attr("transform", d => {
let x = d.y0
let y = d.source.y1 + 2 + (d.extra ? 16 : 0)
return `translate(${x},${y}) rotate(90)`
})
}
// Overlay transparent rect on top of each node, for click events.
let rectElements = svg.selectAll("g.node rect").nodes()
let overlayData = []
// Flash the graph tab to be visible, so that the graph is laid out and
// the BBox is not empty.
let graphTab = d3.select("#graph_tab")
let origDisplay = d3.style(graphTab.node(), "display")
graphTab.style("display", "block")
for (let i = 0; i < nodes.length; i++) {
let rect = rectElements[i].getBBox()
let node = nodes[i]
let recipe = node.recipe
overlayData.push({rect, node, recipe})
}
graphTab.style("display", origDisplay)
svg.append("g")
.classed("overlay", true)
.selectAll("rect")
.data(overlayData)
.join("rect")
.attr("stroke", "none")
.attr("fill", "transparent")
.attr("x", d => d.rect.x)
.attr("y", d => d.rect.y)
.attr("width", d => d.rect.width)
.attr("height", d => d.rect.height)
.on("mouseover", (event, d) => graphMouseOverHandler(event, d.node))
.on("mouseleave", (event, d) => graphMouseLeaveHandler(event, d.node))
.on("click", (event, d) => graphClickHandler(event, d.node))
.append("title")
.text(d => d.node.name + (d.node.count.isZero() ? "" : `\n${d.node.building.name} \u00d7 ${spec.format.count(d.node.count)}`))
}