Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7924911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:01:03+00:00 2026-06-03T18:01:03+00:00

As you can see, when the range axis is spanning over a large area,

  • 0

Overlapping labels

As you can see, when the range axis is spanning over a large area, and the ticklabels are too close together, they start overlapping. I would like to implement some kind of logic to detect if they are overlapping, and if so, just show a preferred one (or as many as will fit). I am a bit unsure of how to accomplish this, however.

The logic would be something like this:

  1. Prioritize each label after importance
  2. Get hold of the Java2d coordinates of each tick label (x,y,width,height)
  3. If the area of a label is overlapping with another, just show the most important one

But which methods are available to do this? From the API docs I bought I found this method

public double valueToJava2D(double value, Rectangle2D area, RectangleEdge edge);

But what is this area and edge I need to supply the function with? And how would I get a hold of the width and height if a ticklabel? And is this the right way to go about it, or is there a better way that will lead to success?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:01:13+00:00Added an answer on June 3, 2026 at 6:01 pm

    I posted the question on the JFree forums (as trashgod found) and got a few nice hints there. The final code I posted there that solved my problem was as follows:

    @SuppressWarnings({"RawUseOfParameterizedType", "serial"})
    private static class CustomTickMarkNumberAxis extends NumberAxis {
        private double[] customTicksSortedOnImportance;
    
        /**
         * A list ticks sorted after importance. In case of overlapping,
         * a higher ranked tick will trump a lower ranked tick, thus <i>not showing</i>
         * the lower ranked (overlapping) tick.
         * Other lower ranked ticks might still be shown, if they are not overlapping
         */
        public CustomTickMarkNumberAxis(double[] customTicksSortedOnImportance) {
            this.customTicksSortedOnImportance = customTicksSortedOnImportance;
        }
    
        protected List<NumberTick> refreshTicksVertical(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {
            List<NumberTick> result = new java.util.ArrayList<NumberTick>();
    
            //(... snip ...)
    
            Area tickLabelArea = new Area(); /** Used for overlap detection */
            for (int i = 0; i < customTicksSortedOnImportance.length; i++) {
                // The next lines of code are different from NumberAxis
                double currentTickValue = customTicksSortedOnImportance[i];
                currentTickValue = NumberUtil.round(currentTickValue, precision);
    
                //(... snip ...)
    
                /* Overlapping logic here */
                NumberTick numberTick = new NumberTick(currentTickValue, tickLabel, anchor, rotationAnchor, angle);
                Rectangle2D labelBounds = getTickBounds(numberTick, g2);
                double java2dValue = valueToJava2D(currentTickValue, g2.getClipBounds(), edge);
                labelBounds.setRect(labelBounds.getX(), java2dValue, labelBounds.getWidth(), labelBounds.getHeight());
    
                if (!tickLabelIsOverlapping(tickLabelArea, labelBounds)) {
                    result.add(numberTick);
                    tickLabelArea.add(new Area(labelBounds));
                }
    
                //(... snip ...)
            }
            return result;
        }
    
        private boolean tickLabelIsOverlapping(Area area, Rectangle2D rectangle) {
            return area.intersects(rectangle);
        }
    
        private Rectangle2D getTickBounds(NumberTick numberTick, Graphics2D g2) {
            FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
            return TextUtilities.getTextBounds(numberTick.getText(), g2, fm);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got database listed below. As you can see, there is a range of
I can see it looks like an alias for an unsigned int pointer, right?
I can see where svn:keywords like Id, LastChangedDate and Author are used and replaced
I can see how to change the fonts and colors in the IDE. Is
I can see that GD library is for images. But I can't see differences
I can see some potential difficulties with this concept but the idea is: I
I can see there are a lot of questions for getting the number of
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
I can see how you can use the Acl Extension to control the push/pull
I can see that the PopUp dropdown of a combobox gets closed if we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.