Elements: Line, Polyline, Polygon, and Path
Explore how to draw and style fundamental SVG elements in D3.js including lines, polylines, polygons, and paths. Understand the attributes needed to position these shapes and use the path mini-language to create complex outlines. This lesson helps you build foundational skills for manipulating graphical elements in interactive visualizations.
Line
A line is a simple line between two points and is described by four required attributes.
x1: The x position of the first end of the line as measured from the left of the screeny1: The y position of the first end of the line as measured from the top of the screenx2: The x position of the second end of the line as measured from the left of the screeny2: The y position of the second end of the line as measured from the top of the screen
The following is an example of the code section required to draw a line. A notable addition to this code is the style declaration. In this case, the line does not have color, and this can be added with the stroke style, which applies color to a line:
This will produce a line as follows: ...