Skip to content

Instantly share code, notes, and snippets.

Created October 2, 2014 00:50
Show Gist options
  • Select an option

  • Save anonymous/59ed470ccdbd3daff43f to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/59ed470ccdbd3daff43f to your computer and use it in GitHub Desktop.
package datacollator;
import java.util.List;
import java.util.ArrayList;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class DataCollator {
public static Graph readGraph(File f){
String title = "",xLabel = "",yLabel = "";
List<Point2D> points = new ArrayList<>();
try(BufferedReader read = new BufferedReader(new FileReader(f));){
String line = read.readLine();
String[] labels = line.split(":");
title = labels[1];
xLabel =labels[2];
yLabel = labels[3];
while((line = read.readLine()) != null){
String[] xyParts = line.split(":");
points.add(new Point2D.Double(Double.parseDouble(xyParts[0]),Double.parseDouble(xyParts[1])));
}
} catch (IOException ignored){
System.out.println(ignored);
}
return new Graph(title,xLabel,yLabel,points);
}
public static void main(String[] args){
File dataFile = new File(args[0]);
Graph graphedData = readGraph(dataFile);
BufferedImage graphImage = graphedData.drawSelf(500,500);
try{
ImageIO.write(graphImage,"bmp",new File(graphedData.graphTitle + ".bmp"));
} catch (IOException ignored){
System.out.println(ignored);
}
}
}
import java.util.List;
import java.awt.geom.Point2D;
import java.util.Comparator;
import java.util.Collections;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Color;
public class Graph{
private static Comparator<Point2D> xValueComparator = new Comparator<Point2D>() {
@Override
public int compare(Point2D p1, Point2D p2){
return p1.getX() > p2.getX()? 1: p2.getX()>p1.getX()? -1:0;
}
};
private static Comparator<Point2D> yValueComparator = new Comparator<Point2D>() {
@Override
public int compare(Point2D p1, Point2D p2){
return p1.getY() > p2.getY()? 1: p2.getY()>p1.getY()? -1:0;
}
}
;
public static class Line {
public final double slope,xIntercept;
public Line(double slope, double xIntercept){
this.slope=slope;
this.xIntercept =xIntercept;
}
@Override
public String toString(){
return String.format("y=%.3f+%.3f",slope,xIntercept);
}
public static Line lineBetween(Point2D p1, Point2D p2){
double m = (p1.getY()-p2.getY())/(p1.getX()-p2.getX());
double b = p1.getY()- m*p1.getX();
return new Line(m,b);
}
}
public final String graphTitle;
public final String xLabel,yLabel;
public final List<Point2D> dataPoints;
public Graph(String title, String xLbl, String yLbl, List<Point2D> points){
graphTitle = title;
xLabel=xLbl;
yLabel=yLbl;
dataPoints=points;
}
public Point2D minimumXPoint(){
return Collections.min(dataPoints,xValueComparator);
}
public Point2D minimumYPoint(){
return Collections.min(dataPoints,yValueComparator);
}
public Point2D maximumXPoint(){
return Collections.max(dataPoints,xValueComparator);
}
public Point2D maximumYPoint(){
return Collections.max(dataPoints,yValueComparator);
}
public Line lineOfBestFit(){
double m=0;
double b=0;
Point2D atIndex0 = dataPoints.get(0);
for(int i=1; i< dataPoints.size()-1;i++){
Line l = Line.lineBetween(atIndex0,dataPoints.get(i));
m+=l.slope;
b+=l.xIntercept;
}
m/=dataPoints.size()-1;
b/=dataPoints.size()-1;
return new Line(m,b);
}
public BufferedImage drawSelf(int width, int height){
BufferedImage graphImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = graphImage.createGraphics();
double maxX = maximumXPoint().getX();
double minX = minimumXPoint().getX();
double maxY = maximumYPoint().getY();
double minY = minimumYPoint().getY();
double xScale = width/(maxX-minX);
double yScale = height/(maxY-minY);
g.drawString(graphTitle,width/2-3*graphTitle.length(),10);
g.drawString(xLabel,width/2-3*xLabel.length(),height);
for(int i = 0; i < yLabel.length();i+=3)
g.drawString(yLabel.substring(i,i+3),0,height/2+10*i/3);
double xShift = -minX;
double yShift = height-minY;
for(Point2D p:dataPoints){
g.fillOval((int) (xScale * (p.getX() + xShift)),(int) -((yScale * p.getY()) - yShift),5,5);
}
Line bestFit = lineOfBestFit();
int bestFitStartX = (int) (xScale * ((minY-bestFit.xIntercept)/bestFit.slope + xShift));
int bestFitStartY = (int) -((yScale * (bestFit.slope*minX+bestFit.xIntercept)) - yShift);
int bestFitEndX = (int) (xScale * ((maxY-bestFit.xIntercept)/bestFit.slope + xShift));
int bestFitEndY = (int) -((yScale * (bestFit.slope*maxX+bestFit.xIntercept)) - yShift);
g.setColor(Color.GREEN);
g.drawLine(bestFitStartX,bestFitStartY,bestFitEndX,bestFitEndY);
return graphImage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment