heatmap-cluster
Version:
The Unipept visualisation library
59 lines (48 loc) • 1.94 kB
text/typescript
import { preprocessValues } from "./colors";
import HeatmapFeature from "./HeatmapFeature";
import HeatmapValue from "./HeatmapValue";
import * as d3 from "d3";
export default class Preprocessor {
/**
* Converts an array of feature labels into correct HeatmapFeature objects. These objects keep track of a name
* and index for a feature.
*
* @param featureLabels All labels that should be converted to true HeatmapFeature objects.
* @return An array with HeatmapFeature objects.
*/
preprocessFeatures(featureLabels: string[]): HeatmapFeature[] {
return Object.entries(featureLabels).map(([idx, feature]) => {
return {
name: feature,
idx: Number.parseInt(idx)
}
});
}
transformValues(
data: (number | HeatmapValue)[][],
colors: string[], // 颜色数组
colorValues: number, // 期望的离散颜色数量
valueRange: number[] = [] // 手动指定数值范围,默认为空数组)
) {
return preprocessValues(data, colors,colorValues, valueRange)
}
/**
* Order all values in a map, per color.
*
* @param values All grid values for which we should determine a color.
* @return A mapping between an HTML-color value and a list of [row, col] positions.
*/
orderPerColor(values: HeatmapValue[][]): Map<string, [number, number][]> {
const output = new Map<string, [number, number][]>();
for (let rowIdx = 0; rowIdx < values.length; rowIdx++) {
for (let colIdx = 0; colIdx < values[rowIdx].length; colIdx++) {
const colorString = values[rowIdx][colIdx].color;
if (!output.has(colorString)) {
output.set(colorString, []);
}
output.get(colorString)?.push([rowIdx, colIdx]);
}
}
return output;
}
}