生物屋さんのためのゼロからのプログラミング

―忘れないための覚書 (たま~に更新)―

JFreeChartでのZoom機能の解除方法

JFreeChartで作成したグラフは、mouseDraggedによりzoom in/outしてしまう。
そこで、zoom機能を解除するコードを書いた。

import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartFix extends JFrame {
	Random rand = new Random();
	
	ChartFix () {
		
		//グラフに表示するデータの作成
		int [] data = new int [50];
		for (int i = 0; i < 50; i++) {
			data[i] = rand.nextInt(100) + 1;
		}
		
		XYSeriesCollection trace = new XYSeriesCollection();
		XYSeries series = new XYSeries("Trace");
		
		//XYSeriesへのデータの追加
		for (int i = 0; i < 50; i++) {
			series.add(i, data[i]);
		}	
		trace.addSeries(series);
		
		//JFreeChartの作成
		JFreeChart chart = ChartFactory.createXYLineChart(
				" ",
				"Frame",
				"Value",
				trace,
				PlotOrientation.VERTICAL,
				true,
				false,
				false);
		
		//Chartを貼り付ける用のPanelの作成とフレームへの貼り付け
		ChartPanel cpane = new ChartPanel(chart);
		
		//zoom機能の解除
		cpane.setMouseZoomable(false);
		
		getContentPane().add(cpane);
	}

	public static void main(String[] args) {
		ChartFix frame = new ChartFix ();
		frame.setTitle("Test");
		frame.setBounds(100, 100, 300, 200);
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

要点は、下記の部分である。

    //zoom機能の解除
    cpane.setMouseZoomable(false);