Commit 648cd97a by liimin

视觉调优:热力层增强,聚合圆圈弱化为数字标签

热力存在感低是三个原因叠加,不只是被圆圈挡住:

1) 归一化上限取全局最大值(maxCount)。heatmap.js 按 count/max 归一化,
   只要存在一个极端桶,其余所有桶的比值被压到渐变最左端 -> 全图靛蓝。
   改为 HeatLayer 内部取 P90,头部 10% 饱和显示,色阶整体拉开。
2) 渐变暖色门槛过高。金黄原在 0.62,中密度区一片青蓝,在 whitesmoke
   浅底上不跳。暖色整体下压:0.52 金黄 / 0.70 橙 / 0.86 橙红 / 1.0 深红。
3) 聚合圆圈实心填充+外发光,半径随案件数 11~32px,单桶直径最大 70px,
   几百个铺下来把热力挡到只剩边角。

聚合层改为「极淡白底衬 + 深色字 + 白描边」:
  - 底衬半径只由文字宽度定(9~16px),不再随案件数放大,遮挡面积约 1/5;
  - 去外发光与彩色实心填充;字色改品牌深靛蓝(圆圈透明后白字会糊在热力里);
  - 字号 12/13 -> 11/10;底衬变小后命中判定加 7px 宽容(HIT_PAD);
  - 新增 CLUSTER_LABEL 配色常量;删 bucketFill/bucketStroke 死函数。

热力层:radius 26->40、opacity 0.82->0.95、minOpacity 0->0.06;
store 默认值改读 DEFAULT_HEAT_STYLE;半径滑块上限 60->90。
MapStage.setDataset 不再传 maxCount(改由 HeatLayer 内部取 P90)。

typecheck 0 错误;build 通过(3165 模块)
1 parent 974b75c1
......@@ -128,8 +128,10 @@ export function MapStage() {
bbox: null,
filter: filterRef.current,
});
// 不传 res.maxCount:热力层的归一化上限在 HeatLayer 内部取 P90,
// 用全局最大值会把绝大多数桶压到渐变最左端,热力几乎看不见。
if (res.bucketCount === 0) layer.clear();
else layer.setDataset(res.lng, res.lat, res.count, res.maxCount);
else layer.setDataset(res.lng, res.lat, res.count);
patchPerf({ heatPoints: res.bucketCount, heatGridPx: res.gridPx });
} catch (err) {
console.error('[aggregate] 热力层失败', err);
......
import { bucketFill, gradientAt, rgbCss } from './palette';
import { CLUSTER_LABEL, gradientAt, rgbCss } from './palette';
export interface ClusterLayerOptions {
/** 是否绘制数量数字。桶均点数过低时会自动关闭,见下方说明 */
......@@ -24,15 +24,17 @@ export type ClusterMode = 'cluster' | 'scatter';
export const DEFAULT_CLUSTER_OPTIONS: ClusterLayerOptions = { showNumbers: true };
const MIN_R = 11;
const MAX_R = 32;
/** 数字底衬的最小半径。只包住文字,不再按案件数放大 */
const LABEL_MIN_R = 9;
/** 命中判定的宽容像素。底衬变小后不加这个会很难点中 */
const HIT_PAD = 7;
/** 散点模式下的点半径 */
const SCATTER_R = 4.5;
/** 半径小于这个像素值时数字已经放不下,直接不画 */
const MIN_R_FOR_TEXT = 11.5;
const SCATTER_R = 4;
/** 视口裁剪的外扩量,取底衬可能出现的最大半径 */
const CULL_PAD = 26;
/**
* 聚合圆圈层 —— 在本项目里**没有**使用高德的 AMap.MarkerCluster,原因有两条,都是硬约束:
* 聚合数字层 —— 在本项目里**没有**使用高德的 AMap.MarkerCluster,原因有两条,都是硬约束:
*
* 1) 数字口径对不上。MarkerCluster 的 cluster.count 是「它自己聚合进去的 marker 个数」。
* 而我们的桶来自离线/视口聚合,代表的是「案件数」。把聚合桶当作 marker 喂进去,
......@@ -48,7 +50,17 @@ const MIN_R_FOR_TEXT = 11.5;
* - 命中检测自己算(屏幕上到各圆心的距离),不依赖覆盖物事件体系;
* - 桶本身已经带了 count,数字口径天然正确。
*
* 额外的模式判断:截图实测发现高缩放下桶均点数会降到 1.4 —— 满屏圆圈都写着「1」
* ⚠️ 视觉取舍(2026-10-13 实测后改):初版画的是「实心渐变圆 + 白字 + 外发光」,
* 圆半径按案件数在 11~32px 之间缩放,加上外发光单桶直径最大 70px。几百个铺下来
* 把热力底色挡得只剩边角 —— 而热力图才是这一页的主角,聚合层要的只是那个数字。
*
* 现在改为「极淡白底衬 + 深色字 + 白色描边」:
* - 底衬半径只由文字宽度决定(9~16px),不再随案件数放大,遮挡面积降到约 1/5;
* - 去掉外发光与彩色实心填充,这两样既是遮挡源又抢视觉;
* - 字色改品牌深靛蓝 + 白色描边:圆圈透明后白字会糊在热力暖色里看不见,
* 描边则保证它在金黄/橙红这类高亮热力上依然可读。
*
* 额外的模式判断:截图实测发现高缩放下桶均点数会降到 1.4 —— 满屏都写着「1」
* 既没信息量也挡住热力。此时自动切到散点模式(只画小圆点、不写数字),
* 对应用户在 z17+ 想看的其实是「这一片具体有哪些事故点」。
*/
......@@ -64,13 +76,15 @@ export class ClusterLayer {
private count: Int32Array = new Int32Array(0);
private gx: Int32Array = new Int32Array(0);
private gy: Int32Array = new Int32Array(0);
private refCount = 12;
/* 绘制缓存:屏幕坐标,供命中检测复用,避免点击时再做一次投影 */
private sx = new Float32Array(0);
private sy = new Float32Array(0);
private sr = new Float32Array(0);
/* 文字宽度缓存:标签种类有限(数字 + 万),缓存后每帧省掉几百次 measureText */
private textW = new Map<string, number>();
private visible = true;
private mode: ClusterMode = 'cluster';
private clickHandler: ((e: { pixel?: { x: number; y: number } }) => void) | null = null;
......@@ -126,13 +140,9 @@ export class ClusterLayer {
this.gx = gx;
this.gy = gy;
// 半径参考值用 P90 而不是最大值:个别超大桶会把其余几百个桶全压成同一个半径,
// 圆圈大小就失去了区分度。
// 桶最大点数 ≤ 2 时满屏都是「1」,没有信息量也挡住热力,自动切散点模式
let max = 0;
for (let i = 0; i < count.length; i += 1) if (count[i] > max) max = count[i];
this.refCount = Math.max(this.percentile(count, 0.9), 12);
// 桶最大点数 ≤ 2 时满屏都是「1」,没有信息量也挡住热力,自动切散点模式
this.mode = max <= 2 ? 'scatter' : 'cluster';
this.sx = new Float32Array(count.length);
this.sy = new Float32Array(count.length);
......@@ -176,13 +186,6 @@ export class ClusterLayer {
return sum / this.count.length;
}
private percentile(arr: Int32Array, p: number): number {
if (!arr.length) return 1;
const sorted = Int32Array.from(arr).sort();
const idx = Math.min(sorted.length - 1, Math.max(0, Math.round((sorted.length - 1) * p)));
return sorted[idx];
}
private drawScheduled = false;
private scheduleDraw() {
if (this.drawScheduled) return;
......@@ -216,16 +219,16 @@ export class ClusterLayer {
const w = size.width;
const h = size.height;
const pad = MAX_R + 6;
const scatter = this.mode === 'scatter';
const logRef = Math.log1p(this.refCount);
// 边界裁剪:桶只覆盖视口(+边距),仍可能有少量在屏幕外,避免无谓的 arc 调用
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
for (let i = 0; i < this.count.length; i += 1) {
const p = this.map.lngLatToContainer([this.lng[i] / 1e6, this.lat[i] / 1e6]);
const x = p.x;
const y = p.y;
if (x < -pad || y < -pad || x > w + pad || y > h + pad) {
if (x < -CULL_PAD || y < -CULL_PAD || x > w + CULL_PAD || y > h + CULL_PAD) {
this.sr[i] = 0;
continue;
}
......@@ -233,51 +236,54 @@ export class ClusterLayer {
this.sy[i] = y;
const n = this.count[i];
let r: number;
let t = 0;
if (scatter) {
r = SCATTER_R;
} else {
t = Math.min(Math.log1p(n) / logRef, 1);
r = MIN_R + (MAX_R - MIN_R) * t;
}
this.sr[i] = r;
const fill = scatter ? rgbCss(gradientAt(0.5), 0.9) : bucketFill(t);
if (scatter) {
this.sr[i] = SCATTER_R;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fillStyle = fill;
ctx.arc(x, y, SCATTER_R, 0, Math.PI * 2);
ctx.fillStyle = rgbCss(gradientAt(0.62), 0.92);
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgba(255,255,255,.85)';
ctx.strokeStyle = 'rgba(255,255,255,.9)';
ctx.stroke();
continue;
}
// 外发光:让圆圈从热力底色里浮出来
ctx.beginPath();
ctx.arc(x, y, r + 3, 0, Math.PI * 2);
ctx.fillStyle = rgbCss(gradientAt(t * 0.82 + 0.18), 0.18);
ctx.fill();
// 关掉数字时只留一个极小的定位点,不画任何底衬
if (!this.opts.showNumbers) {
this.sr[i] = 5;
ctx.beginPath();
ctx.arc(x, y, 3.5, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(34,43,95,.45)';
ctx.fill();
continue;
}
const label = formatCount(n);
const wide = label.length > 3;
ctx.font = wide ? CLUSTER_LABEL.fontWide : CLUSTER_LABEL.font;
let tw = this.textW.get(label);
if (tw === undefined) {
tw = ctx.measureText(label).width;
this.textW.set(label, tw);
}
// 底衬半径只由文字宽度决定,不随案件数放大 —— 这是遮挡能降下来的关键
const r = Math.max(LABEL_MIN_R, tw / 2 + 4);
this.sr[i] = r;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fillStyle = fill;
ctx.fillStyle = CLUSTER_LABEL.plate;
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = rgbCss([255, 255, 255], 0.92);
ctx.stroke();
if (this.opts.showNumbers && r >= MIN_R_FOR_TEXT) {
ctx.fillStyle = '#FFFFFF';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const size2 = r >= 22 ? 13 : 12;
ctx.font = `700 ${size2}px "PingFang SC","Microsoft YaHei",sans-serif`;
ctx.fillText(formatCount(n), x, y + 0.5);
}
// 先描白边再填深色字:热力底色是暖色时,深色字靠白描边才立得住
ctx.lineWidth = CLUSTER_LABEL.haloWidth;
ctx.strokeStyle = CLUSTER_LABEL.halo;
ctx.strokeText(label, x, y + 0.5);
ctx.fillStyle = CLUSTER_LABEL.color;
ctx.fillText(label, x, y + 0.5);
}
}
......@@ -290,7 +296,7 @@ export class ClusterLayer {
if (r <= 0) continue;
const dx = x - this.sx[i];
const dy = y - this.sy[i];
const reach = r + 4;
const reach = r + HIT_PAD;
if (dx * dx + dy * dy > reach * reach) continue;
if (this.count[i] > bestCount) {
bestCount = this.count[i];
......@@ -318,7 +324,7 @@ export class ClusterLayer {
}
}
/** 圆圈里的数字。四位数以上压缩显示,否则圆圈里塞不下 */
/** 圆圈里的数字。四位数以上压缩显示,否则底衬会被撑宽 */
export function formatCount(n: number): string {
if (n >= 10000) return `${(n / 10000).toFixed(1)}万`;
return String(n);
......
......@@ -4,11 +4,26 @@ import { resolveHeatMapCtor } from './loadAmap';
export interface HeatLayerStyle {
/** 热力扩散半径,像素 */
radius: number;
/** 热力不透明度上限(maxOpacity)。下限恒为 0,见 ensure() */
/** 热力不透明度上限(maxOpacity)。下限恒为 MIN_OPACITY,见 ensure() */
opacity: number;
}
export const DEFAULT_HEAT_STYLE: HeatLayerStyle = { radius: 26, opacity: 0.82 };
/** 从 26 → 40:半径越大色块越连贯,低密度区也能连成片而不是散点 */
export const DEFAULT_HEAT_STYLE: HeatLayerStyle = { radius: 40, opacity: 0.95 };
/**
* 不透明度下限。给一点点而不是 0,稀疏区会有一层极淡的底色,
* 让「这里有事但不多」和「这里完全没事」在视觉上区分开。
* 值不能高,否则整张图糊成一片,反而看不出峰值。
*/
const MIN_OPACITY = 0.06;
/**
* 归一化上限取的分位。heatmap.js 按 count/max 归一化权重,
* 取 0.9 意味着头部 10% 的桶饱和显示为最高色(热力图常规做法),
* 其余桶的色阶被拉开 —— 详见 setDataset() 里的说明。
*/
const MAX_REF_PERCENTILE = 0.9;
/**
* 热力底色层。包一层高德原生 AMap.HeatMap,把「数据集 → setDataSet」的细节收口。
......@@ -23,7 +38,7 @@ export const DEFAULT_HEAT_STYLE: HeatLayerStyle = { radius: 26, opacity: 0.82 };
* "Failed to execute 'uniform2fv' on 'WebGLRenderingContext':
* The provided value cannot be converted to a sequence"
* 且该渲染帧挂在地图主渲染循环上,一崩连底图瓦片一起变空白(表现:只剩自绘的
* 聚合圆圈层还在,地图与热力全无)。所以这里恒传 [0, max]。
* 聚合圆圈层还在,地图与热力全无)。所以这里恒传 [MIN_OPACITY, max]。
*
* 数据集来自 Worker 的全量降采样(视口无关),不是原始点位 —— 原因见 config/grid.json。
* 这里只负责把桶中心 + 权重喂给高德,并保证重复 setDataSet 时不会累计泄漏。
......@@ -42,7 +57,7 @@ export class HeatLayer {
this.heat = new Ctor(this.map, {
radius: this.style.radius,
// ⚠️ 必须是 [min, max] 数组,传 number 会在 uniform2fv 处崩溃 —— 见类注释
opacity: [0, this.style.opacity],
opacity: [MIN_OPACITY, this.style.opacity],
gradient: HEAT_GRADIENT,
zooms: [3, 20],
visible: this.visible,
......@@ -52,14 +67,25 @@ export class HeatLayer {
return this.heat;
}
setDataset(lng: Int32Array, lat: Int32Array, count: Int32Array, maxCount: number) {
/**
* 归一化上限为什么用 P90 而不是最大值:
*
* heatmap.js 的权重是 count / max。用全局最大桶当 max 时,只要存在一个
* 极端桶(比如某园区集中报了 300 起),其余几百个桶的比值就被压到 0.05 以下,
* 颜色全落在渐变最左端的靛蓝 —— 实测观感就是「热力几乎看不见,只剩聚合圆圈」。
*
* 取 P90 后,头部 10% 的桶饱和显示为最高色,其余桶的色阶被整体拉开。
* 这是热力图的常规做法:热点区「封顶」在最高色,本来也不需要再区分 300 和 800。
*/
setDataset(lng: Int32Array, lat: Int32Array, count: Int32Array) {
const heat = this.ensure();
const n = lng.length;
const data: Array<{ lng: number; lat: number; count: number }> = new Array(n);
for (let i = 0; i < n; i += 1) {
data[i] = { lng: lng[i] / 1e6, lat: lat[i] / 1e6, count: count[i] };
}
this.dataset = { data, max: Math.max(maxCount, 1) };
const ref = Math.max(percentile(count, MAX_REF_PERCENTILE), 1);
this.dataset = { data, max: ref };
heat.setDataSet(this.dataset);
heat.setOptions({ visible: this.visible });
}
......@@ -72,10 +98,10 @@ export class HeatLayer {
setStyle(style: Partial<HeatLayerStyle>) {
this.style = { ...this.style, ...style };
// opacity 整体是 [min, max],界面上拖的只是上限,下限恒为 0
// opacity 整体是 [min, max],界面上拖的只是上限,下限恒为 MIN_OPACITY
this.heat?.setOptions({
radius: this.style.radius,
opacity: [0, this.style.opacity],
opacity: [MIN_OPACITY, this.style.opacity],
});
}
......@@ -98,3 +124,12 @@ export class HeatLayer {
this.dataset = null;
}
}
/** 取分位值。桶数组量级在几百到两万,排序开销可忽略 */
function percentile(arr: Int32Array, p: number): number {
const n = arr.length;
if (n === 0) return 1;
const sorted = Int32Array.from(arr).sort();
const idx = Math.min(n - 1, Math.max(0, Math.round((n - 1) * p)));
return sorted[idx];
}
......@@ -30,33 +30,61 @@ export const PALETTE = {
} as const;
/**
* 热力渐变:冷 → 暖,起点用品牌靛蓝,末端用告警红。
* 热力渐变:冷 → 暖,起点用品牌靛蓝,末端用告警深红。
*
* 用「冷到暖」而不是「单一色深浅」的理由:监管人员看热力图的第一诉求是
* 「哪儿最危险」,暖色在高密度区天然抓眼;冷色起点同时保住了与品牌主色的关联。
*
* ⚠️ 停靠点分布是调过的,不是均匀铺开。初版把金黄放在 0.62,结果中密度区
* 大面积落在青蓝段,在 whitesmoke 浅色底图上几乎不跳 —— 实测观感是
* 「热力存在感很低」。现在把暖色整体下压:0.52 进金黄、0.70 进橙、
* 0.86 进橙红。代价是低密度区的冷暖差异被压缩,但本项目的诉求本来就是
* 让热点先跳出来,这个交换是值得的。
*
* 格式照高德官方 demo(selflayer/heatmap)的 gradient:{ 0~1: 'rgb(...)' } 字符串。
* 官方示例用的就是 rgb() 而非 hex,沿用它的写法最稳。
*/
export const HEAT_GRADIENT: Record<number, string> = {
0.0: 'rgb(107, 120, 190)',
0.25: 'rgb(40, 167, 225)',
0.45: 'rgb(10, 201, 245)',
0.62: 'rgb(252, 201, 41)',
0.8: 'rgb(255, 138, 61)',
1.0: 'rgb(229, 72, 77)',
0.0: 'rgb(70, 83, 162)',
0.18: 'rgb(40, 167, 225)',
0.36: 'rgb(10, 201, 245)',
0.52: 'rgb(252, 201, 41)',
0.7: 'rgb(255, 138, 61)',
0.86: 'rgb(240, 78, 66)',
1.0: 'rgb(206, 32, 40)',
};
/** 渐变停靠点,供 canvas 自绘时插值用(与 HEAT_GRADIENT 必须同源) */
export const GRADIENT_STOPS: Array<[number, [number, number, number]]> = [
[0.0, [107, 120, 190]],
[0.25, [40, 167, 225]],
[0.45, [10, 201, 245]],
[0.62, [252, 201, 41]],
[0.8, [255, 138, 61]],
[1.0, [229, 72, 77]],
[0.0, [70, 83, 162]],
[0.18, [40, 167, 225]],
[0.36, [10, 201, 245]],
[0.52, [252, 201, 41]],
[0.7, [255, 138, 61]],
[0.86, [240, 78, 66]],
[1.0, [206, 32, 40]],
];
/**
* 聚合数字标签的配色。
*
* 初版是「实心彩色圆 + 白字」,圆圈直径最大 64px,几百个铺下来把热力底色
* 挡得只剩边角 —— 热力才是本页主角,聚合层只需要那个数字。
*
* 现在改为「极淡白底衬 + 深色字 + 白色描边」:
* - 底衬只覆盖文字本身(半径 9~16px),遮挡面积降到原来的 1/5 以下;
* - 字色用品牌深靛蓝而非白色 —— 圆圈透明后白字会糊在热力暖色里看不见;
* - 文字描边保证在金黄/橙红这类高亮热力上仍然可读。
*/
export const CLUSTER_LABEL = {
font: '700 11px "PingFang SC","Microsoft YaHei",sans-serif',
fontWide: '700 10px "PingFang SC","Microsoft YaHei",sans-serif',
color: '#222B5F',
halo: 'rgba(255,255,255,.95)',
haloWidth: 3,
plate: 'rgba(255,255,255,.34)',
} as const;
function hexToRgb(hex: string): [number, number, number] {
const v = hex.replace('#', '');
return [
......@@ -88,15 +116,6 @@ export function rgbCss(rgb: [number, number, number], alpha = 1): string {
return alpha >= 1 ? `rgb(${rgb[0]},${rgb[1]},${rgb[2]})` : `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${alpha})`;
}
/** 按数量归一化值取聚合圆圈填充色 */
export function bucketFill(t: number): string {
return rgbCss(gradientAt(t * 0.82 + 0.18), 0.92);
}
export function bucketStroke(t: number): string {
return rgbCss(gradientAt(t * 0.82 + 0.18), 1);
}
/** 底图样式。默认 whitesmoke:极简灰白,热力色块不会被底图路网颜色干扰 */
export const BASEMAP_OPTIONS = [
{ label: '极简灰白', value: 'amap://styles/whitesmoke' },
......
......@@ -160,8 +160,9 @@ export function FilterPanel() {
<>
<Field label="热力半径" tooltip={`${heatRadius}px`}>
<Slider
min={8}
max={60}
min={12}
max={90}
step={2}
value={heatRadius}
onChange={(v) => setHeatStyle({ radius: v })}
/>
......
......@@ -6,6 +6,7 @@ import type {
StatsResult,
} from '@/features/heatmap/aggregate/types';
import { DEFAULT_BASEMAP } from '@/features/heatmap/map/palette';
import { DEFAULT_HEAT_STYLE } from '@/features/heatmap/map/HeatLayer';
/** 时间快捷项。'all' 用数据实际范围,其余按「截至数据最新时间往回推 N 天」 */
export type TimePreset = 'all' | '7d' | '30d' | '90d' | 'custom';
......@@ -126,8 +127,8 @@ export const useHeatmapStore = create<HeatmapState>((set) => ({
basemap: DEFAULT_BASEMAP,
layerVisible: { heat: true, cluster: true },
heatRadius: 26,
heatOpacity: 0.82,
heatRadius: DEFAULT_HEAT_STYLE.radius,
heatOpacity: DEFAULT_HEAT_STYLE.opacity,
showNumbers: true,
selection: null,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!