function create_div(id_name, class_name) {
var col_div = document.createElement("div")
if (class_name != "")
col_div.setAttribute("class", class_name)
if (id_name != "")
col_div.setAttribute("id", id_name)
return col_div
}
function create_dataframe(df, id) {
var tb = document.createElement('table');
tb.innerHTML = df.trim();
tb.setAttribute("id", id);
tb.setAttribute("class", "table table-bordered")
return tb
}
function create_selection(features_list, id, value_array = [], default_feature = "") {
//Create array of options to be added
var array = features_list;
//Create and append select list
var selectList = document.createElement("select");
selectList.setAttribute("class", "col mr-2 selectpicker form-control")
selectList.id = id;
var array_to_use = []
if (value_array != null) {
array_to_use = value_array;
} else {
array_to_use = array;
}
//Create and append the options
for (var i = 0; i < array.length; i++) {
var option = document.createElement("option");
option.value = array_to_use[i]
option.text = array[i];
selectList.appendChild(option);
}
if (default_feature != "") {
selectList.value = default_feature
}
return selectList
}
// Function to transpose the table
function transpose_table(tableHtml) {
// Convert the HTML string into a jQuery object for manipulation
const originalTable = $(tableHtml);
console.log(tableHtml)
// Get the column names (th elements in the header row)
const colNames = originalTable.find('thead th').map(function () {
return $(this).text().trim();
}).get();
// Get the data rows (as an array of arrays of values)
const rows = originalTable.find('tbody').map(function () {
return $(this).find('td').map(function () {
return $(this).text().trim();
}).get();
}).get(); // We use get() twice to fully extract values as a 2D array
const rowsNum = originalTable.find('tbody tr').length;
// Build the new transposed table
let transposedHtml = '
Feature
';
// Add headers for the values (assuming each row represents an entry)
transposedHtml += `
Original Value
`;
if (rowsNum >= 2)
transposedHtml += `
Counterfactual Value
`;
transposedHtml += '
';
// Create rows for each column (i.e., each feature name becomes a row)
if (rowsNum >= 2) {
for (let colIndex = 0; colIndex < colNames.length; colIndex++) {
transposedHtml += `