1 23 24 package de.progra.charting; 25 26 import de.progra.charting.render.AbstractRenderer; 27 import de.progra.charting.render.ChartRenderingHints; 28 import java.awt.Rectangle ; 29 import java.awt.geom.Point2D ; 30 import java.awt.geom.Rectangle2D ; 31 import java.awt.geom.Line2D ; 32 import java.awt.geom.AffineTransform ; 33 import java.awt.Graphics2D ; 34 import java.awt.Dimension ; 35 import java.awt.Color ; 36 import java.awt.Font ; 37 import java.awt.font.FontRenderContext ; 38 import java.awt.font.TextLayout ; 39 import java.text.DecimalFormat ; 40 import de.progra.charting.model.ChartDataModel; 41 import de.progra.charting.model.ChartDataModelConstraints; 42 43 44 59 public class CoordSystem extends AbstractRenderer { 60 61 62 protected String xaxis_unit = "x"; 63 64 protected String yaxis_unit = "y"; 65 66 67 protected Font font = new Font ("sans", Font.PLAIN, 9); 68 69 70 protected final FontRenderContext frc = new FontRenderContext (null, false, false); 71 72 protected DecimalFormat dfY; 73 74 protected DecimalFormat dfX; 75 76 protected boolean shouldDrawArrows = true; 77 78 protected boolean shouldPaintAltTick = true; 79 81 protected boolean shouldPaintOnlyTick = true; 82 83 84 protected boolean shouldPaintLabels = true; 85 86 87 protected int leftmargin = 50; 88 89 protected int topmargin = 20; 90 91 92 protected int rightmargin = 30; 93 94 protected int bottommargin = 30; 95 96 public final int MINIMALMARGIN = 20; 97 98 public final int ARROWLENGTH = 15; 99 100 101 protected ChartDataModelConstraints constraints; 102 103 protected ChartDataModelConstraints constraints2; 104 105 106 protected ChartDataModel model; 107 108 109 protected CoordSystemUtilities c; 110 111 112 protected Axis xaxis; 113 114 protected Axis yaxis; 115 116 protected Axis yaxis2; 117 118 119 protected AffineTransform y1transform; 120 121 protected AffineTransform y2transform; 122 123 125 public static final int FIRST_YAXIS = 0; 126 128 public static final int SECOND_YAXIS = 1; 129 130 136 public CoordSystem(ChartDataModel cdm) { 137 this.constraints = cdm.getChartDataModelConstraints(FIRST_YAXIS); 138 this.constraints2 = cdm.getChartDataModelConstraints(SECOND_YAXIS); 139 140 this.model = cdm; 141 142 xaxis = new Axis(Axis.HORIZONTAL, constraints); 143 yaxis = new Axis(Axis.VERTICAL, constraints); 144 145 c = new CoordSystemUtilities(this, constraints, constraints2, model); 146 147 dfY = new DecimalFormat (); 148 dfX = new DecimalFormat (); 149 } 150 151 159 public CoordSystem(ChartDataModel c, String xunit, String yunit) { 160 this(c); 161 162 setXAxisUnit(xunit); 163 setYAxisUnit(yunit); 164 } 165 166 175 public CoordSystem(ChartDataModel c, DecimalFormat yAxisFormat, 176 boolean drawArrows, boolean paintAltYTick, boolean paintOnlyYTick) { 177 this(c); 178 dfY = yAxisFormat; 179 shouldDrawArrows = drawArrows; 180 shouldPaintAltTick = paintAltYTick; 181 shouldPaintOnlyTick = paintOnlyYTick; 182 } 183 184 189 public void setTransform(AffineTransform at, int axis) { 190 switch(axis) { 191 case(FIRST_YAXIS): y1transform = at; break; 192 case(SECOND_YAXIS): y2transform = at; break; 193 } 194 } 195 196 199 public AffineTransform getTransform(int axis) { 200 switch(axis) { 201 case(FIRST_YAXIS): return y1transform; 202 case(SECOND_YAXIS): return y2transform; 203 } 204 205 return null; 206 } 207 208 215 public AffineTransform getDefaultTransform(int axis) { 216 double x_pt2px = 0; 217 double y_pt2px = 0; 218 double xcoord0 = 0; 219 double ycoord0 = 0; 220 221 x_pt2px = 1 / getXAxis().getPointToPixelRatio(); 222 xcoord0 = getBounds().getX() + getLeftMargin() + getXAxis().getPixelForValue(0.0); 224 225 switch(axis) { 226 case FIRST_YAXIS: 227 y_pt2px = 1 / getFirstYAxis().getPointToPixelRatio(); 228 ycoord0 = getBounds().getY() + getBounds().getHeight() - getBottomMargin() - 229 getFirstYAxis().getPixelForValue(0.0); 230 break; 231 case SECOND_YAXIS: 232 y_pt2px = 1 / getSecondYAxis().getPointToPixelRatio(); 233 ycoord0 = getBounds().getY() + getBounds().getHeight() - getBottomMargin() - 234 getSecondYAxis().getPixelForValue(0.0); 235 break; 236 } 237 return new AffineTransform (x_pt2px, 0f, 0f, 238 -y_pt2px, xcoord0, ycoord0); 239 } 240 241 244 public void setXAxis(Axis a) { 245 xaxis = a; 246 } 247 248 251 public Axis getXAxis() { 252 return xaxis; 253 } 254 255 258 public void setXAxisUnit(String xunit) { 259 this.xaxis_unit = xunit; 260 } 261 262 265 public String getXAxisUnit() { 266 return xaxis_unit; 267 } 268 269 272 public void setYAxisUnit(String yunit) { 273 this.yaxis_unit = yunit; 274 } 275 276 279 public String getYAxisUnit() { 280 return yaxis_unit; 281 } 282 283 286 public void setFont(Font f) { 287 font = f; 288 } 289 290 293 public Font getFont() { 294 return font; 295 } 296 297 300 public void setFirstYAxis(Axis a) { 301 yaxis = a; 302 } 303 304 307 public Axis getFirstYAxis() { 308 return yaxis; 309 } 310 311 314 public void setSecondYAxis(Axis a) { 315 yaxis2 = a; 316 } 317 318 321 public Axis getSecondYAxis() { 322 return yaxis2; 323 } 324 325 328 public Rectangle getInnerBounds() { 329 Rectangle b = getBounds(); 330 Rectangle i = new Rectangle ((int)b.getX() + getLeftMargin() - 1, 331 (int)b.getY() + getTopMargin() - 1, 332 (int)b.getWidth() - (getLeftMargin() + getRightMargin()) + 2, 333 (int)b.getHeight() - (getTopMargin() + getBottomMargin()) + 2); 334 return i; 335 } 336 337 342 public void setBounds(Rectangle bounds) { 343 super.setBounds(bounds); 344 345 setRightMargin(c.computeRightMargin()); 346 setLeftMargin(c.computeLeftMargin()); 347 348 setTopMargin(c.computeTopMargin()); 349 setBottomMargin(c.computeBottomMargin()); 350 351 xaxis.setLength((int)(bounds.getWidth()) - getLeftMargin() - getRightMargin()); 352 yaxis.setLength((int)(bounds.getHeight()) - getTopMargin() - getBottomMargin()); 354 setTransform(getDefaultTransform(FIRST_YAXIS), FIRST_YAXIS); 356 if(yaxis2 != null) { 357 yaxis2.setLength((int)(bounds.getHeight()) - getTopMargin() - getBottomMargin()); 358 setTransform(getDefaultTransform(SECOND_YAXIS), SECOND_YAXIS); 359 } 360 } 361 362 365 public Dimension getPreferredSize() { 366 return new Dimension (Integer.MIN_VALUE, Integer.MIN_VALUE); 367 } 368 369 372 public void render(Graphics2D g) { 373 paintDefault(g); 374 } 375 376 382 public void paintDefault(Graphics2D g) { 383 g.setColor(Color.black); 384 385 Line2D x = c.getXAxisLine2D(); 386 Line2D y = c.getYAxisLine2D(); 387 388 g.draw(x); 389 g.draw(y); 390 391 if(shouldDrawArrows) { 393 g.drawLine((int)x.getX2(), (int)x.getY2(), (int)x.getX2() + ARROWLENGTH, (int)x.getY2()); 394 g.fillPolygon(new int[] {(int)(x.getX2() + ARROWLENGTH / 3.0), 395 (int)(x.getX2() + ARROWLENGTH / 3.0), 396 (int)(x.getX2() + ARROWLENGTH)}, 397 new int[] {(int)x.getY2() - 3, (int)x.getY2() + 3, (int)x.getY2()}, 398 3); 399 } 400 401 g.setColor(Color.black); 403 TextLayout layoutX = new TextLayout (getXAxisUnit(), getFont(), 404 new FontRenderContext (null, true, false)); 405 layoutX.draw(g, (float)x.getX2() + (float)ARROWLENGTH / 3, (float)x.getY2() + (float)layoutX.getBounds().getHeight() + 5); 406 407 if(shouldDrawArrows) { 409 g.drawLine((int)y.getX1(), (int)y.getY1(), (int)y.getX1(), (int)y.getY1() - ARROWLENGTH); 410 g.fillPolygon(new int[] {(int)(y.getX1() - 3), 411 (int)(y.getX1() + 3), 412 (int)(y.getX1())}, 413 new int[] {(int)(y.getY1() - ARROWLENGTH / 3.0), 414 (int)(y.getY1() - ARROWLENGTH / 3.0), 415 (int)y.getY1() - ARROWLENGTH}, 416 3); 417 } 418 419 g.setColor(Color.black); 421 TextLayout layoutY = new TextLayout (getYAxisUnit(), getFont(), 422 new FontRenderContext (null, true, false)); 423 layoutY.draw(g, (float)y.getX1()-6-(float)layoutY.getBounds().getWidth(), 424 (float)y.getY1() - layoutX.getDescent() - 3); 425 426 if(getSecondYAxis() != null) { 427 Line2D y2 = c.getSecondYAxisLine2D(); 428 g.draw(y2); 429 } 430 431 if(model.isColumnNumeric()) 432 c.drawNumericalXAxisTicks(g); 433 else 434 c.drawXAxisTicks(g); 435 436 c.drawYAxisTicks(g); 437 } 438 439 448 public PointToPixelTranslator getPointToPixelTranslator(int yaxis) { 449 final Axis x = this.getXAxis(); 450 final Axis y; 451 if(yaxis == CoordSystem.FIRST_YAXIS) 452 y = this.getFirstYAxis(); 453 else 454 y = this.getSecondYAxis(); 455 456 return new PointToPixelTranslator() { 457 public Point2D getPixelCoord(Point2D pt) { 458 double x0 = 0.0; 459 double y0 = 0.0; 460 461 x0 = getBounds().getX()+getLeftMargin()+ 462 x.getPixelForValue(pt.getX()); 463 464 y0 = getBounds().getY()+getBounds().getHeight() - getBottomMargin() - 465 y.getPixelForValue(pt.getY()); 466 Point2D p = new Point2D.Double (x0, y0); 467 468 if(getInnerBounds().contains(p)) 469 return p; 470 else 471 return null; 472 } 473 }; 474 } 475 476 477 protected int getLeftMargin() { 478 return leftmargin; 479 } 480 481 482 protected int getRightMargin() { 483 return rightmargin; 484 } 485 486 487 protected int getTopMargin() { 488 return topmargin; 489 } 490 491 492 protected int getBottomMargin() { 493 return bottommargin; 494 } 495 496 499 protected void setLeftMargin(int margin) { 500 leftmargin = margin; 501 } 502 503 506 protected void setRightMargin(int margin) { 507 rightmargin = margin; 508 } 509 510 513 protected void setTopMargin(int margin) { 514 topmargin = margin; 515 } 516 517 520 public void setBottomMargin(int margin) { 521 bottommargin = margin; 522 } 523 524 525 public FontRenderContext getFontRenderContext() { 526 return frc; 527 } 528 529 530 public DecimalFormat getYDecimalFormat() { 531 return dfY; 532 } 533 534 535 public DecimalFormat getXDecimalFormat() { 536 return dfX; 537 } 538 539 540 public boolean isDrawArrows() { 541 return shouldDrawArrows; 542 } 543 544 545 public boolean isPaintAltTick() { 546 return shouldPaintAltTick; 547 } 548 549 551 public boolean isPaintOnlyTick() { 552 return shouldPaintOnlyTick; 553 } 554 555 public boolean isPaintLabels() { 556 return shouldPaintLabels; 557 } 558 559 public void setPaintLabels(boolean label) { 560 shouldPaintLabels = label; 561 } 562 563 564 public ChartDataModelConstraints getChartDataModelConstraints(int axis) { 565 if(axis == FIRST_YAXIS) 566 return constraints; 567 else if(axis == SECOND_YAXIS) 568 return constraints2; 569 else 570 return null; 571 } 572 } 573 | Popular Tags |