Created
February 24, 2017 18:25
-
-
Save jjrobinson/719b7acbd5653903785d8d2120299c9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ASCIIHistogramMaker { | |
public static void main(String[] args){ | |
String inFile="/data/challenge302intermediate.txt"; | |
// ArrayList<int[]> graphData = new ArrayList<int[]>(); | |
ArrayList<String> input = new ChallengeInput() | |
.getInputByLines(ASCIIHistogramMaker.class, inFile); | |
//take first line as the chart bounds and create new BarChart with those bounds | |
IntBasedBarChart chart = new IntBasedBarChart(input.remove(0).split(" ")); | |
//take next line as the number of chart elements | |
int size = Integer.parseInt(input.remove(0)); | |
for(int i=0; i<size ;i++){ | |
// System.out.println("Parsing: graph data line=\""+input.get(i)+"\""); | |
chart.addData(stringArraytoIntArray(input.get(i).split(" "))); | |
}//end adding all lines | |
printChart(chart, size); | |
}//end main | |
private static void printChart(IntBasedBarChart c, int size){ | |
ArrayList<int[]> data = c.getData(); | |
if (data.size() == size){ | |
//what is the step between elements | |
int step = ((c.getBounds().getMaxX() - c.getBounds().getMinX()) / size); | |
//find the MAX # of chars for each entry in the chart | |
//if chart starts at 0 and goes to 100, the step should be 3 chars | |
int charStep = String.valueOf(c.getMaxX()).length(); | |
char chartChar = '*'; | |
int numColumns=((size+1)*(charStep+1)); | |
int yAxisCharMax = String.valueOf(c.getMaxY()).length(); | |
for(int y=c.getMaxY();y>=c.getMinY();y--){ | |
System.out.print(String.format("%0"+yAxisCharMax+"d",y)); | |
int maxX = 0; | |
for(int x = 1;x<numColumns;x++){ | |
//need to find if the current column is a printing column | |
if(x % (charStep+1) == 0 && x >0){ | |
//calc the current column in the data, check the value | |
//if the data value is <= current y axis, print chartChar | |
if(y <= data.get(x/(charStep+1)-1)[2]){ | |
System.out.print(chartChar); | |
} else {//Y is > data value, so print space | |
System.out.print(" "); | |
} | |
} else {//not a printing column, print space | |
System.out.print(" "); | |
} | |
maxX = x; | |
}//end looping through x Axis | |
//print newline at the end of the X axis on this level of Y axis | |
System.out.println();//end the printed line | |
}//end of loop on data | |
//print the X axis | |
System.out.print(StringUtils.repeat(" ", yAxisCharMax)); | |
for(int[] d: c.getData()){ | |
System.out.print(String.format("%0"+String.valueOf(c.getMaxX()).length()+"d", d[0])+" "); | |
} | |
System.out.println(String.valueOf(c.getMaxX())); | |
}//the data we got returned is the expected size | |
else { | |
System.out.println("ERROR: data size \""+data.size()+"\" is !="+size); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment