1 package org.jrobin.demo; 2 3 27 28 import org.jrobin.graph.*; 29 import org.jrobin.core.RrdException; 30 import org.jrobin.core.Util; 31 32 import java.awt.*; 33 import java.io.IOException ; 34 import java.util.Date ; 35 import java.util.GregorianCalendar ; 36 import java.util.Calendar ; 37 38 class PlottableDemo { 39 static final double[] SF_DOWNLOAD_COUNT = { 40 0, 0, 13, 34, 76, 72, 255, 144, 135, 194, 358, 304, 293 41 }; 42 static final double[] SF_PAGE_HITS = { 43 0, 1072, 517, 979, 2132, 2532, 5515, 3519, 3500, 4942, 7858, 7797, 6570 44 }; 45 static final GregorianCalendar [] SF_TIMESTAMPS = 46 new GregorianCalendar [SF_DOWNLOAD_COUNT.length]; 47 static final Date SF_START_DATE = 48 new GregorianCalendar (2003, 4, 1).getTime(); 50 static { 51 for(int i = 0; i < SF_TIMESTAMPS.length; i++) { 52 GregorianCalendar gc = new GregorianCalendar (); 53 gc.setTime(SF_START_DATE); 54 gc.add(Calendar.MONTH, i); 55 SF_TIMESTAMPS[i] = gc; 56 } 57 } 58 59 private PlottableDemo() throws RrdException, IOException { 60 createGraph1(); 61 createGraph2(); 62 createGraph3(); 63 createGraph4(); 64 createGraph5(); 65 createGraph6(); 66 createGraph7(); 67 createGraph8(); 68 createGraph9(); 69 createGraph10(); 70 createGraph11(); 71 createGraph12(); 72 createGraph13(); 73 createGraph14(); 74 createGraph15(); 75 createGraph16(); 76 createGraph17(); 77 } 78 79 private void createGraph1() throws RrdException, IOException { 80 final long t0 = new Date ().getTime() / 1000L, dt = 86400L; 81 final int n = 10; 82 final long t1 = t0 + (n - 1) * dt; 83 Plottable p = new Plottable() { 84 public double getValue(long t) { 85 double x = (t - t0) / (double) (t1 - t0); 86 return Math.exp(-x * 2) * Math.cos(x * 7 * Math.PI); 87 } 88 }; 89 90 long t[] = new long[n]; 91 double x[] = new double[n]; 92 for (int i = 0; i < n; i++) { 93 t[i] = t0 + i * dt; 94 x[i] = p.getValue(t[i]); 95 } 96 LinearInterpolator i1 = new LinearInterpolator(t, x); CubicSplineInterpolator i2 = new CubicSplineInterpolator(t, x); 98 RrdGraphDef gDef = new RrdGraphDef(t0, t1); 100 gDef.setTitle("Plottable demonstration"); 101 gDef.setTimeAxisLabel("days of our lives"); 102 gDef.setVerticalLabel("inspiration"); 103 gDef.datasource("real", p); 104 gDef.datasource("linear", i1); 105 gDef.datasource("spline", i2); 106 gDef.line("real", Color.BLUE, "Real values", 1); 107 gDef.line("linear", Color.RED, "Linear interpolation", 1); 108 gDef.line("spline", Color.MAGENTA, "Spline interpolation@r", 1); 109 gDef.setTimeAxis(TimeAxisUnit.DAY, 1, TimeAxisUnit.DAY, 1, "dd", true); 110 createGraph(gDef); 111 } 112 113 private void createGraph2() throws RrdException, IOException { 114 GregorianCalendar [] timestamps = { 115 new GregorianCalendar (2004, 2, 1, 0, 0, 0), 116 new GregorianCalendar (2004, 2, 1, 2, 0, 0), 117 new GregorianCalendar (2004, 2, 1, 7, 0, 0), 118 new GregorianCalendar (2004, 2, 1, 14, 0, 0), 119 new GregorianCalendar (2004, 2, 1, 17, 0, 0), 120 new GregorianCalendar (2004, 2, 1, 19, 0, 0), 121 new GregorianCalendar (2004, 2, 1, 23, 0, 0), 122 new GregorianCalendar (2004, 2, 1, 24, 0, 0) 123 }; 124 double[] values = {100, 250, 230, 370, 350, 300, 340, 350}; 125 LinearInterpolator linear = new LinearInterpolator(timestamps, values); 126 linear.setInterpolationMethod(LinearInterpolator.INTERPOLATE_LEFT); 127 CubicSplineInterpolator spline = new CubicSplineInterpolator(timestamps, values); 128 RrdGraphDef gDef = new RrdGraphDef(timestamps[0], timestamps[timestamps.length - 1]); 129 gDef.setTitle("Plottable demonstration"); 130 gDef.setTimeAxisLabel("time"); 131 gDef.setVerticalLabel("water level [inches]"); 132 gDef.datasource("linear", linear); 133 gDef.datasource("spline", spline); 134 gDef.area("spline", Color.ORANGE, "Spline interpolation"); 135 gDef.line("linear", Color.RED, "Linear inteprolation@r", 2); 136 gDef.gprint("spline", "AVERAGE", "Average spline value: @0 inches@r"); 137 gDef.gprint("linear", "AVERAGE", "Average linear value: @0 inches@r"); 138 createGraph(gDef); 139 } 140 141 private void createGraph3() throws RrdException, IOException { 142 LinearInterpolator linear = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 143 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 144 gDef.setTitle("JRobin page hits per month"); 145 gDef.setTimeAxisLabel("month"); 146 gDef.setVerticalLabel("page hits"); 147 gDef.datasource("linear", linear); 148 gDef.area("linear", Color.GREEN, null); 149 gDef.line("linear", Color.RED, "page hits@L", 2); 150 gDef.vrule(new GregorianCalendar (2004, 0, 1), Color.BLUE, null, 3); 151 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 152 gDef.comment("Data provided by SourceForge.net@r"); 153 createGraph(gDef); 154 } 155 156 private void createGraph4() throws RrdException, IOException { 157 LinearInterpolator linear = new LinearInterpolator(SF_TIMESTAMPS, SF_DOWNLOAD_COUNT); 158 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 159 gDef.setTitle("JRobin download count per month"); 160 gDef.setTimeAxisLabel("month"); 161 gDef.setVerticalLabel("download count"); 162 gDef.datasource("linear", linear); 163 gDef.area("linear", Color.GREEN, null); 164 gDef.line("linear", Color.RED, "download count@L", 2); 165 gDef.vrule(new GregorianCalendar (2004, 0, 1), Color.BLUE, null, 3); 166 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 167 gDef.comment("Data provided by SourceForge.net@r"); 168 createGraph(gDef); 169 } 170 171 private void createGraph5() throws RrdException, IOException { 172 LinearInterpolator hitsInterpolator = 173 new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 174 LinearInterpolator downloadsInterpolator = 175 new LinearInterpolator(SF_TIMESTAMPS, SF_DOWNLOAD_COUNT); 176 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], 177 SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 178 gDef.setTitle("JRobin statistics at SourceForge"); 179 gDef.setTimeAxisLabel("month"); 180 gDef.setVerticalLabel("hits/downloads"); 181 gDef.datasource("hits", hitsInterpolator); 182 gDef.datasource("downloads", downloadsInterpolator); 183 gDef.datasource("ratio", "downloads,0,EQ,UNKN,hits,downloads,/,IF"); 184 gDef.area("hits", Color.GREEN, null); 185 gDef.line("hits", Color.RED, "page hits", 2); 186 gDef.area("downloads", Color.MAGENTA, "downloads@L"); 187 gDef.vrule(new GregorianCalendar (2004, 0, 1), Color.BLUE, null, 3); 188 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 189 gDef.gprint("ratio", "AVERAGE", "Average number of page hits per download: @0@r"); 190 gDef.comment("Data provided by SourceForge.net@r"); 191 createGraph(gDef); 192 } 193 194 private void createGraph6() throws RrdException, IOException { 195 LinearInterpolator hitsInterpolator = 196 new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 197 LinearInterpolator trendInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 198 trendInterpolator.setInterpolationMethod(LinearInterpolator.INTERPOLATE_REGRESSION); 199 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 200 gDef.setTitle("Trend report"); 201 gDef.setTimeAxisLabel("month"); 202 gDef.setVerticalLabel("hits"); 203 gDef.datasource("hits", hitsInterpolator); 204 gDef.datasource("trend", trendInterpolator); 205 gDef.datasource("diff", "hits,trend,-"); 206 gDef.datasource("absdiff", "diff,ABS"); 207 gDef.area("trend", null, null); 208 gDef.stack("diff", Color.YELLOW, "difference"); 209 gDef.line("hits", Color.RED, "real page hits"); 210 gDef.line("trend", Color.BLUE, "trend@L"); 211 gDef.gprint("absdiff", "AVERAGE", "Average difference: @0@r"); 212 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 213 createGraph(gDef); 214 } 215 216 private void createGraph7() throws RrdException, IOException { 217 CubicSplineInterpolator hitsInterpolator = 218 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 219 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], 220 SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 221 gDef.setTitle("Trick graph"); 222 gDef.setTimeAxisLabel("month"); 223 gDef.setVerticalLabel("hits"); 224 gDef.datasource("hits", hitsInterpolator); 225 gDef.datasource("hits2", "hits,1000,-"); 226 gDef.datasource("invisible", "hits2,0,GE,hits2,0,IF"); 227 gDef.datasource("margin", "hits,invisible,-"); 228 gDef.area("invisible", null, null); 229 gDef.stack("margin", Color.YELLOW, "yellow margin"); 230 gDef.line("hits", Color.RED, "page hits", 3); 231 gDef.line("hits", Color.WHITE, null, 1); 232 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 233 createGraph(gDef); 234 } 235 236 private void createGraph8() throws RrdException, IOException { 237 CubicSplineInterpolator hitsInterpolator = 238 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 239 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], 240 SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 241 gDef.setTitle("Trick graph"); 242 gDef.setTimeAxisLabel("month"); 243 gDef.setVerticalLabel("hits"); 244 gDef.datasource("hits", hitsInterpolator); 245 gDef.datasource("avg", "hits", "AVERAGE"); 246 gDef.datasource("diff", "avg,hits,-"); 247 gDef.datasource("diffpos", "diff,0,GE,diff,0,IF"); 248 gDef.datasource("diffneg", "diff,0,LT,diff,0,IF"); 249 gDef.area("hits", null, null); 250 gDef.stack("diffpos", Color.RED, "bad"); 251 gDef.stack("diffneg", Color.GREEN, "good"); 252 gDef.line("hits", Color.BLUE, "hits", 3); 253 gDef.line("hits", Color.WHITE, null, 1); 254 gDef.line("avg", Color.MAGENTA, "average@L", 3); 255 gDef.line("avg", Color.WHITE, null, 1); 256 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 257 gDef.gprint("hits", "AVERAGE", "Average: @0@r"); 258 createGraph(gDef); 259 } 260 261 private void createGraph9() throws RrdException, IOException { 262 GregorianCalendar [] times = { SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1] }; 263 double[] values = { SF_PAGE_HITS[0], SF_PAGE_HITS[SF_PAGE_HITS.length - 1] }; 264 LinearInterpolator trendLine = new LinearInterpolator(times, values); 265 CubicSplineInterpolator hitsInterpolator = 266 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 267 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], 268 SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 269 gDef.setTitle("Trick graph"); 270 gDef.setTimeAxisLabel("month"); 271 gDef.setVerticalLabel("hits"); 272 gDef.datasource("hits", hitsInterpolator); 273 gDef.datasource("trend", trendLine); 274 gDef.datasource("diff", "trend,hits,-"); 275 gDef.area("hits", null, null); 276 gDef.stack("diff", Color.YELLOW, "difference"); 277 gDef.line("hits", Color.BLUE, "hits"); 278 gDef.line("trend", Color.RED, "trend@L"); 279 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 280 createGraph(gDef); 281 } 282 283 private void createGraph10() throws RrdException, IOException { 284 final int GRADIENT_STEPS = 30; 285 final Color color1 = Color.RED, color2 = Color.YELLOW; 286 CubicSplineInterpolator hitsInterpolator = 287 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 288 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 289 gDef.setTitle("Trick graph"); 290 gDef.setTimeAxisLabel("month"); 291 gDef.setVerticalLabel("hits"); 292 gDef.datasource("hits", hitsInterpolator); 293 for(int i = 0; i <= GRADIENT_STEPS; i++) { 294 gDef.datasource("hits" + i, "hits," + i + ",*," + GRADIENT_STEPS + ",/"); 295 } 296 for(int i = GRADIENT_STEPS; i >=0 ; i--) { 297 Color c = interpolateColor(color1, color2, i / (double) GRADIENT_STEPS); 298 gDef.area("hits" + i, c, null); 299 } 300 gDef.line("hits", Color.BLACK, "Number of page hits"); 301 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 302 createGraph(gDef); 303 } 304 305 private void createGraph11() throws RrdException, IOException { 306 final int GRADIENT_STEPS = 30; 307 final Color color1 = Color.RED, color2 = Color.YELLOW; 308 CubicSplineInterpolator hitsInterpolator = 309 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 310 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 311 gDef.setTitle("Trick graph"); 312 gDef.setTimeAxisLabel("month"); 313 gDef.setVerticalLabel("hits"); 314 gDef.datasource("hits", hitsInterpolator); 315 for(int i = 0; i <= GRADIENT_STEPS; i++) { 316 gDef.datasource("hits" + i, "hits," + i + ",*," + GRADIENT_STEPS + ",/"); 317 } 318 for(int i = GRADIENT_STEPS; i >= 0 ; i--) { 319 Color c = interpolateColor(color1, color2, i / (double) GRADIENT_STEPS); 320 gDef.area("hits" + i, c, null); 321 } 322 gDef.line("hits", color2, "Estimated number of page hits"); 323 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 324 gDef.setCanvasColor(color1); 325 createGraph(gDef); 326 } 327 328 private void createGraph12() throws RrdException, IOException { 329 final int GRADIENT_STEPS = 30; 330 final Color color1 = Color.YELLOW, color2 = Color.RED; 331 CubicSplineInterpolator hitsInterpolator = 332 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 333 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 334 gDef.setTitle("Trick graph"); 335 gDef.setTimeAxisLabel("month"); 336 gDef.setVerticalLabel("hits"); 337 gDef.datasource("hits", hitsInterpolator); 338 gDef.datasource("top", "hits", "MAX"); 339 for(int i = 1; i <= GRADIENT_STEPS; i++) { 340 gDef.datasource("hits" + i, "hits,top," + i + ",*," + GRADIENT_STEPS + ",/,MIN"); 341 } 342 for(int i = GRADIENT_STEPS; i >= 1 ; i--) { 343 Color c = i % 2 == 0? color1: color2; 344 gDef.area("hits" + i, c, null); 345 } 346 gDef.line("hits", color2, "Estimated number of page hits"); 347 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 348 createGraph(gDef); 349 } 350 351 private void createGraph13() throws RrdException, IOException { 352 final int GRADIENT_STEPS = 15; 353 final Color color1 = Color.LIGHT_GRAY, color2 = Color.WHITE; 354 CubicSplineInterpolator hitsInterpolator = 355 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 356 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 357 gDef.setTitle("Trick graph"); 358 gDef.setTimeAxisLabel("month"); 359 gDef.setVerticalLabel("hits"); 360 gDef.datasource("hits", hitsInterpolator); 361 for(int i = GRADIENT_STEPS; i >= 1 ; i--) { 362 Color c = interpolateColor(color1, color2, i / (double) GRADIENT_STEPS); 363 gDef.line("hits", c, null, i); 364 } 365 gDef.line("hits", color1, "Estimated number of page hits"); 366 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 367 createGraph(gDef); 368 } 369 370 private void createGraph14() throws RrdException, IOException { 371 final int GRADIENT_STEPS = 20; 372 final double GRADIENT_WIDTH = 2000.0; 373 final Color color1 = Color.RED, color2 = Color.WHITE; 374 CubicSplineInterpolator hitsInterpolator = 375 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 376 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 377 gDef.setTitle("Trick graph"); 378 gDef.setTimeAxisLabel("month"); 379 gDef.setVerticalLabel("hits"); 380 gDef.datasource("hits", hitsInterpolator); 381 for(int i = 0; i <= GRADIENT_STEPS; i++) { 382 gDef.datasource("hits" + i, 383 "hits," + GRADIENT_WIDTH + "," + i + ",*," + GRADIENT_STEPS + ",/,-,0,MAX"); 384 } 385 for(int i = 0; i <= GRADIENT_STEPS; i++) { 386 gDef.area("hits" + i, interpolateColor(color1, color2, i / (double) GRADIENT_STEPS), null); 387 } 388 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 389 createGraph(gDef); 390 } 391 392 private void createGraph15() throws RrdException, IOException { 393 final int STEPS = 20; 394 final Color color1 = Color.BLACK, color2 = Color.RED; 395 CubicSplineInterpolator hitsInterpolator = 396 new CubicSplineInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 397 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 398 gDef.setTitle("Trick graph"); 399 gDef.setTimeAxisLabel("month"); 400 gDef.setVerticalLabel("hits"); 401 gDef.datasource("hits", hitsInterpolator); 402 gDef.datasource("maxhits", "hits", "MAX"); 403 for(int i = 1; i <= STEPS; i++) { 404 gDef.datasource("hits" + i, "maxhits," + i + ",*," + STEPS + ",/,hits,GE,hits,0,IF"); 405 } 406 for(int i = STEPS; i >= 1; i--) { 407 gDef.area("hits" + i, interpolateColor(color1, color2, i / (double) STEPS), null); 408 } 409 gDef.line("hits", Color.BLUE, "page hits", 2); 410 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 411 createGraph(gDef); 412 } 413 414 private void createGraph16() throws RrdException, IOException { 415 final int STEPS = 20; 416 final Color color1 = Color.BLACK, color2 = Color.RED; 417 LinearInterpolator hitsInterpolator = 418 new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 419 hitsInterpolator.setInterpolationMethod(LinearInterpolator.INTERPOLATE_LEFT); 420 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 421 gDef.setTitle("Trick graph"); 422 gDef.setTimeAxisLabel("month"); 423 gDef.setVerticalLabel("hits"); 424 gDef.datasource("hits", hitsInterpolator); 425 gDef.datasource("maxhits", "hits", "MAX"); 426 for(int i = 1; i <= STEPS; i++) { 427 gDef.datasource("hits" + i, "maxhits," + i + ",*," + STEPS + ",/,hits,GE,hits,0,IF"); 428 } 429 for(int i = STEPS; i >= 1; i--) { 430 gDef.area("hits" + i, interpolateColor(color1, color2, i / (double) STEPS), null); 431 } 432 gDef.line("hits", Color.BLUE, "page hits", 2); 433 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 434 createGraph(gDef); 435 } 436 437 private void createGraph17() throws RrdException, IOException { 438 final int STEPS = 20; 439 final Color color1 = Color.YELLOW, color2 = Color.RED; 440 LinearInterpolator hitsInterpolator = 441 new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 442 LinearInterpolator trendInterpolator = new LinearInterpolator(SF_TIMESTAMPS, SF_PAGE_HITS); 443 trendInterpolator.setInterpolationMethod(LinearInterpolator.INTERPOLATE_REGRESSION); 444 RrdGraphDef gDef = new RrdGraphDef(SF_TIMESTAMPS[0], SF_TIMESTAMPS[SF_TIMESTAMPS.length - 1]); 445 gDef.setTitle("Trick graph"); 446 gDef.setTimeAxisLabel("month"); 447 gDef.setVerticalLabel("difference"); 448 gDef.datasource("hits", hitsInterpolator); 449 gDef.datasource("trend", trendInterpolator); 450 gDef.datasource("diff", "hits,trend,-"); 451 for(int i = 1; i <= STEPS; i++) { 452 gDef.datasource("diff" + i, "diff," + i + ",*," + STEPS + ",/"); 453 } 454 for(int i = STEPS; i >= 1; i--) { 455 String ds = "diff" + i; 456 Color c = interpolateColor(color1, color2, i / (double) STEPS); 457 String legend = (i == 1)? "dissipation": null; 458 gDef.area(ds, c, legend); 459 } 460 gDef.setCanvasColor(color2); 461 gDef.setTimeAxis(TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, "MMM", false); 462 createGraph(gDef); 463 } 464 465 private Color interpolateColor(Color c1, Color c2, double factor) { 466 int r = c1.getRed() + (int)((c2.getRed() - c1.getRed()) * factor); 467 int g = c1.getGreen() + (int)((c2.getGreen() - c1.getGreen()) * factor); 468 int b = c1.getBlue() + (int)((c2.getBlue() - c1.getBlue()) * factor); 469 return new Color(r, g, b); 470 } 471 472 private static int count; 473 474 private static void createGraph(RrdGraphDef gDef) throws IOException , RrdException { 475 RrdGraph graph = new RrdGraph(gDef); 476 String filename = Util.getJRobinDemoPath("plottable" + (++count) + ".png"); 477 graph.saveAsPNG(filename, 400, 200); 478 System.out.println("Saved to: " + filename); 479 } 480 481 public static void main(String [] args) throws RrdException, IOException { 482 new PlottableDemo(); 483 } 484 } 485 | Popular Tags |