JFreeChart: tooltip stop working on rotated plot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
montardon
Posts: 1
Joined: Tue Mar 19, 2013 4:20 pm
antibot: No, of course not.

JFreeChart: tooltip stop working on rotated plot

Post by montardon » Wed Apr 05, 2017 1:11 pm

I have a basic XYPlot with one serie. When the plot orientation is vertical, the tooltip is working fine. When the plot orientation is horizontal, the tooltip does not appear at all, or sometimes by error with wrong values.

Code: Select all

public class HorizontalPlotTooltip {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    XYPlot plot = new XYPlot();
    plot.setOrientation(PlotOrientation.HORIZONTAL);
    NumberAxis xAxis = new NumberAxis("x-axis value");
    xAxis.setAutoRange(true);
    xAxis.setAutoRangeIncludesZero(false);
    xAxis.setInverted(true);
    plot.setDomainAxis(xAxis);
    NumberAxis yAxis = new NumberAxis("y-axis value");
    yAxis.setAutoRange(true);
    yAxis.setAutoRangeIncludesZero(false);
    plot.setRangeAxis(yAxis);
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,false);
    StandardXYToolTipGenerator generator = new StandardXYToolTipGenerator(StandardXYZToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,new DecimalFormat("#"),new DecimalFormat("0.00"));
    renderer.setBaseToolTipGenerator(generator);
    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries series = new XYSeries("value");

    for (int i=0; i < 2000; i++) {
        series.add(i,Math.sin(i/500.0));
    }
    dataset.addSeries(series);
    plot.setDataset(dataset);
    plot.setRenderer(renderer);
    JFreeChart chart = new JFreeChart("Inverted - Horizontal plot problem",plot);
    ChartPanel chartPanel = new ChartPanel(chart);
    // long dismiss delay to observe tooltip
    chartPanel.setDismissDelay(100000);
    frame.setPreferredSize(new Dimension(200,700));
    frame.setMinimumSize(new Dimension(200,700));
    frame.setLayout(new BorderLayout());
    frame.add(chartPanel);
    frame.setVisible(true);
}
Is there extra code to add to have tooltip with correct values when the plot is horizontal ?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: JFreeChart: tooltip stop working on rotated plot

Post by david.gilbert » Wed Apr 05, 2017 9:40 pm

Posted here also:

http://stackoverflow.com/questions/4322 ... tated-plot

I'm looking at it.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: JFreeChart: tooltip stop working on rotated plot

Post by david.gilbert » Wed Apr 05, 2017 10:13 pm

I see the problem. In XYLineAndShapeRenderer, the following code swaps xx and yy depending on the orientation:

Code: Select all

        double xx = transX1;
        double yy = transY1;
        if (orientation == PlotOrientation.HORIZONTAL) {
            xx = transY1;
            yy = transX1;
        }
...but in AbstractXYItemRenderer's addEntity method, the values are treated as if they are not already adjusted for the orientation:

Code: Select all

            if (getPlot().getOrientation() == PlotOrientation.VERTICAL) {
                hotspot = new Ellipse2D.Double(entityX - r, entityY - r, w, w);
            }
            else {
                hotspot = new Ellipse2D.Double(entityY - r, entityX - r, w, w);
            }
I could change the latter method, but first need to check which other renderers are affected.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

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

Re: JFreeChart: tooltip stop working on rotated plot

Post by John Matthews » Wed Apr 05, 2017 11:16 pm

I see it, too. The ChartMouseListener in the example below makes it a little easier to see the effect.

Code: Select all

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see http://www.jfree.org/forum/viewtopic.php?f=3&t=117805
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series = new XYSeries("value");
        for (double t = 0; t < 2 * Math.PI; t += 0.1) {
            series.add(t, Math.sin(t));
        }
        dataset.addSeries(series);
        NumberAxis xAxis = new NumberAxis("domain");
        NumberAxis yAxis = new NumberAxis("range");
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
        renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
        plot.setOrientation(PlotOrientation.HORIZONTAL);
        JFreeChart chart = new JFreeChart("Test", plot);
        ChartPanel chartPanel = new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        chartPanel.addChartMouseListener(new ChartMouseListener() {
            @Override
            public void chartMouseClicked(ChartMouseEvent e) {
            }

            @Override
            public void chartMouseMoved(ChartMouseEvent e) {
                ChartEntity ce = e.getEntity();
                if (ce instanceof XYItemEntity) {
                    System.out.println(ce.getToolTipText() + " " + ce.getShapeCoords());
                }
            }
        });

        f.add(chartPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: JFreeChart: tooltip stop working on rotated plot

Post by david.gilbert » Thu Apr 06, 2017 7:16 am

David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: JFreeChart: tooltip stop working on rotated plot

Post by david.gilbert » Thu Apr 06, 2017 10:02 pm

I committed a fix.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked