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-平均と標準偏差のチャート-DefaultStatisticalCategoryDataset-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.StatisticalBarRenderer;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;

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

/**
■ JFreeChart-平均と標準偏差のチャート-DefaultStatisticalCategoryDataset-Collectionから作成する
・レンダラにはStatisticalBarRendererを使用する

 */
public class Chart12_02 {
  // 目次-平均と標準偏差のチャート-DefaultStatisticalCategoryDatasetを使用したもの
  A_Chart12 a_Chart12;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;

  public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // 元データを取得する
    List<Chart12_02_Data> list = createData();
    // カテゴリの変換Map⇒変換が不要ならばnullにする
    Map<String,String> catNames = ArMap.construct("A=A商品,B=B商品,C=C商品,D=D商品,E=E商品");
    // シリーズの変換Map⇒変換が不要ならばnullにする
    Map<Integer,String> serNames = ArMap.construct(new Integer[]{1,2},new String[]{"1月~3月","4月~6月"});
    // datasetを生成する.カテゴリ(name)、シリーズ(period)、平均値(mean)、標準偏差(devi)のフィールドを指定する
    // 最後の二つはカテゴリとシリーズのソート順⇒不要ならばnullを指定する
    DefaultStatisticalCategoryDataset ds = ArcDefaultStatisticalCategoryDataset.create(list,"name","period",catNames, serNames,"mean","devi",ArSortDir.ASCEND,ArSortDir.DESCEND);

    // 軸の設定
    CategoryAxis xAxis = new CategoryAxis("商品");
    NumberAxis yAxis = new NumberAxis("売上");
    // レンダラの設定
    StatisticalBarRenderer renderer = new StatisticalBarRenderer();
    // 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<Chart12_02_Data> createData() {

    List<Chart12_02_Data> list = new ArrayList<Chart12_02_Data>();
    list.add(new Chart12_02_Data("A",1,50D,5D));
    list.add(new Chart12_02_Data("B",1,30D,2D));
    list.add(new Chart12_02_Data("C",1,34D,4D));
    list.add(new Chart12_02_Data("D",1,33D,3D));
    list.add(new Chart12_02_Data("E",1,60D,5D));

    list.add(new Chart12_02_Data("A",2,55D,4D));
    list.add(new Chart12_02_Data("B",2,40D,3D));
    list.add(new Chart12_02_Data("C",2,45D,3D));
    list.add(new Chart12_02_Data("D",2,37D,5D));
    list.add(new Chart12_02_Data("E",2,60D,6D));

    return list;
  }

  public static class Chart12_02_Data {
    public Chart12_02_Data(String name,Integer period,Double mean,Double devi) {
      this.name = name;
      this.period = period;
      this.mean = mean;
      this.devi = devi;
    }
    String name;
    Integer period;
    Double mean;
    Double devi;
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public Integer getPeriod() {
      return period;
    }
    public void setPeriod(Integer period) {
      this.period = period;
    }
    public Double getMean() {
      return mean;
    }
    public void setMean(Double mean) {
      this.mean = mean;
    }
    public Double getDevi() {
      return devi;
    }
    public void setDevi(Double devi) {
      this.devi = devi;
    }
  }
}

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