/** Updates the visibility of tick labels. */ private void updateTickVisibility() { // initialize the array of tick label visibility state tickVisibilities.clear(); for (int i = 0; i < tickLabelPositions.size(); i++) { tickVisibilities.add(Boolean.TRUE); } if (tickLabelPositions.size() == 0 || axis.getTick().getTickLabelAngle() != 0) { return; } // set the tick label visibility int previousPosition = 0; for (int i = 0; i < tickLabelPositions.size(); i++) { // check if there is enough space to draw tick label boolean hasSpaceToDraw = true; if (i != 0) { hasSpaceToDraw = hasSpaceToDraw(previousPosition, tickLabelPositions.get(i), tickLabels.get(i)); } // check if the tick label value is major boolean isMajorTick = true; if (!axis.isValidCategoryAxis()) { if (axis.isLogScaleEnabled()) { isMajorTick = isMajorTick(tickLabelValues.get(i)); } // check if the same tick label is repeated String currentLabel = tickLabels.get(i); try { double value = Double.parseDouble(currentLabel); if (value != tickLabelValues.get(i)) { isMajorTick = false; } } catch (NumberFormatException e) { // label is not decimal value but string } } if (hasSpaceToDraw && isMajorTick) { previousPosition = tickLabelPositions.get(i); } else { tickVisibilities.set(i, Boolean.FALSE); } } }
/** * Updates the tick labels. * * @param length the axis length */ protected void update(int length) { tickLabelValues.clear(); tickLabels.clear(); tickLabelPositions.clear(); if (axis.isValidCategoryAxis()) { updateTickLabelForCategoryAxis(length); } else if (axis.isLogScaleEnabled()) { updateTickLabelForLogScale(length); } else if (axis.isDateEnabled()) { updateTickLabelForDateAxis(length); } else { updateTickLabelForLinearScale(length); } updateTickVisibility(); updateTickLabelMaxLength(); }
/** * Draw the X tick. * * @param gc the graphics context */ private void drawXTick(GC gc) { int offset = axis.getTick().getAxisTickMarks().getBounds().x; // draw tick labels gc.setFont(axis.getTick().getFont()); int angle = axis.getTick().getTickLabelAngle(); for (int i = 0; i < tickLabelPositions.size(); i++) { if (axis.isValidCategoryAxis() || tickVisibilities.get(i) == true) { String text = tickLabels.get(i); int textWidth = gc.textExtent(text).x; int textHeight = gc.textExtent(text).y; if (angle == 0) { int x = (int) (tickLabelPositions.get(i) - textWidth / 2d + offset); gc.drawText(text, bounds.x + x, bounds.y); continue; } float x, y; if (axis.getPosition() == Position.Primary) { x = (float) (offset + bounds.x + tickLabelPositions.get(i) - textWidth * Math.cos(Math.toRadians(angle)) - textHeight / 2d * Math.sin(Math.toRadians(angle))); y = (float) (bounds.y + textWidth * Math.sin(Math.toRadians(angle))); } else { x = (float) (offset + bounds.x + tickLabelPositions.get(i) - textHeight / 2d * Math.sin(Math.toRadians(angle))); y = (float) (bounds.y + tickLabelMaxLength * Math.sin(Math.toRadians(angle))); } drawRotatedText(gc, text, x, y, angle); } } }