jfreechart生成柱状图
2017-05-09 09:51
453 查看
import org.jfree.chart.ChartColor;
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.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ChartUtils {
public static void setChartDate(String path, String titleName, String aTitleName, String uTitleName,CategoryDataset ds, int width,int height) throws IOException {
// CategoryDataset ds = getDataSet();
// JFreeChart chart = ChartFactory.createBarChart3D(
JFreeChart chart = ChartFactory.createBarChart(
titleName, //图表标题
aTitleName, //目录轴的显示标签
uTitleName, //数值轴的显示标签
ds, //数据集
PlotOrientation.VERTICAL, //图表方向 HORIZONTAL(水平的)
false, //是否显示图例,对于简单的柱状图必须为false
false, //是否生成提示工具
false); //是否生成url链接
// 设置总的背景颜色
chart.setBackgroundPaint(ChartColor.white);
// 获得图表对象
CategoryPlot p = chart.getCategoryPlot();
// 设置图的背景颜色
p.setBackgroundPaint(ChartColor.WHITE);
//设置图的边框
p.setOutlinePaint(ChartColor.white);
BarRenderer customBarRenderer = (BarRenderer) p.getRenderer();
//取消柱子上的渐变色
customBarRenderer.setBarPainter( new StandardBarPainter() );
customBarRenderer.setItemMargin(-0.01);
//设置柱子的颜色
// customBarRenderer.setBaseOutlinePaint(ChartColor.red);
Color c=new Color(0,97,183);
customBarRenderer.setSeriesPaint(0, c);
//设置柱子宽度
// customBarRenderer.setMaximumBarWidth(0.015);
// customBarRenderer.setMinimumBarLength(0.1);
//设置柱子间距
// customBarRenderer.setItemMargin(1);
//
//设置阴影,false代表没有阴影
customBarRenderer.setShadowVisible(false);
// 设置柱状图的顶端显示数字
customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值
customBarRenderer.setBaseItemLabelsVisible(true);
customBarRenderer.setItemLabelAnchorOffset(0);
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
// customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
// ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
// customBarRenderer.setItemLabelAnchorOffset(1D);// 设置柱形图上的文字偏离值
// customBarRenderer.setItemLabelsVisible(true);
NumberAxis numberaxis = (NumberAxis) p.getRangeAxis();
// CategoryAxis domainAxis = categoryplot.getDomainAxis();
CategoryAxis axis = p.getDomainAxis(); //x轴
// /设置最高的一个柱与图片顶端的距离(最高柱的10%)
numberaxis.setUpperMargin(0.3);
// axis.setTickLabelPaint(ChartColor.red);
// axis.setMaximumCategoryLabelLines(10); //标题行数,每个字显示一行
// axis.setMaximumCategoryLabelWidthRatio(0.5f); //每个标题宽度,控制为1个字的宽度
// axis.setLabelInsets();
// axis.setLabelPaint(ChartColor.red);
/*------设置X轴坐标上的文字-----------*/
axis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 6));
/*------设置X轴的标题文字------------*/
axis.setL
abc3
abelFont(new Font("宋体", Font.PLAIN, 6));
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 6));
// 设置显示位置
// p.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// p.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 6));
//设置y轴显示整数数据
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//设置Y轴的标题文字颜色
// numberaxis.setLabelPaint(ChartColor.red);
//设置y轴文字横方向
numberaxis.setLabelAngle(1.5);
//设置 y轴刻度尺为隐藏
// numberaxis.setTickLabelsVisible(false);
/*------这句代码解决了底部汉字乱码的问题-----------*/
// chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
/*******这句代码解决了标题汉字乱码的问题********/
chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));
FileOutputStream out = null;
try {
out = new FileOutputStream(path);//"F://1.jpg"
// File ou=new File("F://1.jpg");
ChartUtilities.saveChartAsPNG(new File(path), chart, width, height);
// ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 320, 140, null);
} finally {
try {
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static CategoryDataset getDataSet() {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(5, "", "8");
ds.addValue(5, "", "9");
ds.addValue(9, "", "10");
ds.addValue(12, "", "11");
ds.addValue(29, "", "12");
ds.addValue(26, "", "13");
ds.addValue(18, "", "14");
ds.addValue(5, "", "15");
ds.addValue(4, "", "16");
ds.addValue(5, "", "17");
ds.addValue(5, "", "18");
ds.addValue(3, "", "19");
ds.addValue(3, "", "20");
ds.addValue(2, "", "21");
return ds;
}
public static void main(String[] args) {
try {
ChartUtils.setChartDate("F://1.png", "三家店 总雨量 mm", "时间", "雨量",ChartUtils.getDataSet(),320,140);
} catch (IOException e) {
e.printStackTrace();
}
}
}
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.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ChartUtils {
public static void setChartDate(String path, String titleName, String aTitleName, String uTitleName,CategoryDataset ds, int width,int height) throws IOException {
// CategoryDataset ds = getDataSet();
// JFreeChart chart = ChartFactory.createBarChart3D(
JFreeChart chart = ChartFactory.createBarChart(
titleName, //图表标题
aTitleName, //目录轴的显示标签
uTitleName, //数值轴的显示标签
ds, //数据集
PlotOrientation.VERTICAL, //图表方向 HORIZONTAL(水平的)
false, //是否显示图例,对于简单的柱状图必须为false
false, //是否生成提示工具
false); //是否生成url链接
// 设置总的背景颜色
chart.setBackgroundPaint(ChartColor.white);
// 获得图表对象
CategoryPlot p = chart.getCategoryPlot();
// 设置图的背景颜色
p.setBackgroundPaint(ChartColor.WHITE);
//设置图的边框
p.setOutlinePaint(ChartColor.white);
BarRenderer customBarRenderer = (BarRenderer) p.getRenderer();
//取消柱子上的渐变色
customBarRenderer.setBarPainter( new StandardBarPainter() );
customBarRenderer.setItemMargin(-0.01);
//设置柱子的颜色
// customBarRenderer.setBaseOutlinePaint(ChartColor.red);
Color c=new Color(0,97,183);
customBarRenderer.setSeriesPaint(0, c);
//设置柱子宽度
// customBarRenderer.setMaximumBarWidth(0.015);
// customBarRenderer.setMinimumBarLength(0.1);
//设置柱子间距
// customBarRenderer.setItemMargin(1);
//
//设置阴影,false代表没有阴影
customBarRenderer.setShadowVisible(false);
// 设置柱状图的顶端显示数字
customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值
customBarRenderer.setBaseItemLabelsVisible(true);
customBarRenderer.setItemLabelAnchorOffset(0);
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
// customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
// ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
// customBarRenderer.setItemLabelAnchorOffset(1D);// 设置柱形图上的文字偏离值
// customBarRenderer.setItemLabelsVisible(true);
NumberAxis numberaxis = (NumberAxis) p.getRangeAxis();
// CategoryAxis domainAxis = categoryplot.getDomainAxis();
CategoryAxis axis = p.getDomainAxis(); //x轴
// /设置最高的一个柱与图片顶端的距离(最高柱的10%)
numberaxis.setUpperMargin(0.3);
// axis.setTickLabelPaint(ChartColor.red);
// axis.setMaximumCategoryLabelLines(10); //标题行数,每个字显示一行
// axis.setMaximumCategoryLabelWidthRatio(0.5f); //每个标题宽度,控制为1个字的宽度
// axis.setLabelInsets();
// axis.setLabelPaint(ChartColor.red);
/*------设置X轴坐标上的文字-----------*/
axis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 6));
/*------设置X轴的标题文字------------*/
axis.setL
abc3
abelFont(new Font("宋体", Font.PLAIN, 6));
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 6));
// 设置显示位置
// p.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// p.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 6));
//设置y轴显示整数数据
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//设置Y轴的标题文字颜色
// numberaxis.setLabelPaint(ChartColor.red);
//设置y轴文字横方向
numberaxis.setLabelAngle(1.5);
//设置 y轴刻度尺为隐藏
// numberaxis.setTickLabelsVisible(false);
/*------这句代码解决了底部汉字乱码的问题-----------*/
// chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
/*******这句代码解决了标题汉字乱码的问题********/
chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));
FileOutputStream out = null;
try {
out = new FileOutputStream(path);//"F://1.jpg"
// File ou=new File("F://1.jpg");
ChartUtilities.saveChartAsPNG(new File(path), chart, width, height);
// ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 320, 140, null);
} finally {
try {
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static CategoryDataset getDataSet() {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(5, "", "8");
ds.addValue(5, "", "9");
ds.addValue(9, "", "10");
ds.addValue(12, "", "11");
ds.addValue(29, "", "12");
ds.addValue(26, "", "13");
ds.addValue(18, "", "14");
ds.addValue(5, "", "15");
ds.addValue(4, "", "16");
ds.addValue(5, "", "17");
ds.addValue(5, "", "18");
ds.addValue(3, "", "19");
ds.addValue(3, "", "20");
ds.addValue(2, "", "21");
return ds;
}
public static void main(String[] args) {
try {
ChartUtils.setChartDate("F://1.png", "三家店 总雨量 mm", "时间", "雨量",ChartUtils.getDataSet(),320,140);
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- JFreeChart生成柱状图、饼状图、折线图详解
- 使用JFreeChart生成柱状图和饼图
- jfreechart应用_经典例题_生成饼状图、生成单组柱状图、生成多组柱状图、生成堆积柱状图、生成折线图
- 使用jfreeChart生成柱状图,折线图,饼图等
- JFreeChart 生成 柱状图(Struts2 + Maven3 + JFreeChart)
- JFreeChart生成柱状图
- JFreeChart生成3D柱状图
- jfreechart柱状图生成
- Jfreechart生成折线图和柱状图混合图
- JFreeChart生成混合图(柱状图折线图)并插入excel中
- JFreeChart生成柱状图
- JFreeChart最新版本1.0.8a的研究系列——生成柱状图
- JFreeChart生成柱状图 jfreechart-0.9.16.jar
- JFreeChart生成柱状图实例
- jFreeChart利用CategoryDatase,ChartFactory.createBarChart生成的柱状图
- 使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP
- #.JFreeChart 图表生成实例(饼图、柱状图、折线图、时序图)
- 利用JFreeChart生成简单柱状图(Java)
- JFreeChart 生成 柱状图(Struts2 + Maven3 + JFreeChart)
- JFreeChart生成柱状图、饼状图、折线图详解