Skip to content

New Added support for custom edge line style - #77

Merged
tonilastre merged 2 commits into
memgraph:release/1.0.0from
parmar-abhinav:customEdgeLine
Sep 4, 2023
Merged

New Added support for custom edge line style#77
tonilastre merged 2 commits into
memgraph:release/1.0.0from
parmar-abhinav:customEdgeLine

Conversation

@parmar-abhinav

Copy link
Copy Markdown
Contributor

This PR introduces a new feature allowing users to have greater control over the appearance of edges in the graph by implementing custom edge line styles. User can set a Solid, Dashed, Dotted or even custom edges by defining own edge patterns.

Comment on lines +31 to +50
const edgeLineStyleType = edge.getEdgeLineStyle().type;
switch (edgeLineStyleType) {
case EdgeLineStyleType.DASHED:
context.setLineDash(DEFAULT_DASHED_LINE_PATTERN);
break;
case EdgeLineStyleType.DOTTED:
context.setLineDash(DEFAULT_DOTTED_LINE_PATTERN);
break;
case EdgeLineStyleType.SOLID:
context.setLineDash(DEFAULT_SOLID_LINE_PATTERN);
break;
case EdgeLineStyleType.CUSTOM: {
const dashPattern: number[] = edge.getEdgeLineStyle().dashPattern;
context.setLineDash(dashPattern);
break;
}
default:
context.setLineDash(DEFAULT_SOLID_LINE_PATTERN);
break;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is repeated in all three types, but it is all the same code. Additionally, rendering should be super fast which means all unnecessary calls should be removed. For example, for each edge calling getEdgeLineStyle creates a new object just to figure out the correct style - when rendering has a min of 25 FPS, this will start to impact the process.

I would suggest removing getEdgeLineStyle - no need for it because you have the latest information in the this.style of the edge model.
I would suggest having new function on the edge model, something like getLineDashPattern that returns number[] | null. In case of solid or undefined lineStyle, it returns null. In case of any other, it returns a default one (for dashed or dotted) or a user-defined one (in case of custom).

Then this code (and in all three edge types) would be simple as:

const lineDashPattern = edge.getLineDashPattern();
context.setLineDashPatern(lineDashPattern ?? []);

@parmar-abhinav parmar-abhinav Aug 19, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your feedback. I've made the following changes to the code as you suggested:

  • removed the getEdgeLineStyle() function, as you pointed out that it is unnecessary and creates a new object each time it is called.

  • added a new function called getLineDashPattern() to the edge model. This function returns a number[] or null, depending on whether the edge has a custom line style or not.

  • updated the code to use the getLineDashPattern() function instead of getEdgeLineStyle().

I believe these changes will make the code more efficient and improve the rendering performance.

Comment thread src/renderer/canvas/edge/shared.ts Outdated
Comment on lines +14 to +16
export const DEFAULT_SOLID_LINE_PATTERN: number[] = [];
export const DEFAULT_DASHED_LINE_PATTERN: number[] = [5, 5];
export const DEFAULT_DOTTED_LINE_PATTERN: number[] = [1, 1]; No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe you will need to move this closer to the models/edge - check the other comment on where the code around patterns should be.

Comment thread src/utils/type.utils.ts Outdated
* @param {any} value Any value
* @return {boolean} True if it is a array of numbers, false otherwise
*/
export const isArrayOfNumbers = (value: any): value is boolean => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
export const isArrayOfNumbers = (value: any): value is boolean => {
export const isArrayOfNumbers = (value: any): value is number[] => {

In typeguards, this param should return the correct type. Typeguards return a boolean by default + this is for the Typescript to be able to know what the type is after typeguard returns true.

Comment thread src/utils/type.utils.ts Outdated
Comment on lines +102 to +103
const isNotNumber = value.some((element) => !isNumber(element));
return !isNotNumber;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
const isNotNumber = value.some((element) => !isNumber(element));
return !isNotNumber;
return value.some((element) => !isNumber(element));

This is pretty much clear enough where you don't have any benefit from an additional variable. If this was a more complex function, then the named variable would be a good choice for better readability.

Comment thread src/models/edge.ts Outdated
widthHover: number;
widthSelected: number;
zIndex: number;
edgeLineStyle: IEdgeLineStyle;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need to have edge because you already have a context of EdgeStyle. I would suggest naming this lineStyle.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed it to lineStyle, pls check.

Comment thread src/models/edge.ts Outdated
Comment on lines +36 to +39
export interface IEdgeLineStyle {
type: EdgeLineStyleType;
dashPattern: number[];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be fixed from a typescript side to be more precise. For example, currently, you can have { type: "solid", dashPattern: [1, 2, 3] } which is not correct. An array can only be used with custom type. E.g.

export type IEdgeLineStyle =
  | { type: EdgeLineStyleType.SOLID }
  | { type: EdgeLineStyleType.DASHED }
  | { type: EdgeLineStyleType.DOTTED }
  | { type: EdgeLineStyleType.CUSTOM, pattern: number[] };

or even

export type IEdgeLineStyle =
  | EdgeLineStyleType.SOLID
  | EdgeLineStyleType.DASHED
  | EdgeLineStyleType.DOTTED
  | { type: EdgeLineStyleType.CUSTOM, pattern: number[] };

Maybe the second one might even work better if users will use SOLID, DASHED or DOTTED more. The first one is easier to use from library point of view, the second one from user point of view.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nitpicking but I am also thinking about renaming dashPattern to just pattern because you already have a context which is a line style. What do you think? Also, what do you think about those two proposals above, which one makes more sense to you?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with your suggestion to fix the TypeScript type for IEdgeLineStyle.

Yes The second approach might even work better if users will use SOLID, DASHED or DOTTED more. However, I am thinking to use the first approach because it gives users a more consistent and predictable experience. With the first approach, users know that they can always specify the line style using the same syntax, regardless of whether they are using a predefined style or a custom style.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yep, agree with you. The first approach is slightly a better option because of the usage predictability.

Comment thread src/models/edge.ts
case EdgeLineStyleType.DOTTED:
return DEFAULT_DOTTED_LINE_PATTERN;
case EdgeLineStyleType.CUSTOM:
return isArrayOfNumbers(lineStyle.pattern) ? lineStyle.pattern : null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nothing to change here, but just a general comment that once node and edge style have a setter for styles setStyle (what 1.0.0 will require), this check will be done there because the check is done once then.

@tonilastre
tonilastre merged commit 900f989 into memgraph:release/1.0.0 Sep 4, 2023
tonilastre added a commit that referenced this pull request Jul 28, 2026
… tooling

* Fix: Fix multiple maps issue (#33)

* Chore: Update package.json version

* New: Change the API to handle OrbView and OrbMapView (#34)

* New: Change the API to handle OrbView and OrbMapView

* New: Change the API for select/hover strategies

* Chore: Release/1.0.0

* New: Add support to get selected/hovered nodes and edges (#61)

* New: Added support to get selected nodes and edges

* New: Added support to get hovered nodes and edges

---------

Co-authored-by: Abhinav Singh Parmar <abhinavparmar147@gmail.com>

* New: Add support for enabling and disabling dragging of nodes (fixes #62) (#69)

* New: Add feature to enable/disable node dragging (fixes #62)

* New: Added support to modify interaction from setSettings

* New: Updated documentation for interaction property

* New: Add feature to enable/disable zoom (fixes #62)

* NEW: Updated documentation for interaction property

* NEW: Updated documentation to include isDragEnabled

* New: Add support for custom edge line style (#77)

* New Added support for custom edges

* Refactor: Streamline edge rendering code and optimize line style handling

* New: Add support for handling device pixel ratio (#45)

* Chore: Move container and canvas creation from the view to the renderer

* New: Add devicePixelRatio render property and handler

* Fix: Add default DPR for older browsers

* Chore: Remove useless check for automatic DPR

* New: Add new simulator (#56) (#57)

* New: Add new simulator (#56)

* New: Add simulator scenarios for manual testing
* New: Refactor simulator (WIP)
* New: Add progress overlay, Update descriptions
* Fix: Introduce new simulator event, Fix main-thread behavior
* Fix: Rearrange class methods based on visibility
* Fix: Improve naming
* Chore(release): 0.2.0
* Fix: Tweak simulator, adjust API slightly
* Fix: Temporarily patch some physics behavior
* Fix: Adjust re-heating parameters
* Fix: tweak physics behavior -> immediately stop sim when disabling
* Chore: Remove the beta from release branches

---------

Co-authored-by: dlozic <davidlozic@gmail.com>

* New: Add zoom and recenter functions (#74)

* New: Add zoom and recenter functions

* Fix: Reduce excessive recentering (#75)

* Fix: Remove excessive recenterings
* Fix: Remove unused code

* Fix: New simulator (#92)

* Chore: Refactor naming

* New: Add new events

* Chore: Refactor code styling

* Docs: Remove unused flags

* Chore: Remove unused simulation functions

* Chore: Refactor view render function calls

* Chore: Add missing tests

* New: Add removal functions (#96)

* New: Add removal functions

* Fix: Add missing callback data

* Chore: Refactor remove return values

* Chore: Refactor remove function type usage

* Fix: Default settings for node placement (#98)

* New: Add properties setters and getters (#93)

* New: Add node properties setters

* New: Add edge properties setters

* New: Add properties getters

* New: Add patch for nodes and edges

* Fix: Make getters return copies

* Fix: Edge factory listeners copying

* Fix: Jest outdated tests

* Fix: Github actions node version

* Chore: Refactor observer interface

* Chore: Refactor node/edge constructor settings

* Chore: Refactor node/edge function grouping

* Chore: Refactor node/edge function grouping

* Fix: Listeners behaviour

* Chore: Refactor property copying

* Chore: Refactor subject implementation

* Fix: Set position behaviour on node drag

* Chore: Upgrade node version

* Chore: Refactor function type check

* Fix: Remove listener behaviour

* Chore: Refactor util naming

* Chore: Remove unused type assertion

* Chore: Refactor position setter options

* Chore: Refactor property patch function

* Fix: Set map node position behaviour

* Chore: Refactor simulator data patching

* Chore: Change observers to callbacks

* New: Add state setters with options (#95)

* New: Add state setters with options

* Chore: Remove leftover comments

* Chore: Refactor state setter logic

* Chore: Refactor state types

* Fix: Rename merged function usage

* Fix: Merged variable naming

* Chore: Fix tests

---------

Co-authored-by: dlozic <davidlozic@gmail.com>
Co-authored-by: Oleksandr Ichenskyi <55350107+AlexIchenskiy@users.noreply.github.com>
Co-authored-by: AlexIchenskiy <aichenskiy@gmail.com>

* New: Add zoom in and out functions (#100)

* Fix: Skip unnecessary listener notify on set style

* Fix: remove unnecessary rerender on state change

* Chore: Update documentation

* Fix: Node/edge getter performance issue

* Chore: Add data change docs example

* Fix: Docs typos

* Fix: Disable source map generation (#105)

* New: Add tree layout (#107)

* New: Add new layouts

* New: Add layout options

* Chore: Make layout dynamically changeable

* Chore: Update docs

* Fix: Naming typo

* Chore: Refactor layouts

* New: Enable layout node add/remove

* Chore: Improve behavior for recurrent nodes

* Chore: Move some simulator settings to layout

* Chore: Refactor code quality and performance

* Fix: Layout behavior on change

* Chore: Add recenter on layout change

* Fix: Layout change behavior

* Fix: Simulation behavior on data deletion

* Chore: Add recenter on layout change

* Fix: Change layout engine logic (#108)

* Fix: Change layout engine logic

* New: Add simulation cancellation logic

* Chore: Remove leftover code

* Fix: Hierarchical layout recenter logic

* Chore: Refactor package versions and code logic

* Fix: Refactor function types

* New: Add multiselect (#110)

* New: Add multiselect

* Chore: Simplify logic

* Chore: Update package.json

* New: Add SVG export (#111)

* New: Add SVG export

* Chore: Refactor code quality

* New: Add WebGL renderer (#109)

* New: Add WebGL renderer

* New: Add naive WebGL force layout computation

* New: Add WebGL improved node/shape geometry options

* New: Add labels and node images

* Fix: GPU drag behavior

* Chore: Remove leftover comments

* Fix: WebGL renderer style invalidating

* Chore: Refactor code quality

* Chore: Add WebGL example and docs

* New: Add docs page (#112)

* New: Add docs page

* New: Add more docs examples

* Update .github/workflows/docs.yml

---------

Co-authored-by: Toni <toni.lastre@memgraph.io>

* Chore: Finish up the release process

* Chore: Fix sync between package.json files

* Chore: Add new package-lock.json

---------

Co-authored-by: David <davidlozic@gmail.com>
Co-authored-by: Abhinv Singh Parmar <abhi171b010@gmail.com>
Co-authored-by: Abhinav Singh Parmar <abhinavparmar147@gmail.com>
Co-authored-by: Abhinv Singh Parmar <abhinav.parmar@infosys.com>
Co-authored-by: Oleksandr Ichenskyi <55350107+AlexIchenskiy@users.noreply.github.com>
Co-authored-by: AlexIchenskiy <aichenskiy@gmail.com>
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