fix: identity scale && text padding (#1541)

* fix: identity scale && text padding

* fix: lint error

* fix: format lint
This commit is contained in:
@thinkinggis 2022-12-16 10:33:56 +08:00 committed by GitHub
parent 8241f5a872
commit 55ae0001c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 30 deletions

View File

@ -23,7 +23,9 @@ export default () => {
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "A" "name": "A",
"color":"red",
size:10,
}, },
"geometry": { "geometry": {
"type": "Point", "type": "Point",
@ -36,7 +38,8 @@ export default () => {
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "B" "name": "B",
"color":"yellow",
}, },
"geometry": { "geometry": {
"type": "Point", "type": "Point",
@ -49,7 +52,39 @@ export default () => {
{ {
"type": "Feature", "type": "Feature",
"properties": { "properties": {
"name": "C" "name": "E",
"color":"blue",
size:13,
},
"geometry": {
"type": "Point",
"coordinates": [
121.9375,
27.059125784374068
]
}
},
{
"type": "Feature",
"properties": {
"name": "C",
"color":"red",
size:10,
},
"geometry": {
"type": "Point",
"coordinates": [
107.22656249999999,
37.020098201368114
]
}
},
{
"type": "Feature",
"properties": {
"name": "F",
"color":"red",
size:15,
}, },
"geometry": { "geometry": {
"type": "Point", "type": "Point",
@ -61,34 +96,44 @@ export default () => {
} }
] ]
}) })
// .scale('color',{
// type:'identity'
// })
.scale('size',{
type:'identity'
})
.shape('circle') .shape('circle')
.size(12) .size('size')
.active({ .active({
color:'red', color:'red',
}) })
.select(true) .select(true)
.color('red') .color('color')
.style({ .style({
stroke: ['name', (name)=>{ // stroke: ['name', (name)=>{
switch (name) { // switch (name) {
case 'A' : // case 'A' :
return '#fc8d59'; // return '#fc8d59';
case 'B' : // case 'B' :
return '#91cf60'; // return '#91cf60';
default: // default:
return '#ffffbf'; // return '#ffffbf';
} // }
} // }
], // 描边颜色 // ], // 描边颜色
strokeWidth: 5, // 描边宽度 strokeWidth: 5, // 描边宽度
}); });
scene.addLayer(pointLayer); scene.addLayer(pointLayer);
pointLayer.on('inited',()=>{
console.log(pointLayer.getLegend('size'))
})
}); });

View File

@ -2582,14 +2582,13 @@ export default () => {
coordinates: "center" coordinates: "center"
} }
}) })
.color("#fff") .color("#f00")
.shape("name", "text") .shape("name", "text")
.size(12) .size(12)
.style({ .style({
opacity: 1, opacity: 1,
stroke: "#fff", stroke: "#fff",
strokeWidth: 0, strokeWidth: 1,
padding: [5, 5],
textAllowOverlap: false textAllowOverlap: false
}); });

View File

@ -17,6 +17,7 @@ import * as d3 from 'd3-scale';
import { injectable } from 'inversify'; import { injectable } from 'inversify';
import { isNil, isString, uniq } from 'lodash'; import { isNil, isString, uniq } from 'lodash';
import 'reflect-metadata'; import 'reflect-metadata';
import identity from '../utils/identityScale';
const dateRegex = const dateRegex =
/^(?:(?!0000)[0-9]{4}([-/.]+)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2\2(?:29))(\s+([01]|([01][0-9]|2[0-3])):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9]))?$/; /^(?:(?!0000)[0-9]{4}([-/.]+)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2\2(?:29))(\s+([01]|([01][0-9]|2[0-3])):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9]))?$/;
@ -25,7 +26,7 @@ const scaleMap = {
[ScaleTypes.LINEAR]: d3.scaleLinear, [ScaleTypes.LINEAR]: d3.scaleLinear,
[ScaleTypes.POWER]: d3.scalePow, [ScaleTypes.POWER]: d3.scalePow,
[ScaleTypes.LOG]: d3.scaleLog, [ScaleTypes.LOG]: d3.scaleLog,
[ScaleTypes.IDENTITY]: d3.scaleIdentity, [ScaleTypes.IDENTITY]: identity,
[ScaleTypes.SEQUENTIAL]: d3.scaleSequential, [ScaleTypes.SEQUENTIAL]: d3.scaleSequential,
[ScaleTypes.TIME]: d3.scaleTime, [ScaleTypes.TIME]: d3.scaleTime,
[ScaleTypes.QUANTILE]: d3.scaleQuantile, [ScaleTypes.QUANTILE]: d3.scaleQuantile,
@ -34,7 +35,6 @@ const scaleMap = {
[ScaleTypes.CAT]: d3.scaleOrdinal, [ScaleTypes.CAT]: d3.scaleOrdinal,
[ScaleTypes.DIVERGING]: d3.scaleDiverging, [ScaleTypes.DIVERGING]: d3.scaleDiverging,
}; };
/** /**
* Source Scale StyleAttribute 使 * Source Scale StyleAttribute 使
*/ */
@ -155,6 +155,8 @@ export default class FeatureScalePlugin implements ILayerPlugin {
case ScaleTypes.THRESHOLD: case ScaleTypes.THRESHOLD:
scale.scale.range(attributeScale.values); // scale.scale.range(attributeScale.values); //
break; break;
case ScaleTypes.IDENTITY: // 不做处理xe
break;
case ScaleTypes.CAT: case ScaleTypes.CAT:
attributeScale.values attributeScale.values
? scale.scale.range(attributeScale.values) ? scale.scale.range(attributeScale.values)
@ -279,14 +281,7 @@ export default class FeatureScalePlugin implements ILayerPlugin {
const values = data?.map((item) => item[field]) || []; const values = data?.map((item) => item[field]) || [];
if (scaleOption?.domain) { if (scaleOption?.domain) {
cfg.domain = scaleOption?.domain; cfg.domain = scaleOption?.domain;
} else if ( } else if (type === ScaleTypes.CAT || type === ScaleTypes.IDENTITY) {
type !== ScaleTypes.CAT &&
type !== ScaleTypes.QUANTILE &&
type !== ScaleTypes.DIVERGING
) {
// linear/
cfg.domain = extent(values);
} else if (type === ScaleTypes.CAT) {
cfg.domain = uniq(values); cfg.domain = uniq(values);
} else if (type === ScaleTypes.QUANTILE) { } else if (type === ScaleTypes.QUANTILE) {
cfg.domain = values; cfg.domain = values;
@ -297,6 +292,9 @@ export default class FeatureScalePlugin implements ILayerPlugin {
? scaleOption?.neutral ? scaleOption?.neutral
: (minMax[0] + minMax[1]) / 2; : (minMax[0] + minMax[1]) / 2;
cfg.domain = [minMax[0], neutral, minMax[1]]; cfg.domain = [minMax[0], neutral, minMax[1]];
} else {
// linear/Power/log
cfg.domain = extent(values);
} }
return { ...cfg, ...scaleOption }; return { ...cfg, ...scaleOption };
} }

View File

@ -474,7 +474,7 @@ export default class TextModel extends BaseModel {
*/ */
private filterGlyphs() { private filterGlyphs() {
const { const {
padding = [4, 4], padding = [0, 0],
textAllowOverlap = false, textAllowOverlap = false,
} = this.layer.getLayerConfig() as IPointLayerStyleOptions; } = this.layer.getLayerConfig() as IPointLayerStyleOptions;
if (textAllowOverlap) { if (textAllowOverlap) {

View File

@ -0,0 +1,34 @@
export default function identity(d: any) {
let unknown: any;
let domain: any = [];
function scale(x: any) {
return x == null ? unknown : x;
}
scale.invert = scale;
scale.domain = scale.range = (v?: any) => {
if (v) {
domain = v;
return v;
}
return domain;
};
scale.unknown = (v: any) => {
if (v) {
unknown = v;
return v;
}
return unknown;
};
scale.copy = () => {
return identity(d).unknown(unknown);
};
return scale;
}