Browse files

Adopt d3.map for d3.svg.line and d3.svg.area.

  • Loading branch information...
1 parent 6affe7e commit d69cbbdaa7744f39f5e87c39d160f6fdfb7f6d28 @mbostock mbostock committed Feb 21, 2012
Showing with 30 additions and 16 deletions.
  1. +10 −6 d3.v2.js
  2. +4 −4 d3.v2.min.js
  3. +2 −1 src/svg/area.js
  4. +8 −5 src/svg/line.js
  5. +3 −0 test/svg/area-test.js
  6. +3 −0 test/svg/line-test.js
View
16 d3.v2.js
@@ -3159,8 +3159,8 @@ function d3_svg_arcEndAngle(d) {
function d3_svg_line(projection) {
var x = d3_svg_lineX,
y = d3_svg_lineY,
- interpolate = "linear",
- interpolator = d3_svg_lineInterpolators[interpolate],
+ interpolate = d3_svg_lineInterpolatorDefault,
+ interpolator = d3_svg_lineInterpolators.get(interpolate),
tension = .7;
function line(d) {
@@ -3181,7 +3181,8 @@ function d3_svg_line(projection) {
line.interpolate = function(v) {
if (!arguments.length) return interpolate;
- interpolator = d3_svg_lineInterpolators[interpolate = v];
+ if (!d3_svg_lineInterpolators.has(v += "")) v = d3_svg_lineInterpolatorDefault;
+ interpolator = d3_svg_lineInterpolators.get(interpolate = v);
return line;
};
@@ -3234,8 +3235,10 @@ function d3_svg_lineY(d) {
return d[1];
}
+var d3_svg_lineInterpolatorDefault = "linear";
+
// The various interpolators supported by the `line` class.
-var d3_svg_lineInterpolators = {
+var d3_svg_lineInterpolators = d3.map({
"linear": d3_svg_lineLinear,
"step-before": d3_svg_lineStepBefore,
"step-after": d3_svg_lineStepAfter,
@@ -3247,7 +3250,7 @@ var d3_svg_lineInterpolators = {
"cardinal-open": d3_svg_lineCardinalOpen,
"cardinal-closed": d3_svg_lineCardinalClosed,
"monotone": d3_svg_lineMonotone
-};
+});
// Linear interpolation; generates "L" commands.
function d3_svg_lineLinear(points) {
@@ -3650,7 +3653,8 @@ function d3_svg_area(projection) {
area.interpolate = function(x) {
if (!arguments.length) return interpolate;
- i0 = d3_svg_lineInterpolators[interpolate = x];
+ if (!d3_svg_lineInterpolators.has(x += "")) x = d3_svg_lineInterpolatorDefault;
+ i0 = d3_svg_lineInterpolators.get(interpolate = x);
i1 = i0.reverse || i0;
return area;
};
View
8 d3.v2.min.js
4 additions, 4 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
3 src/svg/area.js
@@ -55,7 +55,8 @@ function d3_svg_area(projection) {
area.interpolate = function(x) {
if (!arguments.length) return interpolate;
- i0 = d3_svg_lineInterpolators[interpolate = x];
+ if (!d3_svg_lineInterpolators.has(x += "")) x = d3_svg_lineInterpolatorDefault;
+ i0 = d3_svg_lineInterpolators.get(interpolate = x);
i1 = i0.reverse || i0;
return area;
};
View
13 src/svg/line.js
@@ -1,8 +1,8 @@
function d3_svg_line(projection) {
var x = d3_svg_lineX,
y = d3_svg_lineY,
- interpolate = "linear",
- interpolator = d3_svg_lineInterpolators[interpolate],
+ interpolate = d3_svg_lineInterpolatorDefault,
+ interpolator = d3_svg_lineInterpolators.get(interpolate),
tension = .7;
function line(d) {
@@ -23,7 +23,8 @@ function d3_svg_line(projection) {
line.interpolate = function(v) {
if (!arguments.length) return interpolate;
- interpolator = d3_svg_lineInterpolators[interpolate = v];
+ if (!d3_svg_lineInterpolators.has(v += "")) v = d3_svg_lineInterpolatorDefault;
+ interpolator = d3_svg_lineInterpolators.get(interpolate = v);
return line;
};
@@ -76,8 +77,10 @@ function d3_svg_lineY(d) {
return d[1];
}
+var d3_svg_lineInterpolatorDefault = "linear";
+
// The various interpolators supported by the `line` class.
-var d3_svg_lineInterpolators = {
+var d3_svg_lineInterpolators = d3.map({
"linear": d3_svg_lineLinear,
"step-before": d3_svg_lineStepBefore,
"step-after": d3_svg_lineStepAfter,
@@ -89,7 +92,7 @@ var d3_svg_lineInterpolators = {
"cardinal-open": d3_svg_lineCardinalOpen,
"cardinal-closed": d3_svg_lineCardinalClosed,
"monotone": d3_svg_lineMonotone
-};
+});
// Linear interpolation; generates "L" commands.
function d3_svg_lineLinear(points) {
View
3 test/svg/area-test.js
@@ -138,6 +138,9 @@ suite.addBatch({
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0V1H1L1,0H0V0Z");
assert.equal(a.interpolate(), "step-before");
},
+ "invalid interpolates fallback to linear": function(area) {
+ assert.equal(area().interpolate("__proto__").interpolate(), "linear");
+ },
"tension defaults to .7": function(area) {
assert.equal(area().tension(), .7);
View
3 test/svg/line-test.js
@@ -57,6 +57,9 @@ suite.addBatch({
assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1");
assert.equal(l.interpolate(), "step-before");
},
+ "invalid interpolates fallback to linear": function(line) {
+ assert.equal(line().interpolate("__proto__").interpolate(), "linear");
+ },
"tension defaults to .7": function(line) {
assert.equal(line().tension(), .7);

0 comments on commit d69cbbd

Please sign in to comment.