ChartMouseListener not working..please help!!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
deepika
Posts: 13
Joined: Fri May 13, 2005 2:00 pm

ChartMouseListener not working..please help!!

Post by deepika » Mon Oct 23, 2006 8:06 pm

Hi,

I want to be able to display the tool tip information based on a given category and a series. The tool tip generator only displays the information when the mouse is put over the intersection of X and Y axis values...I am trying to use ChartMouseListener to capture the category and series of the area pointed by mouse but for some reason chartMouseClicked() or chartMouseMoved() is not being called.

Below is my example code. Any help is greatly appreciated.


/* -------------------------
* StackedAreaChartDemo.java
* -------------------------
* (C) Copyright 2002-2004, by Object Refinery Limited.
*
*/

package demo;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.CategoryItemEntity;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


/**
* A simple demonstration application showing how to create a stacked area chart using data from a
* {@link CategoryDataset}.
*/
public class MyStackedAreaChartDemo extends ApplicationFrame {

/**
* Creates a new demo.
*
* @param title the frame title.
*/
public MyStackedAreaChartDemo(String title) {
super(title);
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
java.io.FileOutputStream fs;
try {
fs = new FileOutputStream("charts.png");
//Also save to a file
//response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(fs, chart, 400, 300);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* Creates a sample dataset.
*
* @return A sample dataset.
*/
public CategoryDataset createDataset() {
double[][] data = new double[][] {
{1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0 },
{5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0 },
{4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0 }
};

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
"Series ", "Type ", data
);
return dataset;
}

/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return A sample chart.
*/
public JFreeChart createChart(CategoryDataset dataset) {

JFreeChart chart = ChartFactory.createStackedAreaChart(
"Stacked Area Chart", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true,
false
);

chart.setBackgroundPaint(Color.white);

CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setForegroundAlpha(0.5f);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);

CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0.0);
domainAxis.setUpperMargin(0.0);

// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

CategoryItemRenderer renderer = plot.getRenderer();
renderer.setItemLabelsVisible(true);
// CategoryLabelGenerator generator = new StandardCategoryLabelGenerator(
// "{2}", new DecimalFormat("0.00")
// );
renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
// renderer.setLabelGenerator(generator);

// add the chart to a panel...
final ChartPanel chartPanel = new ChartPanel(chart);

chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);


// To Handle Mouse Click -- Can be removed if default handling is enough.
chartPanel.addChartMouseListener(new org.jfree.chart.ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent evt) {
try{
if(evt.getChart() != null & evt.getEntity() != null ){
CategoryItemEntity cie = (CategoryItemEntity)evt.getEntity();
if(cie.getCategory() == null) return;
if(cie.getCategoryIndex()>=0){
cie = (CategoryItemEntity) evt.getEntity();
int i = cie.getCategoryIndex();
System.out.println("Your Selected Index > " + i);
}
} else return;
}
catch(Exception ce){ System.out.println("Error" + ce);}
}

public void chartMouseMoved(ChartMouseEvent event) {

int x = event.getTrigger().getX();
int y = event.getTrigger().getY();
ChartEntity entity = event.getEntity();
if (entity != null) {
System.out.println(
"Mouse moved: " + x + ", " + y + ": " + entity.toString()
);
}
else {
System.out.println(
"Mouse moved: " + x + ", " + y + ": null entity."
);
}
}
});
return chart;

}

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

MyStackedAreaChartDemo demo = new MyStackedAreaChartDemo("Stacked Area Chart Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);

}

}
Thanks & Regards,
Deepika

m3stevep
Posts: 13
Joined: Sun Oct 22, 2006 2:42 am
Location: UTAH, USA
Contact:

Post by m3stevep » Mon Oct 23, 2006 9:46 pm

move the chartPanel.addChartMouseListener(new org.jfree.chart.ChartMouseListener() {... to the constructors chartPanel.

You've created an extra chartPanel in the createChart method that is not necessary.

deepika
Posts: 13
Joined: Fri May 13, 2005 2:00 pm

Post by deepika » Wed Oct 25, 2006 6:43 pm

Yeah, that was the problem. Thanks a lot.
Thanks & Regards,
Deepika

Locked