-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,771 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { G2Spec } from '../../../src'; | ||
|
||
export function HeatmapHeatmapBasic(): G2Spec { | ||
return { | ||
type: 'heatmap', | ||
padding: 0, | ||
data: { | ||
type: 'fetch', | ||
value: 'data/heatmap.json', | ||
}, | ||
encode: { | ||
x: 'g', | ||
y: 'l', | ||
color: 'tmp', | ||
}, | ||
axis: false, | ||
}; | ||
} | ||
|
||
HeatmapHeatmapBasic.maxError = 100; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import { HeatmapRendererData, HeatmapRendererOptions } from './types'; | ||
|
||
/** | ||
* Get a point with template. | ||
* @param radius | ||
* @param blurFactor | ||
* @returns | ||
*/ | ||
function getPointTemplate(radius: number, blurFactor: number) { | ||
const tplCanvas = document.createElement('canvas'); | ||
const tplCtx = tplCanvas.getContext('2d'); | ||
const x = radius; | ||
const y = radius; | ||
tplCanvas.width = tplCanvas.height = radius * 2; | ||
|
||
if (blurFactor === 1) { | ||
tplCtx.beginPath(); | ||
tplCtx.arc(x, y, radius, 0, 2 * Math.PI, false); | ||
tplCtx.fillStyle = 'rgba(0,0,0,1)'; | ||
tplCtx.fill(); | ||
} else { | ||
const gradient = tplCtx.createRadialGradient( | ||
x, | ||
y, | ||
radius * blurFactor, | ||
x, | ||
y, | ||
radius, | ||
); | ||
gradient.addColorStop(0, 'rgba(0,0,0,1)'); | ||
gradient.addColorStop(1, 'rgba(0,0,0,0)'); | ||
tplCtx.fillStyle = gradient; | ||
tplCtx.fillRect(0, 0, 2 * radius, 2 * radius); | ||
} | ||
return tplCanvas; | ||
} | ||
|
||
/** | ||
* Get a color palette with len = 256 base on gradient. | ||
* @param gradientConfig | ||
* @returns | ||
*/ | ||
function getColorPalette(gradientConfig: any) { | ||
const paletteCanvas = document.createElement('canvas'); | ||
const paletteCtx = paletteCanvas.getContext('2d'); | ||
|
||
paletteCanvas.width = 256; | ||
paletteCanvas.height = 1; | ||
|
||
const gradient = paletteCtx.createLinearGradient(0, 0, 256, 1); | ||
for (const key in gradientConfig) { | ||
gradient.addColorStop(+key, gradientConfig[key]); | ||
} | ||
|
||
paletteCtx.fillStyle = gradient; | ||
paletteCtx.fillRect(0, 0, 256, 1); | ||
|
||
return paletteCtx.getImageData(0, 0, 256, 1).data; | ||
} | ||
|
||
/** | ||
* Draw all circle with alpha. | ||
*/ | ||
function drawAlpha( | ||
shadowCtx, | ||
min: number, | ||
max: number, | ||
data: HeatmapRendererData[], | ||
options: HeatmapRendererOptions, | ||
) { | ||
const { blur } = options; | ||
let len = data.length; | ||
while (len--) { | ||
const { x, y, value: v, radius } = data[len]; | ||
// Ff value is bigger than max, use max as value. | ||
const value = Math.min(v, max); | ||
const rectX = x - radius; | ||
const rectY = y - radius; | ||
|
||
// TODO: cache for performance. | ||
const tpl = getPointTemplate(radius, blur); | ||
// 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); | ||
shadowCtx.drawImage(tpl, rectX, rectY); | ||
} | ||
return shadowCtx; | ||
} | ||
|
||
function colorize( | ||
shadowCtx, | ||
maxWidth: number, | ||
maxHeight: number, | ||
palette, | ||
options: HeatmapRendererOptions, | ||
) { | ||
const { maxOpacity, minOpacity, useGradientOpacity } = options; | ||
const x = 0; | ||
const y = 0; | ||
const width = maxWidth; | ||
const height = maxHeight; | ||
|
||
const img = shadowCtx.getImageData(x, y, width, height); | ||
const imgData = img.data; | ||
const len = imgData.length; | ||
|
||
for (let i = 3; i < len; i += 4) { | ||
const alpha = imgData[i]; | ||
const offset = alpha * 4; | ||
|
||
if (!offset) { | ||
continue; | ||
} | ||
|
||
// Should be in [min, max], min >= 0. | ||
const finalAlpha = Math.max( | ||
0, | ||
Math.min(maxOpacity, Math.max(minOpacity, alpha)), | ||
); | ||
// Update rgba. | ||
imgData[i - 3] = palette[offset]; | ||
imgData[i - 2] = palette[offset + 1]; | ||
imgData[i - 1] = palette[offset + 2]; | ||
imgData[i] = useGradientOpacity ? palette[offset + 3] : finalAlpha; | ||
} | ||
|
||
return img; | ||
} | ||
|
||
/** | ||
* Render a heatmap with canvas. | ||
* See [heatmap.js](https://github.com/pa7/heatmap.js/blob/master/src/renderer/canvas2d.js). | ||
*/ | ||
export function HeatmapRenderer( | ||
width: number, | ||
height: number, | ||
min: number, | ||
max: number, | ||
data: HeatmapRendererData[], | ||
options: HeatmapRendererOptions, | ||
) { | ||
const opts = { | ||
blur: 0.5, | ||
gradient: { | ||
0.25: 'rgb(0,0,255)', | ||
0.55: 'rgb(0,255,0)', | ||
0.85: 'yellow', | ||
1.0: 'rgb(255,0,0)', | ||
}, | ||
...options, | ||
opacity: (options.opacity || 0.65) * 255, | ||
maxOpacity: (options.opacity || 1) * 255, | ||
minOpacity: (options.opacity || 0) * 255, | ||
}; | ||
|
||
const canvas = document.createElement('canvas'); | ||
const shadowCanvas = document.createElement('canvas'); | ||
|
||
const ctx = canvas.getContext('2d'); | ||
const shadowCtx = shadowCanvas.getContext('2d'); | ||
|
||
const palette = getColorPalette(opts.gradient); | ||
|
||
drawAlpha(shadowCtx, min, max, data, opts); | ||
const img = colorize(shadowCtx, width, height, palette, opts); | ||
|
||
ctx.putImageData(img, 0, 0); | ||
|
||
return ctx; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export type HeatmapRendererOptions = { | ||
/** | ||
* A background color string in form of hexcode, color name, or rgb(a). | ||
*/ | ||
backgroundColor?: string; | ||
/** | ||
* An gradient string that represents the gradient (syntax: number string [0,1] : color string). | ||
*/ | ||
gradient?: Record<string, string>; | ||
/** | ||
* The radius each datapoint will have (if not specified on the datapoint itself). | ||
*/ | ||
radius?: number; | ||
/** | ||
* A global opacity for the whole heatmap, default = 0.6. | ||
* This overrides maxOpacity and minOpacity if set! | ||
*/ | ||
opacity?: number; | ||
/** | ||
* The maximal opacity the highest value in the heatmap will have. (will be overridden if opacity set). | ||
*/ | ||
maxOpacity?: number; | ||
/** | ||
* The minimum opacity the lowest value in the heatmap will have (will be overridden if opacity set). | ||
*/ | ||
minOpacity?: number; | ||
/** | ||
* The blur factor that will be applied to all datapoints, default = 0.5. | ||
* The higher the blur factor is, the smoother the gradients will be. | ||
*/ | ||
blur?: number; | ||
/** | ||
* Use gradient opacity. | ||
*/ | ||
useGradientOpacity?: boolean; | ||
}; | ||
|
||
export type HeatmapRendererData = { | ||
x: number; | ||
y: number; | ||
value: number; | ||
radius: number; | ||
}; |