Skip to content

Commit e2e3f12

Browse files
committedMay 16, 2018
support filtering cities by name
1 parent 4c34002 commit e2e3f12

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed
 

‎src/commands/cities.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ exports.builder = function (yargs) {
1717

1818
yargs.describe('activity', 'Filter movements to specified activity');
1919

20+
yargs.describe('filter', 'Only render cities containing the given string');
21+
2022
yargs.describe('min-points', '(advanced) Minimum number of points for clustering')
2123
.number('min-points')
2224
.default('min-points', 4);
@@ -34,14 +36,24 @@ exports.handler = function (options) {
3436
segments = _.filter(segments, ['activity', options.activity]);
3537

3638
const controlPoints = clusterLocations(segments, options);
37-
const sortedControlPoints = _.chain(controlPoints)
39+
let clusters = _.chain(controlPoints)
3840
.sortBy(points => -points.length)
39-
.take(options.limit)
41+
.map(points => ({
42+
label: lookupLabel(points),
43+
controlPoints: points
44+
}))
4045
.value();
4146

47+
if (options.filter)
48+
clusters = _.filter(clusters, c => {
49+
return (c.label || '').toLowerCase().indexOf(options.filter.toLowerCase()) > -1;
50+
});
51+
52+
clusters = _.take(clusters, options.limit);
53+
4254
const context = utils.createD3n(options);
4355

44-
_.each(sortedControlPoints, (cluster, index, clusters) => {
56+
_.each(clusters, (cluster, index, clusters) => {
4557
const square = computeSquareDimensions(index, clusters.length, options);
4658
drawMapAtSquare(context, square, cluster, segments, options);
4759
});
@@ -89,11 +101,11 @@ function computeSquareDimensions(index, count, options) {
89101
};
90102
}
91103

92-
function drawMapAtSquare(context, square, controlPoints, segments, options) {
104+
function drawMapAtSquare(context, square, cluster, segments, options) {
93105

94106
const moves = _.filter(segments, ['type', 'move']);
95107

96-
const projection = createProjection(context, controlPoints, square.size, square.size);
108+
const projection = createProjection(context, cluster.controlPoints, square.size, square.size);
97109
const geopath = context.d3.geoPath()
98110
.projection(projection);
99111

@@ -112,8 +124,6 @@ function drawMapAtSquare(context, square, controlPoints, segments, options) {
112124
.attr('width', square.size)
113125
.attr('fill', backgroundColor);
114126

115-
const label = lookupLabel(controlPoints);
116-
117127
const pointToCoord = point => [point.lon, point.lat];
118128
_.each(moves, move => {
119129

@@ -130,7 +140,7 @@ function drawMapAtSquare(context, square, controlPoints, segments, options) {
130140
.attr('d', geopath);
131141
});
132142

133-
const fontSize = square.size * 0.05;
143+
const fontSize = Math.min(square.size * 0.05, 16);
134144
context.svg.append('text')
135145
.attr('text-anchor', 'start')
136146
.attr('font-family', 'sans-serif')
@@ -139,7 +149,7 @@ function drawMapAtSquare(context, square, controlPoints, segments, options) {
139149
.attr('font-size', fontSize)
140150
.attr('x', square.x + fontSize * 0.5)
141151
.attr('y', square.y + square.size - fontSize * 0.5)
142-
.text(label);
152+
.text(cluster.label);
143153

144154
}
145155

0 commit comments

Comments
 (0)
Please sign in to comment.