Skip to content

Instantly share code, notes, and snippets.

@colemanfoley
Created April 11, 2013 20:42
Show Gist options
  • Save colemanfoley/5367010 to your computer and use it in GitHub Desktop.
Save colemanfoley/5367010 to your computer and use it in GitHub Desktop.
Histogram drawer
var testArray = [[0,3], [2,2], [1,1], [1,1]];
//This is the main function, which calls the helper functions.
var drawHistogram = function(array){
//These two steps prepare the inputs for the block that actually draws the histogram.
var combinedHeightsArray = combineHeights(array);
var orderedArray = orderAndFlattenArray(combinedHeightsArray);
//Drawing the histogram. The max height determines how many times drawLine is called.
//It draws the lines from the top down.
var maxHeight = findMaxHeight(orderedArray);
for (var i = maxHeight; i > 0; i--) {
drawLine(i-1, array.length, orderedArray);
};
};
var combineHeights = function(array){
var combinedHeightsArray = [];
/*
If the input array contains more than one array with the same column number
(the number at index 0), the height of the bar in that column should be the sum
of all arrays corresponding to that column.
So, I go through the input array, building the combined heights array. At each element
of the input array, I check if there is already an element with that column number in the
combinedHeightsArray. If there is, I add the height of the element from the old array to
that column's height in the new array. Else, I just copy the element from the input array
to the correct position in the combinedHeightsArray.
*/
for (var i = 0; i < array.length; i++) {
var heightOfCurrentElementInOldArray = array[i][1];
var columnOfCurrentElementInOldArray = array[i][0];
if(combinedHeightsArray[columnOfCurrentElementInOldArray]){
combinedHeightsArray[columnOfCurrentElementInOldArray][1] +=
heightOfCurrentElementInOldArray;
} else {
combinedHeightsArray[columnOfCurrentElementInOldArray] = array[i];
};
};
return combinedHeightsArray;
};
/*
This function iterates through the unordered array returned by the combineHeights function.
For each element of the unordered array, its height is grabbed and it is put at the index in
the orderedArray corresponding to its column number in the unordered array. The array this function
returns is ready to be processed by the line-drawing part of the program.
*/
var orderAndFlattenArray = function(unorderedArray){
var orderedArray = [];
for (var i = 0; i < unorderedArray.length; i++) {
var indexToPutHeightAt = unorderedArray[i][0];
var heightToPutThere = unorderedArray[i][1];
orderedArray[indexToPutHeightAt] = heightToPutThere;
};
return orderedArray;
};
//This function just finds the tallest column that will be drawn.
var findMaxHeight = function(flatArray){
var maxNumber = 0;
for (var i = 0; i < flatArray.length; i++) {
maxNumber = Math.max(flatArray[i], maxNumber)
};
return maxNumber;
};
//This function draws the line.
var drawLine = function(rowNumber, numberOfColumns, bar_heights){
var row = "";
for (i = 0; i < numberOfColumns; i++){
if (bar_heights[i] > rowNumber) {
row += "*";
} else {
row += " ";
};
}
console.log(row);
};
drawHistogram(testArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment