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-積上げバーチャート-人口構成図-DefaultKeyedValues2DDataset

Last updated at Posted at 2019-07-10

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

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.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultKeyedValues2DDataset;

/**
■ JFreeChart-積上げバーチャート-人口構成図-DefaultKeyedValues2DDataset
・よく見かける人口構成図は、専用のチャート/レンダラがあるわけではない。
・(3)バーチャートをStackedBarRendererで表示して実現する。

・簡単に言えば、左右に伸びる棒グラフを描く.
  ・(1)棒を横にするのでHORIZONTALを指定する.
  ・女性の人数をマイナスにするのは違和感があるが、両側に棒を描くためには、このようにする.

・(2)年齢の軸は、文字列なのでCategoryAxisにする.

 */
public class Chart08_01 {

  public static void main(String[] args) {
    // 元データを作成する ⇒ 基底クラスのDefaultCategoryDatasetでもよい
    DefaultKeyedValues2DDataset data = new DefaultKeyedValues2DDataset();
    {
      data.addValue(-136.5,"女性","90~");
      data.addValue(-206.1,"女性","85~89");
      data.addValue(-296.7,"女性","80~84");
      data.addValue(-348.9,"女性","75~79");
      data.addValue(-411.3,"女性","70~74");
      data.addValue(-498.4,"女性","65~69");
      data.addValue(-430.4,"女性","60~64");
      data.addValue(-378.6,"女性","55~59");
      data.addValue(-396.2,"女性","50~54");
      data.addValue(-430.8,"女性","45~49");
      data.addValue(-481.8,"女性","40~44");
      data.addValue(-411.2,"女性","35~39");
      data.addValue(-360.6,"女性","30~34");
      data.addValue(-315.4,"女性","25~29");
      data.addValue(-292.2,"女性","20~24");
      data.addValue(-292.3,"女性","15~19");
      data.addValue(-273.1,"女性","10~14");
      data.addValue(-258.5,"女性"," 5~ 9");
      data.addValue(-243.7,"女性"," 0~ 4");
      //
      data.addValue( 40.5,"男性","90~");
      data.addValue(105.7,"男性","85~89");
      data.addValue(199.4,"男性","80~84");
      data.addValue(287.7,"男性","75~79");
      data.addValue(358.2,"男性","70~74");
      data.addValue(466.0,"男性","65~69");
      data.addValue(415.1,"男性","60~64");
      data.addValue(373.2,"男性","55~59");
      data.addValue(396.8,"男性","50~54");
      data.addValue(435.5,"男性","45~49");
      data.addValue(491.4,"男性","40~44");
      data.addValue(420.4,"男性","35~39");
      data.addValue(368.5,"男性","30~34");
      data.addValue(325.6,"男性","25~29");
      data.addValue(304.6,"男性","20~24");
      data.addValue(308.5,"男性","15~19");
      data.addValue(286.8,"男性","10~14");
      data.addValue(271.5,"男性"," 5~ 9");
      data.addValue(255.1,"男性"," 0~ 4");
    }
    //
    CategoryDataset catDataset = (CategoryDataset)data;
    CategoryAxis catAxis = new CategoryAxis(); // (2)
    NumberAxis numAxis = new NumberAxis();
    StackedBarRenderer renderer = new StackedBarRenderer(); // (3)
    CategoryPlot catPlot = new CategoryPlot(catDataset,catAxis,numAxis,(CategoryItemRenderer)renderer);
    catPlot.setOrientation(PlotOrientation.HORIZONTAL); // (1)
    JFreeChart jfreeChart = new JFreeChart("2015:日本の人口構成",(Plot)catPlot);
    //
    ChartFrame cFrame = new ChartFrame("2015:日本の人口構成",(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?