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-X軸が共通な二つのグラフを上下に表示する

Posted at

目次 ⇒ JFreeChartサンプル

package jp.avaj.lib.chart;

import java.awt.Font;

import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
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.CombinedDomainCategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;

/**
■ JFreeChart-X軸が共通な二つのグラフを上下に表示する

 */
public class Chart18_07 {

  public static void main(String[] paramArrayOfString) {
    CategoryPlot plot0;
    {
      CategoryDataset dataset0 = createDataset0();
      NumberAxis axis0 = new NumberAxis("Sales");
      axis0.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
      LineAndShapeRenderer render0 = new LineAndShapeRenderer();
      plot0 = new CategoryPlot(dataset0,null,axis0,render0); // (1)
      plot0.setDomainGridlinesVisible(true);
    }

    CategoryPlot plot1;
    {
      CategoryDataset dataset1 = createDataset1();
      NumberAxis axis1 = new NumberAxis("Sales");
      axis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
      BarRenderer renderer1 = new BarRenderer();
      plot1 = new CategoryPlot(dataset1,null,axis1,renderer1); // (1)
      plot1.setDomainGridlinesVisible(true);
    }

    CategoryAxis xaxis = new CategoryAxis("BRANCH");
    CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot(xaxis); // (2)
    combinedPlot.add(plot0,2);
    combinedPlot.add(plot1,1);
    //
    JFreeChart chart = new JFreeChart("CombinedCategoryChart",new Font("SansSerif",1,12),combinedPlot,true);
    ChartUtilities.applyCurrentTheme(chart);
    plot0.setAxisOffset(RectangleInsets.ZERO_INSETS);
    plot1.setAxisOffset(RectangleInsets.ZERO_INSETS);
    //
    JFreeChart jfreeChart = new JFreeChart("CombinedCategoryChart",(Plot)combinedPlot);
    ChartFrame cFrame = new ChartFrame("CombinedCategoryChart",(JFreeChart)jfreeChart);
    cFrame.pack();
    cFrame.setVisible(true);
  }

  static final String BRAND0 = "Brand0";
  static final String BRAND1 = "Brand1";
  static final String BRAND2 = "Brand2";
  static final String BRAND3 = "Brand3";

  static final String BRANCH0 = "BRANCH0";
  static final String BRANCH1 = "BRANCH1";
  static final String BRANCH2 = "BRANCH2";
  static final String BRANCH3 = "BRANCH3";
  static final String BRANCH4 = "BRANCH4";
  static final String BRANCH5 = "BRANCH5";
  static final String BRANCH6 = "BRANCH6";
  static final String BRANCH7 = "BRANCH7";

  private static CategoryDataset createDataset0() {

    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
    localDefaultCategoryDataset.addValue(1.0D,BRAND0,BRANCH0);
    localDefaultCategoryDataset.addValue(4.0D,BRAND0,BRANCH1);
    localDefaultCategoryDataset.addValue(3.0D,BRAND0,BRANCH2);
    localDefaultCategoryDataset.addValue(5.0D,BRAND0,BRANCH3);
    localDefaultCategoryDataset.addValue(5.0D,BRAND0,BRANCH4);
    localDefaultCategoryDataset.addValue(7.0D,BRAND0,BRANCH5);
    localDefaultCategoryDataset.addValue(7.0D,BRAND0,BRANCH6);
    localDefaultCategoryDataset.addValue(8.0D,BRAND0,BRANCH7);
    localDefaultCategoryDataset.addValue(5.0D,BRAND1,BRANCH0);
    localDefaultCategoryDataset.addValue(7.0D,BRAND1,BRANCH1);
    localDefaultCategoryDataset.addValue(6.0D,BRAND1,BRANCH2);
    localDefaultCategoryDataset.addValue(8.0D,BRAND1,BRANCH3);
    localDefaultCategoryDataset.addValue(4.0D,BRAND1,BRANCH4);
    localDefaultCategoryDataset.addValue(4.0D,BRAND1,BRANCH5);
    localDefaultCategoryDataset.addValue(2.0D,BRAND1,BRANCH6);
    localDefaultCategoryDataset.addValue(1.0D,BRAND1,BRANCH7);
    return localDefaultCategoryDataset;
  }

  private static CategoryDataset createDataset1() {

    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
    localDefaultCategoryDataset.addValue(11.0D,BRAND2,BRANCH0);
    localDefaultCategoryDataset.addValue(14.0D,BRAND2,BRANCH1);
    localDefaultCategoryDataset.addValue(13.0D,BRAND2,BRANCH2);
    localDefaultCategoryDataset.addValue(15.0D,BRAND2,BRANCH3);
    localDefaultCategoryDataset.addValue(15.0D,BRAND2,BRANCH4);
    localDefaultCategoryDataset.addValue(17.0D,BRAND2,BRANCH5);
    localDefaultCategoryDataset.addValue(17.0D,BRAND2,BRANCH6);
    localDefaultCategoryDataset.addValue(18.0D,BRAND2,BRANCH7);
    localDefaultCategoryDataset.addValue(15.0D,BRAND3,BRANCH0);
    localDefaultCategoryDataset.addValue(17.0D,BRAND3,BRANCH1);
    localDefaultCategoryDataset.addValue(16.0D,BRAND3,BRANCH2);
    localDefaultCategoryDataset.addValue(18.0D,BRAND3,BRANCH3);
    localDefaultCategoryDataset.addValue(14.0D,BRAND3,BRANCH4);
    localDefaultCategoryDataset.addValue(14.0D,BRAND3,BRANCH5);
    localDefaultCategoryDataset.addValue(12.0D,BRAND3,BRANCH6);
    localDefaultCategoryDataset.addValue(11.0D,BRAND3,BRANCH7);
    return localDefaultCategoryDataset;
  }
}

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