changeset 137:a7bd25187fa0

Fixed minor rendering issues with the graph generator - now the size and placement of labels is computed more correctly.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Wed, 04 Dec 2013 09:37:54 +0100
parents 741b6763baad
children 4f9fe58798ea
files ChangeLog src/org/thermostat/qa/reporter/ResultsGraphGenerator.java
diffstat 2 files changed, 64 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Dec 03 10:45:32 2013 +0100
+++ b/ChangeLog	Wed Dec 04 09:37:54 2013 +0100
@@ -1,3 +1,9 @@
+2013-12-04  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/org/thermostat/qa/reporter/ResultsGraphGenerator.java:
+	Fixed minor rendering issues with the graph generator - now
+	the size and placement of labels is computed more correctly.
+
 2013-12-03  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/org/thermostat/qa/reporter/Reporter.java:
--- a/src/org/thermostat/qa/reporter/ResultsGraphGenerator.java	Tue Dec 03 10:45:32 2013 +0100
+++ b/src/org/thermostat/qa/reporter/ResultsGraphGenerator.java	Wed Dec 04 09:37:54 2013 +0100
@@ -34,8 +34,10 @@
 
 import java.awt.Color;
 import java.awt.Dimension;
+import java.awt.FontMetrics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
+import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
@@ -55,9 +57,14 @@
 public class ResultsGraphGenerator implements CommonGenerator
 {
     /**
-     * 
+     * Border between graph limits and the arc shape.
      */
-    private static final int ARC_BORDER = 20;
+    private static final int ARC_BORDER = 25;
+
+    /**
+     * Offset between the arc shape and text labels.
+     */
+    private static final int TEXT_OFFSET = 15;
 
     private int width;
     private int height;
@@ -66,9 +73,13 @@
     private int arcWidth;
     private int arcHeight;
     private Graphics2D graphics;
+    private FontMetrics fontMetrics;
 
     /**
+     * Generate graph and store it as a PNG file to the specified directory.
+     *
      * @param testResult
+     *            data structure containing one test result.
      * @param graphSize
      * @param string
      * @throws IOException 
@@ -87,6 +98,18 @@
         results[3] = testResult.getIgnored();
         Color[] arcColors = {Color.green, Color.red, Color.blue, Color.gray};
 
+        drawFilledArcs(totalTests, results, arcColors);
+        drawArcsAndLabels(totalTests, results);
+
+        ImageIO.write(bitmap, "png", new File(fileName));
+    }
+
+    /**
+     * @param totalTests
+     * @param results
+     * @param arcColors
+     */
+    private void drawFilledArcs(int totalTests, int[] results, Color[] arcColors) {
         float startAngle = 0.0f;
         float arcAngle;
 
@@ -95,16 +118,21 @@
             fillArc(arcColors[i], startAngle, arcAngle);
             startAngle += arcAngle;
         }
+    }
 
-        arcAngle = 0;
+    /**
+     * @param totalTests
+     * @param results
+     */
+    private void drawArcsAndLabels(int totalTests, int[] results) {
+        float startAngle = 0.0f;
+        float arcAngle;
         for (int i = 0; i < results.length; i++) {
             arcAngle = calcArcAngle(results[i], totalTests);
             drawArc(startAngle, arcAngle);
             drawLabels(calcResultsPercentage(results[i], totalTests), startAngle, arcAngle);
             startAngle += arcAngle;
         }
-
-        ImageIO.write(bitmap, "png", new File(fileName));
     }
 
     /**
@@ -149,6 +177,7 @@
      */
     private void setGraphics(BufferedImage bitmap) {
         this.graphics = (Graphics2D) bitmap.getGraphics();
+        this.fontMetrics = this.graphics.getFontMetrics(this.graphics.getFont());
         setRenderingHints();
     }
 
@@ -196,16 +225,19 @@
     private void drawLabels(int percentage, float startAngle, float arcAngle) {
         final int centerX = this.width >> 1;
         final int centerY = this.height >> 1;
-        final int radius = (this.arcHeight >> 1) + 10;
+        final int radius = (this.arcHeight >> 1) + TEXT_OFFSET;
         final float angle = startAngle + arcAngle / 2.0f;
         double x1 = centerX + radius * Math.cos(Math.PI * angle / 180.0);
         double y1 = centerY - radius * Math.sin(Math.PI * angle / 180.0);
-        // TODO: getStringBounds or something like it would be much better
-        x1 -= 8;
+        x1 += 2;
+        String label = percentage + "%";
+        Rectangle2D rect = this.fontMetrics.getStringBounds(label, this.graphics);
+        x1 -= rect.getWidth()/2;
+        y1 += rect.getHeight()/2;
         this.graphics.setColor(Color.white);
-        this.graphics.drawString(percentage + "%", (int)x1, (int)y1);
+        this.graphics.drawString(label, (int)x1, (int)y1);
         this.graphics.setColor(Color.black);
-        this.graphics.drawString(percentage + "%", (int)x1-1, (int)y1-1);
+        this.graphics.drawString(label, (int)x1-1, (int)y1-1);
     }
 
     /**
@@ -218,6 +250,15 @@
         this.graphics.fillArc(this.arcX, this.arcY, this.arcWidth, this.arcHeight, (int)startAngle, (int)arcAngle + 1);
     }
 
+    /**
+     * Generate a graph containing daily test results.
+     *
+     * @param testResults
+     *            data structure containing the test results.
+     * @param params
+     *            parameters passed to the generator.
+     * @throws IOException
+     */
     public static void generate(Map<String, Map<String, List<String>>> testResults, CommandLineParameters params) throws IOException {
         String date = params.getDate();
         // try to find results for the given date
@@ -241,7 +282,13 @@
     public static void main(String[] args) throws IOException {
         Dimension graphSize = new Dimension(256, 256);
         TestResult testResult = new TestResult(100, 10, 5, 20);
-        new ResultsGraphGenerator().generateGraph(testResult, graphSize, "graph.png");
+        new ResultsGraphGenerator().generateGraph(testResult, graphSize, "graph1.png");
+        testResult = new TestResult(25, 25, 25, 25);
+        new ResultsGraphGenerator().generateGraph(testResult, graphSize, "graph2.png");
+        testResult = new TestResult(10, 20, 30, 40);
+        new ResultsGraphGenerator().generateGraph(testResult, graphSize, "graph3.png");
+        testResult = new TestResult(100, 10, 5, 20);
+        new ResultsGraphGenerator().generateGraph(testResult, graphSize, "graph4.png");
     }
 
 }