5

Can anyone tell me how to implement Mouse Tracing on Graph in jfreechart?

Mat
  • 202,337
  • 40
  • 393
  • 406
sushant kumar
  • 105
  • 2
  • 9

1 Answers1

5

You may attach a ChartMouseListener to a ChartPanel, on which the charts and plots are rendered. Here is an example:

public class JFreeChartTest extends JFrame {

    private static JFreeChart createPieChart(){
        // Some dataset to display
        DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("Java", new Double(45.0));
        data.setValue("C++", new Double(20.0));
        data.setValue("Visual Basic", new Double(0.0));
        // Chart creation
        return ChartFactory.createPieChart("Pie Chart", // title
                data, // data
                true, // include legend
                true, false);
    }

    public JFreeChartTest(){
        initialize();
    }

    private void initialize(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        {
            final ChartPanel chartPanel = new ChartPanel((JFreeChart)null);
            contentPane.add(chartPanel, BorderLayout.CENTER);
            final JFreeChart pieChart = createPieChart();
            chartPanel.setChart(pieChart);
            chartPanel.addChartMouseListener(new ChartMouseListener() {

                @Override
                public void chartMouseClicked(final ChartMouseEvent event){
                    System.out.println("chartMouseClicked");
                }

                @Override
                public void chartMouseMoved(final ChartMouseEvent event){
                    int newX = event.getTrigger().getX();
                    int newY = event.getTrigger().getY();
                    System.out.println("chartMouseMoved to " + newX + " " + newY);
                }
            });
        }
    }

    public static void main(String[] args){
        JFreeChartTest frame = new JFreeChartTest();
        frame.setVisible(true);
    }
}    
Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
  • Thanks can you please tell me how to implement--while mouse tracing,pointer display on graph line which moves on line on moving mouse like in stock market graph?? – sushant kumar Sep 16 '11 at 07:47
  • 3
    This functionalty is built-in. You just need to add `chartPanel.setHorizontalAxisTrace(true);` and `chartPanel.setVerticalAxisTrace(true);` – Alex Abdugafarov Sep 16 '11 at 09:08
  • I implemented it also but there is refreshing problem with axis trace as it gets blurred. can you tell how to come out of this? – sushant kumar Sep 16 '11 at 09:12
  • Your question is off-topic. If you want a help with it, create a new question and describe your new problem in details. – Alex Abdugafarov Sep 16 '11 at 09:14
  • using axis trace line appears on graph is there any way that i can show only point on graph not the traceline while mouse move? – sushant kumar Sep 16 '11 at 09:18
  • Anyone know how to get the trace line to stay and not disappear if I stop moving the mouse? – Kevin Parker Oct 04 '13 at 19:25