Shapes API in D3.js

A shape can be defined as an object lying on the map, connected to a latitude and longitude coordinate. Some of the various shapes are- Circle, Rectangles, Polygon, and Lines. After the configuration of any shape, you can drag or edit it.

In this chapter, we will explain that how to design or create a shape in D3.js.

Configuration of Shape API

Shapes Generator

D3.js facilitates with the various type to different shapes. Let’s explain each shape in detail.

Arcs API

It can be applied to generate a circle or an annulus shape. The arc API methods can also be used to design the pie charts. Now, we will discuss some famed methods of arc API in detail.

d3.arc() Method

This method allows you to design a new arc generator.

arc.(args) Method

This method can be applied to create an arc according to the arguments. By default, the settings of angle and object radii are explained in the below code.

arc.centroid(args)

It can be applied to find out the midpoint {x, y} of the centerline of the arc according to the given arguments.

You can use this method to fix the inner radius according to the existing radius. It also returns an arc generator. It is shown in the below script.

You can use this method to fix the outer radius according to the existing radius. It also returns an arc generator.

It is shown in the below script.

arc.startAngle([angle])

You can apply it to fix the start angle according to the provided angle.

It can be explained in the below code.

This method can be used to set the radius according to the existing radius, and it will also return an arc generator.

It is defined as follows.

In the above function, the radius corner is higher than 0, then the arc’s corner should be rounded with the help of the provided radius. The radius of the corner can’t be higher than the value of radius (outerRadius – innerRadius) / 2.

arc.endAngle([angle])

You can apply it to fix the end angle according to the provided angle.

It can be explained in the below code.

You can apply it to fix the pad angle according to the provided angle.

It is explained in the below code.

arc.context([context])

This method helps you to set the context and returns the arc generator.

Lines API

The line API method helps you to design the lines. The line API methods can also be used to design the graphs. Now, we will discuss some famed methods of line API in detail.

line(data)

This method can be applied to design a new line by using the data array.

d3.line()

This method helps you to create a new line generator.

line.x([x])

It allows you to fix the x accessor for the defined function and creates a line. You can define it, as shown below.

line.y([y])

It allows you to fix the y accessor for the defined function and creates a line. You can define it, as shown below.

line.defined([defined])

It allows you to fix the defined accessor for the defined function. You can define it as shown in the below script.

line.context([context])

This method can be applied if you want to fix the context and create a line. If the context seems undefined, then it will return an empty value.

line.curve([curve])

It can be applied to design a curve and creates a line.

You can use this method to draw a radial line. This method is similar to the cartesian line generator.

The accessor of this method return the radius and also draw a radial line. It is also far from the origin (0,0).

Pies API

The fundamental use of pie API is to generate a pie generator. Here, we have discussed some famed methods of pie API in detail.

d3.pie()

This method can be applied to design a pie generator containing the default setting.

pie(data[. Arguments])

It can be applied to create a pie to the given array values. This method returns an array object. The object is considered as a datum angle of an arc. Usually, the object contains the below-given properties.

Value: It contains the numeric value of the arc.

Data: It is an input datum. It is a correlated element within the array of input data.

startAngle: It represents the starting angle of the arc.

index: This property of the object contains the index of the arc.

endAngle: It represents the ending angle of the arc.

pie.sort([compare])

This method helps you to sort the data and design a pie. It is explained below.

pie.value([value])

This method can be applied to fix the function value and also create a pie. The function is defined below.

pie.startAngle([angle])

This method can be applied to set the starting angle of the pie for the stated function. If you don’t specify the angle, then it returns the existing started angle. This function is explained as follows.

pie.sortValues([compare])

If you need to compare the value of the current function and design a pie, then you can use the function. It is explained below.

If you are required to fix the value of the pad angle to the stated function and design a pie, then you can use the function. It is explained below.

pie.endAngle([angle])

This method can be applied to set the ending angle of the pie for the stated function. If you don’t specify the angle, then it returns the existing end angle. This function is explained as follows.

Example

Here, we have an illustration of the methods above-mentioned.

Output

After the execution of the above code, you got the following code.