2

My problem: Using , I need to display unevenly spaced percentage thresholds on the y-axis. For example, the only labels on my y-axis should be the thresholds as follows; all other ordinates should be blank:

-
93%
85%
78%
72%
66%
-
50%
-
-
-
-
-

I am currently using this code snippet for percentages display, but this will just create an evenly spaced percentage axis:

    CategoryPlot plot = (CategoryPlot) chart.getPlot();

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    DecimalFormat pctFormat = new DecimalFormat("#.0%");
    rangeAxis.setNumberFormatOverride(pctFormat);

Any help would be appreciated!

ptstone
  • 478
  • 7
  • 17
  • You still want 50% to be half and 80% to be four-fifths, etc.? – trashgod Apr 03 '17 at 09:16
  • Yes, the relative positioning should not change (no logarithmic scale), its just that only the important thresholds should have a label. – ptstone Apr 03 '17 at 09:22
  • Maybe try `SymbolAxis`, suggested [here](http://stackoverflow.com/q/2365657/230513). – trashgod Apr 03 '17 at 09:34
  • How would you use SymbolAxis to achieve this? If i understand it correctly, it just switches out the labels, but has no effect on the specific selection and spacing. To make sure, the problem is not about displaying percentages, but about selecting only specific percentage thresholds to be displayed. – ptstone Apr 03 '17 at 09:51
  • From your edit to my question, i see how you want to solve my problem using SymbolAxis by just adding empty strings to all evenly spaced labels that should not be displayed. Unfortunately, thats not the intent, the thresholds could just as well be 50%, 66%, 72%, 81%, 85% - i will change my post to reflect this. – ptstone Apr 03 '17 at 10:04

1 Answers1

2

This is a somewhat hacked-up answer, and i will upvote any answer that gets the same result in a good way. But it works - here is what i did.

First, i created a list of the double values i wanted to be used:

public static List<Double> m_lPercentageValuesForY = Arrays.asList(0.1,0.2,0.3,0.4,0.5,0.58,0.66,0.70,0.74,0.78,0.82,0.95,1.0);

Then i overwrite the refreshTicks method in NumberAxis to first set all default ticks to Ticktype.MINOR, and then add and create my custom ticks with Ticktype.MAJOR:

    NumberAxis axisLeft = new NumberAxis(plot.getRangeAxis().getLabel()) {
        @Override
        public List refreshTicks(
                Graphics2D g2, 
                AxisState state,
                Rectangle2D dataArea, 
                RectangleEdge edge) {

            List defaultTicks = super.refreshTicks(g2, state, dataArea, edge);
            List customTicks = new ArrayList();

            for (Object aTick : defaultTicks) {
                NumberTick currenttick = (NumberTick) aTick;

                customTicks.add(new NumberTick(
                        TickType.MINOR, 
                        currenttick.getValue(), 
                        "",  //empty
                        currenttick.getTextAnchor(), 
                        currenttick.getRotationAnchor(),
                        currenttick.getAngle()));
            }
            NumberTick aTick = (NumberTick) defaultTicks.get(0);
            for (double dTest : m_lPercentageValuesForY) {
                customTicks.add(new NumberTick(
                        TickType.MAJOR, 
                        dTest, 
                        String.format("%.0f%%", dTest * 100), //only wanted values are set to major
                        aTick.getTextAnchor(), 
                        aTick.getRotationAnchor(),
                        aTick.getAngle()));
            }
            return customTicks;
        }
    };
    plot.setRangeAxis(axisLeft);
ptstone
  • 478
  • 7
  • 17
  • 1
    A similar approach is suggested [here](http://stackoverflow.com/a/22610568/230513). – trashgod Apr 04 '17 at 17:33
  • Thank you for finding that, and its even from the creator of jfreechart himself! I was/am hoping there was an official way to create a fully customized axis, but this requirement was probably too rare or specific to justify a method or class. Since i just joined stackoverflow, is it ok to mark this thread answered even though the answer is by myself and not that great? – ptstone Apr 04 '17 at 18:07