boolean willLabelsFitInTickSpaceHint ( List < String > tickLabels , int tickSpacingHint ) { // Assume that for Y-Axis the ticks will all fit based on their tickSpace hint because the text // is usually horizontal and "short". This more applies to the X-Axis. if ( this . axisDirection == Direction . Y ) { return true ; } String sampleLabel = " " ; // find the longest String in all the labels for ( String tickLabel : tickLabels ) { if ( tickLabel != null && tickLabel . length ( ) > sampleLabel . length ( ) ) { sampleLabel = tickLabel ; } } // System.out.println("longestLabel: " + sampleLabel); TextLayout textLayout = new TextLayout ( sampleLabel , styler . getAxisTickLabelsFont ( ) , new FontRenderContext ( null , true , false ) ) ; AffineTransform rot = styler . getXAxisLabelRotation ( ) == 0 ? null : AffineTransform . getRotateInstance ( - 1 * Math . toRadians ( styler . getXAxisLabelRotation ( ) ) ) ; Shape shape = textLayout . getOutline ( rot ) ; Rectangle2D rectangle = shape . getBounds ( ) ; double largestLabelWidth = rectangle . getWidth ( ) ; // System.out.println("largestLabelWidth: " + largestLabelWidth); // System.out.println("tickSpacingHint: " + tickSpacingHint); // if (largestLabelWidth * 1.1 >= tickSpacingHint) { // System.out.println("WILL NOT FIT!!!"); // } return ( largestLabelWidth * 1.1 < tickSpacingHint ) ; }
Given the generated tickLabels will they fit side - by - side without overlapping each other and looking bad? Sometimes the given tickSpacingHint is simply too small .