0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JFreeChart-二次元分布チャート-XYSeriesCollection-XYSeriesをListから作成する.X値はListのインデックス、Y値はオブジェクトから取得

Last updated at Posted at 2019-07-11

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
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;

/**
■ JFreeChart-二次元分布チャート-XYSeriesCollection-XYSeriesをListから作成する.X値はListのインデックス、Y値はオブジェクトから取得

 */
public class Chart15_02 {
  // 目次-二次元分布チャート
  A_Chart15 a_Chart15;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;

  public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // 表示するデータを取得する
    XYSeriesCollection collection = createXYDataset();
    // (1)レンダラの指定
    // 最初の引数はラインを引くかどうか⇒今回はラインは引かない
    // 次の引数はデータの場所に形を表示するか否か⇒今回は形を表示する.
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(false,true);
    // 軸はNumberAxis
    NumberAxis xAxis = new NumberAxis();
    NumberAxis yAxis = new NumberAxis();
    // Plotを作成して画面を表示する
    XYPlot xyPlot = new XYPlot((XYDataset)collection,xAxis,yAxis,(XYItemRenderer)renderer);
    JFreeChart jfreeChart = new JFreeChart("XYPlot",(Plot)xyPlot);
    //
    ChartFrame cFrame = new ChartFrame("XYPlot",(JFreeChart)jfreeChart);  // (1)
    cFrame.pack();
    cFrame.setVisible(true);
  }
  /** 表示データを作成する */
  private static XYSeriesCollection createXYDataset() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    Random ran = new Random();
    // データの生成
    for (int series = 0; series<5; series++) {
      List<Chart15_02_Data> list = new ArrayList<Chart15_02_Data>();
      for (int item = 0; item<10; item++) {
        double x = ran.nextDouble()*10;
        double y = x+ran.nextDouble()*5;
        list.add(new Chart15_02_Data(x,y));
      }
      // XYSeriesを作成する,y値はフィールドから取得する,x値はListのインデックスを使用する
      // createXにすれば逆になる
      XYSeries xySeries = ArcXYSeries.createY("S"+series,list,"y");
      // XYSeriesCollectionに追加する
      xySeriesCollection.addSeries(xySeries);
    }
    return xySeriesCollection;
  }
  /** x値,y値を格納するクラス.ただし今回はx値は使用しない */
  public static class Chart15_02_Data {
    public Chart15_02_Data(double x,double y) {
      this.x = x;
      this.y = y;
    }
    private double x;
    private double y;
    public double getX() {
      return x;
    }
    public void setX(double x) {
      this.x = x;
    }
    public double getY() {
      return y;
    }
    public void setY(double y) {
      this.y = y;
    }
  }
}

無題.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?