◆ Lines
Lines by category
When every flow has a category (the mode of transport, the purpose of the trip), each category becomes a coloured line along the corridors, side by side with the others, like the lines of a metro map. The corridors do not change: the lines share out their load.
1. The input: one category per flow
The category field of the flows is enough. With lines: "auto" (the default) and at least one flow with a category, the lines mode turns itself on; lines: "off" ignores the categories and gives exactly the drawing without them. The same origin–destination pair may appear once per category; repeated with the same category, it is summed and the spec warns.
{
"nodes": [ { "id": "2600054", "x": 952274, "y": 9124131, "label": "Abreu e Lima" }, … ],
"flows": [
{ "origin": "2600054", "destination": "2609600", "value": 11815.44, "category": "Recife" },
{ "origin": "2600302", "destination": "2604106", "value": 14719.201, "category": "Caruaru" },
…
]
}
The data of this page is the network of urban influence in Pernambuco (REGIC 2018, IBGE), the same as in the gallery, with one category per link (regic_pe_redes.json): the regional centre that the origin municipality belongs to in the REGIC hierarchy. That is 214 places, 1,092 pairs and 11 networks. The figures use k = 1, the frame on the state outline and max_categories: 12, so that no network goes to “other”. The routed volume of each category, in person-equivalents, as the legend counts it (each pair once):
legend.categories[].id | total | share |
|---|---|---|
Arcoverde | 318,958 | 5% |
Caruaru | 1,042,727 | 17% |
Centros de outros estados | 472,381 | 8% |
Garanhuns | 487,233 | 8% |
Limoeiro | 135,579 | 2% |
Palmares | 93,696 | 2% |
Petrolina | 498,806 | 8% |
Recife | 2,499,309 | 41% |
Salgueiro | 104,187 | 2% |
Serra Talhada | 323,873 | 5% |
Vitória de Santo Antão | 184,654 | 3% |
“Centros de outros estados” (centres in other states) gathers the links of municipalities whose regional centre lies outside Pernambuco. The colours of the figures are the gallery's (line_colours, one per network); section 7 shows the default palette.
lines: "off": the corridors228 segments · mean detour 1.25, max. 28.05 · 100% of the volume in corridors{"k": 1, "lines": "off", "style": {"frame_on_boundary": true, "max_labels": 30, "lang": "en"}}
milkflow regic_pe_redes.json --title "" --k 1 --lines off --frame boundary --labels 30 --lang enlines: "auto" (default): the lines228 segments · 728 lines, 422 crossings · 100% of the volume in corridors{"k": 1, "max_categories": 12, "style": {"frame_on_boundary": true, "max_labels": 30, "line_colours": ["#1F77B4", "#E66100", "#7F7F7F", "#9C2A6B", "#3F2B96", "#D45FB3", "#8C5A00", "#1D1E20", "#17A2B8", "#C9372C", "#2CA02C"], "lang": "en"}}
milkflow regic_pe_redes.json --title "" --k 1 --max-categories 12 --frame boundary --labels 30 --line-colours "#1F77B4,#E66100,#7F7F7F,#9C2A6B,#3F2B96,#D45FB3,#8C5A00,#1D1E20,#17A2B8,#C9372C,#2CA02C" --lang en2. The load by category
Routing is the usual one: every pair follows the shortest path of the skeleton. The difference is that each segment's load is also kept by category. Every segment of the FlowSpec gets lines: one entry per category that runs on it, with the load each way. The loads of a segment's lines add up exactly to the segment's flow_ab and flow_ba.
The most loaded of the segments with the most lines in this network, Camaragibe – Recife:
lines is left to right for a traveller going from a to b (coordinates y up). On a segment drawn west to east, the first line is the northernmost.| # | category | flow_ab | flow_ba | next_a | next_b |
|---|---|---|---|---|---|
| 1 | Centros de outros estados | 51,216 | 6,858 | 1 | 1 |
| 2 | Recife | 469,891 | 17,537 | 1 | 2 |
| 3 | Vitória de Santo Antão | 9,685 | 0 | 1 | 0 |
| 4 | Limoeiro | 65,335 | 0 | 1 | 0 |
| 5 | Arcoverde | 10,401 | 0 | 1 | 0 |
| 6 | Caruaru | 106,054 | 0 | 1 | 0 |
| 7 | Petrolina | 130,584 | 0 | 1 | 1 |
| 8 | Salgueiro | 22,295 | 0 | 1 | 0 |
| 9 | Serra Talhada | 102,502 | 0 | 1 | 0 |
| segment | 967,963 | 24,395 |
Sum of the lines: 967,963 and 24,395. next_a and next_b count the segments into which the line continues at each end (the lists are in the JSON below); where the line ends, the field is absent.
{
"id": 95, "a": 53, "b": 148, "flow_ab": 967962.652, "flow_ba": 24395.449, "class": 4,
"lines": [
{"category": 2, "flow_ab": 51216.464, "flow_ba": 6858.44, "next_a": [96], "next_b": [196], "ends_b": 49735.959},
{"category": 7, "flow_ab": 469890.74, "flow_ba": 17537.009, "next_a": [96], "next_b": [188, 196], "ends_a": 119694.51, "ends_b": 450247.839},
{"category": 10, "flow_ab": 9684.51, "flow_ba": 0, "next_a": [96], "ends_b": 9684.51},
{"category": 4, "flow_ab": 65334.729, "flow_ba": 0, "next_a": [96], "ends_b": 65334.729},
{"category": 0, "flow_ab": 10401.087, "flow_ba": 0, "next_a": [96], "ends_b": 10401.087},
{"category": 1, "flow_ab": 106054.247, "flow_ba": 0, "next_a": [96], "ends_b": 106054.247},
{"category": 6, "flow_ab": 130583.863, "flow_ba": 0, "next_a": [96], "next_b": [196], "ends_b": 129947.907},
{"category": 8, "flow_ab": 22294.682, "flow_ba": 0, "next_a": [96], "ends_b": 22294.682},
{"category": 9, "flow_ab": 102502.33, "flow_ba": 0, "next_a": [96], "ends_b": 102502.33}
]
}
The legend gets legend.categories (id, label, total) and the metrics, line_crossings. The categories beyond max_categories (8 by default, “other” included) and the flows without a category go to the category with an empty id, “other”.
3. The order of the lines, and what a crossing is
On each segment the lines may go in any order; the library chooses the one that makes the lines cross as little as possible at the nodes (the metro-line crossing minimization problem, MLCM, with the periphery condition). The definitions are exact and can be read from the spec:
- A line continues through a node
vbetween the segmentssandtwhen some routed pair of that category runs throughs,v,t. That is whatnext_aandnext_brecord. - On a small circle around
v, the ends of the lines are the ports, in the order of the segments around the node. Each continuation is a chord between two ports. Two chords cross when their four ports are distinct and alternate around the circle. The number of crossings is the sum, over every node, of the pairs of chords that cross.
The algorithm starts from two orders (a trivial one and a traversal of the chains of segments, from the most to the least loaded), improves each one by block descent (the best order of one segment, with everything else fixed, is solved exactly) and by swapping two lines along a whole bundle, and keeps the one with fewer crossings. In the tests, on tiny random instances, it reaches the optimum in 99% of the cases. In this network: 422 crossings with 728 lines on 228 segments; the pinned drawing of section 5 inherits the same order and here has the same 422.
4. Connectors at the nodes
For a line to look continuous, every lane that continues is trimmed back before the node, past the widest bundle there, and a connector joins it to the same category on the next segment. The connector lies on a casing of paper colour, so that a line crossing another at a node shows a clean cut, and it runs under the place symbol. A line that ends at a place converges in a curve to the centre of the symbol. A line that stops at a place and also goes on gets a branch into the centre there when what stops is at least 5% of its volume on the segment; there is one branch per line and place, from the segment where it stops the most (on a tie, the segment with the lowest index).
corners: "mitre" the connector is the corner where the two lanes meet; otherwise a curve tangent to both.The corners (corners) apply to the lines too; the figures of Styles show each one.
5. Geographic and schematic
The same lines as in section 1, now pinned (schematic_options: {pin: true}, --pin): every municipality stays at its point of the map and the segments bend at 0°, 45° and 90°, with smooth corners. The schematic spec is a copy of the geographic one with other positions: it inherits the lines in the same order, which stays right because the layout keeps the order of the segments around every node. It is the figure of the gallery.
{"k": 1, "max_categories": 12, "schematic": true, "schematic_options": {"pin": true}, "style": {"frame_on_boundary": true, "max_labels": 30, "line_colours": ["#1F77B4", "#E66100", "#7F7F7F", "#9C2A6B", "#3F2B96", "#D45FB3", "#8C5A00", "#1D1E20", "#17A2B8", "#C9372C", "#2CA02C"], "corners": "smooth", "corner_radius": 10, "lang": "en"}}
milkflow regic_pe_redes.json --title "" --k 1 --max-categories 12 --pin --frame boundary --labels 30 --line-colours "#1F77B4,#E66100,#7F7F7F,#9C2A6B,#3F2B96,#D45FB3,#8C5A00,#1D1E20,#17A2B8,#C9372C,#2CA02C" --corners smooth --corner-radius 10 --lang en6. Width: fixed or by volume
line_width: "fixed" (default): every line is as wide as class 1, as on a metro map; the bundle grows with the number of lines, and the volume is read in the interactive page's tooltips, not in the width. line_width: "flow": the class width of the segment is shared among its lines by their volumes, none below 0.8 px, and the legend keeps the classes. The default is fixed because, in flow, three or four lines in a thin class become slivers whose colour cannot be told.
line_width: "flow", pinned{"k": 1, "max_categories": 12, "schematic": true, "schematic_options": {"pin": true}, "style": {"frame_on_boundary": true, "max_labels": 30, "line_colours": ["#1F77B4", "#E66100", "#7F7F7F", "#9C2A6B", "#3F2B96", "#D45FB3", "#8C5A00", "#1D1E20", "#17A2B8", "#C9372C", "#2CA02C"], "corners": "smooth", "corner_radius": 10, "line_width": "flow", "lang": "en"}}
milkflow regic_pe_redes.json --title "" --k 1 --max-categories 12 --pin --frame boundary --labels 30 --line-colours "#1F77B4,#E66100,#7F7F7F,#9C2A6B,#3F2B96,#D45FB3,#8C5A00,#1D1E20,#17A2B8,#C9372C,#2CA02C" --corners smooth --corner-radius 10 --line-width flow --lang en7. How many categories, and the colours
max_categories (1 to 64, default 8) bounds the categories, “other” included: the smallest ones beyond the bound merge into “other”, drawn in grey. Colours come from a categorical palette safe for colour-blind readers, one for light paper and one for dark, in the order of legend.categories; line_colours replaces them, by index (#RGB or #RRGGBB; the categories beyond the list keep the palette).
| paper | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | other |
|---|---|---|---|---|---|---|---|---|---|
| light | #0072B2 | #D55E00 | #009E73 | #882255 | #332288 | #AA4499 | #7A5500 | #1D1E20 | #80868D |
| dark | #56B4E9 | #E69F00 | #00B486 | #E08AC0 | #F0E442 | #F26B1D | #7C83F0 | #F4F1EA | #6B737B |
max_categories: 4, the default palette Recife, Caruaru, Petrolina and “other”: the other eight networks, in grey.228 segments · 474 lines, 235 crossings · 100% of the volume in corridors{"k": 1, "max_categories": 4, "schematic": true, "schematic_options": {"pin": true}, "style": {"frame_on_boundary": true, "max_labels": 30, "corners": "smooth", "corner_radius": 10, "lang": "en"}}
milkflow regic_pe_redes.json --title "" --k 1 --max-categories 4 --pin --frame boundary --labels 30 --corners smooth --corner-radius 10 --lang en8. What the lines do not say
- A line is not an observed route. The orange line on a corridor says that pairs of that category were routed there by the shortest path of the skeleton, not that a service, a bus line or a real trip runs along that segment. It is the same graphical aggregate as the corridors, only shared out.
- The order minimizes crossings by a heuristic. The problem is NP-hard in general; the result is not guaranteed optimal, only no worse than the trivial starting order. Forced crossings (chords on four different segments) are counted and cannot be removed. The weight of the lines plays no role: every crossing counts 1.
- Where many lines cross at one node (four segments or more, several lines each), the connectors tangle under the symbol; continuity is there, but the eye only follows it with the line highlight of the interactive page. In a dense geographic centre, short segments can hide under the symbols. Labels never sit on a bundle or a connector (rule L3 of chapter 7): a place with no free position beside it tries a leader line and, with none, goes unlabelled (counted in the honesty line).
- The lines take the width of the corridor: with lines drawn,
directions: "split",direction_coloursandstroke: "rails"are ignored (the command line refuses the combination and asks for--lines off). The TikZ output draws the lines as the SVG does, from the same scene (comparison).
In the interactive page (html: true), each segment's tooltip lists the categories and a click on a category of the legend highlights the line across the network. Try it in the browser, with the example “city with modes”.