turt2live

Grapher

Dec 23rd, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. package me.turt2live.meaSuite.grapher;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.Vector;
  12.  
  13. import javax.imageio.ImageIO;
  14.  
  15. import me.turt2live.meaSuite.API.MeaAPI;
  16. import me.turt2live.meaSuite.plugin.Loader;
  17.  
  18. import org.bukkit.plugin.java.JavaPlugin;
  19.  
  20. public class Grapher {
  21.  
  22.     @SuppressWarnings("unused")
  23.     private JavaPlugin          plugin;
  24.     private Loader              loader;
  25.     private File                directory;
  26.  
  27.     private BufferedImage       image;
  28.     private Graphics2D          g;
  29.  
  30.     private Vector<Double[]>    dataX       = new Vector<Double[]>();
  31.     private Vector<Double[]>    dataY       = new Vector<Double[]>();
  32.     private Vector<String>      dataLabel   = new Vector<String>();
  33.  
  34.     public Grapher(JavaPlugin p, Loader l, File d) {
  35.         this.plugin = p;
  36.         this.loader = l;
  37.         this.directory = d;
  38.         image = new BufferedImage(800, 300, BufferedImage.TYPE_INT_ARGB);
  39.         g = (Graphics2D) image.getGraphics();
  40.     }
  41.  
  42.     public void createGraph(String title) {
  43.         g.setColor(Color.GRAY);
  44.         g.fillRect(0, 0, 800, 300);
  45.         g.setColor(Color.DARK_GRAY);
  46.         g.fillRect(10, 10, 780, 280);
  47.         g.setColor(Color.GRAY);
  48.         g.fillRect(20, 20, 760, 35);
  49.         g.setColor(Color.BLACK);
  50.         g.setFont(new Font("Arial", Font.PLAIN, 26));
  51.         g.drawString(title, 25, 45);
  52.         g.setColor(Color.GRAY);
  53.         g.fillRect(20, 60, 760, 220);
  54.         g.setColor(Color.LIGHT_GRAY);
  55.         g.fillRect(40, 80, 720, 180);
  56.     }
  57.  
  58.     public Double[] convertSeries(Double series[]) {
  59.         Double returnSeries[] = new Double[series.length];
  60.         Double max = Double.MIN_VALUE;
  61.         Double min = Double.MAX_VALUE;
  62.         for (Double d : series)
  63.             if (d > max) max = d;
  64.             else if (d < min) min = d;
  65.         for (int i = 0; i < series.length; i++) {
  66.             Double d = series[i];
  67.             d = (d - min) / (max - min);
  68.             d = d * 100;
  69.             returnSeries[i] = d;
  70.         }
  71.         return returnSeries;
  72.     }
  73.  
  74.     public void addSeries(Double dataX[], Double dataY[], String name, boolean convertX, boolean convertY) {
  75.         if (convertX) dataX = convertSeries(dataX);
  76.         if (convertY) dataY = convertSeries(dataY);
  77.         this.dataX.add(dataX);
  78.         this.dataY.add(dataY);
  79.         dataLabel.add(name);
  80.     }
  81.  
  82.     public void addSeries(Double dataX[], Double dataY[], String name) {
  83.         addSeries(dataX, dataY, name, false, false);
  84.     }
  85.  
  86.     public File generate() {
  87.         File output = new File(directory + "/chart_" + MeaAPI.timestamp(true) + ".png");
  88.         int scaleX[] = { 0, 10, 20, 30, 40, 50 };
  89.         int scaleY[] = { 0, 20, 40, 60, 80, 100 };
  90.         int gpx[] = { 40, 720 };
  91.         int gpy[] = { 80, 180 };
  92.         // Draw scales
  93.         // Draw X-Scale (Also draws lines)
  94.         g.setColor(Color.BLACK);
  95.         g.setFont(new Font("Arial", Font.PLAIN, 14));
  96.         int sx = (gpx[1] - gpx[0]) / (scaleX.length - 1);
  97.         for (int i = 0; i < scaleX.length; i++) {
  98.             String label = scaleX[i] + "";
  99.             g.drawString(label, gpx[0] + (sx * i) + 5, gpy[1] + 95);
  100.             g.drawLine(gpx[0] + (sx * i) + 10, gpy[1] + 80, gpx[0] + (sx * i) + 10, gpy[0]);
  101.         }
  102.         // Draw Y-Scale (Also draws lines)
  103.         g.setColor(Color.BLACK);
  104.         g.setFont(new Font("Arial", Font.PLAIN, 14));
  105.         int sy = ((gpy[1] - gpy[0]) / (scaleY.length - 1)) + 14;
  106.         for (int i = 0; i < scaleY.length; i++) {
  107.             String label = scaleY[i] + "";
  108.             g.drawString(label, gpx[0] - 20, gpy[1] - (sy * i) + 80);
  109.             g.drawLine(gpx[0], gpy[1] - (sy * i) + 75, gpx[1] + 40, gpy[1] - (sy * i) + 75);
  110.         }
  111.         for (int s = 0; s < ((dataX.size() >= dataY.size()) ? ((dataX.size() >= dataLabel.size()) ? dataX.size() : ((dataLabel.size() >= dataY.size()) ? dataLabel.size() : dataY.size())) : ((dataY.size() >= dataLabel.size()) ? dataY.size() : dataLabel.size())); s++) {
  112.             Color seriesColor[] = { Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW };
  113.             Double dx[] = dataX.get(s);
  114.             Double dy[] = dataY.get(s);
  115.             // Find all points
  116.             Vector<Point> points = new Vector<Point>();
  117.             for (int i = 0; i < ((dx.length <= dy.length) ? dx.length : dy.length); i++) {
  118.                 double x = 0;
  119.                 double y = 0;
  120.                 double msx = scaleX[0];
  121.                 double msy = scaleY[0];
  122.                 double masx = scaleX[scaleX.length - 1];
  123.                 double masy = scaleY[scaleY.length - 1];
  124.                 x = ((dx[i] - msx) / (masx - msx));
  125.                 double nx = (x * gpx[1]) + msx;
  126.                 y = ((dy[i] - msy) / (masy - msy));
  127.                 y = 1 - y;
  128.                 double ny = (y * gpy[1]) + msy;
  129.                 points.add(new Point((int) (nx + 38), (int) (ny + 78)));
  130.             }
  131.             // Draw points (as a line graph)
  132.             if (s >= seriesColor.length) {
  133.                 int ts = (seriesColor.length - 1) - s;
  134.                 g.setColor(seriesColor[ts]);
  135.             } else g.setColor(seriesColor[s]);
  136.             g.setStroke(new BasicStroke(3F));
  137.             for (int i = 0; i < points.size() - 1; i++)
  138.                 g.drawLine(points.get(i).x, points.get(i).y, points.get(i + 1).x, points.get(i + 1).y);
  139.         }
  140.         try {
  141.             ImageIO.write(image, "png", output);
  142.         } catch (IOException e) {
  143.             e.printStackTrace();
  144.             loader.api.log(e);
  145.         }
  146.         return output;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment