Skip to content
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

add d3.trail() #168

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

add d3.trail() #168

wants to merge 7 commits into from

Conversation

wyhleonard
Copy link

No description provided.

@wyhleonard
Copy link
Author

This commit added the function as trail marks in Vega, which is similar to line marks, but can have variable width determined by backing data . Moreover, this commit also fixed the two gotchas described in the issue. Users can use d3.trail() like d3.line() to create the trail marks they want.
The first picture is produced by the method in Vega of trail marks, and the second picture is produced by the method in this commit.
res_1
res_2

@Fil
Copy link
Member

Fil commented Aug 26, 2020

Ah excellent! I was worried about this shape too https://observablehq.com/@fil/trail-marks ;-)

Another bit that would need fixing in this shape is when the data has more than 2 points—currently it draws one "loop" for each pair, with the consequence that the stroke is not the outline, and the shape has superpositions.

(You will also need to declare the function in src/index.js, show it in README.md and test it in tests/ for the PR to be complete.)

@wyhleonard
Copy link
Author

wyhleonard commented Aug 26, 2020

Thank you for your suggestions, I will further improve this function with your suggestions. Actually, it's my first experience to contribute to open source libraries. So if i make some mistakes, please accept my most sincere apology.

@wyhleonard
Copy link
Author

wyhleonard commented Sep 1, 2020

The second commit has improved the function of d3.trail(), including fixing the gotchas of superpositions and declaring the function in src/index.js, showing it in README.md and testing it in tests/trail-test.js. Now, d3.trail() can draw the trail marks as
res_3
trail
trail-defined
trail-stroke
However, there are still some problems in README.md. For example, it is unable to display pictures properly, and there is an unexpected slant in the text after Lines.

@Fil
Copy link
Member

Fil commented Sep 1, 2020

Wow this is beautiful!

(The images will display only when the PR is merged (they link to the master branch).)

@curran
Copy link
Contributor

curran commented Sep 1, 2020

Someone should totally build an example with this that produces Minard's "Napoleon's March on Moscow" map.

image

@curran
Copy link
Contributor

curran commented Sep 1, 2020

Also I wonder how close this gets us to being able to automatically produce shapes like this.

image

@Fil
Copy link
Member

Fil commented Sep 1, 2020

Two small issues:

Have you pushed all the code? To build this branch I had to add:

import array from "./array.js";

Secondly, the data from https://observablehq.com/@fil/trail-marks breaks it, because I've left a little 💣 with coincident points ( [300, 200, 0], [300, 200, 0.1] ).

@wyhleonard
Copy link
Author

The third commit has solved some small issus in d3.trail(). Note that the point will be only drawed when its size is greater than 0.

Copy link
Member

@Fil Fil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly style remarks to try and homogenize with the rest of the code base.

I'm wondering: it might be more performant if it did all the calls to x, y, size and defined only once, and saved the values in Float64Arrays. (Plus, you never know what accessors the user is going to call—or maybe someone is going to try size(random).)

src/point.js Outdated Show resolved Hide resolved
src/trail.js Outdated Show resolved Hide resolved
src/trail.js Outdated Show resolved Hide resolved
src/trail.js Outdated Show resolved Hide resolved
src/trail.js Outdated Show resolved Hide resolved
test/trail-test.js Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@wyhleonard
Copy link
Author

The fourth commit has completed the changes requested. Now, d3.trail() can draw a trail mark with coincident points as
coin_1
coincident-02

Copy link
Member

@Fil Fil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

If you would follow D3's general style conventions more closely (with spaces between operators and arguments, before curly brackets etc.) it will help do a cleaner merge. See https://gist.github.com/Fil/faa610218d355d927b19cf8e99d8dcb6

@wyhleonard
Copy link
Author

wyhleonard commented Sep 7, 2020

Thank you for your reminder. I have followed D3's general style conventions in the fifth commit.

@Fil
Copy link
Member

Fil commented Sep 7, 2020

Thank you. Beautiful work! I think @mbostock and @jheer will be interested to review.

@wyhleonard
Copy link
Author

It's my honor to contribute to D3. Thank you for your help again.

@jheer
Copy link

jheer commented Sep 7, 2020

It's great to see someone improving the trail mark in Vega and adapting it to d3! Thanks!

I took a quick glance at the code, but didn't check the adapted logic for correctness. That said, I did notice some possible concerns:

  • Helper methods like intersectPoint and commonTangent generate object-valued outputs, possibly causing some performance overhead due to repeated object instantiation.
  • Inconsistencies in code style spacing. For example: else{ vs. else {, if( vs. if (, function(test){ vs. function(test) {.

@wyhleonard
Copy link
Author

Thank you for your suggestions! The sixth commit has taken the two concerns above.

Copy link
Contributor

@curran curran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Added some minor nits.

I think the consensus is that the README should use let and const, while the implementation uses ES5.

README.md Outdated
When a trail is [generated](#_trail), the x accessor will be invoked for each [defined](#trail_defined) element in the input data array, being passed the element `d`, the index `i`, and the array `data` as three arguments. The default x accessor assumes that the input data are three-element arrays of numbers. If your data are in a different format, or if you wish to transform the data before rendering, then you should specify a custom accessor. For example:

```js
var data = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var data = [
const data = [

README.md Outdated
];

var trail = d3.trail()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var trail = d3.trail()
const trail = d3.trail()

src/trail.js Outdated
return false;
}

// straddle experiment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's an experiment, do we want to add it? Maybe remove this comment?

Suggested change
// straddle experiment.
// straddle.

src/trail.js Outdated

// straddle experiment.
if (cross(x1 - x4, y1 - y4, x3 - x4, y3 - y4) * cross(x2 - x4, y2 - y4, x3 - x4, y3 - y4) > 0 ||
cross(x3 - x2, y3 - y2, x1 - x2, y1 - y2) * cross(x4 - x2, y4 - y2, x1 - x2, y1 - y2) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cross(x3 - x2, y3 - y2, x1 - x2, y1 - y2) * cross(x4 - x2, y4 - y2, x1 - x2, y1 - y2) > 0) {
cross(x3 - x2, y3 - y2, x1 - x2, y1 - y2) * cross(x4 - x2, y4 - y2, x1 - x2, y1 - y2) > 0) {

Alignment reads well.

This is a lot for a single expression also. May be worth factoring out into variables.

src/trail.js Outdated
x: x2,
y: y2
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
} else {

Seems odd to have else on a new line.

src/trail.js Outdated
angle: alpha + beta - pi
};
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this else, as the if returns. Can remove the brackets and un-indent here.

src/trail.js Outdated

if (context == null) context = buffer = path();

var start,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps better to lift these declarations to the top, with the others.

@wyhleonard
Copy link
Author

Thank you for your suggestions! The lastest commit has refined the codes and docs in d3-shape.

@Fil Fil requested a review from mbostock December 19, 2020 16:42
@Fil Fil added the feature label Jan 23, 2021
@Fil Fil mentioned this pull request Apr 28, 2021
5 tasks
@Fil Fil mentioned this pull request May 1, 2021
2 tasks
@Fil Fil mentioned this pull request Jan 5, 2022
@mbostock mbostock removed the feature label May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants