diff --git a/examples/polygon/fill/demo/usa.js b/examples/polygon/fill/demo/usa.js index 8b6e56d9be..dbb3f92f77 100644 --- a/examples/polygon/fill/demo/usa.js +++ b/examples/polygon/fill/demo/usa.js @@ -19,17 +19,11 @@ scene.on('loaded', () => { const color = [ 'rgb(255,255,217)', 'rgb(237,248,177)', 'rgb(199,233,180)', 'rgb(127,205,187)', 'rgb(65,182,196)', 'rgb(29,145,192)', 'rgb(34,94,168)', 'rgb(12,44,132)' ]; const layer = new PolygonLayer({}) .source(data) + .scale('density', { + type: 'quantize' + }) .color( - 'density', d => { - return d > 1000 ? color[7] : - d > 500 ? color[6] : - d > 200 ? color[5] : - d > 100 ? color[4] : - d > 50 ? color[3] : - d > 20 ? color[2] : - d > 10 ? color[1] : - color[0]; - } + 'density', color ) .shape('fill') .active(true) diff --git a/packages/layers/package.json b/packages/layers/package.json index ca2d8acbe2..4dfda0b652 100644 --- a/packages/layers/package.json +++ b/packages/layers/package.json @@ -35,7 +35,7 @@ "@turf/union": "^6.5.0", "d3-array": "1", "d3-color": "^1.4.0", - "d3-interpolate": "1.4.0", + "d3-interpolate": "1.0.0", "d3-scale": "2", "earcut": "^2.2.1", "eventemitter3": "^4.0.0", @@ -51,7 +51,7 @@ "devDependencies": { "@types/d3-array": "^2.0.0", "@types/d3-color": "^1.2.2", - "@types/d3-interpolate": "1.4.0", + "@types/d3-interpolate": "1.1.6", "@types/d3-scale": "^2.1.1", "@types/earcut": "^2.1.0", "@types/gl-matrix": "^2.4.5", diff --git a/packages/layers/src/plugins/FeatureScalePlugin.ts b/packages/layers/src/plugins/FeatureScalePlugin.ts index 9a2d359b10..4bc13c0e18 100644 --- a/packages/layers/src/plugins/FeatureScalePlugin.ts +++ b/packages/layers/src/plugins/FeatureScalePlugin.ts @@ -129,27 +129,46 @@ export default class FeatureScalePlugin implements ILayerPlugin { attributeScale.type = StyleScaleType.VARIABLE; scales.forEach((scale) => { // 如果设置了回调, 这不需要设置range - if (!attributeScale.callback) { - if ( - attributeScale.values && - attributeScale.values !== 'text' && - scale.option?.type !== ScaleTypes.DIVERGING && - scale.option?.type !== ScaleTypes.SEQUENTIAL - ) { - scale.scale.range(attributeScale.values); // 判断常量, 默认值 - } else if (scale.option?.type === ScaleTypes.CAT) { - // 如果没有设置初值且 类型为cat,range ==domain; - scale.scale.range(scale.option.domain); - } else if ( - scale.option?.type === ScaleTypes.DIVERGING || - scale.option?.type === ScaleTypes.SEQUENTIAL - ) { - scale.scale.interpolator( - // @ts-ignore - d3interpolate.interpolateRgbBasis(attributeScale.values), - ); + if (!attributeScale.callback && attributeScale.values !== 'text') { + switch (scale.option?.type) { + case ScaleTypes.LOG: + case ScaleTypes.LINEAR: + case ScaleTypes.POWER: + if ( + attributeScale.values && + attributeScale.values.length > 2 + ) { + const tick = scale.scale.ticks( + attributeScale.values.length, + ); + scale.scale.domain(tick); + } + attributeScale.values + ? scale.scale.range(attributeScale.values) + : scale.scale.range(scale.option.domain); + break; + case ScaleTypes.QUANTILE: + case ScaleTypes.QUANTIZE: + case ScaleTypes.THRESHOLD: + scale.scale.range(attributeScale.values); // + break; + case ScaleTypes.CAT: + attributeScale.values + ? scale.scale.range(attributeScale.values) + : scale.scale.range(scale.option.domain); + break; + case ScaleTypes.DIVERGING: + case ScaleTypes.SEQUENTIAL: + scale.scale.interpolator( + // @ts-ignore + d3interpolate.interpolateRgbBasis(attributeScale.values), + ); + break; } } + if (attributeScale.values === 'text') { + scale.scale.range(scale.option?.domain); + } }); } else { // 设置attribute 常量值 常量直接在value取值