Jfree Chart多類型図
32689 ワード
package com.hoocy.struts;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
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.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
/**
* 16 ,
*
* @author new
*/
public class CreateChartServiceImpl {
private static final String CHART_PATH = "E:/test/";
public static void main(String[] args) {
CreateChartServiceImpl pm = new CreateChartServiceImpl();
//
pm.makePieChart();
//
pm.makeBarChart();
//
pm.makeBarGroupChart();
//
pm.makeStackedBarChart();
//
pm.makeLineAndShapeChart();
}
/**
*
*/
public void makeLineAndShapeChart() {
double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
{ 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
String[] rowKeys = { " ", " ", " " };
String[] columnKeys = { " ", " ", " ", " ", " " };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createTimeXYChar(" ", "x ", "y ", dataset, "lineAndShap.png");
}
/**
*
*/
public void makeBarGroupChart() {
double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
{ 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
String[] rowKeys = { " ", " ", " " };
String[] columnKeys = { " ", " ", " ", " ", " " };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createBarChart(dataset, "x ", "y ", " ", "barGroup.png");
}
/**
*
*/
public void makeBarChart() {
double[][] data = new double[][] { { 672, 766, 223, 540, 126 } };
String[] rowKeys = { " " };
String[] columnKeys = { " ", " ", " ", " ", " " };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createBarChart(dataset, "x ", "y ", " ", "bar.png");
}
/**
*
*/
public void makeStackedBarChart() {
double[][] data = new double[][] { { 0.21, 0.66, 0.23, 0.40, 0.26 },
{ 0.25, 0.21, 0.10, 0.40, 0.16 } };
String[] rowKeys = { " ", " " };
String[] columnKeys = { " ", " ", " ", " ", " " };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createStackedBarChart(dataset, "x ", "y ", " ", "stsckedBar.png");
}
/**
*
*/
public void makePieChart() {
double[] data = { 9, 91 };
String[] keys = { " ", " " };
createValidityComparePimChar(getDataPieSetByUtil(data, keys), " ",
"pie2.png", keys);
}
// ,
public CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys) {
return DatasetUtilities
.createCategoryDataset(rowKeys, columnKeys, data);
}
//
public PieDataset getDataPieSetByUtil(double[] data,
String[] datadescription) {
if (data != null && datadescription != null) {
if (data.length == datadescription.length) {
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < data.length; i++) {
dataset.setValue(datadescription[i], data[i]);
}
return dataset;
}
}
return null;
}
/**
*
*
* @param dataset
*
* @param xName
* x ( , )
* @param yName
* y ( , )
* @param chartTitle
*
* @param charName
*
* @return
*/
public String createBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String charName) {
JFreeChart chart = ChartFactory.createBarChart(chartTitle, //
xName, //
yName, //
dataset, //
PlotOrientation.VERTICAL, // : 、
true, // ( false)
false, //
false // URL
);
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
/*
* VALUE_TEXT_ANTIALIAS_OFF ,
* , 12 14 ,
*/
// chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.white);
// create plot
CategoryPlot plot = chart.getCategoryPlot();
//
plot.setRangeGridlinesVisible(true);
//
plot.setRangeGridlinePaint(Color.gray);
//
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#00.00");
vn.setNumberFormatOverride(df); //
// x
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);//
domainAxis.setTickLabelFont(labelFont);//
// Lable(Math.PI/3.0)
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions
// .createUpRotationLabelPositions(Math.PI / 3.0));
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// Lable
//
domainAxis.setLowerMargin(0.1);
//
domainAxis.setUpperMargin(0.1);
// columnKey
// domainAxis.setSkipCategoryLabelsToFit(true);
plot.setDomainAxis(domainAxis);
// ( , 16 , )
plot.setBackgroundPaint(new Color(255, 255, 204));
// y
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// Item
rangeAxis.setUpperMargin(0.15);
// Item
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer renderer = new BarRenderer();
//
renderer.setMaximumBarWidth(0.05);
//
renderer.setMinimumBarLength(0.2);
//
renderer.setBaseOutlinePaint(Color.BLACK);
//
renderer.setDrawBarOutline(true);
// //
renderer.setSeriesPaint(0, new Color(204, 255, 255));
renderer.setSeriesPaint(1, new Color(153, 204, 255));
renderer.setSeriesPaint(2, new Color(51, 204, 204));
//
renderer.setItemMargin(0.0);
// ,
renderer.setIncludeBaseInRange(true);
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
//
plot.setForegroundAlpha(1.0f);
FileOutputStream fos_jpg = null;
try {
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
*
* @param dataset
*
* @param xName
* x ( , )
* @param yName
* y ( , )
* @param chartTitle
*
* @param charName
*
* @return
*/
public String createHorizontalBarChart(CategoryDataset dataset,
String xName, String yName, String chartTitle, String charName) {
JFreeChart chart = ChartFactory.createBarChart(chartTitle, //
xName, //
yName, //
dataset, //
PlotOrientation.VERTICAL, // : 、
true, // ( false)
false, //
false // URL
);
CategoryPlot plot = chart.getCategoryPlot();
//
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// 0
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0.00");
vn.setNumberFormatOverride(df); //
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //
// Lable
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
domainAxis.setLabelFont(labelFont);//
domainAxis.setTickLabelFont(labelFont);//
domainAxis.setMaximumCategoryLabelWidthRatio(0.8f);// Lable
// domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();
// Item
rangeAxis.setUpperMargin(0.15);
// Item
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer renderer = new BarRenderer();
//
renderer.setMaximumBarWidth(0.03);
//
renderer.setMinimumBarLength(30);
renderer.setBaseOutlinePaint(Color.BLACK);
//
renderer.setSeriesPaint(0, Color.GREEN);
renderer.setSeriesPaint(1, new Color(0, 0, 255));
//
renderer.setItemMargin(0.5);
// ,
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
//
plot.setForegroundAlpha(0.6f);
FileOutputStream fos_jpg = null;
try {
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
*
* @param dataset
*
* @param chartTitle
*
* @param charName
*
* @param pieKeys
*
* @return
*/
public String createValidityComparePimChar(PieDataset dataset,
String chartTitle, String charName, String[] pieKeys) {
JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, // chart
// title
dataset,// data
true,// include legend
true, false);
// ,
// chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setTextAntiAlias(false);
//
chart.setBackgroundPaint(Color.white);
// title
Font font = new Font(" ", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
// :
//
// plot.setBaseSectionOutlinePaint(Color.BLACK);
// plot.setBaseSectionPaint(Color.BLACK);
//
plot.setNoDataMessage(" , 。");
//
plot.setNoDataMessagePaint(Color.red);
// : ,{0} , {1} , {2} ,
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// : , {0} , {1} , {2}
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})"));
plot.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));
// (0.0-1.0)
plot.setForegroundAlpha(0.65f);
// (false) (true)
plot.setCircular(false, true);
// section , 12
plot.setStartAngle(90);
// //
plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));
FileOutputStream fos_jpg = null;
try {
//
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
//
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 230);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
System.out.println("create pie-chart.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* ,
*
* @param chartPath
*/
private void isChartPathExist(String chartPath) {
File file = new File(chartPath);
if (!file.exists()) {
file.mkdirs();
// log.info("CHART_PATH="+CHART_PATH+"create.");
}
}
/**
*
*
* @param chartTitle
* @param x
* @param y
* @param xyDataset
* @param charName
* @return
*/
public String createTimeXYChar(String chartTitle, String x, String y,
CategoryDataset xyDataset, String charName) {
JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,
xyDataset, PlotOrientation.VERTICAL, true, true, false);
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.WHITE);
// title
Font font = new Font(" ", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
//
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// x //
categoryplot.setDomainGridlinesVisible(true);
// y //
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.WHITE);//
categoryplot.setDomainGridlinePaint(Color.WHITE);//
categoryplot.setBackgroundPaint(Color.lightGray);
//
// categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
domainAxis.setLabelFont(labelFont);//
domainAxis.setTickLabelFont(labelFont);//
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //
// Lable
// 45
//
domainAxis.setLowerMargin(0.0);
//
domainAxis.setUpperMargin(0.0);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
// renderer lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true); // series ( )
lineandshaperenderer.setBaseLinesVisible(true); // series ( )
//
// lineandshaperenderer.setBaseItemLabelGenerator(new
// StandardCategoryItemLabelGenerator());
// lineandshaperenderer.setBaseItemLabelsVisible(true);
FileOutputStream fos_jpg = null;
try {
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
// png
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 510);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
System.out.println("create time-createTimeXYChar.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
*
* @param dataset
* @param xName
* @param yName
* @param chartTitle
* @param charName
* @return
*/
public String createStackedBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String charName) {
// 1: CategoryDataset
// 2:JFreeChart
JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, //
xName, //
yName, //
dataset, //
PlotOrientation.VERTICAL, // : 、
true, // ( false)
false, //
false // URL
);
//
chart.setTextAntiAlias(false);
chart.setBackgroundPaint(Color.WHITE);
// 2 .2 TextTitle
chart
.setTitle(new TextTitle(chartTitle, new Font(" ", Font.BOLD,
25)));
// 2 .2.1:
// x,y
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
// 2 .3 Plot Plot
CategoryPlot plot = chart.getCategoryPlot();
//
plot.setRangeGridlinesVisible(true);
//
plot.setRangeGridlinePaint(Color.gray);
//
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// 1
vn.setUpperBound(1);
// 0
// vn.setAutoRangeIncludesZero(true);
//
DecimalFormat df = new DecimalFormat("0.00%");
vn.setNumberFormatOverride(df); //
// DomainAxis ( , x ), RangeAxis ( , y )
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(labelFont);//
domainAxis.setTickLabelFont(labelFont);//
// x , , ,
// (1) Lable 45
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
// (2)Lable(Math.PI 3.0)
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions
// .createUpRotationLabelPositions(Math.PI / 3.0));
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// Lable
plot.setDomainAxis(domainAxis);
// y
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(labelFont);
rangeAxis.setTickLabelFont(labelFont);
// Item
rangeAxis.setUpperMargin(0.15);
// Item
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
// Renderer
StackedBarRenderer renderer = new StackedBarRenderer();
//
renderer.setMaximumBarWidth(0.05);
//
renderer.setMinimumBarLength(0.1);
//
renderer.setBaseOutlinePaint(Color.BLACK);
//
renderer.setDrawBarOutline(true);
// // ( )
renderer.setSeriesPaint(0, new Color(204, 255, 204));
renderer.setSeriesPaint(1, new Color(255, 204, 153));
//
renderer.setItemMargin(0.4);
plot.setRenderer(renderer);
// ( 3D , 2D )
// plot.setForegroundAlpha(0.65f);
FileOutputStream fos_jpg = null;
try {
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}