Skip to content

Commit f1c21e5

Browse files
custom scatter symbols
fixes #1274
1 parent 8da6ebb commit f1c21e5

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

spec/scatter-plot-spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,48 @@ describe('dc.scatterPlot', function () {
7474
});
7575
});
7676

77+
function fishSymbol () {
78+
var size;
79+
var points = [[2, 0], [1, -1], [-1, 1], [-1, -1], [1, 1]];
80+
function symbol (d, i) {
81+
// native size is 3 square pixels, so to get size N, multiply by sqrt(N)/3
82+
var m = size.call(this, d, i);
83+
m = Math.sqrt(m) / 3;
84+
var path = d3.svg.line()
85+
.x(function (d) {
86+
return d[0] * m;
87+
})
88+
.y(function (d) {
89+
return d[1] * m;
90+
});
91+
return path(points) + 'Z';
92+
}
93+
symbol.type = function () {
94+
if (arguments.length) {
95+
throw new Error('no, you must have fish');
96+
}
97+
return 'fish';
98+
};
99+
symbol.size = function (_) {
100+
if (!arguments.length) {
101+
return size;
102+
}
103+
size = d3.functor(_);
104+
return symbol;
105+
};
106+
return symbol;
107+
}
108+
describe('with a fish symbol', function () {
109+
beforeEach(function () {
110+
chart.customSymbol(fishSymbol().size(chart.symbolSize()))
111+
.render();
112+
});
113+
114+
it('should draw fishes', function () {
115+
expect(symbolsMatching(matchSymbol(fishSymbol(), chart.symbolSize())).length).toBe(9);
116+
});
117+
});
118+
77119
describe('with title rendering disabled', function () {
78120
beforeEach(function () {
79121
chart.renderTitle(false).render();
@@ -303,6 +345,16 @@ describe('dc.scatterPlot', function () {
303345
};
304346
}
305347

348+
function matchSymbol (s, r) {
349+
return function () {
350+
var symbol = d3.select(this);
351+
var size = Math.pow(r, 2);
352+
var path = s.size(size)();
353+
var result = comparePaths(symbol.attr('d'), path);
354+
return result.pass;
355+
};
356+
}
357+
306358
function symbolsMatching (pred) {
307359
function getData (symbols) {
308360
return symbols[0].map(function (symbol) {

src/scatter-plot.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ dc.scatterPlot = function (parent, chartGroup) {
5555
var _emptyColor = null;
5656
var _filtered = [];
5757

58-
_symbol.size(function (d, i) {
58+
function elementSize (d, i) {
5959
if (!_existenceAccessor(d)) {
6060
return Math.pow(_emptySize, 2);
6161
} else if (_filtered[i]) {
6262
return Math.pow(_symbolSize, 2);
6363
} else {
6464
return Math.pow(_excludedSize, 2);
6565
}
66-
});
66+
}
67+
_symbol.size(elementSize);
6768

6869
dc.override(_chart, '_filter', function (filter) {
6970
if (!arguments.length) {
@@ -174,6 +175,15 @@ dc.scatterPlot = function (parent, chartGroup) {
174175
return _chart;
175176
};
176177

178+
_chart.customSymbol = function (customSymbol) {
179+
if (!arguments.length) {
180+
return _symbol;
181+
}
182+
_symbol = customSymbol;
183+
_symbol.size(elementSize);
184+
return _chart;
185+
};
186+
177187
/**
178188
* Set or get radius for symbols.
179189
* @method symbolSize

0 commit comments

Comments
 (0)