package jp.co.prot.chart.multi;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.text.NumberFormat;
import jp.co.prot.common.DemoDefine;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
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.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
public class SampleMulti2 extends ApplicationFrame {
// -- 折れ線グラフ用ダミーデータ ----------
private int[] lineData1;
private int[] lineData2;
private int[] lineData3;
// -- 棒グラフ用ダミーデータ ------------
private double[] barData1;
private double[] barData2;
private double[] barData3;
private String[] series = { "商品1", "商品2", "商品3"};
// コンストラクタ
public SampleMulti2(String filenm, String param) {
super("複合サンプル1");
this.barData3 = DemoDefine.MAKER1_BAR_Zero;
if(param.equals("1")){
this.barData1 = DemoDefine.MAKER1_BAR1;
this.barData2 = DemoDefine.MAKER1_BAR2;
this.lineData1 = DemoDefine.LINE_DATA1;
this.lineData2 = DemoDefine.LINE_DATA2;
this.lineData3 = DemoDefine.LINE_DATA3;
}else if(param.equals("2")){
this.barData1 = DemoDefine.MAKER1_BAR4;
this.barData2 = DemoDefine.MAKER1_BAR5;
this.lineData1 = DemoDefine.LINE_DATA4;
this.lineData2 = DemoDefine.LINE_DATA5;
this.lineData3 = DemoDefine.LINE_DATA6;
}else if(param.equals("3")){
}else{
return;
}
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
File outFile = new File(DemoDefine.SAVE_DIR + filenm);
try{
ChartUtilities.saveChartAsJPEG(outFile, chart, 590,410);
}catch (Exception e) {
e.printStackTrace();
}
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(600, 430));
setContentPane(chartPanel);
}
/**
* グラフ作成処理
*
* @param dataset
* @return JFreeChart
*/
private JFreeChart createChart(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
"Test", "", "", dataset, PlotOrientation.VERTICAL,
true, true, false
);
// 図表用に背景色をセット
chart.setBackgroundPaint(new Color(0xBBBBDD));
// 一層のカスタマイゼーションのためにプロット取得
CategoryPlot plot = chart.getCategoryPlot();
// 範囲軸に整数だけを表示
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
RectangleEdge edge = plot.getDomainAxisEdge();
plot.mapDatasetToDomainAxis(1,0);
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setMaximumCategoryLabelWidthRatio(5.0f);
CategoryDataset dataset2 = createLineDataset();
ValueAxis axis2 = new NumberAxis();
plot.setRangeAxis(2, axis2);
// 折れ線グラフデータ設定
plot.setDataset(2, dataset2);
// グラフ軸調整
plot.mapDatasetToRangeAxis(2, 2);
// 折れ線グラフ作成
CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
renderer2.setSeriesPaint(0, Color.YELLOW);
plot.setRenderer(1, renderer2);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
return chart;
}
/**
* 棒グラフデータセット
* @return CategoryDataset
*/
private CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i=0; i<DemoDefine.CATEGORYS.length; i++){
if(i==3){
for(int j=0; j<series.length; j++){
dataset.addValue(barData1[j], series[j], DemoDefine.CATEGORYS[i]);
}
}else if(i==5){
for(int j=0; j<series.length; j++){
dataset.addValue(barData2[j], series[j], DemoDefine.CATEGORYS[i]);
}
}else{
for(int j=0; j<series.length; j++){
dataset.addValue(barData3[j], series[j], DemoDefine.CATEGORYS[i]);
}
}
}
return dataset;
}
/**
* 折れ線グラフデータセット
* @return CategoryDataset
*/
private CategoryDataset createLineDataset() {
DefaultCategoryDataset defCateDataset = new DefaultCategoryDataset();
for(int i=0; i<DemoDefine.CATEGORYS.length; i++){
defCateDataset.addValue(lineData1[i], series[0], DemoDefine.CATEGORYS[i]);
}
for(int i=0; i<DemoDefine.CATEGORYS.length; i++){
defCateDataset.addValue(lineData2[i], series[1], DemoDefine.CATEGORYS[i]);
}
for(int i=0; i<DemoDefine.CATEGORYS.length; i++){
defCateDataset.addValue(lineData3[i], series[2], DemoDefine.CATEGORYS[i]);
}
return defCateDataset;
}
}
|