Thursday, February 5, 2026
HomeLanguagesJavascriptD3.js lineRadial.context() method

D3.js lineRadial.context() method

The d3.lineRadial.context() method lets you render the line in a canvas element’s context. The line will be rendered in the current context when the line generator is invoked. We can set the context of our line on our own using this method like color, stroke, fill, etc. The default value is null.

Syntax:

d3.lineRadial.context(_context);

Parameters: 

  • _context: context set by the user.

Return Value: This method returns the current context.

Example 1: In this example, we will change color, stroke using this method.




<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src=
</script>
<script src="https://d3js.org/d3.v4.min.js"></script
<body>
    <h1 style="text-align: center; color: green;">
         neveropen
    </h1>
  <center>
    <canvas id="gfg" width="200" height="200">   
</canvas>
</center>
    
</body>
<script>
 var data = [
    {angle: 0, radius: 10},
    {angle: 3.14 * .5, radius: 35},
    {angle: 3.14 * .75, radius: 55},
    {angle: 3.14, radius: 60},
    {angle: 3.14 * 1.25, radius: 65},
    {angle: 3.14 * 1.5, radius: 70},
    {angle: 3.14 * 1.75, radius: 75},
    {angle: 3.14 * 2, radius: 80},
    {angle: 3.14 * 2.25, radius: 85},
    {angle: 3.14 * 2.5, radius: 90},
    {angle: 3.14 * 2.75, radius: 95},
    {angle: 3.14 * 3, radius: 100},
    {angle: 3.14 * 3.25, radius: 105},
    {angle: 3.14 * 3.5, radius: 110}
    ];
  
 var context = d3.select("#gfg").node().getContext("2d");
  
 var lineRadialGenerator = d3.lineRadial()
    .angle((d) => d.angle)
    .radius((d) => d.radius)
    .context(context);
  
  
  
 context.translate(100, 100);              
 lineRadialGenerator(data);
 context.strokeStyle = "green";
 context.stroke();
  
</script>   
</html>


Output:

Example 2: In this example, we will change color, fill using this method.




<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src=
</script>
<script src="https://d3js.org/d3.v4.min.js"></script
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  <center>
    <canvas id="gfg" width="200" height="200">   
</canvas>
</center>
    
</body>
<script>
 var data = [
    {angle: 0, radius: 10},
    {angle: 3.14 * .5, radius: 35},
    {angle: 3.14 * .75, radius: 55},
    {angle: 3.14, radius: 60},
    {angle: 3.14 * 1.25, radius: 65},
    {angle: 3.14 * 1.5, radius: 70},
    {angle: 3.14 * 1.75, radius: 75},
    {angle: 3.14 * 2, radius: 80},
    {angle: 3.14 * 2.25, radius: 85},
    {angle: 3.14 * 2.5, radius: 90},
    {angle: 3.14 * 2.75, radius: 95},
    {angle: 3.14 * 3, radius: 100},
    {angle: 3.14 * 3.25, radius: 105},
    {angle: 3.14 * 3.5, radius: 110}
    ];
  
 var context = d3.select("#gfg").node().getContext("2d");
  
 var lineRadialGenerator = d3.lineRadial()
    .angle((d) => d.angle)
    .radius((d) => d.radius)
    .context(context);
  
 context.translate(100, 100);              
 lineRadialGenerator(data);
 context.fillStyle = "green";
 context.fill();
  
</script>   
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32489 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6861 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12071 POSTS0 COMMENTS
Shaida Kate Naidoo
6994 POSTS0 COMMENTS
Ted Musemwa
7234 POSTS0 COMMENTS
Thapelo Manthata
6946 POSTS0 COMMENTS
Umr Jansen
6928 POSTS0 COMMENTS