JFreeChartを使って、折れ線グラフ を上書きする。
今回は以前取り扱っていたJFreeChartを使ったプログラミングを記す。
とある事情で、ボタンを押したときに折れ線グラフ(計3つ)を上書きするプログラミングを書いてみた。
まずは全体のソースコードを下記に記す。
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.plot.PlotOrientation; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class PlotOver extends JFrame implements ActionListener { Random rand = new Random(); JFrame frame2; XYSeriesCollection trace; ChartPanel cpane; int no = 30; //データを入れる配列 int data [] = new int [no]; int data1 [] = new int [no]; int data2 [] = new int [no]; //データの作成 public void makeData (int [] data) { for (int i = 0; i < no; i++) { data [i] = rand.nextInt(100) + 1; } } //Seriesへのadd public void seriesAdd (XYSeries a, int [] data) { for (int i = 0; i < no; i++) { a.add(i, data[i]); } } //最初の折れ線グラフを描く public void MakeFig () { makeData(data); frame2 = new JFrame ("ShowData"); frame2.setSize(500, 500); frame2.setLocationRelativeTo(null); frame2.setVisible (true); trace = new XYSeriesCollection (); XYSeries series = new XYSeries("Test"); seriesAdd(series, data); trace.addSeries(series); JFreeChart chart = ChartFactory.createXYLineChart ( "Data", "Frame", "Value", trace, PlotOrientation.VERTICAL, true, false, false); cpane = new ChartPanel(chart); frame2.add(cpane); } //2つ目の折れ線グラフの追加 public void add1 () { makeData(data1); XYSeries series1 = new XYSeries("Test1"); seriesAdd(series1, data1); trace.addSeries(series1); frame2.add(cpane); } //3つ目の折れ線グラフの追加 public void add2 () { makeData(data2); XYSeries series2 = new XYSeries("Test2"); seriesAdd(series2, data2); trace.addSeries(series2); frame2.add(cpane); } public void actionPerformed (ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("fig")) { MakeFig(); } else if (cmd.equals("1")) { add1(); } else if (cmd.equals("2")) { add2(); } } PlotOver (String title) { setTitle (title); setBounds (10, 10, 240, 170); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JButton button = new JButton ("MakeFig"); JButton button1 = new JButton ("Add1"); JButton button2 = new JButton ("Add2"); button.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); button.setActionCommand ("fig"); button1.setActionCommand("1"); button2.setActionCommand("2"); button.setBounds (60, 10, 100, 30); button1.setBounds (60, 50, 100, 30); button2.setBounds(60, 90, 100, 30); JPanel pane = new JPanel (); pane.setLayout(null); pane.add(button); pane.add(button1); pane.add(button2); getContentPane().add(pane, BorderLayout.CENTER); } public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ PlotOver frame = new PlotOver ("OverWrite"); frame.setVisible (true); } }
今回のプログラミングでは、簡単のために全てのデータをRandomで作成した。
プログラミングとしては綺麗ではないが、ボタンを押すことでJFreeChartに載せるデータをアップグレードするようにした(おそらく、もっとまともなコードがあるはずだが、プログラムが動くので良しをする)。
今回は折れ線グラフを作成するため、下記のように
JFreeChart chart = ChartFactory.createXYLineChart ( "Data", "Frame", "Value", trace, PlotOrientation.VERTICAL, true, false, false);
"ChartFactory.createXYLineChart"を使用した。使い方は以前使用した"createScatterPlot"と同じである。
データの上書きは下記のようにした。
//2つ目の折れ線グラフの追加 public void add1 () { makeData(data1); XYSeries series1 = new XYSeries("Test1"); seriesAdd(series1, data1); trace.addSeries(series1); frame2.add(cpane); }
このプログラムの実行結果の例を下記のようになる。
ボタンを押すごとに折れ線グラフが付加できることが確認できて、ちょっと安心。