LoginSignup
0
0

More than 3 years have passed since last update.

時系列チャート/TimeSeriesCollectionをArMatrix<T0,T1,T2>から作成

Posted at

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

import java.util.Map;
import java.util.Random;

import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
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.time.Day;
import org.jfree.data.time.TimeSeriesCollection;

import jp.avaj.lib.algo.ArDate;
import jp.avaj.lib.algo.ArMap;
import jp.avaj.lib.algo.ArMatrix;

/**
■ JFreeChart-時系列チャート/TimeSeriesCollectionをArMatrix<T0,T1,T2>から作成
・X値は日時のデータで、その基底クラスはRegularTimePeriod.
・X軸はDateAxisを使用する
・DatasetはTimeSeriesCollectionをMap<T0,List<T1>>から作成する

・TimeSeriesCollectionはArcTimeSeriesCollectionを使用して以下から作成することができる
  ・Map<T0,List<T1>>-T1オブジェクトから日時と値を取得する、シリーズキーはT0
  ・ArListOnMap<T0,T1>-T1オブジェクトから日時と値を取得する、シリーズキーはT0
  ・ArMapOnMap<T0,T1,T2>-T2オブジェクトから値を取得する、日時とシリーズキーは、T0またはT1オブジェクトから取得する.
    ・T0:日時、T1:シリーズキーあるいはT0:シリーズキー、T1:日時
  ・ArMatrix<T0,T1,T2>-T2オブジェクトから値を取得する、日時とシリーズキーは、T0またはT1オブジェクトから取得する.
    ・T0:日時、T1:シリーズキーあるいはT0:シリーズキー、T1:日時

  ・本例ではArMatrix<T0,T1,T2>からTimeSeriesCollectionを作成する.
 */
public class Chart16_02 {
  // 目次-時系列データの表示.
  A_Chart15 a_Chart15;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;

  public static void main(String[] args) throws Exception {
    // 表示データを作成する
    TimeSeriesCollection ds;
    {
      // 元データを取得する
      ArMatrix<String,ArDate,Chart16_02_Data> matrix = createData();
      // 必要ならばシリーズキーを変換できる.nullを指定すれば変換しない、ここで"b"⇒"B"の変換を行う
      Map<String,String> names = ArMap.construct("b=B");
      // 値のフィールド名を指定する、日時の単位はDayに変換する
      // ArMatrix<ArDate,String,Chart16_02_Data>ならば ArcTimeSeriesCollectionCreate.TIME_SERIESを指定する
      // 値はChart16_02_Dataから取得したが、ArMatrix<ArDate,String,値>の場合も処理可能⇒別サンプル
      ds = ArcTimeSeriesCollection.create(matrix,Day.class,ArcTimeSeriesCollectionCreate.SERIES_TIME,names,"sales");
    }
    // レンダラ
    // 最初の引数はラインを引くかどうか⇒今回はラインは引く
    // 次の引数はデータの場所に形を表示するか否か⇒今回は形を表示する.
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,true);
    // X軸はDateAxis
    DateAxis xAxis = new DateAxis();
    // 軸の表示単位を設定する,1は一日単位の意味,2にすれば二日ごとに表示する.
    xAxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY,1));
    NumberAxis yAxis = new NumberAxis();
    // Plotを作成してチャートを表示する
    XYPlot xyPlot = new XYPlot(ds,xAxis,yAxis,(XYItemRenderer)renderer);
    JFreeChart jfreeChart = new JFreeChart("売上推移",(Plot)xyPlot);
    ChartFrame cFrame = new ChartFrame("売上推移",(JFreeChart)jfreeChart);  // (1)
    cFrame.pack();
    cFrame.setVisible(true);
  }
  // 表示データを作成する
  private static ArMatrix<String,ArDate,Chart16_02_Data> createData() {
    // 商品名の定義
    final String[] items = new String[]{"A","b","C"};
    // 最初の日
    final ArDate startDate = new ArDate(2030,7,1);
    Random ran = new Random();
    //
    ArMatrix<String,ArDate,Chart16_02_Data> matrix = new ArMatrix<String,ArDate,Chart16_02_Data>();
    for (int item=0; item<items.length; item++) {
      ArDate arDate = (ArDate)startDate.clone();

      for (int day=0; day<5; day++) {
        // 売上 ⇒ サンプルなので適当.
        int sales = item*10+ran.nextInt(10);
        // 誤解がないようにChart16_02_Dataの日付にはnullを入れておく
        matrix.put(items[item],(ArDate)arDate.clone(),new Chart16_02_Data(null,sales));
        // 次の日
        arDate.forward(1);
      }
    }
    return matrix;
  }
  /** 表示データを格納するクラス */
  public static class Chart16_02_Data {
    public Chart16_02_Data(ArDate date,int sales) {
      this.date = date;
      this.sales = sales;
    }
    private ArDate date;
    private int sales;
    public ArDate getDate() {
      return date;
    }
    public void setDate(ArDate date) {
      this.date = date;
    }
    public int getSales() {
      return sales;
    }
    public void setSales(int sales) {
      this.sales = sales;
    }
  }
}

無題.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