1 25 package org.jrobin.graph; 26 27 import java.awt.Color ; 28 import java.awt.BasicStroke ; 29 import java.util.HashMap ; 30 31 import org.jrobin.core.RrdException; 32 import org.jrobin.core.XmlWriter; 33 34 39 class CustomLine extends Line 40 { 41 private long xVal1; 45 private long xVal2; 46 47 private double yVal1; 48 private double yVal2; 49 50 private double dc; 51 52 53 65 CustomLine( long startTime, double startValue, long endTime, double endValue, Color color ) 66 { 67 this.color = color; 68 if ( color == null ) 69 visible = false; 70 71 this.xVal1 = startTime; 72 this.xVal2 = endTime; 73 this.yVal1 = startValue; 74 this.yVal2 = endValue; 75 76 try 77 { 78 long xc = xVal2 - xVal1; 79 if ( xc != 0 ) 80 this.dc = ( yVal2 - yVal1 ) / xc; 81 else 82 this.dc = 0; 83 } 84 catch (Exception e) { 85 this.dc = 0; 86 } 87 } 88 89 98 CustomLine( long startTime, double startValue, long endTime, double endValue, Color color, int lineWidth ) 99 { 100 this( startTime, startValue, endTime, endValue, color ); 101 this.lineWidth = lineWidth; 102 } 103 104 105 115 void draw( ChartGraphics g, int[] xValues, double[] stackValues, int lastPlotType ) throws RrdException 116 { 117 g.setColor( color ); 118 g.setStroke( lineWidth != 1 ? new BasicStroke (lineWidth) : DEF_LINE_STROKE ); 119 120 int ax, ay, nx, ny; 121 122 if ( xVal1 == Long.MIN_VALUE ) 124 ax = g.getMinX(); 125 else if ( xVal1 == Long.MAX_VALUE ) 126 ax = g.getMaxX(); 127 else 128 ax = g.getX( xVal1 ); 129 130 if ( xVal2 == Long.MIN_VALUE ) 131 nx = g.getMinX(); 132 else if ( xVal2 == Long.MAX_VALUE ) 133 nx = g.getMaxX(); 134 else 135 nx = g.getX( xVal2 ); 136 137 if ( yVal1 == Double.MIN_VALUE ) 139 ay = g.getMinY(); 140 else if ( yVal1 == Double.MAX_VALUE ) 141 ay = g.getMaxY(); 142 else 143 ay = g.getY( yVal1 ); 144 145 if ( yVal2 == Double.MIN_VALUE ) 146 ny = g.getMinY(); 147 else if ( yVal2 == Double.MAX_VALUE ) 148 ny = g.getMaxY(); 149 else 150 ny = g.getY( yVal2 ); 151 152 if ( visible ) 154 g.drawLine( ax, ay, nx, ny ); 155 156 int rx = nx - ax; 158 if ( rx != 0 ) 159 { 160 double rc = ((ny - ay) * 1.0d) / rx; 161 for (int i = 0; i < xValues.length; i++) { 162 if ( xValues[i] < ax || xValues[i] > nx ) 163 stackValues[i] = g.getInverseY(0); 164 else if ( ay == ny ) 165 stackValues[i] = g.getInverseY(ay); 166 else 167 stackValues[i] = g.getInverseY( (int) (rc * (xValues[i] - ax) + ay) ); 168 } 169 } 170 171 g.setStroke( STROKE ); 172 } 173 174 181 double getValue( int tblPos, long[] timestamps ) 182 { 183 long time = timestamps[tblPos]; 184 185 if ( time > xVal2 || time < xVal1 ) 187 return Double.NaN; 188 189 if ( yVal1 == yVal2 ) 191 return yVal1; 192 193 if ( yVal1 == Double.MIN_VALUE && yVal2 == Double.MAX_VALUE ) 195 return Double.NaN; 196 197 if ( xVal1 == xVal2 ) 199 return Double.NaN; 200 201 return ( dc * ( time - xVal1 ) + yVal1 ); 203 } 204 205 void setSource( Source[] sources, HashMap sourceIndex ) throws RrdException { 207 } 208 209 void setValue( int tableRow, long preciseTime, long[] reducedTimestamps ) { 211 } 212 213 void exportXmlTemplate( XmlWriter xml, String legend ) { 214 if(yVal1 == yVal2 && xVal1 != xVal2) { 215 xml.startTag("hrule"); 217 xml.writeTag("value", yVal1); 218 xml.writeTag("color", color); 219 xml.writeTag("legend", legend); 220 xml.writeTag("width", lineWidth); 221 xml.closeTag(); } 223 else if(yVal1 != yVal2 && xVal1 == xVal2) { 224 xml.startTag("vrule"); 226 xml.writeTag("time", xVal1); 227 xml.writeTag("color", color); 228 xml.writeTag("legend", legend); 229 xml.writeTag("width", lineWidth); 230 xml.closeTag(); } 232 else if(yVal1 != yVal2 && xVal1 != xVal2) { 233 xml.startTag("line"); 235 xml.writeTag("time1", xVal1); 236 xml.writeTag("value1", yVal1); 237 xml.writeTag("time2", xVal2); 238 xml.writeTag("value2", yVal2); 239 xml.writeTag("color", color); 240 xml.writeTag("legend", legend); 241 xml.writeTag("width", lineWidth); 242 xml.closeTag(); } 244 } 245 } 246 | Popular Tags |