Update on tracelines

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
bumblebee63
Posts: 2
Joined: Thu Jul 03, 2014 4:47 pm
antibot: No, of course not.

Update on tracelines

Post by bumblebee63 » Thu Jul 03, 2014 4:54 pm

I have come across a number of previous threads from 2009 and earlier on the issue with tracelines and tooltips but no updates (tracelines were not repainting properly when tooltips were shown). I then saw a post from David which said that in 1.0.13 that they were to be removed and a replacement created with JXPanel(?) implementation but that does not seem to have happened. I am using version 1.0.17 and still have the issue of tracelines not repainting properly when tooltips are being shown. Could someone give an update on whether this is still an expected outstanding bug and if there are any work arounds for it?

Thanks

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

Re: Update on tracelines

Post by david.gilbert » Thu Jul 03, 2014 5:27 pm

You should use the CrosshairOverlay class for this. I don't think there was a demo for this in the 1.0.17 release, but the feature has been there for a while. Here is one of the demos that will be included in the JFreeChart demo collection for the 1.0.18 release (which is not far off):

Code: Select all

/* --------------------------
 * CrosshairOverlayDemo1.java
 * --------------------------
 * (C) Copyright 2003-2014, by Object Refinery Limited.
 *
 */

package demo;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
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.ValueAxis;
import org.jfree.chart.panel.CrosshairOverlay;
import org.jfree.chart.plot.Crosshair;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;

/**
 * A demo showing crosshairs that follow the data points on an XYPlot.
 */
public class CrosshairOverlayDemo1 extends JFrame {
  
    static class MyDemoPanel extends JPanel implements ChartMouseListener {
        
        private ChartPanel chartPanel;
    
        private Crosshair xCrosshair;
    
        private Crosshair yCrosshair;
    
        public MyDemoPanel() {
            super(new BorderLayout());
            JFreeChart chart = createChart(createDataset());
            this.chartPanel = new ChartPanel(chart);
            this.chartPanel.addChartMouseListener(this);
            CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
            this.xCrosshair = new Crosshair(Double.NaN, Color.GRAY, 
                    new BasicStroke(0f));
            this.xCrosshair.setLabelVisible(true);
            this.yCrosshair = new Crosshair(Double.NaN, Color.GRAY, 
                    new BasicStroke(0f));
            this.yCrosshair.setLabelVisible(true);
            crosshairOverlay.addDomainCrosshair(xCrosshair);
            crosshairOverlay.addRangeCrosshair(yCrosshair);
            this.chartPanel.addOverlay(crosshairOverlay);
            add(this.chartPanel);
        }
    
        private JFreeChart createChart(XYDataset dataset) {
            JFreeChart chart = ChartFactory.createXYLineChart(
                    "CrosshairOverlayDemo1", "X", "Y", dataset);
            return chart;
        }

        private XYDataset createDataset() {
            XYSeries series = new XYSeries("S1");
            for (int x = 0; x < 10; x++) {
                series.add(x, x + Math.random() * 4.0);
            }
            XYSeriesCollection dataset = new XYSeriesCollection(series);
            return dataset;
        }
    
        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            // ignore
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            Rectangle2D dataArea = this.chartPanel.getScreenDataArea();
            JFreeChart chart = event.getChart();
            XYPlot plot = (XYPlot) chart.getPlot();
            ValueAxis xAxis = plot.getDomainAxis();
            double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, 
                    RectangleEdge.BOTTOM);
            // make the crosshairs disappear if the mouse is out of range
            if (!xAxis.getRange().contains(x)) { 
                x = Double.NaN;                  
            }
            double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x);
            this.xCrosshair.setValue(x);
            this.yCrosshair.setValue(y);
        }  
        
    }

    public CrosshairOverlayDemo1(String title) {
        super(title);
        setContentPane(createDemoPanel());
    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        return new MyDemoPanel();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                CrosshairOverlayDemo1 app = new CrosshairOverlayDemo1(
                        "JFreeChart: CrosshairOverlayDemo1.java");
                app.pack();
                app.setVisible(true);
            }
        });
    }

}
David Gilbert
JFreeChart Project Leader

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

bumblebee63
Posts: 2
Joined: Thu Jul 03, 2014 4:47 pm
antibot: No, of course not.

Re: Update on tracelines

Post by bumblebee63 » Thu Jul 03, 2014 5:53 pm

Thanks David, I thought there must be a replacement somewhere.

chrisrhyno2003
Posts: 30
Joined: Thu Jun 18, 2015 5:42 pm
antibot: No, of course not.

Re: Update on tracelines

Post by chrisrhyno2003 » Mon Jul 13, 2015 6:08 pm

Hi David,

I was looking for the source code of the CrossHairOverlayDemo2. I need multiple crosshairs a chart and i couldn't find the source code as such. Is it open source ?

Locked