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.util.ArrayList;
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.Plot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import jp.avaj.lib.algo.ArTuple;
import jp.avaj.lib.algo.ArUtil;

/**
■ JFreeChart-積上バーチャート-DefaultCategoryDataset-特定データの強調

・積上バーチャートでは一番下以外のデータは変化が分かりにくいので、順番を入れ替え一番下に表示してみる.

・問題点、色が前と変化するので分かりにくい.rotateRowを汎用的に作る必要がある.
  ・しかし方式は確認できた.
 */
public class Chart08_02 {
  // 目次-積上げバーチャート(StackedBarChart)
  A_Chart08 a_Chart08;
  // 目次-Artery-JFreeChart用のライブラリ
  A_Chart00 a_LibChartSampeContents;

  public static void main(String[] args) {
    // 表示データの作成
    DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
    {
      //
      dataSet.addValue(136.5,"商品A","2014");
      dataSet.addValue(206.1,"商品A","2015");
      dataSet.addValue(296.7,"商品A","2016");
      //
      dataSet.addValue( 40.5,"商品B","2014");
      dataSet.addValue(105.7,"商品B","2015");
      dataSet.addValue(199.4,"商品B","2016");
      //
      dataSet.addValue(150.5,"商品C","2014");
      dataSet.addValue(87.5,"商品C","2015");
      dataSet.addValue(40.0,"商品C","2016");
    }
    //
    CategoryAxis catAxis = new CategoryAxis();
    NumberAxis numAxis = new NumberAxis();
    // 積上棒グラフにはStackedBarRendererを使用する
    StackedBarRenderer renderer = new StackedBarRenderer();
    CategoryPlot catPlot = new CategoryPlot(dataSet,catAxis,numAxis,(CategoryItemRenderer)renderer); // (1)
    JFreeChart jfreeChart = new JFreeChart("売上構成の変化",(Plot)catPlot);
    //
    ChartFrame cFrame = new ChartFrame("売上構成の変化",(JFreeChart)jfreeChart);
    cFrame.pack();
    cFrame.setVisible(true);
    //
    // 商品Bの売上の変化を掴みたいので、商品Bを一番下に表示する
    {
      // 目で確認するために、少し休む
      ArUtil.sleep(2000);
      //
      rotateRow(dataSet);
    }
  }
  private static void rotateRow(DefaultCategoryDataset dataSet) {
    int colms = dataSet.getColumnCount();
    Comparable rowKey = dataSet.getRowKey(0);
    List<ArTuple> values = new ArrayList();
    for (int col=0; col<colms; col++) {
      Comparable colKey = dataSet.getColumnKey(col);
      values.add(new ArTuple(colKey,dataSet.getValue(rowKey,colKey)));
    }
    dataSet.removeRow(rowKey);
    for (int i=0; i<values.size(); i++) {
      dataSet.addValue((Double)(values.get(i).getV1()),rowKey,(Comparable)values.get(i).getV0());
    }
  }
}

無題.png

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