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

Posted at

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

import java.text.NumberFormat;
import java.util.List;

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.DatasetRenderingOrder;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

/**
■ JFreeChart-パレート図-DefaultCategoryDataset
・パレート図はイタリアの経済学者ヴィルフレド・パレートが考案したもの.
・棒グラフとその累積グラフを重ねたもので、分かりやすく非常によく用いられる.
・パレート図のための特別なPlotがあるわけではなく、棒グラフの上に累積折れ線グラフを重ねることで実現する.

 */
public class Chart09_00 {
  // 目次-パレート図(ParetoChart)
  A_Chart09 a_Chart09;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;

  public static void main(String[] args) {
    // 売上棒グラフを作成する
    String rowKey = "売上";
    DefaultCategoryDataset salesDataset = new DefaultCategoryDataset();
    salesDataset.addValue(5000,rowKey,"商品A");
    salesDataset.addValue(3000,rowKey,"商品B");
    salesDataset.addValue(2000,rowKey,"商品C");
    salesDataset.addValue(1500,rowKey,"商品D");
    salesDataset.addValue(1300,rowKey,"商品E");
    salesDataset.addValue(1000,rowKey,"商品F");
    salesDataset.addValue(700,rowKey,"商品G");
    salesDataset.addValue(500,rowKey,"商品H");
    //
    CategoryAxis xAxis = new CategoryAxis();
    NumberAxis yAxis = new NumberAxis(rowKey);
    BarRenderer renderer = new BarRenderer();
    CategoryPlot catPlot = new CategoryPlot(salesDataset,xAxis,yAxis,(CategoryItemRenderer)renderer);
    //
    // CategoryPlotに累積グラフを追加する
    {
      // トータルの売上を計算する
      Double total = 0D;
      DefaultCategoryDataset percentDataset;
      {
        List<Comparable> items = salesDataset.getColumnKeys();
        for (int i=0; i<items.size(); i++) {
          Comparable item = items.get(i);
          total += (Double)salesDataset.getValue(rowKey,item);
        }
        percentDataset = new DefaultCategoryDataset();
      }
      // 累積パーセントを計算する
      {
        Double accumlated = 0D;
        percentDataset = new DefaultCategoryDataset();
        List<Comparable> items = salesDataset.getColumnKeys();
        for (int i=0; i<items.size(); i++) {
          Comparable item = items.get(i);
          accumlated += (Double)salesDataset.getValue(rowKey,item);
          percentDataset.addValue((double)accumlated/(double)total,"累積",item);
        }
      }
      // 累積グラフを追加する⇒グラフ番号は1になる
      final NumberAxis axis1 = new NumberAxis("Percent");
      axis1.setNumberFormatOverride(NumberFormat.getPercentInstance());
      catPlot.setRangeAxis(1,axis1);
      catPlot.setDataset(1,percentDataset);

      final LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
      catPlot.setRenderer(1,renderer1);
      catPlot.mapDatasetToRangeAxis(1,1);
      // このようにすれば累積グラフが上に描かれる
      catPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    }
    JFreeChart jfreeChart = new JFreeChart("パレート図",(Plot)catPlot);
    ChartFrame cFrame = new ChartFrame("パレート図",(JFreeChart)jfreeChart);  // (1)
    cFrame.pack();
    cFrame.setVisible(true);
  }
}

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