diff --git a/build/jubilee.js b/build/jubilee.js index 31e5987..07a3eda 100644 --- a/build/jubilee.js +++ b/build/jubilee.js @@ -10119,7 +10119,7 @@ define('src/modules/component/boxplot',['require','d3'],function (require) { }); } - component.gClass = function (_) { + component.class = function (_) { if (!arguments.length) { return gClass; } gClass = _; return component; @@ -10369,6 +10369,8 @@ define('src/modules/component/axis/axis',['require','d3','builder','./rotate'],f transform: "translate(0,0)", text: "" }; + var g; + var titleText; function component(selection) { selection.each(function () { @@ -10383,8 +10385,12 @@ define('src/modules/component/axis/axis',['require','d3','builder','./rotate'],f .tickPadding(tick.padding) .tickFormat(tick.format); - var g = d3.select(this).append("g") - .attr("class", gClass) + if (!g) { + g = d3.select(this).append("g"); + } + + // Attach axis + g.attr("class", gClass) .attr("transform", transform) .call(axis); @@ -10396,7 +10402,11 @@ define('src/modules/component/axis/axis',['require','d3','builder','./rotate'],f g.call(builder(rotateLabels, rotation)); } - g.append("text") + if (!titleText) { + titleText = g.append("text"); + } + + titleText.append("text") .attr("class", title.class) .attr("x", title.x) .attr("y", title.y) @@ -10885,13 +10895,14 @@ define('src/modules/element/svg/rect',['require','d3'],function (require) { var d3 = require("d3"); return function rect() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var x = function (d) { return d.x; }; var y = function (d) { return d.y; }; var rx = 0; var ry = 0; var width = null; var height = null; + var color = d3.scale.category10(); // Options var cssClass = "bar"; @@ -10901,12 +10912,9 @@ define('src/modules/element/svg/rect',['require','d3'],function (require) { var opacity = 1; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var bars = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var bars = d3.select(this).selectAll("rect") + .data(data, key); bars.exit().remove(); @@ -10928,13 +10936,13 @@ define('src/modules/element/svg/rect',['require','d3'],function (require) { } function colorFill(d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; @@ -11632,7 +11640,7 @@ define('src/modules/element/svg/path',['require','d3'],function (require) { var d3 = require("d3"); return function path() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var pathGenerator = null; // Options @@ -11644,12 +11652,9 @@ define('src/modules/element/svg/path',['require','d3'],function (require) { var opacity = 1; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var path = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var path = d3.select(this).selectAll("path") + .data(data, key); path.exit().remove(); @@ -11667,9 +11672,9 @@ define('src/modules/element/svg/path',['require','d3'],function (require) { } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; @@ -11723,7 +11728,7 @@ define('src/modules/element/svg/text',['require','d3'],function (require) { var d3 = require("d3"); return function text() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var x = function (d) { return d.x; }; var y = function (d) { return d.y; }; var dx = 0; @@ -11737,12 +11742,9 @@ define('src/modules/element/svg/text',['require','d3'],function (require) { var texts = ""; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var text = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var text = d3.select(this).selectAll("text") + .data(data, key); text.exit().remove(); @@ -11762,9 +11764,9 @@ define('src/modules/element/svg/text',['require','d3'],function (require) { } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; @@ -12087,12 +12089,15 @@ define('src/modules/component/clippath',['require','d3'],function (require) { var y = 0; var width = 0; var height = 0; + var g; function element(selection) { selection.each(function () { - d3.select(this) - .append("clipPath") - .attr("id", id) + if (!g) { + g = d3.select(this).append("clipPath"); + } + + g.attr("id", id) .attr("transform", transform) .append("rect") .attr("x", x) @@ -12167,6 +12172,7 @@ define('src/modules/component/events/brush',['require','d3'],function (require) var brushStartCallback = []; var brushCallback = []; var brushEndCallback = []; + var brushG; function component(selection) { selection.each(function (data, index) { @@ -12199,8 +12205,12 @@ define('src/modules/component/events/brush',['require','d3'],function (require) if (extent) { brush.extent(extent); } if (clamp) { brush.clamp(clamp); } - var brushG = d3.select(this).append("g") - .attr("class", cssClass) + if (!brushG) { + brushG = d3.select(this).append("g"); + } + + // Attach new brush + brushG.attr("class", cssClass) .attr("opacity", opacity) .call(brush) .selectAll("rect"); @@ -12296,11 +12306,12 @@ define('src/modules/element/svg/line',['require','d3'],function (require) { var d3 = require("d3"); return function line() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var x1 = 0; var x2 = 0; var y1 = 0; var y2 = 0; + var color = d3.scale.category10(); var cssClass = "line"; var stroke = colorFill; @@ -12308,12 +12319,9 @@ define('src/modules/element/svg/line',['require','d3'],function (require) { var opacity = 1; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var lines = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var lines = d3.select(this).selectAll("line") + .data(data, key); // Exit lines.exit().remove(); @@ -12335,13 +12343,13 @@ define('src/modules/element/svg/line',['require','d3'],function (require) { } function colorFill(d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; @@ -12419,6 +12427,7 @@ define('src/modules/component/series/area',['require','d3','src/modules/element/ strokeWidth: 0, opacity: 1 }; + var area; function component(selection) { selection.each(function () { @@ -12429,9 +12438,11 @@ define('src/modules/component/series/area',['require','d3','src/modules/element/ var areaPath = path().pathGenerator(areas); - d3.select(this) - .append("g") - .call(builder(properties, areaPath)); + if (!area) { + area = d3.select(this).append("g"); + } + + area.call(builder(properties, areaPath)); }); } @@ -12542,6 +12553,7 @@ define('src/modules/component/series/bars',['require','d3','src/modules/element/ strokeWidth: 0, opacity: 1 }; + var bars; function component(selection) { selection.each(function (data) { @@ -12588,11 +12600,21 @@ define('src/modules/component/series/bars',['require','d3','src/modules/element/ .rx(rx) .ry(ry); - d3.select(this).append("g") - .selectAll("g") - .data(data) - .enter().append("g") - .call(builder(properties, rects)); + if (!bars) { + bars = d3.select(this).append("g"); + } + + bars.selectAll("g") + .data(data); + + // Exit + bars.exit().remove(); + + // Enter + bars.enter().append("g"); + + // Update + bars.call(builder(properties, rects)); }); } @@ -12674,10 +12696,11 @@ define('src/modules/element/svg/circle',['require','d3'],function (require) { var d3 = require("d3"); return function circle() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var cx = function (d) { return d.x; }; var cy = function (d) { return d.y; }; var radius = 5; + var color = d3.scale.category10(); // Options var cssClass = "circles"; @@ -12687,12 +12710,9 @@ define('src/modules/element/svg/circle',['require','d3'],function (require) { var opacity = null; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var circles = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var circles = d3.select(this).selectAll("circle") + .data(data, key); // Exit circles.exit().remove(); @@ -12715,13 +12735,13 @@ define('src/modules/element/svg/circle',['require','d3'],function (require) { } function colorFill (d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; @@ -12797,6 +12817,7 @@ define('src/modules/component/series/points',['require','d3','src/modules/elemen strokeWidth: 0, opacity: 1 }; + var point; function component(selection) { selection.each(function (data) { @@ -12805,8 +12826,11 @@ define('src/modules/component/series/points',['require','d3','src/modules/elemen .cy(Y) .radius(radius); - d3.select(this).append("g") - .datum(data.reduce(function (a, b) { + if (!point) { + point = d3.select(this).append("g"); + } + + point.datum(data.reduce(function (a, b) { return a.concat(b); },[]).filter(y)) .call(builder(properties, circles)); @@ -12888,19 +12912,22 @@ define('src/modules/component/series/line',['require','d3','src/modules/element/ strokeWidth: 3, opacity: 1 }; + var lines; function component(selection) { selection.each(function () { - var lines = d3.svg.line().x(X).y(Y) + var line = d3.svg.line().x(X).y(Y) .interpolate(interpolate) .tension(tension) .defined(defined); - var linePath = path().pathGenerator(lines); + var linePath = path().pathGenerator(line); - d3.select(this) - .append("g") - .call(builder(properties, linePath)); + if (!lines) { + lines = d3.select(this).append("g"); + } + + lines.call(builder(properties, linePath)); }); } @@ -13847,11 +13874,12 @@ define('src/modules/element/svg/ellipse',['require','d3'],function (require) { var d3 = require("d3"); return function ellipse() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var cx = function (d) { return d.x; }; var cy = function (d) { return d.y; }; var rx = 20; var ry = 20; + var color = d3.scale.catgory10(); // Options var cssClass = "ellipses"; @@ -13862,11 +13890,8 @@ define('src/modules/element/svg/ellipse',['require','d3'],function (require) { function element(selection) { selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var ellipses = d3.select(this) - .selectAll("." + cssClass) - .data(data); + var ellipses = d3.select(this).selectAll("ellipse") + .data(data, key); // Exit ellipses.exit().remove(); @@ -13889,13 +13914,13 @@ define('src/modules/element/svg/ellipse',['require','d3'],function (require) { } function colorFill(d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; @@ -13961,7 +13986,7 @@ define('src/modules/element/svg/image',['require','d3'],function (require) { var d3 = require("d3"); return function image() { - var accessor = function (d) { return d; }; + var key = function (d) { return d; }; var x = function (d) { return d.x; }; var y = function (d) { return d.y; }; var width = 10; @@ -13973,12 +13998,9 @@ define('src/modules/element/svg/image',['require','d3'],function (require) { var cssClass = "image"; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var images = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var images = d3.select(this).selectAll("image") + .data(data, key); // Exit images.exit().remove(); @@ -13999,9 +14021,9 @@ define('src/modules/element/svg/image',['require','d3'],function (require) { } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/chart/series.js b/src/modules/chart/series.js index 6b1b5fc..d4987d8 100644 --- a/src/modules/chart/series.js +++ b/src/modules/chart/series.js @@ -103,6 +103,23 @@ define(function (require) { var points = {}; var listeners = {}; + var elements = [ + { type: "area", func: areas() }, + { type: "bar", func: bars() }, + { type: "line", func: lines() }, + { type: "points", func: circles() } + ]; + + var svg; + var g; + var brush; + var zLine; + var axisX; + var axisY; + var axisZ; + var clippath; + var clippedG; + function chart(selection) { selection.each(function (data, index) { data = accessor.call(this, data, index); @@ -144,23 +161,28 @@ define(function (require) { /* ******************************** */ /* Canvas ******************************** */ - var svg = d3.select(this).selectAll("svg") - .data([data]) - .enter().append("svg") - .attr("width", width) - .attr("height", height) - .call(svgEvents); - - var g = svg.append("g") - .attr("transform", "translate(" + margin.left + ", " + margin.top + ")"); + if (!svg && !g) { + svg = d3.select(this).append("svg") + .attr("width", width) + .attr("height", height) + .call(svgEvents); + + g = svg.append("g"); + } + + g.attr("transform", "translate(" + margin.left + ", " + margin.top + ")"); /* ******************************** */ /* Brush ******************************** */ if (listeners.brushstart && listeners.brushstart.length || listeners.brush && listeners.brush.length || listeners.brushend && listeners.brushend.length) { - var brush = brushComponent() - .class(brushOpts.class) + + if (!brush) { + brush = brushComponent(); + } + + brush.class(brushOpts.class) .xScale(brushOpts.x ? x : null) .yScale(brushOpts.y ? y : null) .opacity(brushOpts.opacity) @@ -177,8 +199,11 @@ define(function (require) { /* Zero-line ******************************** */ if (zeroLine.show) { - var zLine = zeroAxisLine() - .x1(function () { return x.range()[0]; }) + if (!zLine) { + zLine = zeroAxisLine(); + } + + zLine.x1(function () { return x.range()[0]; }) .x2(function () { return x.range()[1]; }) .y1(function () { return y(0); }) .y2(function () { return y(0); }); @@ -198,8 +223,11 @@ define(function (require) { } if (xAxis.show) { - var axisX = axis() - .scale(x) + if (!axisX) { + axisX = axis(); + } + + axisX.scale(x) .class(xAxis.class) .transform(xAxis.transform || "translate(0," + (y.range()[0]) + ")") .tick(xAxis.tick) @@ -211,8 +239,11 @@ define(function (require) { } if (yAxis.show) { - var axisY = axis() - .scale(y) + if (!axisY) { + axisY = axis(); + } + + axisY.scale(y) .orient("left") .class(yAxis.class) .transform(yAxis.transform || "translate(0,0)") @@ -224,8 +255,11 @@ define(function (require) { } if (zAxis.show) { - var axisZ = axis() - .scale(z) + if (!axisZ) { + axisZ = axis(); + } + + axisZ.scale(z) .orient("right") .class(zAxis.class) .transform(zAxis.transform || "translate(" + x.range()[1] + "," + "0)") @@ -238,24 +272,31 @@ define(function (require) { /* ******************************** */ /* ClipPath ******************************** */ - var clippath = clip().width(adjustedWidth).height(adjustedHeight); - var clippedG = g.call(clippath).append("g") - .attr("clip-path", "url(#" + clippath.id() + ")"); + if (!clippath) { + clippath = clip(); + } + + clippath = clip().width(adjustedWidth).height(adjustedHeight); + + if (!clippedG) { + clippedG = g.call(clippath).append("g"); + } + + clippedG.attr("clip-path", "url(#" + clippath.id() + ")"); /* ******************************** */ /* SVG Elements ******************************** */ - var elements = [ - {type: "area", func: areas(), opts: area}, - {type: "bar", func: bars(), opts: bar}, - {type: "line", func: lines(), opts: line}, - {type: "points", func: circles(), opts: points} - ]; - elements.forEach(function (d) { + if (d.type === "area") { d.opts = area; } + if (d.type === "bar") { d.opts = bar; } + if (d.type === "line") { d.opts = line; } + if (d.type === "points") { d.opts = points; } // Only render elements when api called if (d3.keys(d.opts).length) { - var element = functor().function(d.func); + if (!d.element) { + d.element = functor().function(d.func); + } if (d.type === "area") { d.opts.offset = stacks.offset; } d.opts = !Array.isArray(d.opts) ? [d.opts] : d.opts; @@ -267,7 +308,7 @@ define(function (require) { props.xScale = x; props.yScale = isZ ? z : y; - clippedG.call(element.options(props)); + clippedG.datum(data).call(d.element.options(props)); }); } }); diff --git a/src/modules/component/axis/axis.js b/src/modules/component/axis/axis.js index e81eebf..899d6c1 100644 --- a/src/modules/component/axis/axis.js +++ b/src/modules/component/axis/axis.js @@ -37,6 +37,8 @@ define(function (require) { transform: "translate(0,0)", text: "" }; + var g; + var titleText; function component(selection) { selection.each(function () { @@ -51,14 +53,12 @@ define(function (require) { .tickPadding(tick.padding) .tickFormat(tick.format); - var g = d3.select(this); - - // Remove previous axis - g.select("g." + gClass).remove(); + if (!g) { + g = d3.select(this).append("g"); + } // Attach axis - g.append("g") - .attr("class", gClass) + g.attr("class", gClass) .attr("transform", transform) .call(axis); @@ -70,7 +70,11 @@ define(function (require) { g.call(builder(rotateLabels, rotation)); } - g.append("text") + if (!titleText) { + titleText = g.append("text"); + } + + titleText.append("text") .attr("class", title.class) .attr("x", title.x) .attr("y", title.y) diff --git a/src/modules/component/boxplot.js b/src/modules/component/boxplot.js index c66ff9f..aafb684 100644 --- a/src/modules/component/boxplot.js +++ b/src/modules/component/boxplot.js @@ -122,7 +122,7 @@ define(function (require) { }); } - component.gClass = function (_) { + component.class = function (_) { if (!arguments.length) { return gClass; } gClass = _; return component; diff --git a/src/modules/component/clippath.js b/src/modules/component/clippath.js index 4edf3cd..dce97da 100644 --- a/src/modules/component/clippath.js +++ b/src/modules/component/clippath.js @@ -8,16 +8,15 @@ define(function (require) { var y = 0; var width = 0; var height = 0; + var g; function element(selection) { selection.each(function () { - var g = d3.select(this); + if (!g) { + g = d3.select(this).append("clipPath"); + } - // Remove previous clip-path - g.select("clipPath").remove(); - - g.append("clipPath") - .attr("id", id) + g.attr("id", id) .attr("transform", transform) .append("rect") .attr("x", x) diff --git a/src/modules/component/events/brush.js b/src/modules/component/events/brush.js index 124762c..00f4c45 100644 --- a/src/modules/component/events/brush.js +++ b/src/modules/component/events/brush.js @@ -18,6 +18,7 @@ define(function (require) { var brushStartCallback = []; var brushCallback = []; var brushEndCallback = []; + var brushG; function component(selection) { selection.each(function (data, index) { @@ -50,14 +51,12 @@ define(function (require) { if (extent) { brush.extent(extent); } if (clamp) { brush.clamp(clamp); } - var brushG = d3.select(this); - - // Remove previous brush - brushG.select("g." + cssClass).remove(); + if (!brushG) { + brushG = d3.select(this).append("g"); + } // Attach new brush - brushG.append("g") - .attr("class", cssClass) + brushG.attr("class", cssClass) .attr("opacity", opacity) .call(brush) .selectAll("rect"); diff --git a/src/modules/component/series/area.js b/src/modules/component/series/area.js index b3a475f..e1db9f2 100644 --- a/src/modules/component/series/area.js +++ b/src/modules/component/series/area.js @@ -21,6 +21,7 @@ define(function (require) { strokeWidth: 0, opacity: 1 }; + var g; function component(selection) { selection.each(function () { @@ -31,9 +32,11 @@ define(function (require) { var areaPath = path().pathGenerator(areas); - d3.select(this) - .append("g") - .call(builder(properties, areaPath)); + if (!g) { + g = d3.select(this).append("g"); + } + + g.call(builder(properties, areaPath)); }); } diff --git a/src/modules/component/series/bars.js b/src/modules/component/series/bars.js index c623817..77a5971 100644 --- a/src/modules/component/series/bars.js +++ b/src/modules/component/series/bars.js @@ -24,6 +24,7 @@ define(function (require) { strokeWidth: 0, opacity: 1 }; + var g; function component(selection) { selection.each(function (data) { @@ -70,11 +71,21 @@ define(function (require) { .rx(rx) .ry(ry); - d3.select(this).append("g") - .selectAll("g") - .data(data) - .enter().append("g") - .call(builder(properties, rects)); + if (!g) { + g = d3.select(this).append("g"); + } + + var bars = g.selectAll("g") + .data(data); + + // Exit + bars.exit().remove(); + + // Enter + bars.enter().append("g"); + + // Update + bars.call(builder(properties, rects)); }); } diff --git a/src/modules/component/series/line.js b/src/modules/component/series/line.js index a813e90..14e4800 100644 --- a/src/modules/component/series/line.js +++ b/src/modules/component/series/line.js @@ -20,19 +20,22 @@ define(function (require) { strokeWidth: 3, opacity: 1 }; + var g; function component(selection) { selection.each(function () { - var lines = d3.svg.line().x(X).y(Y) + var line = d3.svg.line().x(X).y(Y) .interpolate(interpolate) .tension(tension) .defined(defined); - var linePath = path().pathGenerator(lines); + var linePath = path().pathGenerator(line); - d3.select(this) - .append("g") - .call(builder(properties, linePath)); + if (!g) { + g = d3.select(this).append("g"); + } + + g.call(builder(properties, linePath)); }); } diff --git a/src/modules/component/series/points.js b/src/modules/component/series/points.js index 9479d85..6c48ba0 100644 --- a/src/modules/component/series/points.js +++ b/src/modules/component/series/points.js @@ -18,6 +18,7 @@ define(function (require) { strokeWidth: 0, opacity: 1 }; + var g; function component(selection) { selection.each(function (data) { @@ -26,8 +27,11 @@ define(function (require) { .cy(Y) .radius(radius); - d3.select(this).append("g") - .datum(data.reduce(function (a, b) { + if (!g) { + g = d3.select(this).append("g"); + } + + g.datum(data.reduce(function (a, b) { return a.concat(b); },[]).filter(y)) .call(builder(properties, circles)); diff --git a/src/modules/element/svg/circle.js b/src/modules/element/svg/circle.js index 9eff716..269fda4 100644 --- a/src/modules/element/svg/circle.js +++ b/src/modules/element/svg/circle.js @@ -2,10 +2,11 @@ define(function (require) { var d3 = require("d3"); return function circle() { - var accessor = function (d) { return d; }; + var key = null; var cx = function (d) { return d.x; }; var cy = function (d) { return d.y; }; var radius = 5; + var color = d3.scale.category10(); // Options var cssClass = "circles"; @@ -15,12 +16,9 @@ define(function (require) { var opacity = null; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var circles = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var circles = d3.select(this).selectAll("circle") + .data(data, key); // Exit circles.exit().remove(); @@ -43,13 +41,13 @@ define(function (require) { } function colorFill (d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/element/svg/ellipse.js b/src/modules/element/svg/ellipse.js index aabcc9c..eb79fc0 100644 --- a/src/modules/element/svg/ellipse.js +++ b/src/modules/element/svg/ellipse.js @@ -2,11 +2,12 @@ define(function (require) { var d3 = require("d3"); return function ellipse() { - var accessor = function (d) { return d; }; + var key = null; var cx = function (d) { return d.x; }; var cy = function (d) { return d.y; }; var rx = 20; var ry = 20; + var color = d3.scale.category10(); // Options var cssClass = "ellipses"; @@ -16,12 +17,9 @@ define(function (require) { var opacity = null; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var ellipses = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var ellipses = d3.select(this).selectAll("ellipse") + .data(data, key); // Exit ellipses.exit().remove(); @@ -44,13 +42,13 @@ define(function (require) { } function colorFill(d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/element/svg/image.js b/src/modules/element/svg/image.js index b66d8ff..cbb130e 100644 --- a/src/modules/element/svg/image.js +++ b/src/modules/element/svg/image.js @@ -2,7 +2,7 @@ define(function (require) { var d3 = require("d3"); return function image() { - var accessor = function (d) { return d; }; + var key = null; var x = function (d) { return d.x; }; var y = function (d) { return d.y; }; var width = 10; @@ -14,12 +14,9 @@ define(function (require) { var cssClass = "image"; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var images = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var images = d3.select(this).selectAll("image") + .data(data, key); // Exit images.exit().remove(); @@ -40,9 +37,9 @@ define(function (require) { } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/element/svg/line.js b/src/modules/element/svg/line.js index 5685e3a..4a3bb10 100644 --- a/src/modules/element/svg/line.js +++ b/src/modules/element/svg/line.js @@ -2,11 +2,12 @@ define(function (require) { var d3 = require("d3"); return function line() { - var accessor = function (d) { return d; }; + var key = null; var x1 = 0; var x2 = 0; var y1 = 0; var y2 = 0; + var color = d3.scale.category10(); var cssClass = "line"; var stroke = colorFill; @@ -14,12 +15,9 @@ define(function (require) { var opacity = 1; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var lines = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var lines = d3.select(this).selectAll("line") + .data(data, key); // Exit lines.exit().remove(); @@ -41,13 +39,13 @@ define(function (require) { } function colorFill(d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/element/svg/path.js b/src/modules/element/svg/path.js index 8c0410c..df97f6c 100644 --- a/src/modules/element/svg/path.js +++ b/src/modules/element/svg/path.js @@ -2,7 +2,7 @@ define(function (require) { var d3 = require("d3"); return function path() { - var accessor = function (d) { return d; }; + var key = null; var pathGenerator = null; // Options @@ -14,12 +14,9 @@ define(function (require) { var opacity = 1; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var path = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var path = d3.select(this).selectAll("path") + .data(data, key); path.exit().remove(); @@ -37,9 +34,9 @@ define(function (require) { } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/element/svg/rect.js b/src/modules/element/svg/rect.js index 812a009..3681b3b 100644 --- a/src/modules/element/svg/rect.js +++ b/src/modules/element/svg/rect.js @@ -2,13 +2,14 @@ define(function (require) { var d3 = require("d3"); return function rect() { - var accessor = function (d) { return d; }; + var key = null; var x = function (d) { return d.x; }; var y = function (d) { return d.y; }; var rx = 0; var ry = 0; var width = null; var height = null; + var color = d3.scale.category10(); // Options var cssClass = "bar"; @@ -18,12 +19,9 @@ define(function (require) { var opacity = 1; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var bars = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var bars = d3.select(this).selectAll("rect") + .data(data, key); bars.exit().remove(); @@ -45,13 +43,13 @@ define(function (require) { } function colorFill(d, i) { - return d3.scale.category10()(i); + return color(i); } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/src/modules/element/svg/text.js b/src/modules/element/svg/text.js index e56f459..81f86d4 100644 --- a/src/modules/element/svg/text.js +++ b/src/modules/element/svg/text.js @@ -2,7 +2,7 @@ define(function (require) { var d3 = require("d3"); return function text() { - var accessor = function (d) { return d; }; + var key = null; var x = function (d) { return d.x; }; var y = function (d) { return d.y; }; var dx = 0; @@ -16,12 +16,9 @@ define(function (require) { var texts = ""; function element(selection) { - selection.each(function (data, index) { - data = accessor.call(this, data, index); - - var text = d3.select(this) - .selectAll("." + cssClass) - .data(data); + selection.each(function (data) { + var text = d3.select(this).selectAll("text") + .data(data, key); text.exit().remove(); @@ -41,9 +38,9 @@ define(function (require) { } // Public API - element.accessor = function (_) { - if (!arguments.length) { return accessor; } - accessor = _; + element.key = function (_) { + if (!arguments.length) { return key; } + key = _; return element; }; diff --git a/test/unit/specs/modules/element/circle.js b/test/unit/specs/modules/element/circle.js index 3628bb6..30a5663 100644 --- a/test/unit/specs/modules/element/circle.js +++ b/test/unit/specs/modules/element/circle.js @@ -26,26 +26,6 @@ define(function (require) { chai.assert.equal(isFunction, true); }); - describe("accessor API", function () { - var defaultAccessor; - - beforeEach(function () { - removeChildren(fixture); - defaultAccessor = function (d) { return d; }; - element.accessor(defaultAccessor); - }); - - it("should get the property", function () { - chai.assert.equal(element.accessor(), defaultAccessor); - }); - - it("should set the property", function () { - var newAccessor = function (d) { return d.series; }; - element.accessor(newAccessor); - chai.assert.equal(element.accessor(), newAccessor); - }); - }); - describe("cx API", function () { var defaultCX; diff --git a/test/unit/specs/modules/element/path.js b/test/unit/specs/modules/element/path.js index b975829..1727f65 100644 --- a/test/unit/specs/modules/element/path.js +++ b/test/unit/specs/modules/element/path.js @@ -59,25 +59,6 @@ define(function (require) { }); }); - describe("accessor API", function () { - var defaultAccessor; - - beforeEach(function () { - defaultAccessor = function (d) { return d; }; - element.accessor(defaultAccessor); - }); - - it("should get the property", function () { - chai.assert.equal(element.accessor(), defaultAccessor); - }); - - it("should set the property", function () { - var newAccessor = function (d) { return d.values; }; - element.accessor(newAccessor); - chai.assert.equal(element.accessor(), newAccessor); - }); - }); - describe("transform API", function () { var defaultTransform; var newTransform;