简单实现3D柱状图
package yh.JFreeChart;
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
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.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;
/**
*
* @Title:JFreeChart实现柱状图
* @Description:Comment for created type
* @author 张颖辉
* @date 2017年1月24日上午10:43:44
* @version 1.0
*/
public class JFreeChartPillar {
/**
* 获得数据集。
*
* @return org.jfree.data.category.CategoryDataset
*/
private static CategoryDataset getDataset1() {
double[][] data = new double[][] { { 751, 800, 260, 600, 200 }, { 400, 560, 240, 300, 150 },
{ 600, 450, 620, 220, 610 } };
String[] rowKeys = { "CPU", "硬盘", "内存" };
String[] columnKeys = { "北京", "上海", "广州", "南京", "深圳" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
return dataset;
}
/**
* 生成柱状图。
*/
public static void main(String[] args) {
String title = "统计表";
// 获得数据集
CategoryDataset dataset = getDataset1();
JFreeChart chart = ChartFactory.createBarChart3D(title, // 图表标题
"时间", // 目录轴的显示标签
"次数", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例
true, // 是否生成工具(提示)
false // 是否生成URL链接
);
// 设置标题字体
Font font = new Font("宋体", Font.BOLD, 18);
TextTitle textTitle = new TextTitle(title);
textTitle.setFont(font);
chart.setTitle(textTitle);
chart.setTextAntiAlias(false);
// 设置背景色
chart.setBackgroundPaint(new Color(255, 255, 255));
// 设置图例字体
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋体", Font.TRUETYPE_FONT, 14));
// 获得柱状图的Plot对象
CategoryPlot plot = chart.getCategoryPlot();
BarRenderer3D customBarRenderer = (BarRenderer3D) plot.getRenderer();
// 取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
// 设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 16));
// 设置横轴标记的字体
categoryAxis.setTickLabelFont(new Font("宋休", Font.TRUETYPE_FONT, 16));
// 取得纵轴
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
// 设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 16));
numberAxis.setTickLabelFont(new Font("Fixedsys", Font.PLAIN, 13));
customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// 显示每个柱的数值
customBarRenderer.setBaseItemLabelsVisible(true);
// 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
customBarRenderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
ChartFrame frame = new ChartFrame(title, chart, true);
frame.pack();
frame.setVisible(true);
}
}
运行结果:
说明:中文标题文字可能不显示出现方格,解决办法是设置字体。
将图片写入流中的示例:
package org.ruigai.util;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import org.jfree.chart.ChartFactory;
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.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;
public class JFreeChartPillar {
private String title;// 大标题(如:什么什么图)
private String categoryAxisLabel;// X轴标题(如:按季度)
private String valueAxisLabel;// Y轴标题 (如:销量)
private OutputStream outputStream;// 接受数据的输出流
private CategoryDataset dataset;// 数据集对象
public JFreeChartPillar() {
super();
}
public JFreeChartPillar(String title, String categoryAxisLabel,
String valueAxisLabel, OutputStream outputStream,
CategoryDataset dataset) {
super();
this.title = title;
this.categoryAxisLabel = categoryAxisLabel;
this.valueAxisLabel = valueAxisLabel;
this.outputStream = outputStream;
this.dataset = dataset;
}
public String getCategoryAxisLabel() {
return categoryAxisLabel;
}
public void setCategoryAxisLabel(String categoryAxisLabel) {
this.categoryAxisLabel = categoryAxisLabel;
}
public CategoryDataset getDataset() {
return dataset;
}
public void setDataset(CategoryDataset dataset) {
this.dataset = dataset;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getValueAxisLabel() {
return valueAxisLabel;
}
public void setValueAxisLabel(String valueAxisLabel) {
this.valueAxisLabel = valueAxisLabel;
}
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
public void drawAsPNG() throws IOException {
// 创建3D柱形图标
JFreeChart jfreechart = ChartFactory.createBarChart3D(title,
categoryAxisLabel, valueAxisLabel, dataset,
PlotOrientation.VERTICAL, true, true, false);
// 设置标题字体
jfreechart.getTitle().setFont(new Font("隶书", Font.ITALIC, 20));
// 获得柱形图表情节对象
CategoryPlot categoryPlot = (CategoryPlot) jfreechart.getPlot();
// 设置网格线可见
categoryPlot.setDomainGridlinesVisible(true);
/********/
NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
numberAxis.setRange(0.0, 100.0);
numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
/********/
// 获得x轴对象
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
// 设置x轴显示的分类名称的显示位置,如果不设置则水平显示
// 设置后,可以斜像显示,但分类角度,图表空间有限时,建议采用
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(0.39269908169872414D));
categoryAxis.setLabelFont(new Font("楷体", Font.BOLD, 14)); // X轴标题
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12)); // x轴下标
// 种类外部的间距
categoryAxis.setCategoryMargin(0.2);
// 柱形渲染器
BarRenderer3D renderer = (BarRenderer3D) categoryPlot.getRenderer();
renderer.setDrawBarOutline(false);// 设置不显示边框线
renderer.setItemMargin(0.1);// 分类内部间距
// 显示柱形数值
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
// 获取柱状
ValueAxis rangeAxis = categoryPlot.getRangeAxis();
rangeAxis.setLabelFont(new Font("楷体", Font.BOLD, 14)); // Y轴标题
// 图示项字体
LegendTitle legend = jfreechart.getLegend();
Font labelFont = new Font("宋体", Font.BOLD, 12);
legend.setItemFont(labelFont);
// 将图表已数据流的方式返回给客户端
ChartUtilities.writeChartAsPNG(outputStream, jfreechart, 500, 300);
}
public static void main(String[] agrs) throws IOException {
double[][] data = new double[][] { { 75.1, 80.8, 26.9, 60.5 },
{ 40.3, 56.3, 24.5, 30.9 } };
String[] rowKeys = {"上季", "本季"};
String[] columnKeys = {"准度练习", "走位练习", "围球练习", "K球练习"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
// 打开一个输出流
OutputStream outputStream = new FileOutputStream("D:\\BarChart.png");
new JFreeChartPillar("专项成功率统计", null, "成功率(%)", outputStream,
dataset).drawAsPNG();
}
}
运行结果:
还未解决的问题:怎么将显示的数值和左侧标尺的数值显示为百分比?若您知道,请回复告知。
上面的解决办法只是制定了标尺的显示范围为1-100,通过Y轴标题说明是百分比。