| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractGraph |
|
| 2.8;2.8 |
| 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.util.ArrayList; | |
| 22 | import java.util.Arrays; | |
| 23 | import java.util.Collections; | |
| 24 | import java.util.List; | |
| 25 | ||
| 26 | /** | |
| 27 | * An abstract graph, intended to be extended by classes drawing graphs. | |
| 28 | * @author Martin Vysny | |
| 29 | */ | |
| 30 | public abstract class AbstractGraph { | |
| 31 | ||
| 32 | /** | |
| 33 | * The maximum value. | |
| 34 | */ | |
| 35 | protected final int max; | |
| 36 | /** | |
| 37 | * The graph values. | |
| 38 | */ | |
| 39 | 0 | protected final List<int[]> values = new ArrayList<int[]>(); |
| 40 | /** | |
| 41 | * The graph style. | |
| 42 | */ | |
| 43 | protected final GraphStyle style; | |
| 44 | ||
| 45 | /** | |
| 46 | * Constructs a new graph instance. | |
| 47 | * @param max the maximum value. | |
| 48 | * @param style the graph style, must be valid. | |
| 49 | */ | |
| 50 | 0 | public AbstractGraph(final int max, final GraphStyle style) { |
| 51 | 0 | style.validate(); |
| 52 | 0 | this.max = max; |
| 53 | 0 | this.style = style; |
| 54 | 0 | } |
| 55 | /** | |
| 56 | * Automatically make incoming values ascending. | |
| 57 | */ | |
| 58 | 0 | public boolean makeAscending = false; |
| 59 | ||
| 60 | /** | |
| 61 | * Adds given value array to the graph. The array must be ascending. | |
| 62 | * @param values the values to add. There must be exactly one value for each {@link GraphStyle#colors graph column}. | |
| 63 | */ | |
| 64 | public final void add(final int[] values) { | |
| 65 | 0 | if (values.length != style.colors.length) { |
| 66 | 0 | throw new IllegalArgumentException("Expected " + style.colors.length + " columns but got " + values.length); |
| 67 | } | |
| 68 | 0 | for (int i = 0; i < values.length - 1; i++) { |
| 69 | 0 | if (values[i] > values[i + 1]) { |
| 70 | 0 | if (!makeAscending) { |
| 71 | 0 | throw new IllegalArgumentException("Array " + Arrays.toString(values) + " is not ascending"); |
| 72 | } else { | |
| 73 | 0 | values[i + 1] = values[i]; |
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | 0 | this.values.add(values); |
| 78 | 0 | } |
| 79 | ||
| 80 | /** | |
| 81 | * Append several zero values until a desired data length is reached. | |
| 82 | * @param desiredLength desired length of X axis. | |
| 83 | * @param fillToRight if true then the zero values are appended to the value list. If false then the zero values are prepended before all list values. | |
| 84 | */ | |
| 85 | public final void fillWithZero(final int desiredLength, final boolean fillToRight) { | |
| 86 | 0 | final int dif = desiredLength - values.size(); |
| 87 | 0 | if (dif <= 0) { |
| 88 | 0 | return; |
| 89 | } | |
| 90 | 0 | final List<int[]> listOfZeroes = Collections.nCopies(dif, new int[style.colors.length]); |
| 91 | 0 | if (fillToRight) { |
| 92 | 0 | values.addAll(listOfZeroes); |
| 93 | } else { | |
| 94 | 0 | values.addAll(0, listOfZeroes); |
| 95 | } | |
| 96 | 0 | } |
| 97 | ||
| 98 | /** | |
| 99 | * Draws the graph and returns the html code. | |
| 100 | * @return the html code of the graph. | |
| 101 | */ | |
| 102 | public final String draw() { | |
| 103 | 0 | final StringBuilder sb = new StringBuilder(); |
| 104 | 0 | draw(sb); |
| 105 | 0 | return sb.toString(); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Draws the graph and returns the html code. | |
| 110 | * @param sb draw the graph here | |
| 111 | */ | |
| 112 | public abstract void draw(final StringBuilder sb); | |
| 113 | } |