i have a table which is created dynamically from database.
for (int m = 0; m < table.size(); m++) {
out.print("<tr>");
for (int k = 0; k < table.get(0).length; k++)
{ out.print("<td width='10'>");
out.print(table.get(m)[k]);
out.println("</td>");
}
out.println("</tr>");
}
I want to make a each cell coloured based on its value , The out put of my table is ;
You can think of a heat map as a data-driven “paint by numbers” canvas overlaid on top of an image. In short, an image is divided into a grid and within each square, the heat map shows the relative intensity of values captured by your eye tracker by assigning each value a color representation.
A heatmap (aka heat map) depicts values for a main variable of interest across two axis variables as a grid of colored squares. The axis variables are divided into ranges like a bar chart or histogram, and each cell's color indicates the value of the main variable in the corresponding cell range.
Use HSL instead of RGB to represent your colors.
You will need something like this:
let table = '<table>';
for (let i = 0; i < 4; ++i) {
table += '<tr>';
for(let k = 0; k < 10; ++k) {
const value = Math.random();
const h = 240 - value * 240;
table += '<td style="background: hsl(' + h + ', 100%, 50%)">' + value.toFixed(2) + '</td>';
}
table += '</tr>';
}
document.write(table);
table {
border: 1px solid #000;
border-collapse: collapse;
font-family: Sans-Serif;
color: #000;
}
td {
border: 2px solid #000;
padding: .5rem;
}
Ideally, add the style
attributes in your backend so that the page that you send to the client already has the colors. If for any reason you can't do that, then just go over the cells in the client, reading their values and adding them the appropriated background color.
Using the other components of HSL you could generate different colors schemas. For example, a single-hue scale, with black and white on its ends and a colors of your choice in the middle, blue in this case:
let table = '<table>';
for (let i = 0; i < 4; ++i) {
table += '<tr>';
for(let k = 0; k < 10; ++k) {
const value = Math.random();
const l = value * 100;
const textColor = l < 35 ? '#FFF' : '#000';
table += '<td style="background: hsl(200, 100%, ' + l + '%); color: ' + textColor + '">' + value.toFixed(2) + '</td>';
}
table += '</tr>';
}
document.write(table);
table {
border: 1px solid #000;
border-collapse: collapse;
font-family: Sans-Serif;
color: #000;
}
td {
border: 2px solid #000;
padding: .5rem;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With