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-インターヴァルチャート-DefaultIntervalCategoryDataset-Collectionから作成する.

Posted at

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

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

import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.IntervalBarRenderer;
import org.jfree.data.category.DefaultIntervalCategoryDataset;

import jp.avaj.lib.algo.ArMap;
import jp.avaj.lib.def.ArSortDir;

/**
■ JFreeChart-インターヴァルチャート-DefaultIntervalCategoryDataset-Collectionから作成する.

・DefaultIntervalCategoryDatasetをCollectionから作成する.
・カテゴリーキー、シリーズキー、二個の値をフィールドから取得する

 */
public class Chart11_02 {
  // 目次-インターヴァルチャート(IntervalChart)
  A_Chart11 a_Chart11;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;


  public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // 元データの取得
    List<Chart11_02_Data> list = createData();
    // 表示データの作成
    DefaultIntervalCategoryDataset ds;
    {
      // 商品名を表示用のものに変更する⇒不必要ならばnullにする
      Map<String,String> names = ArMap.construct("A=A商品,B=B商品,C=C商品,D=D商品,E=E商品");
      // 期間名を表示用のものに変換する⇒不必要ならばnullにする
      Map<String,String> periods = ArMap.construct("1=1月~3月,2=4月~6月");
      // カテゴリーキー、シリーズキー、二個の値をフィールドから取得する
      // 最後の二つの指定はシリーズキーとカテゴリーキーのソート方法⇒表示順に影響する⇒nullを指定すればソートされない.
      ds = ArcDefaultIntervalCategoryDataset.create(list,"name","period",names,periods,"low","high",ArSortDir.ASCEND,ArSortDir.ASCEND);
    }
    // 軸の設定
    CategoryAxis xAxis = new CategoryAxis("商品");
    NumberAxis yAxis = new NumberAxis("売上");
    // レンダラの設定
    IntervalBarRenderer renderer = new IntervalBarRenderer();
    // Plotの作成
    CategoryPlot plot = new CategoryPlot(ds,xAxis,yAxis,renderer);
    //
    JFreeChart jfreeChart = new JFreeChart("売上の最小と最大",(Plot)plot);
    ChartFrame cFrame = new ChartFrame("売上の最小と最大",(JFreeChart)jfreeChart);
    cFrame.pack();
    cFrame.setVisible(true);
  }

  private static List<Chart11_02_Data> createData() {
    List<Chart11_02_Data> list = new ArrayList<Chart11_02_Data>();
    list.add(new Chart11_02_Data("A","1",50,55));
    list.add(new Chart11_02_Data("B","1",30,48));
    list.add(new Chart11_02_Data("C","1",34,70));
    list.add(new Chart11_02_Data("D","1",33,48));
    list.add(new Chart11_02_Data("E","1",60,65));

    list.add(new Chart11_02_Data("A","2",55,60));
    list.add(new Chart11_02_Data("B","2",40,45));
    list.add(new Chart11_02_Data("C","2",45,75));
    list.add(new Chart11_02_Data("D","2",37,50));
    list.add(new Chart11_02_Data("E","2",60,70));

    return list;
  }

  /** 期間内での商品の売上の最小と最大を保持するクラス. */
  public static class Chart11_02_Data {
    Chart11_02_Data(String name,String period,int low,int high) {
      this.name = name;
      this.period = period;
      this.low = low;
      this.high = high;
    }
    private String name;
    private String period;
    private Integer low;
    private Integer high;
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public String getPeriod() {
      return period;
    }
    public void setPeriod(String period) {
      this.period = period;
    }
    public Integer getLow() {
      return low;
    }
    public void setLow(Integer low) {
      this.low = low;
    }
    public Integer getHigh() {
      return high;
    }
    public void setHigh(Integer high) {
      this.high = high;
    }
  }
}

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