import $ from "jquery";
import * as vega from "vega";
import * as d3 from "d3";
import { precision_formatter } from "../utils";
import { createLinkHtml } from "./link-to-url";
export function colorizeColumn(
ah,
columns,
heatmap,
detail_mode,
header_label_length,
columnIndexMap,
link,
link_columns,
) {
let index = columnIndexMap[heatmap.title];
let row = 0;
var table_rows = $("#table").bootstrapTable("getData", {
useCurrentPage: "true",
});
var custom_func = heatmap.heatmap["custom-content"];
let scale = datavzrdScale(heatmap);
$(`table > tbody > tr td:nth-child(${index})`).each(function () {
var value = table_rows[row][heatmap.title];
let color = scale(value);
if (value !== "" && d3.color(color)) {
this.style.setProperty("background-color", color, "important");
if (isDark(color)) {
this.style.setProperty("color", "white", "important");
}
}
let shown_value = value;
if (custom_func) {
var data_function = window[custom_func];
shown_value = data_function(value, table_rows[row]);
this.innerHTML = shown_value;
} else {
shown_value = this.innerHTML;
}
if (link && value !== "") {
this.classList.add("linked-cell");
this.innerHTML = createLinkHtml(
link_columns,
link.links,
value,
shown_value,
table_rows[row],
);
}
row++;
});
}
export function datavzrdScale(heatmap) {
let scale = null;
if (heatmap.heatmap.scale == "ordinal") {
if (heatmap.heatmap["color-scheme"]) {
scale = vega
.scale(heatmap.heatmap.scale)()
.domain(heatmap.heatmap.domain)
.range(vega.scheme(heatmap.heatmap["color-scheme"]));
} else if (heatmap.heatmap.range?.length) {
scale = vega
.scale(heatmap.heatmap.scale)()
.domain(heatmap.heatmap.domain)
.range(heatmap.heatmap.range);
} else {
scale = vega
.scale(heatmap.heatmap.scale)()
.domain(heatmap.heatmap.domain);
}
} else {
if (heatmap.heatmap["color-scheme"]) {
let scheme = heatmap.heatmap["color-scheme"];
let d3_scheme =
d3[
`interpolate${scheme.charAt(0).toUpperCase()}${scheme.slice(1).toLowerCase()}`
];
let s = heatmap.heatmap.scale;
if (heatmap.heatmap.scale == "linear") {
s = ""; }
scale = d3[
`scaleSequential${s.charAt(0).toUpperCase()}${s.slice(1).toLowerCase()}`
](heatmap.heatmap.domain, d3_scheme);
} else if (heatmap.heatmap.range?.length) {
scale = vega
.scale(heatmap.heatmap.scale)()
.domain(heatmap.heatmap.domain)
.clamp(heatmap.heatmap.clamp)
.range(heatmap.heatmap.range);
} else {
scale = vega
.scale(heatmap.heatmap.scale)()
.domain(heatmap.heatmap.domain)
.clamp(heatmap.heatmap.clamp);
}
}
return scale;
}
export function colorizeDetailCard(
value,
div,
heatmap,
row,
is_float,
precision,
link,
link_columns,
) {
let scale = datavzrdScale(heatmap);
if (value !== "") {
let color = scale(value);
if (d3.color(color)) {
$(`${div}`).css("background-color", color);
if (isDark(color)) {
$(`${div}`).css("color", "white", "important");
}
}
}
let shown_value = value;
if (heatmap.heatmap["custom-content"]) {
var data_function = window[heatmap.heatmap["custom-content"]];
shown_value = data_function(value, row);
$(`${div}`)[0].innerHTML = shown_value;
} else if (is_float && precision !== undefined) {
shown_value = precision_formatter(precision, value);
$(`${div}`)[0].innerHTML = shown_value;
}
if (link && value !== "") {
$(`${div}`).addClass("linked-cell");
$(`${div}`)[0].innerHTML = createLinkHtml(
link_columns,
link.links,
value,
shown_value,
row,
);
}
}
export function colorizeHeaderRow(row, heatmap, header_label_length) {
let scale = null;
if (heatmap.scale == "ordinal") {
if (heatmap.domain != null) {
if (heatmap["color-scheme"]) {
scale = vega
.scale(heatmap.scale)()
.domain(heatmap.domain)
.range(vega.scheme(heatmap["color-scheme"]));
} else if (heatmap.range?.length) {
scale = vega
.scale(heatmap.scale)()
.domain(heatmap.domain)
.range(heatmap.range);
} else {
scale = vega.scale(heatmap.scale)().domain(heatmap.domain);
}
} else {
if (heatmap["color-scheme"]) {
scale = vega
.scale(heatmap.scale)()
.range(vega.scheme(heatmap["color-scheme"]));
} else if (heatmap.range?.length) {
scale = vega.scale(heatmap.scale)().range(heatmap.range);
} else {
scale = vega.scale(heatmap.scale)();
}
}
} else {
if (heatmap["color-scheme"]) {
let scheme = heatmap["color-scheme"];
let d3_scheme =
d3[
`interpolate${scheme.charAt(0).toUpperCase()}${scheme.slice(1).toLowerCase()}`
];
let s = heatmap.scale;
if (heatmap.scale == "linear") {
s = "";
}
scale = d3[
`scaleSequential${s.charAt(0).toUpperCase()}${s.slice(1).toLowerCase()}`
](heatmap.domain, d3_scheme);
} else if (heatmap.range && heatmap.range.length > 0) {
scale = vega
.scale(heatmap.scale)()
.domain(heatmap.domain)
.clamp(heatmap.clamp)
.range(heatmap.range);
} else {
scale = vega
.scale(heatmap.scale)()
.domain(heatmap.domain)
.clamp(heatmap.clamp);
}
}
var start = 0;
if (header_label_length > 0) {
start = 1;
}
$(`table > thead > tr:nth-child(${row + 1}) > td:gt(${start})`).each(
function () {
var value = this.innerHTML;
if (value !== "") {
let color = scale(value);
if (d3.color(color)) {
this.style.setProperty("background-color", color, "important");
if (isDark(color)) {
this.style.setProperty("color", "white", "important");
}
}
}
},
);
}
export function isDark(c) {
let rgb = d3.color(c).rgb();
return 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b < 128;
}