1

I want to create a script in JS that produces color boxes according to the numerical value of the CSV data provided. So far, I have only managed to pass the data into an HTML table and I'm looking for two things;

  1. Change the rgb background-color of <td>s according to the number inserted (eg. a value of 128 gives rgb(128,0,0))
  2. Leave the cell content blank but still create a new <td> element.

Here's my script;

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body >
  <div>
    <form class="form-horizontal well">
      <fieldset>
          <label for="csvFileInput"> <strong>CSV File:</strong>
          </label>
          <input type="file" id="csvFileInput" onchange="handleFiles(this.files)"
          accept=".csv">
        </div>
      </fieldset>
    </form>
    <div id="output">
    </div>
  </div>
 
  <footer>
  </footer>
  <script type="text/javascript" >
  function handleFiles(files) {
	if (window.FileReader) {
		getAsText(files[0]);
	} else {
		alert('FileReader are not supported in this browser.');
	}
}

function getAsText(fileToRead) {
	var reader = new FileReader();
	reader.onload = loadHandler;
	reader.onerror = errorHandler;
	reader.readAsText(fileToRead);
}

function loadHandler(event) {
	var csv = event.target.result;
	processData(csv);             
}

function processData(csv) {
    var allTextLines = csv.split(/\r\n|\n/);
    var lines = [];
    while (allTextLines.length) {
        lines.push(allTextLines.shift().split(','));
    }
	document.getElementById("output").innerHTML = (lines);
	drawOutput(lines);
}

function errorHandler(evt) {
	if(evt.target.error.name == "NotReadableError") {
		alert("Canno't read file !");
	}
}

function drawOutput(lines){
	document.getElementById("output").innerHTML = "";
	var table = document.createElement("table");
	for (var i = 0; i < lines.length; i++) {
		var row = table.insertRow(-1);
		for (var j = 0; j < lines[i].length; j++) {
			var firstNameCell = row.insertCell(-1);
			firstNameCell.appendChild(document.createTextNode(lines[i][j]));
		}
	}
	document.getElementById("output").appendChild(table);
}
</script>
</body>
</html>

2
  • What are the possible values of colours you are looking at? What if the value is greater than 255 or how do you determine if you'd want a colour other than Red (since one number would change only one part of the RGB)? Commented Dec 29, 2015 at 9:32
  • My values are between 0 and 255 and for the time being, I'm only interested in the shades of red. Commented Dec 29, 2015 at 9:37

2 Answers 2

0

This should do the trick:

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body >
  <div>
    <form class="form-horizontal well">
      <fieldset>
          <label for="csvFileInput"> <strong>CSV File:</strong>
          </label>
          <input type="file" id="csvFileInput" onchange="handleFiles(this.files)"
          accept=".csv">
        </div>
      </fieldset>
    </form>
    <div id="output">
    </div>
  </div>
 
  <footer>
  </footer>
  <script type="text/javascript" >
  function handleFiles(files) {
	if (window.FileReader) {
		getAsText(files[0]);
	} else {
		alert('FileReader are not supported in this browser.');
	}
}

function getAsText(fileToRead) {
	var reader = new FileReader();
	reader.onload = loadHandler;
	reader.onerror = errorHandler;
	reader.readAsText(fileToRead);
}

function loadHandler(event) {
	var csv = event.target.result;
	processData(csv);             
}

function processData(csv) {
    var allTextLines = csv.split(/\r\n|\n/);
    var lines = [];
    while (allTextLines.length) {
        lines.push(allTextLines.shift().split(','));
    }
	document.getElementById("output").innerHTML = (lines);
	drawOutput(lines);
}

function errorHandler(evt) {
	if(evt.target.error.name == "NotReadableError") {
		alert("Canno't read file !");
	}
}

function drawOutput(lines){
	document.getElementById("output").innerHTML = "";
	var table = document.createElement("table");
	for (var i = 0; i < lines.length; i++) {
		var row = table.insertRow(-1);
		for (var j = 0; j < lines[i].length; j++) {
			var firstNameCell = row.insertCell(-1);
			firstNameCell.appendChild(document.createTextNode(lines[i][j]));

            firstNameCell.style.backgroundColor = "rgb(" + lines[i][j] + ",0,0)"
		}
	}
	document.getElementById("output").appendChild(table);
}
</script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome!. Another thing; if I set a custom value above your correction like lines[i][j] = 255 an extra cell appears on the last line. Can you check it ?
No, just tried, doesn't add an extra cell. Probably there's an extra \n on the last line (or extra but empty row)?
0

Assuming lines contains the number,

var firstNameCell = row.insertCell(-1);
firstNameCell.setAttribute('bgcolor', 'rgb('+lines[i][j]+',0,0)');
firstNameCell.appendChild(document.createTextNode(lines[i][j]));

1 Comment

The code seems fine, but for some reason I get results in green

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.