Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labels with LayeredBarRenderer #169

Closed
trashgod opened this issue Aug 23, 2020 · 3 comments
Closed

Labels with LayeredBarRenderer #169

trashgod opened this issue Aug 23, 2020 · 3 comments

Comments

@trashgod
Copy link
Contributor

trashgod commented Aug 23, 2020

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);
        });
    }
}
@jfree
Copy link
Owner

jfree commented Sep 26, 2020

Thanks for the great example and for pointing out the fix. I could also simplify the code a little with this change, since there is no need to calculate the Java2D translations of the values before comparing them to see if the bar represents a negative value:

--- a/src/main/java/org/jfree/chart/renderer/category/LayeredBarRenderer.java
+++ b/src/main/java/org/jfree/chart/renderer/category/LayeredBarRenderer.java
@@ -426,10 +426,8 @@
         CategoryItemLabelGenerator generator = getItemLabelGenerator(row, 
                 column);
         if (generator != null && isItemLabelVisible(row, column)) {
-            double transX1 = rangeAxis.valueToJava2D(base, dataArea, edge);
-            double transX2 = rangeAxis.valueToJava2D(value, dataArea, edge);
             drawItemLabel(g2, dataset, row, column, plot, generator, bar,
-                    (transX1 > transX2));
+                    value < base);
         }

         // collect entity and tool tip information...

@jfree
Copy link
Owner

jfree commented Sep 26, 2020

I committed the fix. I also noticed that this renderer doesn't respect the getBase()/setBase() methods inherited from the BarRenderer, but I'll open a separate ticket for that.

@jfree
Copy link
Owner

jfree commented Sep 26, 2020

Closing - fixed for the next release.

@jfree jfree closed this as completed Sep 26, 2020
jfree added a commit that referenced this issue Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants