SVG Shapes - zziuni/d3 GitHub Wiki
Wiki โธ API Reference โธ SVG โธ SVG Shapes
SVG has a number of built-in simple shapes, such as axis-aligned rectangles and circles. For greater flexibility, you can use SVG's path element in conjunction with D3's path data generators. If you're familiar with Protovis, you'll note that D3's path generators are similar to Protovis marks.
A shape generator, such as that returned by d3.svg.arc, is both an object and a function. That is: you can call the shape like any other function, and the shape has additional methods that change its behavior. Like other classes in D3, shapes follow the method chaining pattern where setter methods return the shape itself, allowing multiple setters to be invoked in a concise statement.
All SVG shapes can be transformed using the transform attribute. You can apply the transform either to the shape directly, or to a containing g element. Thus, when a shape is defined as "axis-aligned", that merely means axis-aligned within the local coordinate system; you can still rotate and otherwise transform the shape. Shapes can be filled and stroked using the fill and stroke styles. (You can also use the attributes of the same name, but styles are recommended as they are compatible with external stylesheets.)
# svg:rect x="0" y="0" width="0" height="0" rx="0" ry="0"
The rect element defines an axis-aligned rectangle. The top-left corner of the rectangle is positioned using the x and y attributes, while its size is specified using width and height. A rounded rectangle can be produced using the optional rx and ry attributes.
# svg:circle cx="0" cy="0" r="0"
The circle element defines a circle based on a center point and a radius. The center is positioned using the cx and cy attributes, while the radius is specified using the r attribute.
# svg:ellipse cx="0" cy="0" rx="0" ry="0"
The ellipse element defines an axis-aligned ellipse based on a center point and two radii. The center is positioned using the cx and cy attributes, while the radii are specified using the rx and ry attributes.
# svg:line x1="0" y1="0" x2="0" y2="0"
The line element defines a line segment that starts at one point and ends at another. The first point is specified using the x1 and y1 attributes, while the second point is specified using the x2 and y2 attributes. The line element is a popular choice for drawing rules, reference lines, axes and tick marks.
# svg:polyline points=""
The polyline element defines a set of connected straight line segments. Typically, polyline elements define open shapes. The points that make up the polyline are specified using the points attribute. Note: in D3, it is typically more convenient and flexible to use the d3.svg.line path generator in conjunction with a path element.
# svg:polygon points=""
The polygon element defines a closed shape consisting of a set of connected straight line segments. The points that make up the polygon are specified using the points attribute. Note: in D3, it is typically more convenient and flexible to use the d3.svg.line path generator in conjunction with a path element. The line can be closed using the closepath "Z" command.
# svg:text x="0" y="0" dx="0" dy="0" text-anchor="start"
The text element defines a graphics element consisting of text. The text content of the text element (see the text operator) define the characters to be rendered. The anchor position of the text element is controlled using the x and y attributes; additionally, the text can be offset from the anchor using dx and dy attributes. This offset is particularly convenient for controlling the text margin and baseline, as you can use "em" units which are relative to the font size. The horizontal text alignment is controlling using the text-anchor attribute. Here are a few examples:
<svg:text text-anchor="start">left-align, bottom-baseline</svg:text>
<svg:text text-anchor="middle">center-align, bottom-baseline</svg:text>
<svg:text text-anchor="end">right-align, bottom-baseline</svg:text>
<svg:text dy=".35em" text-anchor="start">left-align, middle-baseline</svg:text>
<svg:text dy=".35em" text-anchor="middle">center-align, middle-baseline</svg:text>
<svg:text dy=".35em" text-anchor="end">right-align, middle-baseline</svg:text>
<svg:text dy=".71em" text-anchor="start">left-align, top-baseline</svg:text>
<svg:text dy=".71em" text-anchor="middle">center-align, top-baseline</svg:text>
<svg:text dy=".71em" text-anchor="end">right-align, top-baseline</svg:text>
It's possible that there is a better way to specify the text baseline using SVG's baseline alignment properties, but these don't seem to be widely supported by browsers. Lastly, the font color is typically specified using the fill style (you can also use stroke), and the font is controlled using the font, font-family, font-size and related styles. Some browsers also support CSS3 properties, such as text-shadow.
# svg:path d="" transform=""
The path element represents the outline of a shape which can be filled, stroked, used as a clipping path, or any combination of the three. The d attribute defines the path data, which is a mini-language of path commands, such as moveto (M), lineto (L) and closepath (Z). The path element is a generalization of all other shapes in SVG, and can be used to draw nearly anything!
To simplify the construction of the d attribute for path elements, D3 includes a number of helper classes for generating path data. If you're familiar with Protovis, you'll find that these path generators are similar to Protovis mark types: each generator is a function of data. So, if your data is a sequence of xy coordinates, you can define accessor functions that the path generators use to produce path data. For example, you might define a line generator:
var line = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("basis");
Then later on, you can use this function to set the d attribute:
g.append("svg:path")
.attr("d", line);
Whatever data is bound to g
(in this example) will be passed to the line
instance. Thus, the data must be specified as an array. For every element in the data array, the x- and y-accessor functions are used to pull out the control point coordinates.
A path generator, such as that returned by d3.svg.line, is both an object and a function. That is: you can call the generator like any other function, and the generator has additional methods that change its behavior. Like other classes in D3, path generators follow the method chaining pattern where setter methods return the generator itself, allowing multiple setters to be invoked in a concise statement.
# d3.svg.line()
x, y ์ ๊ทผ์ ํจ์์ ์ ํ interpolation์ ๊ธฐ๋ณธ์ผ๋ก ๊ฐ์ง ๋ผ์ธ ์ ๋ค๋ ์ดํฐ๋ฅผ ์์ฑํ๋ค. (์์๊ฐ ๋๊ฐ์ธ ์ซ์ ๋ฐฐ์ด์ ์ ๋ ฅ ๋ฐ์ดํฐ๋ก ์ทจํ๋ค. ์์ธํ๊ฑด ์๋๋ฅผ ๋ณด์.) ๋ฐํ๋ ํจ์๋ ๊บฝ์์ ๊ณก์ (piecewise linear curve)์ด๋ ํด๋ฆฌ๋ผ์ธ(polyline)์ ์ํ ํจ์ค ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ค. ๋ค์๊ณผ ๊ฐ์ ๋ผ์ธ ์ฐจํธ์ฒ๋ผ.
interpolation๋ฅผ ๋ณ๊ฒฝํจ์ผ๋ก spline๊ณผ step ํจ์๋ฅผ ์์ฑํ ์๋ ์๋ค. ๋ํ, line() ๋ช ๋ น ๋ค์ ํจ์ค ๋ช ๋ น์ด๋ฅผ ์ถ๊ฐํ ์ ์๋ค. ์๋ฅผ ๋ค์ด. ๋ซํ ํจ์ค๋ฅผ ์์ฑํ๊ธฐ๋ฅผ ์ํ๋ฉด ๋ซํ ํจ์ค ๋ช ๋ น์ด(Z)๋ฅผ ์ถ๊ฐํ๋ค.
g.append("svg:path")
.attr("d", function(d) { return line(d) + "Z"; });
line ์ ๋ค๋ ์ดํฐ๋ area ์ ๋ค๋ ์ดํฐ์ ๊ฒฐํฉํด์ ๋์ํ๋๋ก ์ค๊ณ๋์๋ค. ์๋ฅผ ๋ค์ด, area ์ฐจํธ๋ฅผ ์ ์ํ ๋, fill
์คํ์ผ์ด ์๋ area ์ ๋ค๋ ์ดํฐ์ ์์ญ์ ์์ ์ ๊ฐ์กฐํ๊ธฐ ์ํ stroke
์คํ์ผ์ ์ค line ์ ๋ค๋ ์ดํฐ๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ค. line ์ ๋ค๋ ์ดํฐ๋ d ์์ฑ์ ์ง์ ํ๊ธฐ ์ํด์๋ง ์ฌ์ฉ๋๋ฏ๋ก fill, stroke, stroke-width๊ฐ์ SVG ํ์ค ์คํ์ผ๊ณผ ์์ฑ์ ์ฌ์ฉํ๋ ๋ผ์ธ์ ์ธํ์ ์กฐ์ ํ ์ ์๋ค.
# line(data[, index])
์ง์ ํ data ๋ฐฐ์ด์ ์๋ก๋ก ํจ์ค ๋ฐ์ดํฐ ๋ฌธ์์ด์ ๋ฐํํ๋ค. ์ต์ ์ธ index๋ line์ ์ ๊ทผ์ ํจ์๋ฅผ ํตํด์ ์ ๋ฌ๋์ด ์ง์ ๋๋ค.
# line.x([x])
x ์ธ์๋ฅผ ๋๊ธฐ๋ฉด ๋๊ธด ํจ์๋ ์์๊ฐ x-์ ๊ทผ์๊ฐ ๋๋ค. ์ธ์๋ฅผ ๋๊ธฐ์ง ์์ผ๋ฉด ํ์ฌ x-์ ๊ธ์๋ฅผ ๋ฐํํ๋ค. ์ด ์ ๊ทผ์๋ ๋ผ์ธ ์ ๋ค๋ ์ดํฐ๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ ๋ฐฐ์ด์์ ๊ฐ ์์์ ๋ํด์ ํธ์ถ๋๋ค. ๊ธฐ๋ณธ ์ ๊ทผ์๋ ์ ๋ ฅ ์์๊ฐ ์ซ์ ์์ ๋๊ฐ์ ๋ฐฐ์ด๋ก ๊ฐ์ ํ๋ค.
function x(d) {
return d[0];
}
ํ์ง๋ง ์ผ๋ฐ์ ์ผ๋ก x-์ ๊ทผ์๋ ๊ธฐ๋ณธ ์ ๊ทผ์๋ฅผ ์ฐ์ง ์๊ณ ๊ฐ๋ฐ์๊ฐ ์ง์ ํ๋ค. ์๋ํ๋ฉด ์
๋ ฅ ๋ฐ์ดํฐ๊ฐ ์ถ๋ ฅ๊ณผ ํํ๊ฐ ๋ค๋ฅด๊ฑฐ๋, scale๋ฅผ ์ ์ฉํ๊ธธ ์ํ๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฅผ ๋ค์ด ๋ฐ์ดํฐ๊ฐ ํํ(tuple)์ด ์๋ x
, y
์์ฑ์ด ์๋ ๊ฐ์ฒด๋ผ๋ฉด ๊ฐ๋ฐ์๋ ๊ทธ ์์ฑ์ ์ญ์ฐธ์กฐํจ๊ณผ ๋์์ ๊ทธ ์ค์ผ์ผ์ ์ ์ฉํ ๊ฒ์ด๋ค.
var x = d3.scale.linear().range([0, w]),
y = d3.scale.linear().range([h, 0]);
var line = d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
x-์ ๊ทผ์๋ ๊ฐ์ ์ํ ๋ค๋ฅธ D3 ํจ์์ ๋์ผํ ๋ฐฉ์์ผ๋ก ํธ์ถ๋๋ค. this ์ปจํ ์คํธ๋ ์ ํ๋ฌผ์์ ํ์ฌ ์์์ด๋ค. (์ผ๋ฐ์ ์ผ๋ก ๊ทธ line ํจ์๋ฅผ ํธ์ถํ๋ this์ปจํ ์คํธ์ ๊ฐ์ง๋ง ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ line ์ ๋ค๋ ์ดํฐ๋ attr ์คํผ๋ ์ดํฐ๋ก ์ ๋ฌ๋๋ค. ๊ทธ๋์ this ์ปจํ ์คํธ๋ ์ฐ๊ด๋ DOM ์์๊ฐ ๋๋ค. ) ํจ์๋ ์ ๋ฌ์ธ์๊ฐ ๋ ๊ฐ๋ค. ํ์ฌ dataum(d)์ ํ์ฌ index(i)๋ค. ์ด ์ปจํ ์คํธ(x-์ ๊ทผ์ ํจ์ ๋ด๋ถ)์์๋ index๊ฐ ์ ํ๋ฌผ์ ํ์ฌ ์์ ์ธ๋ฑ์ค๊ฐ ์๋๋ผ ๋ผ์ธ์ ์ ์ด ์ง์ ์ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ค. x-์ ๊ทผ์๋ data ๋ฐฐ์ด์ ์ง์ ๋ ์์๋๋ก datum๋น ์ ํํ ํ๋ฒ์ฉ ํธ์ถ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ์์ฑ์ ๊ฐ์ ๋น๊ฒฐ์ ์ ์ ๊ทผ์๋ฅผ ์ง์ ํ๋๊ฒ ๊ฐ๋ฅํ๋ค. ๋ํ ํจ์๊ฐ ์๋ ์์๋ก x-์ ๊ทผ์๋ฅผ ์ง์ ํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค. ๊ทธ๋ฌ๋ฉด ๋ชจ๋ ์ง์ ์ด ๊ฐ์ x ์ขํ๋ฅผ ๊ฐ๊ฒ ๋๋ค.
# line.y([y])
y ์ ๋ฌ์ธ์๋ฅผ ๋๊ธฐ๋ฉด, ๊ทธ ํจ์๋ ์์๋ก y-์ ๊ทผ์๋ฅผ ์ง์ ํ๋ค. ์ธ์๋ฅผ ๋๊ธฐ์ง ์์ผ๋ฉด ํ์ y-์ ๊ทผ์๋ฅผ ๋ฐํํ๋ค. ์ด ์ ๊ทผ์๋ line ์ ๋ค๋ ์ดํฐ๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ ๋ฐฐ์ด์์ ๊ฐ๋ณ ์์๋ณ๋ก ํธ์ถ๋๋ค. ๊ธฐ๋ณธ ์ ๊ทผ์๋ ์ ๋ ฅ ์์๊ฐ ์ซ์ ์์ ๋๊ฐ์ ๋ฐฐ์ด๋ก ๊ฐ์ ํ๋ค.
function y(d) {
return d[1];
}
y ์ ๋ฌ์ธ์๋ฅผ ์ง์ ํ๋ ์์ ๋ x ์ ๊ทผ์์ ์ ์ฌํด ๋ณด์ธ๋ค. ๋ค๋ฅธ์ ์ ๋ค๋ฅธ ๊ทธ๋ํฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฒ๋ผ SVG๋ ์์ ์ง์ ์ผ๋ก ์๋จ ์ข์ธก ๋ชจ์๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค๋ ์ ์ด๋ค. ๊ทธ๋์ y ๊ฐ์ด ๋์ผ๋ฉด ํ๋ฉด์์ ์๋์ชฝ์ ์์นํ๋ค. ์๊ฐํ(์ฐจํธ)๋ฅผ ์ํด์๋ ํ๋จ ์ข์ธก ๋ชจ์๋ฆฌ๋ฅผ ์์์ ์ผ๋ก ํ ํ์๊ฐ ์๋ค. ์ด๋ฅผ ์ํ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ range([0, h])
๋์ ์ range([h, 0])
์ ์ฌ์ฉํด์ y-์ค์ผ์ผ์ ๋ฒ์๋ฅผ ์ญ์น ์ํค๋ ๊ฒ์ด๋ค.
# line.interpolate([interpolate])
If interpolate is specified, sets the interpolation mode to the specified string. If interpolate is not specified, returns the current interpolation mode. The following modes are supported:
- linear - piecewise linear segments, as in a polyline.
- step-before - alternate between vertical and horizontal segments, as in a step function.
- step-after - alternate between horizontal and vertical segments, as in a step function.
- basis - a B-spline, with control point duplication on the ends.
- basis-open - an open B-spline; may not intersect the start or end.
- basis-closed - a closed B-spline, as in a loop.
- bundle - equivalent to basis, except the tension parameter is used to straighten the spline.
- cardinal - a Cardinal spline, with control point duplication on the ends.
- cardinal-open - an open Cardinal spline; may not intersect the start or end, but will intersect other control points.
- cardinal-closed - a closed Cardinal spline, as in a loop.
- monotone - cubic interpolation that preserves monotonicity in y.
The behavior of some of these interpolation modes may be further customized by specifying a tension.
# line.tension([tension])
If tension is specified, sets the Cardinal spline interpolation tension to the specified number in the range [0, 1]. If tension is not specified, returns the current tension. The tension only affects the Cardinal interpolation modes: cardinal, cardinal-open and cardinal-closed. The default tension is 0.7. In some sense, this can be interpreted as the length of the tangent; 1 will yield all zero tangents, and 0 yields a Catmull-Rom spline.
Note that the tension must be specified as a constant, rather than a function, as it is constant for the entirety of the line. However, it is still possible to generate multiple lines with different tensions using the same generator. For example:
svg.selectAll("path")
.data([0, 0.2, 0.4, 0.6, 0.8, 1])
.enter().append("svg:path")
.attr("d", function(d) { return line.tension(d)(data); });
In this example (see the live version), the tension is set before each invocation of the line generator, thus resulting in lines with the same data but different paths.
# line.defined([defined])
Gets or sets or sets the accessor function that controls where the line is defined. If defined is specified, sets the new accessor function and returns the line. If defined is not specified, returns the current accessor which defaults to function() { return true; }
. The defined accessor can be used to define where the line is defined and undefined, which is typically useful in conjunction with missing data; the generated path data will automatically be broken into multiple distinct subpaths, skipping undefined data. For example, if you want to ignore y-values that are not a number (or undefined), you can say:
line.defined(function(d) { return !isNaN(d[1]); });
# d3.svg.line.radial()
Constructs a new radial line generator with the default radius- and angle-accessor functions (that assume the input data is a two-element array of numbers; see below for details), and linear interpolation. The returned function generates path data for an open piecewise linear curve, or polyline, as with the Cartesian line generator.
# line(data[, index])
Returns the path data string for the specified array of data elements. An optional index may be specified, which is passed through to the line's accessor functions.
# line.radius([radius])
If radius is specified, sets the radius-accessor to the specified function or constant. If radius is not specified, returns the current radius-accessor. This accessor is invoked for each element in the data array passed to the line generator. The default accessor assumes that each input element is a two-element array of numbers:
function radius(d) {
return d[0];
}
This method is a transformation of the Cartesian line.x method.
# line.angle([angle])
If angle is specified, sets the angle-accessor to the specified function or constant in radians. If angle is not specified, returns the current angle-accessor. This accessor is invoked for each element in the data array passed to the line generator. The default accessor assumes that each input element is a two-element array of numbers:
function angle(d) {
return d[1];
}
This method is a transformation of the Cartesian line.y method.
# d3.svg.area()
๊ธฐ๋ณธ๊ฐ์ผ๋ก x-, y0-, y1-์ ๊ทผ์ ํจ์(์์๊ฐ ๋๊ฐ์ธ ์ซ์ ๋ฐฐ์ด์ ์ ๋ ฅ ๋ฐ์ดํฐ๋ก ์ทจํ๋ค. ์์ธํ๊ฑด ์๋๋ฅผ ๋ณด์.)์ linear interpolation๋ฅผ ๊ฐ์ง area ์ ๋ค๋ ์ดํฐ๋ฅผ ์๋ก ์์ฑํ๋ค. ๋ฐํ๋ ํจ์๋ ๊บพ์์ ๊ณก์ (piecewise linear curve)์ด๋ ํด๋ฆฌ๋ผ์ธ(polyline)์ ์ํ ํจ์ค ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ค. ๋ค์๊ณผ ๊ฐ์ ์์ญ ์ฐจํธ์ฒ๋ผ.
์ด๋ก ์ ์ผ๋ก ํด๋ฆฌ๊ณค์ ๋๊ฐ์ lines๋ฅผ ์ฌ์ฉํด ํ์ฑํ๋ค. ํ๋ผ์ธ์ x-๊ณผ y1์ ๊ทผ์ ํจ์๋ฅผ ์ฌ์ฉํด์ ํ์ฑ๋๊ณ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์งํ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฐ๋ฅ ๋ผ์ธ์ด ์ด ๋ผ์ธ์ ์ถ๊ฐ๋๋๋ฐ x-๊ณผ y0-์ ๊ทผ์๋ฅผ ์ฌ์ฉํ๊ณ ์ค๋ฅธ์ชฝ์์ ์ผ์ชฝ์ผ๋ก ์งํํ๋ค. 90ห๋ก ํจ์ค ์์๋ฅผ ํ์ ํ๊ธฐ ์ํด transform ์์ฑ์ ์ง์ ํด์ ์์ง ์์ญ๋ฅผ ์์ฑํ ์๋ ์๋ค. interpolation์ ๋ณ๊ฒฝํด์ spline๊ณผ stepํจ์๋ฅผ ์์ฑํ ์๋ ์๋ค.
area ์ ๋ค๋ ์ดํฐ๋ line ์ ๋ค๋ ์ดํฐ์ ๊ฒฐํฉํด์ ๋์ํ๋๋ก ์ค๊ณ๋์๋ค. ์๋ฅผ ๋ค์ด. area ์ฐจํธ๋ฅผ ์ ์ํ ๋, fill
์คํ์ผ์ ์ ์ฉํ area ์ ๋ค๋ ์ดํฐ์ ์์ญ์ ์์ ์ ๊ฐ์กฐํ๊ธฐ ์ํ stroke ์คํ์ผ ์ ์ฉ์ ํ line ์ ๋ค๋ ์ดํฐ๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ค. area ์ ๋ค๋ ์ดํฐ๋ d ์์ฑ์ ์ง์ ํ๊ธฐ ์ํด์๋ง ์ฌ์ฉ๋๋ฏ๋ก, fill๊ฐ์ SVG ํ์ค ์คํ์ผ๊ณผ ์์ฑ์ ์ฌ์ฉํ๋ ๋ผ์ธ์ ์ธํ์ ์กฐ์ ํ ์ ์๋ค.
streamgraphs( ์ ์ผ๋ก ๊ตฌ๋ถ๋ ์์ญ ์ฐจํธ )๋ฅผ ๋ง๋ค๊ธฐ ์ํด์๋ stack ๋ ์ด์์์ ์ฌ์ฉํ๋ค. ์ด ๋ ์ด์์์ ํ ์ค๋ฆฌ์ฆ์ ๊ฐ ๊ฐ์ ๋ํด์ y0 ์์ฑ์ ์ง์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ y0-์ y1-์ ๊ทผ์์์ ์ฌ์ฉ๋ ์ ์๋ค. ์ฃผ์ํ ์ ์ ๊ฐ ์๋ฆฌ์ฆ๋ ์๋ฆฌ์ฆ๋ณ ๊ฐ์ด ๋์ผ ๊ฐ์ด์ฌ์ผ๋ง ํ๋ค๋ ์ฌ์ค์ด๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ ๊ฐ์ ๋์ผ ์ขํ์ฌ์ผ ํ๋ค. ๋ง์ฝ ๋ฐ์ดํฐ๊ฐ ์ ์ค๋๊ฑฐ๋ ์๋ฆฌ์ฆ์ x์ขํ๊ฐ ์ผ์นํ์ง ์์ผ๋ฉด ๋ ์ด์์์ ์ ํ๊ธฐ์ ์ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ์ง๊ณํ๊ณ ๋ณด์ ํด์ผ ํ๋ค.
# area(data[, index])
์ง์ ํ data๋ฐฐ์ด์ ์์๋ก ํจ์ค ๋ฐ์ดํฐ ๋ฌธ์์ด์ ๋ฐํํ๋ค. ์ต์ ์ธ index๋ area์ ์ ๊ทผ์ ํจ์๋ฅผ ํตํด์ ์ ๋ฌ๋์ด ์ง์ ๋๋ค.
# area.x([x])
x ์ธ์๋ฅผ ๋๊ธฐ๋ฉด ๋๊ธด ํจ์๋ ์์๊ฐ x-์ ๊ทผ์๊ฐ ๋๋ค. ์ธ์๋ฅผ ๋๊ธฐ์ง ์์ผ๋ฉด ํ์ฌ x-์ ๊ธ์๋ฅผ ๋ฐํํ๋ค. ์ด ์ ๊ทผ์๋ area ์ ๋ค๋ ์ดํฐ๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ ๋ฐฐ์ด์์ ๊ฐ ์์์ ๋ํด์ ํธ์ถ๋๋ค. ๊ธฐ๋ณธ ์ ๊ทผ์๋ ์ ๋ ฅ ์์๊ฐ ์ซ์ ์์ ๋๊ฐ์ ๋ฐฐ์ด๋ก ๊ฐ์ ํ๋ค.
function x(d) {
return d[0];
}
ํ์ง๋ง ์ผ๋ฐ์ ์ผ๋ก x-์ ๊ทผ์๋ ๊ธฐ๋ณธ ์ ๊ทผ์๋ฅผ ์ฐ์ง ์๊ณ ๊ฐ๋ฐ์๊ฐ ์ง์ ํ๋ค. ์๋ํ๋ฉด ์
๋ ฅ ๋ฐ์ดํฐ๊ฐ ์ถ๋ ฅ๊ณผ ํํ๊ฐ ๋ค๋ฅด๊ฑฐ๋, scale๋ฅผ ์ ์ฉํ๊ธธ ์ํ๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฅผ ๋ค์ด ๋ฐ์ดํฐ๊ฐ ํํ(tuple)์ด ์๋ x
, y
์์ฑ์ด ์๋ ๊ฐ์ฒด๋ผ๋ฉด ๊ฐ๋ฐ์๋ ๊ทธ ์์ฑ์ ์ญ์ฐธ์กฐํจ๊ณผ ๋์์ ๊ทธ ์ค์ผ์ผ์ ์ ์ฉํ ๊ฒ์ด๋ค.
var x = d3.scale.linear().range([0, w]),
y = d3.scale.linear().range([h, 0]);
var area = d3.svg.area()
.x(function(d) { return x(d.x); })
.y0(h)
.y1(function(d) { return y(d.y); });
x-์ ๊ทผ์๋ ๊ฐ์ ์ํ ๋ค๋ฅธ D3 ํจ์์ ๋์ผํ ๋ฐฉ์์ผ๋ก ํธ์ถ๋๋ค. this ์ปจํ ์คํธ๋ ์ ํ๋ฌผ์์ ํ์ฌ ์์์ด๋ค. (์ผ๋ฐ์ ์ผ๋ก ๊ทธ line ํจ์๋ฅผ ํธ์ถํ๋ this์ปจํ ์คํธ์ ๊ฐ์ง๋ง ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ area ์ ๋ค๋ ์ดํฐ๋ attr ์คํผ๋ ์ดํฐ๋ก ์ ๋ฌ๋๋ค. ๊ทธ๋์ this ์ปจํ ์คํธ๋ ์ฐ๊ด๋ DOM ์์๊ฐ ๋๋ค. ) ํจ์๋ ์ ๋ฌ์ธ์๊ฐ ๋ ๊ฐ๋ค. ํ์ฌ dataum(d)์ ํ์ฌ index(i)๋ค. ์ด ์ปจํ ์คํธ(x-์ ๊ทผ์ ํจ์ ๋ด๋ถ)์์๋ index๊ฐ ์ ํ๋ฌผ์ ํ์ฌ ์์ ์ธ๋ฑ์ค๊ฐ ์๋๋ผ ๋ผ์ธ์ ์ ์ด ์ง์ ์ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ค. x-์ ๊ธ์๋ data ๋ฐฐ์ด์ ์ง์ ๋ ์์๋๋ก datum๋น ์ ํํ ํ๋ฒ์ฉ ํธ์ถ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก ๋์ ์์ฑ์ ๊ฐ์ ๋น๊ฒฐ์ ์ ์ ๊ทผ์๋ฅผ ์ง์ ํ๋๊ฒ ๊ฐ๋ฅํ๋ค. ๋ํ ํจ์๊ฐ ์๋ ์์๋ก x-์ ๊ทผ์๋ฅผ ์ง์ ํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค. ๊ทธ๋ฌ๋ฉด ๋ชจ๋ ์ง์ ์ด ๊ฐ์ x ์ขํ๋ฅผ ๊ฐ๊ฒ ๋๋ค.
# area.x0([x0])
โฆ
# area.x1([x1])
โฆ
# area.y([y])
โฆ
# area.y0([y0])
y0 ์ธ์๋ฅผ ๋๊ธฐ๋ฉด ๋๊ธด ํจ์๋ ์์๊ฐ y0-์ ๊ทผ์๊ฐ ๋๋ค. ์ธ์๋ฅผ ๋๊ธฐ์ง ์์ผ๋ฉด ํ์ฌ y0-์ ๊ทผ์๋ฅผ ๋ฐํํ๋ค. ์ด ์ ๊ทผ์๋ area ์ ๋ค๋ ์ดํฐ๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ ๋ฐฐ์ด์์ ๊ฐ ์์์ ๋ํด์ ํธ์ถ๋๋ค. ๊ธฐ๋ณธ ์ ๊ทผ์๋ ์์ 0์ด๊ณ ๊ทธ๋ฌ๋ฉด y = 0 ์ผ๋ก ๊ณ ์ ๋ ๊ธฐ์ค์ ์ ์ฌ์ฉํ๋ค. y0-์ ๊ทผ์๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ์ ์ฌํ x ์ ๊ทผ์ ๋ด์ฉ์ ์ฐธ๊ณ ํ๋ผ.
# area.y1([y1])
y1 ์ธ์๋ฅผ ๋๊ธฐ๋ฉด ๋๊ธด ํจ์๋ ์์๊ฐ y1-์ ๊ทผ์๊ฐ ๋๋ค. ์ธ์๋ฅผ ๋๊ธฐ์ง ์์ผ๋ฉด ํ์ฌ y1-์ ๊ทผ์๋ฅผ ๋ฐํํ๋ค. ์ด ์ ๊ทผ์๋ area ์ ๋ค๋ ์ดํฐ๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ ๋ฐฐ์ด์์ ๊ฐ ์์์ ๋ํด์ ํธ์ถ๋๋ค. ๊ธฐ๋ณธ ์ ๊ทผ์๋ ์ ๋ ฅ ์์๋ฅผ ์ซ์ ์์ ๋๊ฐ์ ๋ฐฐ์ด๋ก ๊ฐ์ ํ๋ค.
function y1(d) {
return d[1];
}
y1-์ ๊ทผ์๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ์ ์ฌํ x ์ ๊ทผ์ ๋ด์ฉ์ ์ฐธ๊ณ ํ๋ผ. ๋ค๋ฅธ ๊ทธ๋ํฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฒ๋ผ SVG๋ ์์ ์ง์ ์ผ๋ก ์๋จ ์ข์ธก ๋ชจ์๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค. ๊ทธ๋์ y๊ฐ์ด ๋์ผ๋ฉด ํ๋ฉด์์ ์๋์ชฝ์ ์์นํ๋ค. ์๊ฐํ(์ฐจํธ)๋ฅผ ์ํด์๋ ํ๋จ ์ข์ธก ๋ชจ์๋ฆฌ๋ฅผ ์์์ ์ผ๋ก ํ ํ์๊ฐ ์๋ค. ์ด๋ฅผ ์ํ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ range([0, h])
๋์ ์ range([h, 0])
์ ์ฌ์ฉํด์ y-์ค์ผ์ผ์ ๋ฒ์๋ฅผ ์ญ์น ์ํค๋ ๊ฒ์ด๋ค.
# area.interpolate([interpolate])
If interpolate is specified, sets the interpolation mode to the specified string. If interpolate is not specified, returns the current interpolation mode. The following modes are supported:
- linear - piecewise linear segments, as in a polyline.
- step-before - alternate between vertical and horizontal segments, as in a step function.
- step-after - alternate between horizontal and vertical segments, as in a step function.
- basis - a B-spline, with control point duplication on the ends.
- basis-open - an open B-spline; may not intersect the start or end.
- cardinal - a Cardinal spline, with control point duplication on the ends.
- cardinal-open - an open Cardinal spline; may not intersect the start or end, but will intersect other control points.
- monotone - cubic interpolation that preserves monotonicity in y.
The behavior of some of these interpolation modes may be further customized by specifying a tension. Technically, the basis-closed and cardinal-closed interpolation modes are also supported, but these make more sense in the context of a line rather than an area.
# area.tension([tension])
If tension is specified, sets the Cardinal spline interpolation tension to the specified number in the range [0, 1]. If tension is not specified, returns the current tension. The tension only affects the Cardinal interpolation modes: cardinal, cardinal-open and cardinal-closed. The default tension is 0.7. In some sense, this can be interpreted as the length of the tangent; 1 will yield all zero tangents, and 0 yields a Catmull-Rom spline. Note that the tension must be specified as a constant, rather than a function, as it is constant for the entirety of the area.
# area.defined([defined])
Gets or sets or sets the accessor function that controls where the area is defined. If defined is specified, sets the new accessor function and returns the area. If defined is not specified, returns the current accessor which defaults to function() { return true; }
. The defined accessor can be used to define where the area is defined and undefined, which is typically useful in conjunction with missing data; the generated path data will automatically be broken into multiple distinct subpaths, skipping undefined data. For example, if you want to ignore y-values that are not a number (or undefined), you can say:
area.defined(function(d) { return !isNaN(d[1]); });
# d3.svg.area.radial()
โฆ
# area(data[, index])
Returns the path data string for the specified array of data elements. An optional index may be specified, which is passed through to the area's accessor functions.
# area.radius([radius])
โฆ
# area.innerRadius([radius])
โฆ
# area.innerRadius([radius])
โฆ
# area.angle([angle])
โฆ
# area.startAngle([angle])
โฆ
# area.endAngle([angle])
โฆ
# d3.svg.arc()
Constructs a new arc generator with the default innerRadius-, outerRadius-, startAngle- and endAngle-accessor functions (that assume the input data is an object with named attributes matching the accessors; see below for details). While the default accessors assume that the arc dimensions are all specified dynamically, it is very common to set one or more of the dimensions as a constant, such as setting the inner radius to zero for a pie chart. The returned function generates path data for a closed solid arc, as in a pie or donut chart:
In fact, four forms are possible: a circle (when the inner radius is zero and the angular span is greater than or equal to 2ฯ), a circular sector (when the inner radius is zero and the angular span is less than 2ฯ), an [annulus](http://en.wikipedia.org/wiki/Annulus_(mathematics\)) (when the inner radius is non-zero and the angular span is greater than or equal to 2ฯ), and an annular sector (when the inner radius is non-zero and the angular span is less than 2ฯ).
# arc(datum[, index])
Returns the path data string for the specified datum. An optional index may be specified, which is passed through to the arc's accessor functions.
# arc.innerRadius([radius])
If radius is specified, sets the innerRadius-accessor to the specified function or constant. If radius is not specified, returns the current innerRadius-accessor. This accessor is invoked on the argument passed to the arc generator. The default accessor assumes that the input data is an object with suitably-named attributes:
function innerRadius(d) {
return d.innerRadius;
}
Typically, a innerRadius-accessor is specified because the input data is in a different format, because you want to apply a scale, or because you want to specify a constant inner radius for a donut chart.
The innerRadius-accessor is invoked in the same manner as other value functions in D3. The this context of the function is the current element in the selection. (Technically, the same this context that invokes the arc function; however, in the common case that the arc generator is passed to the attr operator, the this context will be the associated DOM element.) The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the innerRadius-accessor as a constant rather than a function.
# arc.outerRadius([radius])
If radius is specified, sets the outerRadius-accessor to the specified function or constant. If radius is not specified, returns the current outerRadius-accessor. This accessor is invoked on the argument passed to the arc generator. The default accessor assumes that the input data is an object with suitably-named attributes:
function outerRadius(d) {
return d.outerRadius;
}
Typically, a outerRadius-accessor is specified because the input data is in a different format, because you want to apply a scale, or because you want to specify a constant inner radius for a donut chart.
The outerRadius-accessor is invoked in the same manner as other value functions in D3. The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the outerRadius-accessor as a constant rather than a function.
# arc.startAngle([angle])
If angle is specified, sets the startAngle-accessor to the specified function or constant. If angle is not specified, returns the current startAngle-accessor. Angles are specified in radians, even though SVG typically uses degrees. This accessor is invoked on the argument passed to the arc generator. The default accessor assumes that the input data is an object with suitably-named attributes:
function startAngle(d) {
return d.startAngle;
}
For constructing pie or donut charts, you will need to compute the start angle of each arc as the end angle of the previous arc. This can be done very conveniently using the pie layout, which is similar to the stack layout; given a set of input data, the pie layout will construct arc objects with startAngle and endAngle attributes that you can use with the default arc accessors.
The startAngle-accessor is invoked in the same manner as other value functions in D3. The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the startAngle-accessor as a constant rather than a function.
# arc.endAngle([angle])
If angle is specified, sets the endAngle-accessor to the specified function or constant. If angle is not specified, returns the current startAngle-accessor. Angles are specified in radians, even though SVG typically uses degrees. This accessor is invoked on the argument passed to the arc generator. The default accessor assumes that the input data is an object with suitably-named attributes:
function endAngle(d) {
return d.endAngle;
}
For constructing pie or donut charts, you will need to compute the end angle of each arc as offset from the start angle. This can be done very conveniently using the pie layout, which is similar to the stack layout; given a set of input data, the pie layout will construct arc objects with startAngle and endAngle attributes that you can use with the default arc accessors.
The endAngle-accessor is invoked in the same manner as other value functions in D3. The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the endAngle-accessor as a constant rather than a function.
# arc.centroid(argumentsโฆ)
Computes the centroid of the arc that would be generated from the specified input arguments; typically, the arguments are the current datum (d), and optionally the current index (i). The centroid is defined as the midpoint in polar coordinates of the inner and outer radius, and the start and end angle. This provides a convenient location for arc labels. For example:
arcs.append("svg:text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.value; });
Alternatively, you can use SVG's transform attribute to rotate text into position, though you may need to convert radians back into degrees. Yet another possibility is to use a textPath element to curve the label along the path of the arc!
# d3.svg.symbol()
Constructs a new symbol generator with the default type- and size-accessor functions (that make no assumptions about input data, and produce a circle sized 64 square pixels; see below for details). While the default accessors generate static symbols, it is common to set one or more of the accessors using a function, such as setting the size proportional to a dimension of data for a scatterplot. The returned function generates path data for various symbols, as in a dot plot:
Note that the symbol does not include accessors for x and y. Instead, you can use the path element's transform attribute to position the symbols, as in:
vis.selectAll("path")
.data(data)
.enter().append("svg:path")
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; })
.attr("d", d3.svg.symbol());
In the future, we may add x- and y-accessors for parity with the line and area generators. The symbol will be centered at the origin (0,0) of the local coordinate system. You can also use SVG's built-in basic shapes to produce many of these symbol types, though D3's symbol generator is useful in conjunction with path elements because you can easily change the symbol type and size as a function of data.
# symbol(datum[, index])
Returns the path data string for the specified datum. An optional index may be specified, which is passed through to the symbol's accessor functions.
# symbol.type([type])
If type is specified, sets the type-accessor to the specified function or constant. If type is not specified, returns the current type-accessor. The default accessor is the constant "circle", and the following types are supported:
- circle - a circle.
- cross - a Greek cross or plus sign.
- diamond - a rhombus.
- square - an axis-aligned [square](http://en.wikipedia.org/wiki/Square_(geometry\)).
- triangle-down - a downward-pointing equilateral triangle.
- triangle-up - an upward-pointing equilateral triangle.
Types are normalized to have the same area in square pixels, according to the specified size. However, note that different types' sizes may be affected by the stroke and stroke width in different ways. All of the types are designed to be visible when only a fill style is used (unlike the Protovis cross), although they generally look better when both a fill and stroke is used.
The type-accessor is invoked in the same manner as other value functions in D3. The this context of the function is the current element in the selection. (Technically, the same this context that invokes the arc function; however, in the common case that the symbol generator is passed to the attr operator, the this context will be the associated DOM element.) The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the type-accessor as a constant rather than a function.
# symbol.size([size])
If size is specified, sets the size-accessor to the specified function or constant in square pixels. If size is not specified, returns the current size-accessor. The default is 64. This accessor is invoked on the argument passed to the symbol generator. Typically, a size-accessor is specified as a function when you want the size of the symbol to encode a quantitative dimension of data, or a constant it you simply want to make all the dots bigger or smaller. If you want to specify a radius rather than the size, you must do so indirectly, for example using a pow scale with exponent 2.
# d3.svg.chord()
Constructs a new chord generator with the default accessor functions (that assume the input data is an object with named attributes matching the accessors; see below for details). While the default accessors assume that the chord dimensions are all specified dynamically, it is very common to set one or more of the dimensions as a constant, such as the radius. The returned function generates path data for a closed shape connecting two [arcs](http://en.wikipedia.org/wiki/Arc_(geometry\)) with quadratic Bรฉzier curves, as in a chord diagram:
A chord generator is often used in conjunction with an arc generator, so as to draw annular segments at the start and end of the chords. In addition, the chord layout is useful for generating objects that describe a set of grouped chords from a matrix, compatible with the default accessors.
# chord(datum[, index])
Returns the path data string for the specified datum. An optional index may be specified, which is passed through to the chord's accessor functions.
# chord.source([source])
If source is specified, sets the source-accessor to the specified function or constant. If source is not specified, returns the current source-accessor. The purpose of the source accessor is to return an object that describes the starting arc of the chord. The returned object is subsequently passed to the radius, startAngle and endAngle accessors. This allows these other accessors to be reused for both the source and target arc descriptions. The default accessor assumes that the input data is an object with suitably-named attributes:
function source(d) {
return d.source;
}
The source-accessor is invoked in the same manner as other value functions in D3. The this context of the function is the current element in the selection. (Technically, the same this context that invokes the arc function; however, in the common case that the symbol generator is passed to the attr operator, the this context will be the associated DOM element.) The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the source-accessor as a constant rather than a function.
# chord.target([target])
If target is specified, sets the target-accessor to the specified function or constant. If target is not specified, returns the current target-accessor. The purpose of the target accessor is to return an object that describes the ending arc of the chord. The returned object is subsequently passed to the radius, startAngle and endAngle accessors. This allows these other accessors to be reused for both the source and target arc descriptions. The default accessor assumes that the input data is an object with suitably-named attributes:
function target(d) {
return d.target;
}
The target-accessor is invoked in the same manner as other value functions in D3. The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the target-accessor as a constant rather than a function.
# chord.radius([radius])
If radius is specified, sets the radius-accessor to the specified function or constant. If radius is not specified, returns the current radius-accessor. The default accessor assumes that the input source or target description is an object with suitably-named attributes:
function radius(d) {
return d.radius;
}
The radius-accessor is invoked in a similar manner as other value functions in D3. The function is passed two arguments, the current source description (derived from the current datum, d) and the current index (i). It is also possible to specify the radius-accessor as a constant rather than a function.
# chord.startAngle([angle])
If startAngle is specified, sets the startAngle-accessor to the specified function or constant. If startAngle is not specified, returns the current startAngle-accessor. Angles are specified in radians, even though SVG typically uses degrees. The default accessor assumes that the input source or target description is an object with suitably-named attributes:
function startAngle(d) {
return d.startAngle;
}
The startAngle-accessor is invoked in a similar manner as other value functions in D3. The function is passed two arguments, the current source or target description (derived from the current datum, d) and the current index (i). It is also possible to specify the startAngle-accessor as a constant rather than a function.
# chord.endAngle([angle])
If endAngle is specified, sets the endAngle-accessor to the specified function or constant. If endAngle is not specified, returns the current endAngle-accessor. Angles are specified in radians, even though SVG typically uses degrees. The default accessor assumes that the input source or target description is an object with suitably-named attributes:
function endAngle(d) {
return d.endAngle;
}
The endAngle-accessor is invoked in a similar manner as other value functions in D3. The function is passed two arguments, the current source or target description (derived from the current datum, d) and the current index (i). It is also possible to specify the endAngle-accessor as a constant rather than a function.
# d3.svg.diagonal()
Constructs a new diagonal generator with the default accessor functions (that assume the input data is an object with named attributes matching the accessors; see below for details). The returned function generates the path data for a cubic Bรฉzier connecting the source and target points; the tangents are specified to produce smooth fan-in and fan-out when connecting nodes, as in a node-link diagram:
Although diagonals default to Cartesian (axis-aligned) orientations, they can be used in radial and other orientations using a projection.
# diagonal(datum[, index])
Returns the path data string for the specified datum. An optional index may be specified, which is passed through to the diagonal's accessor functions.
# diagonal.source([source])
If source is specified, sets the source-accessor to the specified function or constant. If source is not specified, returns the current source-accessor. The purpose of the source accessor is to return an object that describes the starting point of the diagonal. The returned object is subsequently passed to the projection. The default accessor assumes that the input data is an object with suitably-named attributes:
function source(d) {
return d.source;
}
The source-accessor is invoked in the same manner as other value functions in D3. The this context of the function is the current element in the selection. (Technically, the same this context that invokes the diagonal function; however, in the common case that the symbol generator is passed to the attr operator, the this context will be the associated DOM element.) The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the source-accessor as a constant rather than a function.
# diagonal.target([target])
If target is specified, sets the target-accessor to the specified function or constant. If target is not specified, returns the current target-accessor. The purpose of the target accessor is to return an object that describes the ending point of the diagonal. The returned object is subsequently passed to the projection. The default accessor assumes that the input data is an object with suitably-named attributes:
function target(d) {
return d.target;
}
The target-accessor is invoked in the same manner as other value functions in D3. The function is passed two arguments, the current datum (d) and the current index (i). It is also possible to specify the source-accessor as a constant rather than a function.
# diagonal.projection([projection])
If projection is specified, sets the projection to the specified function. If projection is not specified, returns the current projection. The projection converts the starting or ending point returned by the source and target accessors, returning a two-element array of numbers. The default accessor assumes that the input point is an object with x and y attributes:
function projection(d) {
return [d.x, d.y];
}
The default accessor is thus compatible with D3's various node layouts, including tree, partition and cluster. For example, to produce a radial diagonal, assuming that the y attribute defines the radius in pixels, and the x attribute defines the angle in degrees:
function projection(d) {
var r = d.y, a = (d.x - 90) / 180 * Math.PI;
return [r * Math.cos(a), r * Math.sin(a)];
}
The projection is invoked in a similar manner as other value functions in D3. The function is passed two arguments, the current source or target point (derived from the current data, d) and the current index (i).
# d3.svg.diagonal.radial()
โฆ
# diagonal(datum[, index])
Returns the path data string for the specified datum. An optional index may be specified, which is passed through to the diagonal's accessor functions.