From 81be580644bf5dc57c2a8f16d9e3def1b829d078 Mon Sep 17 00:00:00 2001 From: hustcc Date: Wed, 24 May 2023 16:07:37 +0800 Subject: [PATCH] test: add test cases --- .../plots/static/diamond-heatmap-density.ts | 64 +++++++++++++++++++ __tests__/plots/static/index.ts | 1 + package.json | 1 + src/shape/heatmap/renderer/index.ts | 5 +- 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 __tests__/plots/static/diamond-heatmap-density.ts diff --git a/__tests__/plots/static/diamond-heatmap-density.ts b/__tests__/plots/static/diamond-heatmap-density.ts new file mode 100644 index 0000000000..650a2467c6 --- /dev/null +++ b/__tests__/plots/static/diamond-heatmap-density.ts @@ -0,0 +1,64 @@ +import DataSet from '@antv/data-set'; +import { G2Spec } from '../../../src'; + +export function DiamondHeatmapDensity(): G2Spec { + return { + type: 'view', + data: { + type: 'fetch', + value: 'data/diamond.csv', + }, + scale: { + x: { nice: true, domainMin: -0.5 }, + y: { nice: true, domainMin: -2000 }, + color: { nice: true }, + }, + children: [ + { + type: 'heatmap', + data: { + transform: [ + { + type: 'custom', + callback: (data) => { + const dv = new DataSet.View().source(data); + // @ts-ignore + dv.transform({ + type: 'kernel-smooth.density', + fields: ['carat', 'price'], + as: ['carat', 'price', 'density'], + }); + return dv.rows; + }, + }, + ], + }, + encode: { + x: 'carat', + y: 'price', + color: 'density', + }, + style: { + opacity: 0.3, + gradient: { + '0': 'white', + '0.2': 'blue', + '0.4': 'cyan', + '0.6': 'lime', + '0.8': 'yellow', + '0.9': 'red', + }, + }, + }, + { + type: 'point', + encode: { + x: 'carat', + y: 'price', + }, + }, + ], + }; +} + +DiamondHeatmapDensity.maxError = 100; diff --git a/__tests__/plots/static/index.ts b/__tests__/plots/static/index.ts index 83e2238fa5..056bf25fa3 100644 --- a/__tests__/plots/static/index.ts +++ b/__tests__/plots/static/index.ts @@ -228,3 +228,4 @@ export { vaccinesCellScaleRelationAutoPaddingTickFilter } from './vaccines-cell- export { marketIntervalMarimekkoAutoPaddingFlex } from './market-interval-marimekko-auto-padding-flex'; export { aaplIntervalDateEncodeX } from './aapl-interval-date-encode-x'; export { HeatmapHeatmapBasic } from './heatmap-heatmap-basic'; +export { DiamondHeatmapDensity } from './diamond-heatmap-density'; diff --git a/package.json b/package.json index 049e39916e..da42973109 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "pdfast": "^0.2.0" }, "devDependencies": { + "@antv/data-set": "^0.11.8", "@antv/g-plugin-rough-canvas-renderer": "^1.7.9", "@antv/g-plugin-rough-svg-renderer": "^1.7.9", "@antv/g-svg": "^1.8.6", diff --git a/src/shape/heatmap/renderer/index.ts b/src/shape/heatmap/renderer/index.ts index ba72f306af..8187b10e3b 100644 --- a/src/shape/heatmap/renderer/index.ts +++ b/src/shape/heatmap/renderer/index.ts @@ -93,12 +93,11 @@ function drawAlpha( const rectX = x - radius; const rectY = y - radius; - // TODO: cache for performance. const tpl = getPointTemplate(radius, 1 - blur, createCanvas); // Value from minimum / value range, => [0, 1]. const templateAlpha = (value - min) / (max - min); - // Small values are not visible because globalAlpha < .01 cannot be read from imageData. - shadowCtx.globalAlpha = Math.max(templateAlpha, 0.01); + // Small values are not visible because globalAlpha < .001 cannot be read from imageData. + shadowCtx.globalAlpha = Math.max(templateAlpha, 0.001); shadowCtx.drawImage(tpl, rectX, rectY); } return shadowCtx;