This content applies to a previous version of CARTO
In October 2021 we released a new version of our platform. You can find the latest documentation at docs.carto.com
How to customize the Map Viewer style
Customizing your map style using deck.gl declarative language
deck.gl supports a declarative system for describing layers and their props. This feature can be used to generate powerful visualizations directly from a JSON document. You can describe a visualization in abstract terms and view the results without needing to write JavaScript code.
This declarative system is used in deck.gl Playground and supports the layers from CARTO’s module. It is also the system used in CARTO Viewer to create and share tilesets visualizations.
The declarative language allows you to define the style and cartography of your visualization and set other general map options. Technically this language is based on JSON that gets transformed into Javascript objects and functions. Check out the official documentation on the JSON format for further technical details.
API reference
The declarative system creates deck.gl objects from a JSON representation. For instance, the initialViewState element in the JSON representation (declarative way) corresponds to a view state object when you are working with deck.gl in a programmatic way.
Usually the name-value pairs in the JSON representation correspond to object properties in deck.gl objects and the arrays correspond to JavaScript arrays. There are a couple of specific name-value pairs that are very useful:
@@type: Indicates the class of the object you want to create; for instance, for specifying the type of layer.@@function: Specifies a function to be executed to evaluate a data accessor; for instance, for using helpers to specify the color assigned to features.
General parameters
initialViewState
Type: object
Description: Defines the initial view state for the visualization.
| Property Name | Type | Description |
|---|---|---|
| latitude | number | Latitude at the map center. |
| longitude | number | Longitude at the map center. |
| zoom | number | Initial zoom level. |
| pitch | number | Default: 0. Pitch angle (in degrees). |
| bearing | number | Default: 0. Bearing angle (in degrees). |
| minZoom | number | Default: 0. Min zoom level. |
| maxZoom | number | Default: 20. Max zoom level. |
| minPitch | number | Default: 0. Min pitch angle. |
| maxPitch | number | Default: 60. Max pitch angle. |
Example: Visualization centered at (0, 0) coordinates, zoom level 2.
{
"initialViewState": {
"latitude": 0,
"longitude": 0,
"zoom": 2
}
}
views
Type: array
Description: Defines the views.
Each view object has the following properties:
| Property Name | Type | Description |
|---|---|---|
| @@type | string | Use MapView in the CARTO viewer for a Web Mercator Projection. Other types of views are also supported by the declarative syntax (but not in CARTO viewer) like FirstPersonView, GlobeView, OrthographicView or OrbitView. |
| controller | boolean | Indicates if the map is interactive (supports pan, zoom…) or not. |
| mapStyle | number | Basemap to use in the map. It can be a full URL to a vector style, or it can be a string from the deck.gl basemap dictionary. |
Example: Configuring an interactive MapView with the CARTO Positron basemap.
{
"views": [
{
"@@type": "MapView",
"controller": true,
"mapStyle": "@@#CARTO_BASEMAP.POSITRON"
}
]
}
Layers (Basic Properties)
Type: array
Description: Layers to overlay on the map. It is an array so you can compose multiple layers and define their blending functions.
| Property Name | Type | Description |
|---|---|---|
| @@type | string | “CartoBQTilerLayer” or “CartoSQLLayer” |
| data | string | In the case of a BigQuery TileSet you indicate the project.dataset.table_name. In the case of CartoSQLLayer you indicate the SQL used to retrieve data from your CARTO account. |
| credentials | object | CARTO authentication credentials |
| credentials.username | object | CARTO username |
| credentials.apiKey | object | CARTO API Key. For private maps you add the api key here, for a public map you can use default_public. |
| credentials.region | object | Default: us. Region where the user’s database is located; possible values are us or eu. |
| credentials.mapsUrl | object | Default: https://{user}.carto.com/api/v1/map. For on-premise users, set the URL to point to your instance. |
| credentials.sqlUrl | object | Default: https://{user}.carto.com/api/v2/sql. For on-premise users, set the URL to point to your instance. |
| pickable | boolean | Default: false. Indicates whether the layer responds to mouse pointer picking events. |
| uniqueIdProperty | string | Default: cartodb_id. Needed for highlighting a feature split across two or more tiles. |
| visible | boolean | Default: true. Indicates whether the layer is visible or not. |
Example: Visualizing a BigQuery Tileset Layer from a public tileset named cartobq.maps.osm_buildings that responds to mouse pointer picking events.
{
"layers": [
{
"@@type": "CartoBQTilerLayer",
"data": "cartobq.maps.osm_buildings",
"credentials": {
"username": "public",
"apiKey": "default_public"
},
"pickable": true
}
]
}
Layers (Styling Properties)
Styling properties for layers based on the GeoJsonLayer:
| Property Name | Type | Description |
|---|---|---|
| autoHighlight | boolean | Default: false. When true, current object pointed to by mouse is highlighted with highlightColor. Requires pickable to be true. |
| elevationScale | number | Default: 1. Elevation multiplier. |
| extruded | boolean | Default: false. Whether to extrude the polygons based on the elevations provided by getElevation. |
| filled | boolean | Default: true. Whether to draw filled polygons (solid fill). |
| getFillColor | array or @@function | The fill color in RGBA array format, or a function called on each object. |
| getLineColor | array or @@function | The outline color in RGBA array format, or a function called on each object. |
| getRadius | number or @@function | The radius of each object, in units specified by radiusUnits (default meters). |
| getLineWidth | number or @@function | The width of the outline, in units specified by lineWidthUnits (default meters). |
| highlightColor | array or @@function | Default: [0, 0, 128, 128]. RGBA color to blend with the highlighted object. |
| lineJointRounded | boolean | Default: false. If true, draw round joints. Otherwise draw miter joints. |
| lineWidthMinPixels | number | Default: 0. The minimum line width in pixels. |
| lineWidthScale | number | Default: 1. The line width multiplier applied to all outlines. |
| opacity | number | Default: 1. The opacity of the layer. |
| pointRadiusMinPixels | number | The minimum radius for point features in pixels. |
| stroked | boolean | Default: true. Whether to draw an outline around the polygon. |
Creating visualizations
Style helpers
In common types of thematic map visualizations, a color is assigned to each feature based on a property. You can use the available helper functions:
colorBins: Data values are rounded down to the nearest value in the domain and styled with the corresponding color.colorCategories: Data values listed in the domain are mapped one to one with corresponding colors in the range.colorContinuous: Data values are interpolated linearly across values in the domain and styled with a blend of the corresponding color.
These helper functions support the following properties:
| Property Name | Type | Description |
|---|---|---|
| attr | string | Attribute or column to symbolize by. |
| domain | array | Class break values (colorBins), category list (colorCategories), or data range (colorContinuous). |
| colors | string or array | Default: PurpOr. A valid named CARTOColors palette string or an array of RGBA colors. |
| nullColor | array | Default: [204, 204, 204]. Color for null values. |
| othersColor | array | Default: [119, 119, 119]. Only for colorCategories. Fallback color for unassigned categories. |
ColorBins example
Create bins on a numeric feature property and assign a different color to each bin:
{
"layers": [
{
"getFillColor": {
"@@function": "colorBins",
"attr": "aggregated_total",
"domain": [10, 100, 1000, 10000, 100000, 1000000],
"colors": "Temps"
}
}
]
}
ColorCategories example
Assign a different color to each feature depending on a string property value:
{
"layers": [
{
"getFillColor": {
"@@function": "colorCategories",
"attr": "pop_cat",
"domain": ["low", "medium", "high"],
"colors": "RedOr"
}
}
]
}
ColorContinuous example
Assign a different color to each feature by interpolating between numeric property values:
{
"layers": [
{
"getFillColor": {
"@@function": "colorContinuous",
"attr": "aggregated_total",
"domain": [1, 100, 1000, 10000, 500000],
"colors": "Peach"
}
}
]
}
Highlighting features
It is possible to highlight features on hover using the following attributes in the layer object:
{
"autoHighlight": true,
"pickable": true,
"highlightColor": [232, 133, 113],
"uniqueIdProperty": "id"
}
The uniqueIdProperty is needed to specify the attribute that identifies the feature uniquely across the dataset. It is case sensitive.
