Open In App

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: 

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;">
         GeeksforGeeks
    </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;">
        GeeksforGeeks
    </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:


Article Tags :