DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • 5 Simple Tips to Keep Dockerized Apps Secure
  • Moving From Full-Stack Developer To Web3 Pioneer
  • How to Build a Concurrent Chat App With Go and WebSockets
  • How to Build a Pokedex React App with a Slash GraphQL Backend

Trending

  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Traditional Testing and RAGAS: A Hybrid Strategy for Evaluating AI Chatbots
  • Go 1.24+ Native FIPS Support for Easier Compliance
  • Creating a Web Project: Caching for Performance Optimization
  1. DZone
  2. Data Engineering
  3. Data
  4. Creating the Perfect Rounded Edge With D3 Curves

Creating the Perfect Rounded Edge With D3 Curves

D3.js is one of the most popular libraries for data visualization. Read on to learn from an expert how to use his library to programmatically create curved lines.

By 
Swizec Teller user avatar
Swizec Teller
·
Aug. 02, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
13.7K Views

Join the DZone community and get the full member experience.

Join For Free

A coaching client showed me this design and asked: Ok, how do I build this?

Image title

Tree structure with rounded edges

Well hmm... it's a tree of some sort. Each icon is a node, and each line is an edge. Nodes can have multiple children. I think...

You can build this with D3. Calculate each node's position, then iterate and render. An image imported as an SVG component will do. Webpack can handle that for you.

So you have node positions and you know which nodes are connected. Now what?

You can turn that into pairs of coordinates. Point A to point B. With a line between them.

D3 is perfect for that! Drawing lines from A to B is super easy: 

// create a line path generator
const line = d3.line ( ) ;
// pair of coordinates
const data = [ [ 0 , 0 ] , [ 100 , 100 ] ] ;

// draw, using JSX notation
<svg>
  <path d= {line(data) } style= {lineStyle} />
</svg>

That draws a line from point (0, 0) to point (100, 100).

But we don't want straight lines. Straight lines don't look good.

Lucky for us, D3 has ample support for curves. Many different curve formulas to choose from.

We can add some curve to our line with the .curve method.

// create a line path generator
const line2 = d3.line ( ).curve (d3.curveCardinal ) ;

If you try that, you'll see that nothing happens. Curves need multiple points in your line data to work well.

Like this:

That's a nice curve and all, but not quite what we're looking for. And if you look at the curve examples in D3 docs, you'll see that nothing quite fits.

After some experimentation, I found a solution.


A React component that takes two points, injects two just perfectly spaced points, and draws a D3 curve between them.

The poorly named <RoundedCorner> component is just 15 lines of Prettier'd code. All values discovered experimentally.

const RoundedCorner = ( { start, end, radius = 5 } ) => {
  const line = d3
    .line ( )
    .x (d => d[ 0 ] )
    .y (d => d[ 1 ] )
    .curve (d3.curveBundle.beta ( 1 ) ) ;

  const points = [
    start,
    [start[ 0 ] , end[ 1 ] - radius] ,
    [start[ 0 ] + radius, end[ 1 ] ] ,
    end
  ] ;

  return <path d= {line(points) } style= {lineStyle} />;
} ;

We take start and end coordinates, and the desired radius. Similar to CSS rounded borders.

Experimentally, I discovered that for the best results, you have to place two points between the two endpoints. One on each side of the rounded corner you want.

Then you have to use the curveBundle generator with the beta factor set to 1. I honestly don't know what that means but it works.

You can see I tried a few different configurations in the example CodeSandbox. That's because some curves produced weird edges when turned around like that.

But not good ol' curveBundle.

Tree (data structure) Build (game engine) CSS Data (computing) SVG Icon React (JavaScript library) IT

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 5 Simple Tips to Keep Dockerized Apps Secure
  • Moving From Full-Stack Developer To Web3 Pioneer
  • How to Build a Concurrent Chat App With Go and WebSockets
  • How to Build a Pokedex React App with a Slash GraphQL Backend

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!