开发者

How to rotate group labels when using a GroupedStackedBarRenderer in JFreeChart?

开发者 https://www.devze.com 2023-03-31 12:31 出处:网络
I have a followed the demo from JFreeChart (StackedBarChartDemo5) in order to create a plot allowing me to use the GroupedStackedBarRenderer.

I have a followed the demo from JFreeChart (StackedBarChartDemo5) in order to create a plot allowing me to use the GroupedStackedBarRenderer.

Basically, I'm plotting air times for each station (represents a stack in a bar), connected to a radio (represents a bar in the plot), for both receive and transmit (RX and TX).

The plot looks something like this :

How to rotate group labels when using a GroupedStackedBarRenderer in JFreeChart?

There are two categories (RX and TX) and several groups (radios). As you can see, currently, the radio MACs are unreadable. I would like to rotate these labels, in order to make the plot legible.

I have used the following piece of code in order to try and rotate the labels: subCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4));

This only rotated the labels of the categories (RX and TX) instead of the labels of the groups (radio MACs):

How to rotate group labels when using a GroupedStackedBarRenderer in JFreeChart?

I have also tried several other approaches, all of which lead to nowhere... How do I rotate these sublabels?

Here is the underlying code that I used to populate the labels with :

SubCategoryAxis subCategoryAxis = new SubCategoryAxis("Radio MACs (separate Rx and Tx plots)");
subCategoryAxis.setCategoryMargin(0.05D);  

    // subCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4));

    KeyToGroupMap keyToGroupMap = null;
    final Set<String> radios = Sets.newHashSet();
    for (Entry<XRadio, Triplet<XStation, Double, Double>> entryMap: channelTimes.entries()) {
        final String radioMac = entryMap.getKey().getMac();
        if (keyToGroupMap == null)
            keyToGroupMap = new KeyToGroupMap(radioMac);

        if (!radios.contains(radioMac)) {
            subCategoryAxis.addSubCategory(radioMac);
            radios.add(radioMac);
        }

        final Triplet<XStation, Double, Double> chTriplet = entryMap.getValue();                           
        final String seriesKey = radioMac + ":" + chTriplet.a.getMac();
        keyToGroupMap.mapKeyToGroup(seriesKey, radioMac);                                
        model.getDataset().addValue(chTriplet.b, seriesKey, "Rx");
        model.getDataset().addValue(chTriplet.c, seriesKey, "Tx");
    }

    if (keyToGroupMap == null)
        return;

    groupedStackedBarRenderer.setSeriesToGroupMap(keyToGroupMap);                
    groupedStackedBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator())开发者_Go百科;      
    groupedStackedBarRenderer.setBaseItemLabelsVisible(true);                    
    groupedStackedBarRenderer.setItemMargin(0.1D); // 10 %
    groupedStackedBarRenderer.getPlot().setDomainAxis(subCategoryAxis);


You can set the rotation on the CategoryAxis in radians:

subCategoryAxis.setLabelAngle(-Math.PI / 2);

EDIT: It seems the SubCategoryAxis ignores rotation for sub-lables.

In the SubCategoryAxis#drawSubCategoryLabels method there is a call to TextUtilities.drawRotatedString() with angle=0.

Note: The examples are from 1.13.

The correct solution would be to make a patch that fixes the issue and submit said patch to the developers.

As for fixing it for your case, you could extend the SubCategoryAxis and call the TextUtilities.drawRotatedString() with the correct angle.

0

精彩评论

暂无评论...
验证码 换一张
取 消