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-バーチャート-DefaultCategoryDatasetの作成-ArMatrixから作成する

Posted at

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

import java.lang.reflect.InvocationTargetException;

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.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

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

/**
JFreeChart-バーチャート-DefaultCategoryDatasetの作成-ArMatrixから作成する
・ key0はrowKey,key1をcolumnキーとする、値はArcMatrixの値オブジェクトから取得する.値のフィールド名を指定する

・ArMatrixからCategoryDatasetを生成する
  ・DefaultCategoryDatasetの作成機能は、ArcDefaultCategoryDatasetにまとめられている
  ・DefaultCategoryDatasetは、いくつかのデータ構造から作成できる

 */
public class Chart07_02 {
  // 目次-バーチャート(BarChart)
  A_Chart07 a_Chart07;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;


  static final String ITEM_0 = "商品0";
  static final String ITEM_1 = "商品1";
  static final String DOMAIN_0 = "地域0";
  static final String DOMAIN_1 = "地域1";
  static final String DOMAIN_2 = "地域2";

  public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // 表示データを生成する
    CategoryDataset dataset = createDataset();
    // X軸
    CategoryAxis domainAxis = new CategoryAxis();
    // Y軸
    ValueAxis rangeAxis = new NumberAxis();

    // レンダラ
    BarRenderer renderer = new BarRenderer();
    // PlotはCategoryPlot
    CategoryPlot plot = new CategoryPlot(dataset,domainAxis,rangeAxis,(CategoryItemRenderer)renderer);
    JFreeChart jfreeChart = new JFreeChart("売上集計",(Plot)plot);
    //
    ChartFrame cFrame = new ChartFrame("売上集計",(JFreeChart)jfreeChart);
    cFrame.pack();
    cFrame.setVisible(true);
  }

  /** MatrixからCatetoryDatasetを作成する */
  private static CategoryDataset createDataset() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // 元データのArMatrixを作成する
    ArMatrix<String,String,Chart07_02_Data> mat = new ArMatrix<String,String,Chart07_02_Data>();
    mat.put(ITEM_0,DOMAIN_0,new Chart07_02_Data("A",100.0D));
    mat.put(ITEM_0,DOMAIN_1,new Chart07_02_Data("B",44.0D));
    mat.put(ITEM_0,DOMAIN_2,new Chart07_02_Data("C",93.0D));
    mat.put(ITEM_1,DOMAIN_0,new Chart07_02_Data("D",80.0D));
    mat.put(ITEM_1,DOMAIN_1,new Chart07_02_Data("E",75.0D));
    mat.put(ITEM_1,DOMAIN_2,new Chart07_02_Data("F",15.0D));

    // ArMatrixをDefaultCategoryDatasetに変換する
    // 商品名と地域名を昇順にソートしてDefaultCategoryDatasetを作成する
    DefaultCategoryDataset catDataSet = ArcDefaultCategoryDataset.create(mat,"value",ArSortDir.ASCEND,ArSortDir.ASCEND);
    return catDataSet;
  }
  /** 売上データのクラス */
  public static class Chart07_02_Data {
    public Chart07_02_Data(String tantosha,Double value) {
      this.tantosha = tantosha;
      this.value = value;
    }
    private String tantosha;
    private Double value;
    public String getTantosha() {
      return tantosha;
    }
    public void setTantosha(String tantosha) {
      this.tantosha = tantosha;
    }
    public Double getValue() {
      return value;
    }
    public void setValue(Double value) {
      this.value = value;
    }
  }
}

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