From f1c21e5d7fac29155210525ae98374b69f98c579 Mon Sep 17 00:00:00 2001 From: Gordon Woodhull Date: Wed, 24 May 2017 15:58:10 -0400 Subject: [PATCH] custom scatter symbols fixes #1274 --- spec/scatter-plot-spec.js | 52 +++++++++++++++++++++++++++++++++++++++ src/scatter-plot.js | 14 +++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/spec/scatter-plot-spec.js b/spec/scatter-plot-spec.js index f4bffe3fd..1f5eb4863 100644 --- a/spec/scatter-plot-spec.js +++ b/spec/scatter-plot-spec.js @@ -74,6 +74,48 @@ describe('dc.scatterPlot', function () { }); }); + function fishSymbol () { + var size; + var points = [[2, 0], [1, -1], [-1, 1], [-1, -1], [1, 1]]; + function symbol (d, i) { + // native size is 3 square pixels, so to get size N, multiply by sqrt(N)/3 + var m = size.call(this, d, i); + m = Math.sqrt(m) / 3; + var path = d3.svg.line() + .x(function (d) { + return d[0] * m; + }) + .y(function (d) { + return d[1] * m; + }); + return path(points) + 'Z'; + } + symbol.type = function () { + if (arguments.length) { + throw new Error('no, you must have fish'); + } + return 'fish'; + }; + symbol.size = function (_) { + if (!arguments.length) { + return size; + } + size = d3.functor(_); + return symbol; + }; + return symbol; + } + describe('with a fish symbol', function () { + beforeEach(function () { + chart.customSymbol(fishSymbol().size(chart.symbolSize())) + .render(); + }); + + it('should draw fishes', function () { + expect(symbolsMatching(matchSymbol(fishSymbol(), chart.symbolSize())).length).toBe(9); + }); + }); + describe('with title rendering disabled', function () { beforeEach(function () { chart.renderTitle(false).render(); @@ -303,6 +345,16 @@ describe('dc.scatterPlot', function () { }; } + function matchSymbol (s, r) { + return function () { + var symbol = d3.select(this); + var size = Math.pow(r, 2); + var path = s.size(size)(); + var result = comparePaths(symbol.attr('d'), path); + return result.pass; + }; + } + function symbolsMatching (pred) { function getData (symbols) { return symbols[0].map(function (symbol) { diff --git a/src/scatter-plot.js b/src/scatter-plot.js index b0e34cd88..a945d2d5a 100644 --- a/src/scatter-plot.js +++ b/src/scatter-plot.js @@ -55,7 +55,7 @@ dc.scatterPlot = function (parent, chartGroup) { var _emptyColor = null; var _filtered = []; - _symbol.size(function (d, i) { + function elementSize (d, i) { if (!_existenceAccessor(d)) { return Math.pow(_emptySize, 2); } else if (_filtered[i]) { @@ -63,7 +63,8 @@ dc.scatterPlot = function (parent, chartGroup) { } else { return Math.pow(_excludedSize, 2); } - }); + } + _symbol.size(elementSize); dc.override(_chart, '_filter', function (filter) { if (!arguments.length) { @@ -174,6 +175,15 @@ dc.scatterPlot = function (parent, chartGroup) { return _chart; }; + _chart.customSymbol = function (customSymbol) { + if (!arguments.length) { + return _symbol; + } + _symbol = customSymbol; + _symbol.size(elementSize); + return _chart; + }; + /** * Set or get radius for symbols. * @method symbolSize