How to change table cell color based on numeric value from .csv report AND when tables are dynamically created?

后端 未结 2 1622
野的像风
野的像风 2021-01-28 21:24

I\'m using some code from d3.js to build a cell sheet in an html page dynamically. So if it is a 5x5 .csv report, it will produce 5x5 on the page and so forth.

This repo

2条回答
  •  孤独总比滥情好
    2021-01-28 22:09

    You should be able to just append a .style after your last .text(). It will be something along the lines of:

    .append('td')
      .text(...)
      .style('background-color', function(d) { 
        if (d < 3) {return 'green';}
        else if (d < 5) {return 'yellow';}
        else if (d < 10) {return 'orange';}
        else {return 'red';}
      };
    

    Might take a bit of fiddling around with the return values for the colours, not sure whether returning strings will work, might be better to return rgb(...) colours instead.

提交回复
热议问题