different itemLabelPositions for each item

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
fm64
Posts: 5
Joined: Thu Feb 25, 2016 3:43 pm
antibot: No, of course not.

different itemLabelPositions for each item

Post by fm64 » Thu Mar 15, 2018 9:50 pm

Hi, I have a problem with ItemLabelPosition in a XYplot. I've been asked to set the ItemlabelPosition por each item separately, that is, for example that one item may have its ItemLabelAnchor.OUTSIDE12 and other would have ItemLabelAnchor.OUTSIDE7. I understand that this can be set for the renderer of the whole series only, but is it a way to set it for each item of the renderer?

The problem originates from this situation:

Image

when I have many points, two itemlabels will overlap and you won't be able to read it clearly, so the client wants it to be possible to put the Itemlabel in another position for that item only (I already suggested to show/hide the Itemlabels in this case, to no avail). Is this technically possible with the JfreeChart library?

Thank you.

fm64
Posts: 5
Joined: Thu Feb 25, 2016 3:43 pm
antibot: No, of course not.

Re: different itemLabelPositions for each item

Post by fm64 » Mon Mar 19, 2018 2:08 pm

Looks Like I found a solution, in case anyone needs it:

you can't have N TextAnchor for the renderer for you can set 1 only, but overwriting the Renderer you can set N ItemLabelPosition to the renderer, I used a Map which had the Comparable of the chart and the TextAnchor which is TextAnchor.CENTER by default but the user can pick any of the 9 positions.
This way you can set a different TextAnchor for any point, I made a small dialog where you can pick the position by double-clicking the point:

Image

here's the code from the overwritten method:

Code: Select all

//JackKnifeComparable extends from Comparable
private Map<JackKnifeComparable, TextAnchor> anchorMap = new HashMap<>();
private XYSeriesCollection dataset;

//...

        renderer = new XYLineAndShapeRenderer(false, true) {

            @Override
            public ItemLabelPosition getSeriesPositiveItemLabelPosition(int series) {
                TextAnchor txt = anchorMap.get(dataset.getSeriesKey(series)) != null ? anchorMap.get(dataset.getSeriesKey(series)) : TextAnchor.BASELINE_CENTER;
                return new ItemLabelPosition(ItemLabelAnchor.CENTER, txt);
            }
Hope it helps someone.

Locked