Skip to content

Commit 412c071

Browse files
committed
getLineColor/getFillColor accessors
1 parent 645f69f commit 412c071

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

modules/carto/src/layers/trajectory-tile-layer.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import {GeoJsonLayer} from '@deck.gl/layers';
88

99
import type {TilejsonResult} from '@carto/api-client';
1010
import VectorTileLayer from './vector-tile-layer';
11-
import {EdgedPathLayer, EdgedTripsLayer} from './edged-layers';
1211
import {transformTrajectoryData, type TileBounds, normalizeTimestamp} from './trajectory-utils';
1312
import {applyTrajectoryColors} from './trajectory-speed-utils';
14-
import { createEmptyBinary } from '../utils';
13+
import {createEmptyBinary} from '../utils';
1514

1615
const defaultProps: DefaultProps<TrajectoryTileLayerProps> = {
1716
...TripsLayer.defaultProps,
@@ -164,8 +163,23 @@ export default class TrajectoryTileLayer<
164163
const {minTime, maxTime} = this.state;
165164
const totalTimeSpan = maxTime - minTime;
166165

166+
const getLineColorFromFillColor = (d: any, info: any) => {
167+
const {index, data} = info;
168+
const startIndex = data.startIndices[index];
169+
const endIndex = data.startIndices[index + 1];
170+
const nVertices = endIndex - startIndex;
171+
const colors = new Array(nVertices).fill(0).map((_, index) => {
172+
return props.getFillColor!(undefined, {index, data});
173+
});
174+
175+
return colors;
176+
//return props.getLineColor(d, info);
177+
}
178+
const getLineColor = props.getFillColor ? getLineColorFromFillColor : props.getLineColor;
179+
167180
const modifiedProps = {
168181
...props,
182+
getLineColor,
169183
_subLayerProps: {
170184
...props._subLayerProps,
171185
linestrings: {

test/apps/carto-trajectories/app.tsx

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -310,27 +310,22 @@ export default function App() {
310310
renderMode: showTrips ? 'trips' : 'paths',
311311
uniqueIdProperty: 'trajectoryId', // TODO internalize this
312312

313-
// Color entire line by trajectoryId
314-
//_getLineColor: colorContinuous({
315-
// attr: 'trajectoryId',
316-
// domain: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
317-
// colors: palette
318-
//}),
319-
getLineColor: (d: any, info: any) => {
320-
//return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
321-
const { data, index } = info;
322-
const nVertices = data.startIndices[index + 1] - data.startIndices[index];
323-
const colors = new Array(nVertices).fill(0).map((_, i) => {
324-
return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
325-
});
313+
// Color entire line by trajectoryId, will be invoked once per line
314+
getLineColor: colorContinuous({
315+
attr: 'trajectoryId',
316+
domain: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
317+
colors: palette
318+
}),
326319

327-
// Return an array to color each vertex
328-
return colors;
320+
// Color each vertex, will be invoked once per vertex
321+
getFillColor: (d: any, info: any) => {
322+
return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
329323
},
330324

331325

326+
332327
// Alternative API, dedicated accessor: Color each vertex by speed
333-
getVertexColor: colorContinuous({
328+
_getVertexColor: colorContinuous({
334329
attr: (d: any) => d.speed,
335330
domain: [5, 7, 9, 11, 13, 15],
336331
colors: palette
@@ -396,20 +391,6 @@ export default function App() {
396391
<ObjectSelect title="Map Style" obj={MAP_STYLES} value={mapStyle} onSelect={setMapStyle} top={60} />
397392
<ObjectSelect title="Palette" obj={PALETTES} value={palette} onSelect={setPalette} top={90}/>
398393
<ObjectSelect title="Outline" obj={OUTLINES} value={outline} onSelect={setOutline} top={120} />
399-
{clickedTrajectoryId && (
400-
<div style={{
401-
position: 'absolute',
402-
right: 2,
403-
top: 150,
404-
background: 'rgba(0,0,0,0.8)',
405-
color: 'white',
406-
padding: '4px 8px',
407-
borderRadius: '4px',
408-
fontSize: '12px'
409-
}}>
410-
📍 Following: {clickedTrajectoryId}
411-
</div>
412-
)}
413394
{showTrips && <RangeInput
414395
name={'Time'}
415396
animationSpeed={animationSpeed}
@@ -447,19 +428,21 @@ export default function App() {
447428
onChange={setLineWidth}
448429
formatLabel={(x: number) => formatLabel(x, 'pixels')}
449430
/>
450-
<div style={{
451-
position: 'absolute',
452-
left: 10,
453-
bottom: 10,
454-
background: 'rgba(0,0,0,0.7)',
455-
color: 'white',
456-
padding: '8px',
457-
borderRadius: '4px',
458-
fontSize: '12px',
459-
maxWidth: '300px'
460-
}}>
461-
💡 Click any trajectory to jump to its start time
462-
</div>
431+
{showTrips && (
432+
<div style={{
433+
position: 'absolute',
434+
left: 10,
435+
bottom: 10,
436+
background: 'rgba(0,0,0,0.7)',
437+
color: 'white',
438+
padding: '8px',
439+
borderRadius: '4px',
440+
fontSize: '12px',
441+
maxWidth: '300px'
442+
}}>
443+
💡 Click any trajectory to jump to its start time
444+
</div>
445+
)}
463446
</>
464447
);
465448
}

0 commit comments

Comments
 (0)