< prev index next >

application/org.openjdk.jmc.ui/src/main/java/org/openjdk/jmc/ui/charts/AWTChartToolkit.java

Print this page


   1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.

   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.


 276                         int barHeight = (int) points.getPixelY(i);
 277                         int x1 = (int) points.getPixelX(i);
 278                         int x2 = (int) points.getPixelX(i + 1);
 279                         int barWidth = x2 - x1;
 280                         if (barWidth > 10) {
 281                                 barWidth -= 4;
 282                                 x1 += 2;
 283                         }
 284                         // FIXME: Draw with gradient fill?
 285                         ctx.setPaint(cp == null ? paint : cp.getColor((payload == null) ? null : payload[i]));
 286                         ctx.fillRect(x1, 0, barWidth, barHeight);
 287                         ctx.setPaint(Color.GRAY);
 288                         ctx.drawRect(x1, 0, barWidth, barHeight);
 289                 }
 290                 ctx.setTransform(oldTransform);
 291         }
 292 
 293         public static void drawAxis(
 294                 Graphics2D ctx, SubdividedQuantityRange range, int axisPos, boolean labelAhead, int labelLimit,
 295                 boolean vertical) {






 296                 int axisSize = range.getPixelExtent();
 297                 FontMetrics fm = ctx.getFontMetrics();
 298                 int textAscent = fm.getAscent();
 299                 int textYadjust = textAscent / 2;
 300                 int labelYPos = labelAhead ? axisPos - TICK_SIZE : axisPos + TICK_SIZE + textAscent;
 301                 final int labelSpacing;
 302 
 303                 if (vertical) {
 304                         ctx.drawLine(axisPos, Y_AXIS_TOP_SPACE, axisPos, axisSize - 1);
 305                         drawUpArrow(ctx, axisPos, Y_AXIS_TOP_SPACE, Math.min(ARROW_SIZE, axisSize - 2));
 306                         labelSpacing = fm.getHeight() - textAscent;
 307                 } else {
 308                         ctx.drawLine(0, axisPos, axisSize - 1, axisPos);
 309                         labelSpacing = fm.charWidth(' ') * 2;
 310                 }
 311 
 312                 IRange<IQuantity> firstBucket = QuantityRange.createWithEnd(range.getSubdivider(0), range.getSubdivider(1));
 313                 IQuantity lastShownTick = null;
 314                 final IFormatter<IQuantity> formatter = range.getStart().getType().getFormatterResolving(firstBucket);
 315                 final IIncrementalFormatter changeFormatter;
 316                 if (formatter instanceof IIncrementalFormatter) {
 317                         changeFormatter = (IIncrementalFormatter) formatter;
 318                         if (!vertical && (labelLimit < 0)) {
 319                                 lastShownTick = range.getSubdivider(0);
 320                                 if (lastShownTick.compareTo(range.getStart()) < 0) {
 321                                         lastShownTick = range.getSubdivider(1);
 322                                 }
 323                                 String label = changeFormatter.formatContext(lastShownTick);
 324                                 int labelWidth = fm.stringWidth(label);
 325                                 ctx.drawString(label, labelLimit, labelYPos);
 326                                 labelLimit += labelWidth + labelSpacing;
 327                         }
 328                 } else {


 337                         } else if (tickPos >= 0) {
 338                                 IQuantity currentTick = range.getSubdivider(i);
 339                                 final String label;
 340                                 if (vertical) {
 341                                         ctx.drawLine(axisPos - TICK_LINE, axisSize - 1 - tickPos, axisPos + TICK_LINE,
 342                                                         axisSize - 1 - tickPos);
 343                                         if ((tickPos + textYadjust) >= axisSize) {
 344                                                 break;
 345                                         } else if ((tickPos - textYadjust) >= labelLimit) {
 346                                                 label = formatter.format(currentTick);
 347                                                 int labelXPos = labelAhead ? axisPos - TICK_SIZE - fm.stringWidth(label) : axisPos + TICK_SIZE;
 348                                                 ctx.drawString(label, labelXPos, axisSize - 1 - tickPos + textYadjust);
 349                                                 labelLimit = tickPos + textYadjust + labelSpacing;
 350                                         }
 351                                 } else {
 352                                         if (changeFormatter != null) {
 353                                                 label = changeFormatter.formatAdjacent(lastShownTick, range.getSubdivider(i));
 354                                         } else {
 355                                                 label = formatter.format(currentTick);
 356                                         }
 357                                         ctx.drawLine(tickPos, axisPos - TICK_LINE, tickPos, axisPos + TICK_LINE);
 358                                         int textXadjust = fm.stringWidth(label) / 2;
 359                                         // FIXME: Decide if truncated labels should be shown
 360 //                                      if ((tickPos + textXadjust) >= axisSize) {
 361                                         if (tickPos >= axisSize) {
 362                                                 break;
 363                                         } else if ((tickPos - textXadjust) >= labelLimit) {
 364                                                 ctx.drawString(label, tickPos - textXadjust, labelYPos);
 365                                                 labelLimit = tickPos + textXadjust + labelSpacing;
 366                                                 lastShownTick = currentTick;
 367                                         }
 368                                 }
 369                         }
 370                 }
 371         }
 372 
 373         private static void drawUpArrow(Graphics2D ctx, int axisX, int axisYTop, int size) {
 374                 int yArrow = axisYTop + size;
 375                 ctx.drawLine(axisX - size, yArrow, axisX, axisYTop);
 376                 ctx.drawLine(axisX + size, yArrow, axisX, axisYTop);
 377         }
 378 
 379         public static void drawGrid(Graphics2D ctx, SubdividedQuantityRange range, int gridSize, boolean verticalAxis) {
 380                 int axisSize = range.getPixelExtent();
 381                 Stroke oldStroke = ctx.getStroke();
 382                 ctx.setStroke(DASH_STROKE);
 383                 int numTicks = range.getNumSubdividers();
 384                 for (int i = 0; i < numTicks; i++) {


   1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Red Hat Inc. All rights reserved.
   4  *
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * The contents of this file are subject to the terms of either the Universal Permissive License
   8  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   9  *
  10  * or the following license:
  11  *
  12  * Redistribution and use in source and binary forms, with or without modification, are permitted
  13  * provided that the following conditions are met:
  14  *
  15  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  16  * and the following disclaimer.
  17  *
  18  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  19  * conditions and the following disclaimer in the documentation and/or other materials provided with
  20  * the distribution.
  21  *
  22  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  23  * endorse or promote products derived from this software without specific prior written permission.


 277                         int barHeight = (int) points.getPixelY(i);
 278                         int x1 = (int) points.getPixelX(i);
 279                         int x2 = (int) points.getPixelX(i + 1);
 280                         int barWidth = x2 - x1;
 281                         if (barWidth > 10) {
 282                                 barWidth -= 4;
 283                                 x1 += 2;
 284                         }
 285                         // FIXME: Draw with gradient fill?
 286                         ctx.setPaint(cp == null ? paint : cp.getColor((payload == null) ? null : payload[i]));
 287                         ctx.fillRect(x1, 0, barWidth, barHeight);
 288                         ctx.setPaint(Color.GRAY);
 289                         ctx.drawRect(x1, 0, barWidth, barHeight);
 290                 }
 291                 ctx.setTransform(oldTransform);
 292         }
 293 
 294         public static void drawAxis(
 295                         Graphics2D ctx, SubdividedQuantityRange range, int axisPos, boolean labelAhead, int labelLimit,
 296                         boolean vertical) {
 297                 drawAxis(ctx, range, axisPos, labelAhead, labelLimit, vertical, 0);
 298         }
 299 
 300         public static void drawAxis(
 301                 Graphics2D ctx, SubdividedQuantityRange range, int axisPos, boolean labelAhead, int labelLimit,
 302                 boolean vertical, int xOffset) {
 303                 int axisSize = range.getPixelExtent();
 304                 FontMetrics fm = ctx.getFontMetrics();
 305                 int textAscent = fm.getAscent();
 306                 int textYadjust = textAscent / 2;
 307                 int labelYPos = labelAhead ? axisPos - TICK_SIZE : axisPos + TICK_SIZE + textAscent;
 308                 final int labelSpacing;
 309 
 310                 if (vertical) {
 311                         ctx.drawLine(axisPos, Y_AXIS_TOP_SPACE, axisPos, axisSize - 1);
 312                         drawUpArrow(ctx, axisPos, Y_AXIS_TOP_SPACE, Math.min(ARROW_SIZE, axisSize - 2));
 313                         labelSpacing = fm.getHeight() - textAscent;
 314                 } else {
 315                         ctx.drawLine(0 + xOffset, axisPos, axisSize + xOffset, axisPos);
 316                         labelSpacing = fm.charWidth(' ') * 2;
 317                 }
 318 
 319                 IRange<IQuantity> firstBucket = QuantityRange.createWithEnd(range.getSubdivider(0), range.getSubdivider(1));
 320                 IQuantity lastShownTick = null;
 321                 final IFormatter<IQuantity> formatter = range.getStart().getType().getFormatterResolving(firstBucket);
 322                 final IIncrementalFormatter changeFormatter;
 323                 if (formatter instanceof IIncrementalFormatter) {
 324                         changeFormatter = (IIncrementalFormatter) formatter;
 325                         if (!vertical && (labelLimit < 0)) {
 326                                 lastShownTick = range.getSubdivider(0);
 327                                 if (lastShownTick.compareTo(range.getStart()) < 0) {
 328                                         lastShownTick = range.getSubdivider(1);
 329                                 }
 330                                 String label = changeFormatter.formatContext(lastShownTick);
 331                                 int labelWidth = fm.stringWidth(label);
 332                                 ctx.drawString(label, labelLimit, labelYPos);
 333                                 labelLimit += labelWidth + labelSpacing;
 334                         }
 335                 } else {


 344                         } else if (tickPos >= 0) {
 345                                 IQuantity currentTick = range.getSubdivider(i);
 346                                 final String label;
 347                                 if (vertical) {
 348                                         ctx.drawLine(axisPos - TICK_LINE, axisSize - 1 - tickPos, axisPos + TICK_LINE,
 349                                                         axisSize - 1 - tickPos);
 350                                         if ((tickPos + textYadjust) >= axisSize) {
 351                                                 break;
 352                                         } else if ((tickPos - textYadjust) >= labelLimit) {
 353                                                 label = formatter.format(currentTick);
 354                                                 int labelXPos = labelAhead ? axisPos - TICK_SIZE - fm.stringWidth(label) : axisPos + TICK_SIZE;
 355                                                 ctx.drawString(label, labelXPos, axisSize - 1 - tickPos + textYadjust);
 356                                                 labelLimit = tickPos + textYadjust + labelSpacing;
 357                                         }
 358                                 } else {
 359                                         if (changeFormatter != null) {
 360                                                 label = changeFormatter.formatAdjacent(lastShownTick, range.getSubdivider(i));
 361                                         } else {
 362                                                 label = formatter.format(currentTick);
 363                                         }
 364                                         ctx.drawLine(tickPos + xOffset, axisPos - TICK_LINE, tickPos + xOffset, axisPos + TICK_LINE);
 365                                         int textXadjust = fm.stringWidth(label) / 2;
 366                                         // FIXME: Decide if truncated labels should be shown
 367 //                                      if ((tickPos + textXadjust) >= axisSize) {
 368                                         if (tickPos >= axisSize) {
 369                                                 break;
 370                                         } else if ((tickPos - textXadjust) >= labelLimit) {
 371                                                 ctx.drawString(label, tickPos - textXadjust + xOffset, labelYPos);
 372                                                 labelLimit = tickPos + textXadjust + labelSpacing;
 373                                                 lastShownTick = currentTick;
 374                                         }
 375                                 }
 376                         }
 377                 }
 378         }
 379 
 380         private static void drawUpArrow(Graphics2D ctx, int axisX, int axisYTop, int size) {
 381                 int yArrow = axisYTop + size;
 382                 ctx.drawLine(axisX - size, yArrow, axisX, axisYTop);
 383                 ctx.drawLine(axisX + size, yArrow, axisX, axisYTop);
 384         }
 385 
 386         public static void drawGrid(Graphics2D ctx, SubdividedQuantityRange range, int gridSize, boolean verticalAxis) {
 387                 int axisSize = range.getPixelExtent();
 388                 Stroke oldStroke = ctx.getStroke();
 389                 ctx.setStroke(DASH_STROKE);
 390                 int numTicks = range.getNumSubdividers();
 391                 for (int i = 0; i < numTicks; i++) {


< prev index next >