Coverage Report - sk.baka.webvm.misc.BluffGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
BluffGraph
0%
0/62
0%
0/23
4
BluffGraph$1
0%
0/1
N/A
4
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 18  
  */
 19  
 package sk.baka.webvm.misc;
 20  
 
 21  
 /**
 22  
  * Renders a graph using the Bluff javascript rendering engine.
 23  
  * @author Martin Vysny
 24  
  */
 25  
 public final class BluffGraph extends AbstractGraph {
 26  
 
 27  
     /**
 28  
      * Creates new graph.
 29  
      * @param max the maximum value, ignored.
 30  
      * @param style the style.
 31  
      */
 32  
     public BluffGraph(int max, GraphStyle style) {
 33  0
         super(max, style);
 34  0
     }
 35  
 
 36  
     @Override
 37  
     public void draw(StringBuilder sb) {
 38  0
         final String id = Integer.toString(System.identityHashCode(this));
 39  0
         sb.append("<canvas id=\"");
 40  0
         sb.append(id);
 41  0
         sb.append("\"");
 42  0
         if (style.border != null) {
 43  0
             sb.append(" style=\"border: 1px solid ");
 44  0
             sb.append(style.border);
 45  0
             sb.append("; \"");
 46  
         }
 47  0
         sb.append("></canvas><script type=\"text/javascript\">\n");
 48  0
         drawBluffInitializationScript(sb, id);
 49  0
         for (int i = 0; i < style.colors.length; i++) {
 50  0
             drawBluffDataAxis(sb, i);
 51  
         }
 52  0
         sb.append("  g.minimum_value=0;g.maximum_value=");
 53  0
         sb.append(max);
 54  0
         sb.append(";\n");
 55  0
         sb.append("  g.draw();\n");
 56  0
         sb.append("</script>\n");
 57  0
     }
 58  
 
 59  
     private void drawBluffDataAxis(StringBuilder sb, int i) {
 60  0
         sb.append("  g.data(\"\", [");
 61  0
         boolean first = true;
 62  
         // hack to suppress the "No Data" message - displayed when a bunch of zeroes is fed to the graph.
 63  
         // Just add a single "1" to the end of the graph :)
 64  0
         boolean zeroesOnly = true;
 65  0
         for (final int[] vals : this.values) {
 66  0
             if (first) {
 67  0
                 first = false;
 68  
             } else {
 69  0
                 sb.append(",");
 70  
             }
 71  0
             int val = vals[i];
 72  0
             if (style.style == GraphStyle.GraphStyleEnum.StackedBar) {
 73  
                 // elements are stacked on top of each other, not behind themselves. Recompute items values to fix this.
 74  0
                 for (int j = 0; j < i; j++) {
 75  0
                     val -= vals[j];
 76  
                 }
 77  
             }
 78  0
             if (val != 0) {
 79  0
                 zeroesOnly = false;
 80  
             }
 81  0
             sb.append(val);
 82  0
         }
 83  0
         if (zeroesOnly) {
 84  0
             sb.append('1');
 85  
         }
 86  0
         sb.append("], '");
 87  0
         sb.append(style.colors[i]);
 88  0
         sb.append("');\n");
 89  0
     }
 90  
 
 91  
     private void drawBluffInitializationScript(StringBuilder sb, final String id) {
 92  0
         sb.append("  var g = new Bluff.");
 93  0
         switch (style.style) {
 94  
             case StackedBar:
 95  0
                 sb.append("StackedArea");
 96  0
                 break;
 97  
             case Line:
 98  0
                 sb.append("Line");
 99  
                 break;
 100  
         }
 101  0
         sb.append("('");
 102  0
         sb.append(id);
 103  0
         sb.append("', '");
 104  0
         sb.append(style.width);
 105  0
         sb.append('x');
 106  0
         sb.append(style.height);
 107  0
         sb.append("');\n");
 108  0
         if (style.caption == null) {
 109  0
             sb.append("  g.hide_title = true;\n");
 110  
         } else {
 111  0
             sb.append("  g.title='");
 112  0
             sb.append(style.caption);
 113  0
             sb.append("';\n");
 114  
         }
 115  0
         if (!style.yLegend) {
 116  0
             sb.append("g.hide_line_numbers = true;");
 117  
         }
 118  0
         sb.append("g.hide_legend = true;g.set_margins(0);g.sort = false;g.theme_37signals();\n");
 119  0
     }
 120  
 }