Label XY points in a single series differently

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
vscalfari
Posts: 6
Joined: Mon Apr 04, 2016 6:43 pm
antibot: No, of course not.

Label XY points in a single series differently

Post by vscalfari » Mon Apr 04, 2016 6:49 pm

I have an XY plot with 4 different series in my dataset. I'm looking to label each point of the 4 series differently. For example, if series 0 contains 4 points, they will be labeled 1, 2, 3, 4 on my plot. I tried searching for how to do this but couldn't find much. I found solutions for if I wanted a custom label that is the same on each point, but nothing for different labels for each point in the same series. I want the label to be the item index in the series, so the first point added to the series would be labeled 1, the 2nd item to be added to the series would be labeled 2, etc.

Does anyone know how I can achieve this?

Thanks

vscalfari
Posts: 6
Joined: Mon Apr 04, 2016 6:43 pm
antibot: No, of course not.

Re: Label XY points in a single series differently

Post by vscalfari » Mon Apr 04, 2016 7:32 pm

Was able to do this by overriding the generateLabel method. I used this post as inspiration:
http://stackoverflow.com/questions/7904 ... jfreechart

My generateLabel method looks like this now:

Code: Select all

    @Override
    public String generateLabel(XYDataset dataset, int series, int item) {
        return (item+1)+"";
    }
The (item+1) is so the label numbering starts at 1 instead of 0.

vscalfari
Posts: 6
Joined: Mon Apr 04, 2016 6:43 pm
antibot: No, of course not.

Re: Label XY points in a single series differently

Post by vscalfari » Tue May 10, 2016 8:01 pm

Another problem I was having related to labeling my points (and I thought was a bug with the label generator code, but isn't) was that the data items in a series are sorted by default unless you use a different constructor call.

Previously, my series was created using the line:
XYSeries seriesBA = new XYSeries("Benefit/Action");

But any points added to this series were being sorted automatically and throwing off my numbering/label scheme. Now, my series are created using the line:

XYSeries seriesBA = new XYSeries("Benefit/Action", false);

The false parameter sets the auto sorting to OFF.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Label XY points in a single series differently

Post by John Matthews » Thu May 12, 2016 10:33 am

In the alternative approach shown here, label text is stored in a subclass of AbstractXYDataset for access by the label generator.

Locked