import java.awt.Color;
import java.io.File;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;
/**
*CreateGraph
クラスは、3D積み重ね棒グラフを作成します。
*@author junmix
*@version 1.0
*/
public class CreateGraph {
/**
*作成する3D積み重ね棒グラフの各グラフの名称の格納用変数です。
*/
String[] sarGraphName;
/**
*作成する3D積み重ね棒グラフの各グラフの中に含める区分の名称の格納用変数です。
*/
String[] sarItemName;
/**
*作成する3D積み重ね棒グラフの各グラフの中に含める区分の値の格納用変数です。
*/
double[][] darItemValue;
/**
*作成する3D積み重ね棒グラフのグラフ上部コメントの格納用変数です。
*/
String sGraphUpperComment;
/**
*作成する3D積み重ね棒グラフのグラフ左部コメントの格納用変数です。
*/
String sGraphLeftComment;
/**
*ファイル出力先のパスの格納用変数です。
*/
String sFilePath;
/**
*グラフの画像サイズの格納用変数です。
*/
int[] iarGraphSize;
/**
*グラフの背景色の格納用変数です。
*/
int[] iarGraphBackgroundColor;
/**
*CreateGraph
クラスのインスタンスを生成します。
*@param sarGraphNameArg 作成する3D積み重ね棒グラフの各グラフの名称
*@param sarItemNameArg 作成する3D積み重ね棒グラフの各グラフの中に含める区分の名称
*@param darItemValueArg 作成する3D積み重ね棒グラフの各グラフの中に含める区分の値
*@param sGraphUpperCommentArg 作成する3D積み重ね棒グラフのグラフ上部コメント
*@param sGraphLeftCommentArg 作成する3D積み重ね棒グラフのグラフ左部コメント
*@param sFilePathArg ファイル出力先のパス
*@param iarGraphSizeArg グラフの画像サイズ(横と縦のサイズ)
*@param iarGraphBackgroundColorArg グラフの背景色(0から255でRGBをそれぞれ指定)
*/
public CreateGraph(String[] sarGraphNameArg,
String[] sarItemNameArg,
double[][] darItemValueArg,
String sGraphUpperCommentArg,
String sGraphLeftCommentArg,
String sFilePathArg,
int[] iarGraphSizeArg,
int[] iarGraphBackgroundColorArg) {
sarGraphName = sarGraphNameArg;
sarItemName = sarItemNameArg;
darItemValue = darItemValueArg;
sGraphUpperComment = sGraphUpperCommentArg;
sGraphLeftComment = sGraphLeftCommentArg;
sFilePath = sFilePathArg;
iarGraphSize = iarGraphSizeArg;
iarGraphBackgroundColor = iarGraphBackgroundColorArg;
}
/**
*グラフを作成します。
*/
public void workChart() throws Exception {
JFreeChart chart = null;
String sFileName = new String();
try {
chart = this.getChart();
this.configChart(chart);
long lTime = System.currentTimeMillis();
sFileName = sFilePath + lTime + ".png";
File fFile = new File(sFileName);
ChartUtilities.saveChartAsPNG(fFile, chart, iarGraphSize[0], iarGraphSize[1]);
} catch(Exception e) {
throw e;
}
}
/**
*チャートを生成します。
*/
public JFreeChart getChart() throws Exception {
CategoryDataset cd = null;
JFreeChart jfreechart = null;
try {
cd = DatasetUtilities.createCategoryDataset(sarItemName, sarGraphName, darItemValue);
jfreechart = ChartFactory.createStackedBarChart3D(sGraphUpperComment, "", sGraphLeftComment, cd, PlotOrientation.VERTICAL, true, true, true);
return jfreechart;
} catch (Exception e) {
throw e;
}
}
/**
*チャートの詳細を設定します。
*@param chart org.jfree.chart.JFreeChart
クラスのインスタンス
*/
public void configChart(JFreeChart chart) throws Exception {
CategoryPlot cPlot = null;
CategoryAxis domainAxis = null;
ValueAxis valueAxis = null;
CategoryItemRenderer renderer = null;
try {
cPlot = chart.getCategoryPlot();
chart.setBackgroundPaint(new Color(iarGraphBackgroundColor[0], iarGraphBackgroundColor[1], iarGraphBackgroundColor[2]));
chart.setAntiAlias(false);
domainAxis = cPlot.getDomainAxis();
domainAxis.setCategoryMargin(0.4);
domainAxis.setLowerMargin(0.1);
domainAxis.setUpperMargin(0.1);
valueAxis = cPlot.getRangeAxis();
valueAxis.setAutoRange(false);
valueAxis.setRange(0.0, 100.0);
renderer = cPlot.getRenderer();
renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
renderer.setItemLabelsVisible(true);
renderer.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
renderer.setNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
renderer.setSeriesPaint(0, Color.green);
renderer.setSeriesPaint(1, Color.red);
int iItemTotalNum = sarItemName.length;
for (int i = 0; i < iItemTotalNum; i++) {
renderer.setSeriesOutlinePaint(i, Color.gray);
}
} catch (Exception e) {
throw e;
}
}
/**
*メイン処理を行います。
*/
public static void main(String[] args) {
try {
String[] sarGraphName = new String[]{"Category 1", "Category 2", "Category 3"};
String[] sarItemName = new String[]{"Available", "Not Available"};
double[][] darItemValue = new double[][]{
{86.9, 20.4, 48.7},
{13.1, 79.6, 51.3}
};
String sGraphUpperComment = "Graph View";
String sGraphLeftComment = "Percentage (%)";
String sFilePath = "./";
int[] iarGraphSize = new int[]{600, 400};
int[] iarGraphBackgroundColor = new int[]{204, 255, 204};
CreateGraph cg = new CreateGraph(sarGraphName,
sarItemName,
darItemValue,
sGraphUpperComment,
sGraphLeftComment,
sFilePath,
iarGraphSize,
iarGraphBackgroundColor);
cg.workChart();
} catch (Exception e) {
e.printStackTrace();
}
}
}