Skip to content

Labels with LayeredBarRenderer #169

@trashgod

Description

@trashgod

As shown here using JFreeChart v1.5, item labels remain invisible when added to a VERTICAL plot using LayeredBarRenderer; a HORIZONTAL plot appears unaffected. As shown here, drawItemLabel() is invoked with an incorrect actual value for the formal parameter negative. Instead of transX1 > transX2, substituting transX1 < transX2 results in the expected appearance. The following complete example reproduces the effect.

import java.awt.Dimension;
import java.awt.EventQueue;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LayeredBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtils;
import org.jfree.chart.ui.ApplicationFrame;

/** @see https://stackoverflow.com/a/63464855/230513 */
public final class LayeredBarChartDemo extends ApplicationFrame {

    private static final String TITLE = "Layered Bar Chart Demo";

    public LayeredBarChartDemo(final String title) {
        super(title);
        final double[][] data = new double[][]{{55, 60}, {25, 13}};
        final CategoryDataset dataset = DatasetUtils.createCategoryDataset("Series ", "Factor ", data);
        final CategoryAxis categoryAxis = new CategoryAxis("Category");
        final ValueAxis valueAxis = new NumberAxis("Score (%)");
        final LayeredBarRenderer renderer = new LayeredBarRenderer();
        renderer.setDefaultToolTipGenerator(new StandardCategoryToolTipGenerator());
        renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setDefaultItemLabelsVisible(true);
        final CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
        final JFreeChart chart = new JFreeChart(TITLE, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        ChartUtils.applyCurrentTheme(chart);
        add(new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        });
    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(() -> {
            final LayeredBarChartDemo demo = new LayeredBarChartDemo2(TITLE);
            demo.pack();
            demo.setLocationRelativeTo(null);
            demo.setVisible(true);
        });
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions