Coverage Report - sk.baka.webvm.misc.DivGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
DivGraph
14%
16/109
10%
7/70
5.111
 
 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  
 import java.lang.management.MemoryUsage;
 22  
 import sk.baka.webvm.Graphs;
 23  
 import static sk.baka.webvm.misc.Constants.*;
 24  
 
 25  
 /**
 26  
  * Draws stacked bars.
 27  
  * @author Martin Vysny
 28  
  */
 29  
 public final class DivGraph {
 30  
 
 31  
     /**
 32  
      * The height of a memory status bar.
 33  
      */
 34  
     public static final int MEMSTAT_BAR_HEIGHT = 20;
 35  
 
 36  
     /**
 37  
      * Draws a HTML DIV element with required properties and style.
 38  
      * @param pixels the desired pixel width/height.
 39  
      * @param i the value index, used to determine the div color.
 40  
      * @param style the style
 41  
      * @param values the values
 42  
      * @param max maximum value
 43  
      * @return a HTML DIV element.
 44  
      */
 45  
     private static String drawBarValueAsDiv(final int[] pixels, int i, final GraphStyle style, final int[] values, final int max) {
 46  0
         final StringBuilder sb = new StringBuilder();
 47  0
         final boolean isEmpty = pixels[i] == 0;
 48  0
         if (isEmpty) {
 49  0
             return "";
 50  
         }
 51  0
         final boolean isLastValue = i == pixels.length - 1;
 52  0
         drawDivOpening(sb, isLastValue, style, i, pixels[i]);
 53  0
         drawDivContents(isLastValue, style, sb, values, i, max);
 54  0
         sb.append("</div>");
 55  0
         return sb.toString();
 56  
     }
 57  
 
 58  
     private static void drawDivContents(final boolean isLastValue, final GraphStyle style, final StringBuilder sb, final int[] values, final int i, final int max) {
 59  
         // here a value in percents should be drawn (if desired)
 60  0
         if (!isLastValue) {
 61  0
             if (style.showValues) {
 62  0
                 sb.append(values[i]);
 63  0
                 if (style.showPercentage) {
 64  0
                     sb.append(" (");
 65  
                 }
 66  
             }
 67  0
             if (style.showPercentage) {
 68  0
                 if (max <= 0) {
 69  0
                     sb.append('?');
 70  
                 } else {
 71  0
                     sb.append(values[i] * HUNDRED_PERCENT / max);
 72  
                 }
 73  0
                 sb.append('%');
 74  0
                 if (style.showValues) {
 75  0
                     sb.append(')');
 76  
                 }
 77  
             }
 78  
         }
 79  0
         if (isLastValue || (!style.showValues && !style.showPercentage)) {
 80  
             // nothing should be shown in the div itself. Implement an IE hack
 81  0
             sb.append("<!-- -->");
 82  
         }
 83  0
     }
 84  
 
 85  
     /**
 86  
      * Draws an opening div element for given value.
 87  
      * @param sb add the element to this builder
 88  
      * @param isLastValue if true then this value is the last one shown in the graph. Last value is always drawn with transparent background. Use a custom background color only for non-last value.
 89  
      * @param style the style to use
 90  
      * @param i the value index
 91  
      * @param pixel the desired pixel width/heigth.
 92  
      */
 93  
     private static void drawDivOpening(final StringBuilder sb, final boolean isLastValue, final GraphStyle style, int i, final int pixel) {
 94  0
         sb.append("<div style=\"");
 95  
         // last value is always drawn with transparent background. Use a custom background color only for non-last value.
 96  0
         if (!isLastValue) {
 97  0
             sb.append("background-color: ");
 98  0
             sb.append(style.colors[i]);
 99  0
             sb.append("; ");
 100  0
             if (style.fontColors != null) {
 101  0
                 final String fontColor = style.fontColors[i];
 102  0
                 if (fontColor != null) {
 103  0
                     sb.append("color: ");
 104  0
                     sb.append(fontColor);
 105  0
                     sb.append("; ");
 106  
                 }
 107  
             }
 108  
         }
 109  0
         if (!isLastValue && !style.vertical) {
 110  0
             sb.append("float: left; ");
 111  
         }
 112  0
         sb.append(style.vertical ? "height: " : "width: ");
 113  0
         sb.append(pixel);
 114  0
         sb.append("px;\">");
 115  0
     }
 116  
 
 117  
     /**
 118  
      * Retrieves a HTML div element style, based on given graph style.
 119  
      * @param style the style
 120  
      * @param floatLeft if true then the <code>float:left</code> will be used.
 121  
      * @return the HTML style
 122  
      */
 123  
     private static String getDivStyle(final GraphStyle style, final boolean floatLeft) {
 124  0
         final StringBuilder sb = new StringBuilder();
 125  0
         sb.append("style=\"");
 126  0
         if (style.border != null) {
 127  0
             sb.append("border: 1px solid ");
 128  0
             sb.append(style.border);
 129  0
             sb.append("; ");
 130  
         }
 131  0
         if (style.showPercentage || style.showValues) {
 132  0
             sb.append("text-align: ");
 133  0
             sb.append(style.vertical ? "center" : "right");
 134  0
             sb.append("; ");
 135  
         }
 136  
         // if multiple stacked bars are drawn, make sure they are positioned horizontally.
 137  0
         if (style.vertical && floatLeft) {
 138  0
             sb.append("float:left; ");
 139  
         }
 140  0
         sb.append("width: ");
 141  0
         sb.append(style.width);
 142  0
         sb.append("px; height: ");
 143  0
         sb.append(style.height);
 144  0
         sb.append("px;\"");
 145  0
         return sb.toString();
 146  
     }
 147  
 
 148  0
     private DivGraph() {
 149  0
         throw new AssertionError();
 150  
     }
 151  
 
 152  
     /**
 153  
      * Draws a stacked bar graph.
 154  
      * @param style the graph style, must be valid.
 155  
      * @param values the values to draw. The array must be sorted from least to highest. Negative values are not permitted.
 156  
      * @param max the maximum value. A transparent div will be added at the end if necessary.
 157  
      * @param floatLeft if true then a float:left style is added to ensure that next HTML content is positioned to the right of this bar.
 158  
      * @return a html source
 159  
      */
 160  
     public static String drawStackedBar(final GraphStyle style, final int[] values, final int max, final boolean floatLeft) {
 161  0
         final StringBuilder sb = new StringBuilder();
 162  0
         drawStackedBar(style, values, max, floatLeft, sb);
 163  0
         return sb.toString();
 164  
     }
 165  
 
 166  
     /**
 167  
      * Draws a stacked bar graph.
 168  
      * @param style the graph style, must be valid.
 169  
      * @param values the values to draw. The array must be sorted from least to highest. Negative values are not permitted.
 170  
      * @param max the maximum value. A transparent div will be added at the end if necessary.
 171  
      * @param floatLeft if true then a float:left style is added to ensure that next HTML content is positioned to the right of this bar.
 172  
      * @param sb puts the HTML code here.
 173  
      */
 174  
     public static void drawStackedBar(final GraphStyle style, final int[] values, final int max, final boolean floatLeft, final StringBuilder sb) {
 175  0
         style.validate();
 176  0
         if (max < 0) {
 177  0
             throw new IllegalArgumentException("Invalid max value: " + max);
 178  
         }
 179  0
         final int[] pixels = toPixels(values, max, style.vertical ? style.height : style.width);
 180  0
         sb.append("<div ");
 181  0
         sb.append(getDivStyle(style, floatLeft));
 182  0
         sb.append(">");
 183  
         // go from pixels.length-1 to 0 when vertical
 184  
         // go from 0 to pixels.length-1 when horizontal
 185  0
         if (style.vertical) {
 186  0
             for (int i = pixels.length - 1; i >= 0; i--) {
 187  0
                 sb.append(drawBarValueAsDiv(pixels, i, style, values, max));
 188  
             }
 189  
         } else {
 190  0
             for (int i = 0; i < pixels.length; i++) {
 191  0
                 sb.append(drawBarValueAsDiv(pixels, i, style, values, max));
 192  
             }
 193  
         }
 194  0
         sb.append("</div>");
 195  0
     }
 196  
 
 197  
     /**
 198  
      * Converts given value array into pixel dimensions for each value.
 199  
      * @param values the values to convert. The array must be sorted from least to highest. Negative values are not permitted.
 200  
      * @param max the maximum value to draw.
 201  
      * @param maxPixels the desired pixel dimension.
 202  
      * @return a sorted list of pixel dimensions. Contains one additional value for the max value.
 203  
      * @throws IllegalArgumentException if the values is empty, max is not a positive number or maxPixels is not a positive number
 204  
      * @throws NullPointerException if the values array is null
 205  
      */
 206  
     static int[] toPixels(final int[] values, final int max, final int maxPixels) {
 207  4
         if (values.length == 0) {
 208  0
             throw new IllegalArgumentException("values array is empty");
 209  
         }
 210  4
         final int[] result = new int[values.length + 1];
 211  4
         int i = 0;
 212  
         // when a value reaches maximum there is no need to draw other (greater) values.
 213  4
         boolean isMaximumReached = false;
 214  4
         int pixelSize = 0;
 215  4
         int prevPixelSize = 0;
 216  10
         for (int value : values) {
 217  6
             prevPixelSize = pixelSize;
 218  6
             if (value >= max) {
 219  1
                 isMaximumReached = true;
 220  
             }
 221  6
             if (isMaximumReached) {
 222  1
                 pixelSize = maxPixels;
 223  
             } else {
 224  5
                 pixelSize = value * maxPixels / max;
 225  
             }
 226  6
             result[i++] = pixelSize - prevPixelSize;
 227  
         }
 228  4
         result[values.length] = maxPixels - pixelSize;
 229  4
         return result;
 230  
     }
 231  
 
 232  
     /**
 233  
      * Converts a memory usage to a horizontal bar graph.
 234  
      * @param usage the memory usage. Should be converted to megabytes as longs will be converted to integers.
 235  
      * @param width width of result in pixels.
 236  
      * @return a string representation of a horizontal bar line.
 237  
      */
 238  
     public static String drawMemoryStatus(final MemoryUsage usage, final int width) {
 239  0
         final GraphStyle gs = new GraphStyle();
 240  0
         gs.vertical = false;
 241  0
         gs.width = width;
 242  0
         gs.height = MEMSTAT_BAR_HEIGHT;
 243  0
         gs.showValues = true;
 244  0
         gs.colors = new String[]{Graphs.COLOR_BLUE, Graphs.COLOR_BROWN};
 245  0
         gs.border = Graphs.COLOR_GREY;
 246  0
         gs.fontColors = new String[]{Graphs.COLOR_WHITE, null};
 247  
         int max;
 248  0
         if (usage.getMax() >= 0) {
 249  0
             max = (int) usage.getMax();
 250  
         } else {
 251  0
             max = (int) (usage.getCommitted() * 7 / 5);
 252  
         }
 253  0
         if (max <= 0) {
 254  
             // if "0 out of 0" is about to be displayed, show the graph as empty.
 255  0
             max = 1;
 256  
         }
 257  0
         return DivGraph.drawStackedBar(gs, new int[]{(int) usage.getUsed(), (int) usage.getCommitted()}, max, false);
 258  
     }
 259  
 }