JFreeChartの横軸のレンジの調整
今回は、横軸の表示レンジの調整方法を書く。
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; 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.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class RangeTest extends JFrame implements ActionListener { Random rand = new Random(); XYSeriesCollection trace; XYSeries series; int [] data; int [] xData; //グラフ作成用のデータの作成とXYSeriesへのDataの貼り付け public void DataMake() { //グラフに表示するデータの作成 int [] data = new int [50]; int [] xData = new int [50]; for (int i = 0; i < 50; i++) { data[i] = rand.nextInt(100) + 1; xData[i] = 100 + i; } trace = new XYSeriesCollection(); series = new XYSeries("Trace"); //XYSeriesへのデータの追加 for (int i = 0; i < 50; i++) { series.add(xData[i], data[i]); } trace.addSeries(series); } public void ChartMake () { JFrame cFrame = new JFrame("Fig"); cFrame.setBounds(100, 100, 300, 200); cFrame.setVisible(true); //JFreeChartの作成 JFreeChart chart = ChartFactory.createXYLineChart( " ", "Frame", "Value", trace, PlotOrientation.VERTICAL, true, false, false); //Chartを貼り付ける用のPanelの作成とフレームへの貼り付け ChartPanel cpane = new ChartPanel(chart); //Legendの除去 chart.removeLegend(); //zoom機能の解除 cpane.setMouseZoomable(false); cFrame.getContentPane().add(cpane); } //Figの一部を表示 public void zoomFig() { JFrame zFrame = new JFrame("Zoom"); zFrame.setBounds(400, 400, 300, 200); zFrame.setVisible(true); //JFreeChartの作成 JFreeChart ZoomChart = ChartFactory.createXYLineChart( " ", "Frame", "Value", trace, PlotOrientation.VERTICAL, true, false, false); //Chartを貼り付ける用のPanelの作成とフレームへの貼り付け ChartPanel cpane = new ChartPanel(ZoomChart); //Legendの除去 ZoomChart.removeLegend(); //zoom機能の解除 cpane.setMouseZoomable(false); //X軸の表示レンジの調整 XYPlot plot = ZoomChart.getXYPlot(); ValueAxis xAxis = plot.getDomainAxis(); xAxis.setRange(120, 150); ChartPanel zpane = new ChartPanel(ZoomChart); zFrame.getContentPane().add(zpane); } public void actionPerformed (ActionEvent e){ String cmd = e.getActionCommand(); if (cmd.equals("open")) { ChartMake(); } else if (cmd.equals("zoom")) { zoomFig(); } } RangeTest () { DataMake(); JButton button = new JButton("Make"); JButton zButton = new JButton ("Zoom"); button.addActionListener(this); zButton.addActionListener (this); button.setActionCommand("open"); zButton.setActionCommand("zoom"); button.setBounds(40, 10, 80, 30); zButton.setBounds(40, 50, 80, 30); JPanel pane = new JPanel(); pane.setLayout(null); pane.add(button); pane.add(zButton); getContentPane().add(pane, BorderLayout.CENTER); } public static void main(String[] args) { RangeTest frame = new RangeTest(); frame.setTitle("Test"); frame.setBounds(10, 10, 180, 130); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
このコードの実行結果が下記になる。
左が横軸のレンジを調整していないグラフで、右が下記のコードで調整したグラフである。
//X軸の表示レンジの調整 XYPlot plot = ZoomChart.getXYPlot(); ValueAxis xAxis = plot.getDomainAxis(); xAxis.setRange(120, 150);
設定通りに、横軸の表示レンジが変えることができた。