Crosshair lock doesn't work properly with multiple axes

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
SystemsEngineer
Posts: 16
Joined: Thu Jan 19, 2017 5:54 pm
antibot: No, of course not.

Crosshair lock doesn't work properly with multiple axes

Post by SystemsEngineer » Fri Jan 27, 2017 11:42 am

Hello,

I'm using JFreeChart 1.0.19 and I want to lock the crosshair on data points even when I have multiple vertical axes. However, when I click on a series bound to the secondary axis the crosshair points to the corresponding value of the primary axis which is annoying! Do you have any idea how this could be fixed?

Thanks a lot.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Crosshair lock doesn't work properly with multiple axes

Post by paradoxoff » Fri Jan 27, 2017 1:13 pm

Can you show a short and runnable piece of code that is showing the issue?

SystemsEngineer
Posts: 16
Joined: Thu Jan 19, 2017 5:54 pm
antibot: No, of course not.

Re: Crosshair lock doesn't work properly with multiple axes

Post by SystemsEngineer » Wed Apr 05, 2017 3:17 pm

Hello,

First, I apologise for the (very) late response. You can see below a small sample runnable piece of code which shows the issue.

If you try to click on the points of the blue line (which is related to the 2nd axis), for example on (1994, 25), you can see that the vertical axis crosshair points at the correct value ('25') but on the 1st axis and not on the 2nd which the blue line is bound with.

Code: Select all

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * -------------------
 * LineChartDemo5.java
 * -------------------
 * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: LineChartDemo5.java,v 1.20 2004/05/10 16:45:23 mungady Exp $
 *
 * Changes
 * -------
 * 23-Apr-2003 : Version 1 (DG);
 *
 */

package sample;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;

/**
 * A line chart demo showing the use of a custom drawing supplier.
 *
 */
public class dashedLineTest extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public dashedLineTest(final String title) {
        super(title);
        final XYDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }

    /**
     * Creates a sample dataset.
     *
     * @return a sample dataset.
     */
    private XYDataset createDataset( )
    {
        final XYSeries firefox = new XYSeries( "Firefox" );
        firefox.add( 1.0 , 1.0 );
        firefox.add( 2.0 , 4.0 );
        firefox.add( 3.0 , 3.0 );
        final XYSeries chrome = new XYSeries( "Chrome" );
        chrome.add( 1.0 , 4.0 );
        chrome.add( 2.0 , 5.0 );
        chrome.add( 3.0 , 6.0 );
        final XYSeries iexplorer = new XYSeries( "InternetExplorer" );
        iexplorer.add( 3.0 , 4.0 );
        iexplorer.add( 4.0 , 5.0 );
        iexplorer.add( 5.0 , 4.0 );
        final XYSeriesCollection dataset = new XYSeriesCollection( );
        dataset.addSeries( firefox );
        dataset.addSeries( chrome );
        dataset.addSeries( iexplorer );
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return a chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {

        final JFreeChart chart = ChartFactory.createXYLineChart(
                "Line Chart Demo 5",      // chart title
                "Type",                   // domain axis label
                "Value",                  // range axis label
                dataset,                  // data
                PlotOrientation.VERTICAL, // orientation
                true,                     // include legend
                true,                     // tooltips
                false                     // urls
        );


        final XYPlot plot = chart.getXYPlot();
        //plot.setDrawingSupplier(supplier);
        org.jfree.chart.axis.NumberAxis primaryYaxis = new org.jfree.chart.axis.NumberAxis("1st axis");
        org.jfree.chart.axis.NumberAxis secondaryYaxis = new org.jfree.chart.axis.NumberAxis("2nd axis");

        /* Define some XY Data series for the chart */
        XYSeries team1_xy_data = new XYSeries("Team 1");
        team1_xy_data.add(1990, 45);
        team1_xy_data.add(1991, 16);
        team1_xy_data.add(1992, 80);
        team1_xy_data.add(1993, 1);
        team1_xy_data.add(1994, 6);

        XYSeries team2_xy_data = new XYSeries("Team 2");
        team2_xy_data.add(1990, 2);
        team2_xy_data.add(1991, 10);
        team2_xy_data.add(1992, 60);
        team2_xy_data.add(1993, 60);
        team2_xy_data.add(1994, 18);

        XYSeries team3_xy_data = new XYSeries("Team 3");
        team3_xy_data.add(1990, 15);
        team3_xy_data.add(1991, 5);
        team3_xy_data.add(1992, 14);
        team3_xy_data.add(1993, 18);
        team3_xy_data.add(1994, 25);

                /* Add all XYSeries to XYSeriesCollection */
        //XYSeriesCollection implements XYDataset
        XYSeriesCollection my_data_series= new XYSeriesCollection();
        XYSeriesCollection my_data_series2= new XYSeriesCollection();
        // add series using addSeries method
        my_data_series.addSeries(team1_xy_data);
        my_data_series.addSeries(team2_xy_data);
        my_data_series2.addSeries(team3_xy_data);

        plotParameter(plot, 0, primaryYaxis, my_data_series);
        plotParameter(plot, 1, secondaryYaxis, my_data_series2);
        chart.setBackgroundPaint(Color.yellow);

        plot.setDomainCrosshairVisible(true);
        plot.setDomainCrosshairLockedOnData(true);
        plot.setRangeCrosshairVisible(true);
        plot.setRangeCrosshairLockedOnData(true);

        XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
        renderer1.setSeriesPaint(0, java.awt.Color.RED);
        XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
        renderer1.setSeriesPaint(0, java.awt.Color.GREEN);

        plot.setRenderer(0, renderer1);
        plot.setRenderer(1, renderer2);

        return chart;

    }

    private void plotParameter(XYPlot plot, int axesNumber, ValueAxis yAxis, XYDataset dataset)
    {
        plot.setRangeAxis(axesNumber, yAxis);
        plot.setDataset(axesNumber, dataset);
        plot.mapDatasetToRangeAxis(axesNumber, axesNumber);
        if (axesNumber%2==0)
        {
            plot.setRangeAxisLocation(axesNumber, AxisLocation.BOTTOM_OR_LEFT);
        }
        else
        {
            plot.setRangeAxisLocation(axesNumber, AxisLocation.BOTTOM_OR_RIGHT);
        }
    }

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    *
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final dashedLineTest demo = new dashedLineTest("Line Chart Demo 5");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}
Is there something wrong with my crosshair implementation or is it a bug?

Thanks a lot!

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

Re: Crosshair lock doesn't work properly with multiple axes

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

Let me check. I know I changed some code in this area for the next release...I think it might be fixed.
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: Crosshair lock doesn't work properly with multiple axes

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

For me, the problem is fixed when running with the latest code from GitHub. So I need to get on with making a new release...
David Gilbert
JFreeChart Project Leader

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

SystemsEngineer
Posts: 16
Joined: Thu Jan 19, 2017 5:54 pm
antibot: No, of course not.

Re: Crosshair lock doesn't work properly with multiple axes

Post by SystemsEngineer » Thu Apr 06, 2017 9:33 am

That's good news David thanks a lot.
I guess I shall wait until the next release then!

Locked