Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
Add text label in Point layer (#166)
Javid2e35082aa1ac
diff --git a/src/components/side-panel/layer-panel/layer-configurator.js b/src/components/side-panel/layer-panel/layer-configurator.jsindex 1dff65fd5..4483bf301 100644--- a/src/components/side-panel/layer-panel/layer-configurator.js+++ b/src/components/side-panel/layer-panel/layer-configurator.js@@ -38,6 +38,7 @@ import SourceDataSelector from '../source-data-selector';import VisConfigSwitch from './vis-config-switch';import VisConfigSlider from './vis-config-slider';import LayerConfigGroup from './layer-config-group';+import TextLabelPanel from './text-label-panel';import {LAYER_VIS_CONFIGS} from 'layers/layer-factory';@@ -151,6 +152,13 @@ export default class LayerConfigurator extends Component {/></LayerConfigGroup>) : null}++ {/* text label */}+ <TextLabelPanel+ visConfiguratorProps={visConfiguratorProps}+ layerConfiguratorProps={layerConfiguratorProps}+ textLabel={layer.config.textLabel}+ />{/* high precision */}<LayerConfigGroup{...LAYER_VIS_CONFIGS['hi-precision']}diff --git a/src/components/side-panel/layer-panel/text-label-panel.js b/src/components/side-panel/layer-panel/text-label-panel.jsnew file mode 100644index 000000000..d08b60706--- /dev/null+++ b/src/components/side-panel/layer-panel/text-label-panel.js@@ -0,0 +1,105 @@++// Copyright (c) 2018 Uber Technologies, Inc.+//+// Permission is hereby granted, free of charge, to any person obtaining a copy+// of this software and associated documentation files (the "Software"), to deal+// in the Software without restriction, including without limitation the rights+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+// copies of the Software, and to permit persons to whom the Software is+// furnished to do so, subject to the following conditions:+//+// The above copyright notice and this permission notice shall be included in+// all copies or substantial portions of the Software.+//+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+// THE SOFTWARE.++import React, {Component} from 'react';+import PropTypes from 'prop-types';++import {PanelLabel} from 'components/common/styled-components';+import ColorSelector from './color-selector';+import FieldSelector from 'components/common/field-selector';+import ItemSelector from 'components/common/item-selector/item-selector';+import LayerConfigGroup from './layer-config-group';+import RangeSlider from 'components/common/range-slider';++import {LAYER_TEXT_CONFIGS} from 'layers/layer-factory';++export default class LayerConfigurator extends Component {+ static propTypes = {+ layerConfiguratorProps: PropTypes.object.isRequired,+ textLabel: PropTypes.object.isRequired,+ visConfiguratorProps: PropTypes.object.isRequired+ };++ onAttributeChange = attribute => {+ const {layerConfiguratorProps, textLabel} = this.props;+ return v => layerConfiguratorProps.onChange({+ textLabel: {+ ...textLabel,+ [attribute]: v+ }+ });+ };++ onChangeTextAnchor = anchor => {+ const {layerConfiguratorProps, textLabel} = this.props;+ // TODO: we can be smarter on determining the offset of the text+ layerConfiguratorProps.onChange({+ textLabel: {+ ...textLabel,+ anchor,+ offset: [+ anchor === 'start' ? 10 : anchor === 'end' ? -10 : 0,+ anchor === 'middle' ? 10 : 0+ ]+ }+ });+ };++ render() {+ const {+ visConfiguratorProps,+ textLabel+ } = this.props;+ return (+ <LayerConfigGroup label={'text'}>+ <PanelLabel>{`Show text label based on`}</PanelLabel>+ <FieldSelector+ fields={visConfiguratorProps.fields}+ value={textLabel.field && textLabel.field.name || 'select a field'}+ placeholder={'empty'}+ onSelect={this.onAttributeChange('field')}+ erasable+ />+ <PanelLabel>{`Font size`}</PanelLabel>+ <RangeSlider+ {...LAYER_TEXT_CONFIGS.fontSize}+ value1={textLabel.size}+ onChange={v => this.onAttributeChange('size')(v[1])}+ />+ <PanelLabel>{`Font color`}</PanelLabel>+ <ColorSelector+ colorSets={[+ {+ selectedColor: textLabel.color,+ setColor: this.onAttributeChange('color')+ }+ ]}+ />+ <PanelLabel>{`Text anchor`}</PanelLabel>+ <ItemSelector+ {...LAYER_TEXT_CONFIGS.textAnchor}+ selectedItems={textLabel.anchor}+ onChange={this.onChangeTextAnchor}+ />+ </LayerConfigGroup>+ );+ }+}diff --git a/src/layers/base-layer.js b/src/layers/base-layer.jsindex d8b9bfbb6..e6bb3a733 100644--- a/src/layers/base-layer.js+++ b/src/layers/base-layer.js@@ -297,7 +297,15 @@ export default class Layer {sizeScale: 'linear',sizeField: null,- visConfig: {}+ visConfig: {},++ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ }};}diff --git a/src/layers/layer-factory.js b/src/layers/layer-factory.jsindex 704c1f26b..6e0712608 100644--- a/src/layers/layer-factory.js+++ b/src/layers/layer-factory.js@@ -307,3 +307,21 @@ export const LAYER_VIS_CONFIGS = {property: 'radius'}};++export const LAYER_TEXT_CONFIGS = {+ fontSize: {+ type: 'number',+ range: [1, 100],+ value0: 1,+ step: 1,+ isRanged: false,+ label: 'Font size',+ showInput: true+ },+ textAnchor: {+ type: 'select',+ options: ['start', 'middle', 'end'],+ multiSelect: false,+ searchable: false+ }+};diff --git a/src/layers/point-layer/point-layer.js b/src/layers/point-layer/point-layer.jsindex f6a5a7637..666f6abfd 100644--- a/src/layers/point-layer/point-layer.js+++ b/src/layers/point-layer/point-layer.js@@ -20,6 +20,7 @@import Layer from '../base-layer';import memoize from 'lodash.memoize';+import {TextLayer} from 'deck.gl';import ScatterplotBrushingLayer from 'deckgl-layers/scatterplot-brushing-layer/scatterplot-brushing-layer';import {hexToRgb} from 'utils/color-utils';import PointLayerIcon from './point-layer-icon';@@ -264,7 +265,30 @@ export default class PointLayer extends Layer {colorScale: this.config.colorScale}}- })+ }),+ // text label layer+ ...(this.config.textLabel.field+ ? [+ new TextLayer({+ id: `${this.id}-label`,+ data: data.data,+ getPosition: data.getPosition,+ getPixelOffset: this.config.textLabel.offset,+ getSize: this.config.textLabel.size,+ getTextAnchor: this.config.textLabel.anchor,+ getText: d => String(d.data[this.config.textLabel.field.tableFieldIndex - 1]),+ getColor: d => this.config.textLabel.color,+ updateTriggers: {+ getPosition: data.getPosition,+ getPixelOffset: this.config.textLabel.offset,+ getText: this.config.textLabel.field,+ getTextAnchor: this.config.textLabel.anchor,+ getSize: this.config.textLabel.size,+ getColor: this.config.textLabel.color+ }+ })+ ]+ : [])];}}diff --git a/test/fixtures/state-saved-v0.js b/test/fixtures/state-saved-v0.jsindex 154651640..5aadf6311 100644--- a/test/fixtures/state-saved-v0.js+++ b/test/fixtures/state-saved-v0.js@@ -1033,6 +1033,13 @@ mergedLayer0.config = {sizeField: null,sizeScale: 'linear',sizeDomain: [0, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },visConfig: {radius: 270.4,opacity: 0.8,@@ -1103,6 +1110,13 @@ mergedLayer1.config = {sizeField: null,sizeScale: 'linear',sizeDomain: [0, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },visConfig: {opacity: 0.41,thickness: 2,@@ -1162,6 +1176,13 @@ mergedLayer2.config = {},sizeDomain: [78, 694],sizeScale: 'linear',+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },visConfig: {radius: 10,opacity: 0.8,@@ -1232,6 +1253,13 @@ mergedLayer3.config = {sizeField: null,sizeScale: 'linear',sizeDomain: [0, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },visConfig: {colorAggregation: 'maximum',sizeAggregation: 'average',@@ -1300,6 +1328,13 @@ mergedLayer4.config = {sizeField: null,sizeScale: 'linear',sizeDomain: [0, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },heightField: null,heightDomain: [0, 1],heightScale: 'linear',diff --git a/test/fixtures/state-saved-v1-1.js b/test/fixtures/state-saved-v1-1.jsindex 5f6651fcc..9cae56383 100644--- a/test/fixtures/state-saved-v1-1.js+++ b/test/fixtures/state-saved-v1-1.js@@ -888,6 +888,13 @@ mergedLayer0.config = {sizeField: null,sizeScale: 'linear',sizeDomain: [0, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },heightField: {name: 'c_a_v',type: 'real',@@ -2680,6 +2687,13 @@ mergedLayer1.config = {},sizeScale: 'linear',sizeDomain: [0.970877074, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },heightField: null,heightScale: 'linear',heightDomain: [0, 1],diff --git a/test/fixtures/state-saved-v1-3.js b/test/fixtures/state-saved-v1-3.jsindex 205910dce..d4d289e14 100644--- a/test/fixtures/state-saved-v1-3.js+++ b/test/fixtures/state-saved-v1-3.js@@ -395,6 +395,13 @@ mergedLayer0.config = {},sizeScale: 'sqrt',sizeDomain: [1000, 3000],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },visConfig: {radius: 10,fixedRadius: true,@@ -447,6 +454,13 @@ mergedLayer1.config = {sizeField: null,sizeScale: 'linear',sizeDomain: [0, 1],+ textLabel: {+ field: null,+ color: [255, 255, 255],+ size: 50,+ offset: [0, 0],+ anchor: 'middle'+ },visConfig: {opacity: 0.8,worldUnitSize: 2.73,