I recently started using JFreeChart and there is something i would like to do but i\'m not sure if it\'s possible. My program is supposed to draw a spectrogram (a sound grap
Don't do this by zooming, use a MouseListener
added to your ChartPanel
to add a Marker
to the Domain
axis.
In this example I have subclassed MouseAdapter
so that when you press the mouse down the domain value is captured (markerStart
) and when you release the mouse end postition is updated ('markerEnd') and a Marker is added to the plot (updateMarker
)
You will then get a chart like this:
Note that in this example I've actually turned off the zooming.
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.Layer;
public class MouseMarkerDemo extends JFrame {
public MouseMarkerDemo(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private final static class MouseMarker extends MouseAdapter{
private Marker marker;
private Double markerStart = Double.NaN;
private Double markerEnd = Double.NaN;
private final XYPlot plot;
private final JFreeChart chart;
private final ChartPanel panel;
public MouseMarker(ChartPanel panel) {
this.panel = panel;
this.chart = panel.getChart();
this.plot = (XYPlot) chart.getPlot();
}
private void updateMarker(){
if (marker != null){
plot.removeDomainMarker(marker,Layer.BACKGROUND);
}
if (!( markerStart.isNaN() && markerEnd.isNaN())){
if ( markerEnd > markerStart){
marker = new IntervalMarker(markerStart, markerEnd);
marker.setPaint(new Color(0xDD, 0xFF, 0xDD, 0x80));
marker.setAlpha(0.5f);
plot.addDomainMarker(marker,Layer.BACKGROUND);
}
}
}
private Double getPosition(MouseEvent e){
Point2D p = panel.translateScreenToJava2D( e.getPoint());
Rectangle2D plotArea = panel.getScreenDataArea();
XYPlot plot = (XYPlot) chart.getPlot();
return plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
}
@Override
public void mouseReleased(MouseEvent e) {
markerEnd = getPosition(e);
updateMarker();
}
@Override
public void mousePressed(MouseEvent e) {
markerStart = getPosition(e);
}
}
private static XYDataset createDataset() {
XYSeries series1 = new XYSeries("Data");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
series1.add(3.0, 3.0);
series1.add(4.0, 5.0);
series1.add(5.0, 5.0);
series1.add(6.0, 7.0);
series1.add(7.0, 7.0);
series1.add(8.0, 8.0);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
return dataset;
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"Mouse Marker",
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
return chart;
}
public static JPanel createDemoPanel() {
final JFreeChart chart = createChart(createDataset());
final ChartPanel panel = new ChartPanel(chart);
panel.setRangeZoomable(false);
panel.setDomainZoomable(false);
panel.addMouseListener(new MouseMarker(panel));
return panel;
}
public static void main(String[] args) {
MouseMarkerDemo demo = new MouseMarkerDemo("JFreeChart: MouseMarkerDemo.java");
demo.pack();
demo.setVisible(true);
}
}