const MIN = 27.9;
const MAX = 77.4;
class RGB {
constructor(
public r: number,
public g: number,
public b: number
) {
}
}
function normalize(value: number, fromSource: number, toSource: number, fromTarget: number, toTarget: number) {
return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
}
function getTwoColorGradientRGB(min: number, max: number, value: number): string {
var startRGB = new RGB(255, 0, 0);
var endRGB = new RGB(0, 0, 255);
var percentFade = normalize(value, min, max, 0, 1);
var diffRed = endRGB.r - startRGB.r;
var diffGreen = endRGB.g - startRGB.g;
var diffBlue = endRGB.b - startRGB.b;
diffRed = (diffRed * percentFade) + startRGB.r;
diffGreen = (diffGreen * percentFade) + startRGB.g;
diffBlue = (diffBlue * percentFade) + startRGB.b;
return `rgb(${Math.round(diffRed)}, ${Math.round(diffGreen)}, ${Math.round(diffBlue)})`;
}
export function createHeatMapModel(): TableModelIf {
const bodyAreaModel = new AreaModelArrayOfArrays("body", data, defaultRowHeights.body);
const columnSizes = new Array(headerLabel.length).fill(50);
const headerAreaModel = new AreaModelArrayOfArrays("header", [headerLabel], defaultRowHeights.header);
bodyAreaModel.getCustomStyleAt = (rowIndex: number, columnIndex: number) => {
let ret: { [key: string]: string } = {};
if (columnIndex > 0) {
const n = bodyAreaModel.getValueAt(rowIndex, columnIndex) as number;
ret = {
"background": getTwoColorGradientRGB(MIN, MAX, n),
"color": "#fff"
};
}
ret["paddingTop"] = "10px";
return ret;
};
return TableFactory.createTableModel({
headerAreaModel,
bodyAreaModel,
columnSizes
});
}