Skip to content

group segments by z #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

group segments by z #764

wants to merge 4 commits into from

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Feb 17, 2022

This PR makes groupIndex return a nested collection of indices per z. In short, it creates segmented lines and areas as a svg:g for each line, containing all the path segments, rather than flattening all the segments together. See #761 (comment) for context.

As a consequence, obviously, is that each line (in the "normal" case) is wrapped in a svg:g.

This allows an easier manipulation of lines and areas with D3 or with vector graphics software, and also makes it easier to create alternative grouped marks.

@Fil Fil mentioned this pull request Feb 17, 2022
@Fil
Copy link
Contributor Author

Fil commented Feb 17, 2022

Let me share one experiment here, even if it's not super conclusive. In the image below, we have one path per z-line and a svg:use for each segment, with a specific stroke-dasharray to tell where the ink should go:
Capture d’écran 2022-02-17 à 13 09 52
As you can see, it's better than the original, but still "choppy" (and, tbh, rendering so many use:path is quite slow…). I'm not pushing the code—it's obviously unfinished (the dash offset is not computed as the actual length that is needed)—, and I feel it's still going to be unsatisfying anyway. The diff shows what I've tried so far:

diff --git a/src/marks/line.js b/src/marks/line.js
index 7816216..38949ae 100644
--- a/src/marks/line.js
+++ b/src/marks/line.js
@@ -39,17 +39,29 @@ export class Line extends Mark {
         .call(g => g.selectAll()
           .data(groupIndex(I, [X, Y], channels))
           .join("g")
+            .call(g => g.append("path")
+              .attr("d", segments => shapeLine()
+                .curve(this.curve)
+                .defined(i => i >= 0)
+                .x(i => X[i])
+                .y(i => Y[i])
+                (segments.flat()))
+              .attr("id", function(segments) {
+                const id = "pu" + segments.flat().join("-"); // TODO: unique id generator
+                const href = `#${id}`;
+                // TODO: proper measurement of the correct cover start and end
+                const cover = this.getTotalLength() / segments.length;
+                segments.forEach((segment, count) => Object.assign(segment, {cover, count, href}));
+                return id;
+              })
+            )
             .selectAll()
             .data(segments => segments)
-            .join("path")
+            .join("use")
+              .attr("href", ({href}) => href)
+              .attr("stroke-dasharray", ({count, cover}) => [0, count * cover, cover, 100000])
               .call(applyDirectStyles, this)
               .call(applyGroupedChannelStyles, this, channels)
-              .call(applyGroupedMarkers, this, channels)
-              .attr("d", shapeLine()
-                .curve(this.curve)
-                .defined(i => i >= 0)
-                .x(i => X[i])
-                .y(i => Y[i])))
+              .call(applyGroupedMarkers, this, channels))
       .node();
   }
 }

Base automatically changed from mbostock/group-aesthetics to main February 17, 2022 18:31
@Fil Fil closed this Mar 22, 2022
@Fil Fil deleted the fil/group-aesthetics branch March 22, 2022 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants