Skip to content

Commit

Permalink
for lines with variable aesthetics we want to maintain the higher-lev…
Browse files Browse the repository at this point in the history
…el semantics of markers:

- markerStart matches the start of the line
- markerMid matches the points which are not at the start or the end
- markerEnd matches the end of the line

Since these lines are implemented as multiple paths, we have change the low-level implementation of markers:
- markerStart only applies to the first segment of a line
- markerMid applies to all the segments, complemented by the start of all but the first segments
- markerEnd only applies to the last segment of a line

closes #2093
  • Loading branch information
Fil committed Jun 17, 2024
1 parent 0e5c684 commit 0a0176b
Show file tree
Hide file tree
Showing 7 changed files with 1,280 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/marker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {select, selectAll, group} from "d3";
import {create} from "./context.js";

export function markers(mark, {marker, markerStart = marker, markerMid = marker, markerEnd = marker} = {}) {
Expand Down Expand Up @@ -100,16 +101,15 @@ function markerTick(orient) {
let nextMarkerId = 0;

export function applyMarkers(path, mark, {stroke: S}, context) {
return applyMarkersColor(path, mark, S && ((i) => S[i]), context);
return applyMarkersColor(path, mark, S && ((i) => S[i]), null, context);
}

export function applyGroupedMarkers(path, mark, {stroke: S}, context) {
return applyMarkersColor(path, mark, S && (([i]) => S[i]), context);
export function applyGroupedMarkers(path, mark, {stroke: S, z: Z}, context) {
return applyMarkersColor(path, mark, S && (([i]) => S[i]), Z, context);
}

function applyMarkersColor(path, {markerStart, markerMid, markerEnd, stroke}, strokeof = () => stroke, context) {
function applyMarkersColor(path, {markerStart, markerMid, markerEnd, stroke}, strokeof = () => stroke, Z, context) {
const iriByMarkerColor = new Map();

function applyMarker(marker) {
return function (i) {
const color = strokeof(i);
Expand All @@ -126,7 +126,26 @@ function applyMarkersColor(path, {markerStart, markerMid, markerEnd, stroke}, st
};
}

if (markerStart) path.attr("marker-start", applyMarker(markerStart));
if (markerMid) path.attr("marker-mid", applyMarker(markerMid));
if (markerEnd) path.attr("marker-end", applyMarker(markerEnd));
if (!(markerStart || markerMid || markerEnd)) return;

const start = markerStart && applyMarker(markerStart);
const mid = markerMid && applyMarker(markerMid);
const end = markerEnd && applyMarker(markerEnd);
if (Z) {
for (const g of group(
path.filter((i) => i.length >= 2),
(d) => Z[select(d).datum()[0]]
).values()) {
if (start) select(g.at(0)).attr("marker-start", start);
if (mid) {
selectAll(g.slice(1)).attr("marker-start", mid);
selectAll(g).attr("marker-mid", mid);
}
if (end) select(g.at(-1)).attr("marker-end", end);
}
} else {
if (start) path.attr("marker-start", start);
if (mid) path.attr("marker-mid", mid);
if (end) path.attr("marker-end", end);
}
}
58 changes: 58 additions & 0 deletions test/output/groupMarker.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0a0176b

Please sign in to comment.